|
1 | 1 | # php-set-data-structure |
2 | | -An implementation of a Java-like Set data structure for PHP. A Set allows storage of any values without duplicates. |
| 2 | +PHP-Sets allows a Java-like Set data structure in PHP in a way which feels completely native to the language |
3 | 3 |
|
4 | 4 | Set objects are collections of values, you can iterate its elements in insertion order. A value in the Set may only occur once; it is unique in the Set's collection. |
5 | 5 |
|
6 | 6 | Implementation is based on the [MDN JS Reference](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set) for Sets in EMCA 6 JavaScript. |
7 | 7 |
|
8 | 8 | Sets require a min PHP version of 5.4. |
9 | 9 |
|
10 | | -### Todo |
11 | | -* Method/function to perform UNION operations on sets (joining 2 or more sets together) |
12 | | -* Method/function to return INTERSECTION of sets (common elements between 2 or more sets) |
13 | | -* Method/function to return DIFFERENCE between 2 sets |
14 | | -* Method/function to return whether a set is SUBSET of a different set |
15 | 10 |
|
16 | | -## Composer |
| 11 | +## Installing |
| 12 | +You can download the latest release via the releases link on this page. |
| 13 | + |
17 | 14 | PHP-Sets is available via [Composer/Packagist](https://packagist.org/packages/jakewhiteley/php-sets), so just add this line to your composer.json file: |
18 | 15 |
|
19 | 16 | `"jakewhiteley/php-sets": "~1.0"` |
|
23 | 20 | `composer require jakewhiteley/php-sets` |
24 | 21 |
|
25 | 22 | ## Initialization |
| 23 | +If you have included PHP-Sets via Composer, include the autoloader and PHP-Sets class: |
| 24 | +````php |
| 25 | +include('vendor/autoload.php'); |
| 26 | + |
| 27 | +use JakeWhiteley\PhpSets\Set; |
| 28 | +```` |
| 29 | + |
26 | 30 | A set instance can be created either as an object, or as a native-like function. When you create a set, you can insert intial values or keep it empty. |
27 | 31 | ````php |
28 | 32 | // Function creation method |
@@ -192,4 +196,55 @@ foreach ( $vals as $val ){ |
192 | 196 | // prints 12 |
193 | 197 | ```` |
194 | 198 |
|
| 199 | +## Helper functions |
| 200 | +#### set_diff() |
| 201 | +A function for finding the difference between 2 Sets. Returns a new Set object. |
| 202 | +````php |
| 203 | +$a = set( 1, 2, 3 ); |
| 204 | +$b = set( 2, 3, 4 ); |
| 205 | + |
| 206 | +$difference = set_diff( $a, $b ); |
| 207 | + |
| 208 | +// prints [1,4] |
| 209 | +var_dump( $difference->values() ); |
| 210 | +```` |
| 211 | + |
| 212 | +#### set_intersect() |
| 213 | +Returns a new Set object containing the items common between two sets: |
| 214 | +````php |
| 215 | +$a = set( 1, 2, 3 ); |
| 216 | +$b = set( 2, 3, 4 ); |
| 217 | + |
| 218 | +$intersect = set_intersect( $a, $b ); |
| 219 | + |
| 220 | +// prints [2,3] |
| 221 | +var_dump( $intersect->values() ); |
| 222 | +```` |
| 223 | + |
| 224 | +#### set_subset() |
| 225 | +Checks if all the values of the second Set are present within the first Set: |
| 226 | +````php |
| 227 | +$a = set( 1, 2, 3 ); |
| 228 | +$b = set( 2, 3 ); |
| 229 | + |
| 230 | +// true |
| 231 | +var_Dump( set_subset( $a, $b ) ); |
| 232 | + |
| 233 | +$c = set( 3, 4 ); |
| 234 | + |
| 235 | +// false |
| 236 | +var_Dump( set_subset( $a, $c ) ); |
| 237 | +```` |
| 238 | + |
| 239 | +#### set_merge() |
| 240 | +Appends a second Set onto a given Set without creating duplicates: |
| 241 | +````php |
| 242 | +$a = set( 1, 2, 3 ); |
| 243 | +$b = set( 2, 3, 4 ); |
| 244 | + |
| 245 | +$merged = set_merge( $a, $b ); |
| 246 | + |
| 247 | +// outputs [1,2,3,4] |
| 248 | +var_Dump( $merged->values() ); |
| 249 | +```` |
195 | 250 | Contributions and changes welcome! |
0 commit comments