Skip to content

Commit 2fd862a

Browse files
author
John Allsup
committed
Added tests for FromArray, IntersectionOfArray, and
UnionOfArray. Added documentation for these functions to README.md
1 parent 386b95c commit 2fd862a

File tree

7 files changed

+848
-608
lines changed

7 files changed

+848
-608
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.idea/
33

44
Bench.php
5+
.phpunit.result.cache

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ Sets require a min PHP version of 7.1.
2727
* [Symmetric difference](#symmetric-difference)
2828
* [Intersect](#intersect)
2929
* [Subsets](#subsets)
30-
30+
* [**Set family operations**](#set-family-operations)
31+
* [UnionOfArray](#union-of-array)
32+
* [IntersectionOfArray](#intersection-of-array)
3133

3234
## Installation
3335
You can download the latest release via the releases link on this page.
@@ -62,6 +64,11 @@ Sets cannot contain duplicate values, and values are stored in insertion order.
6264
$set = new Set(1, 2, 1, 3, 2);
6365
````
6466

67+
If you have an array of elements, you can create a set containing those values.
68+
```php
69+
$set = Set::FromArray([1, 2, 3]);
70+
```
71+
6572
#### Adding values
6673
Values of any type (Including Objects, arrays, and other `Sets`) are added to a set via the `add()` method.
6774

@@ -246,5 +253,45 @@ var_Dump($b->isSupersetOf($a)); // true
246253
var_Dump($a->isSupersetOf($b)); // false
247254
````
248255

256+
## Set family operations
257+
258+
If we have a collection of sets, for example `{ { 1,2,3 }, { 3,4,5 } , { 3, 5, 6, 7 } }`, often called a *family* of sets
259+
in Set Theory, we can take the union and intersection of the family. That is, `( { 1, 2, 3 } union { 3, 4, 5 } ) union { 3, 5, 6, 7 }` and likewise for intersection. In Set Theory a large union and intersection symbol is used for this purpose.
260+
261+
Note that, at present, these operations are implemented
262+
naively by iteratively calling the `union` and `intersect`
263+
methods above. More efficient implementations are possible
264+
and welcome.
265+
266+
#### Union of a Family of Sets
267+
268+
At present the family of sets needs to be in an array of `Set`s.
269+
Then we pass that array of sets to `Set::UnionOfArray()`.
270+
For example:
271+
```php
272+
$set1 = Set(1,2,3);
273+
$set2 = Set(3,4,5);
274+
$set_family = [ $set1, $set2 ];
275+
$set_union = Set::UnionOfArray($set_family);
276+
```
277+
278+
#### Intersection of a Family of Sets
279+
280+
As with `UnionOfArray`, the family of sets needs to be in an array of `Set`s.
281+
Then pass that array of sets to `Set::IntersectionOfArray()`.
282+
For example:
283+
```php
284+
$set1 = Set(1,2,3);
285+
$set2 = Set(3,4,5);
286+
$set_family = [ $set1, $set2 ];
287+
$set_intersection = Set::IntersectionOfArray($set_family);
288+
```
289+
290+
Note that, contrary to Set Theory, the result of
291+
taking the intersection of an empty array results
292+
in an empty array. (In Set Theory the intersection
293+
of an empty family is undefined as it would be the
294+
'set of all sets'.)
295+
249296
### Contributing
250297
Contributions and changes welcome! Just open an issue or submit a PR :muscle:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"php": ">=7.1"
1313
},
1414
"require-dev": {
15-
"phpunit/phpunit": "^7.0"
15+
"phpunit/phpunit": ">=7.0"
1616
},
1717
"autoload": {
1818
"psr-4": {

0 commit comments

Comments
 (0)