Skip to content

Commit cb671be

Browse files
authored
Update README.md
1 parent 46cf77a commit cb671be

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

README.md

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
# 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
33

44
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.
55

66
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.
77

88
Sets require a min PHP version of 5.4.
99

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
1510

16-
## Composer
11+
## Installing
12+
You can download the latest release via the releases link on this page.
13+
1714
PHP-Sets is available via [Composer/Packagist](https://packagist.org/packages/jakewhiteley/php-sets), so just add this line to your composer.json file:
1815

1916
`"jakewhiteley/php-sets": "~1.0"`
@@ -23,6 +20,13 @@ or
2320
`composer require jakewhiteley/php-sets`
2421

2522
## 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+
2630
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.
2731
````php
2832
// Function creation method
@@ -192,4 +196,55 @@ foreach ( $vals as $val ){
192196
// prints 12
193197
````
194198

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+
````
195250
Contributions and changes welcome!

0 commit comments

Comments
 (0)