Skip to content

Commit 46cf77a

Browse files
authored
Update functions.php
1 parent 3b2e293 commit 46cf77a

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

functions.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,123 @@
11
<?php
22

3+
/**
4+
* Native-like helper functions for working with Sets
5+
*/
6+
37
/**
48
* An additional function which allows a non OOP initialization of the Set object
59
*
610
* @param mixed ...$args Values to initially add to the Set object
11+
* @return \JakeWhiteley\PhpSets\Set
712
*/
813
function set()
914
{
1015
return ( new \ReflectionClass('\JakeWhiteley\PhpSets\Set') )->newInstanceArgs( func_get_args() );
1116
}
17+
18+
/**
19+
* Returns a new Set which contains the unique items of both sets
20+
*
21+
* @param \JakeWhiteley\PhpSets\Set $set The original set
22+
* @param \JakeWhiteley\PhpSets\Set $additionalSet The set to append
23+
* @return \JakeWhiteley\PhpSets\Set A new set containing the merged items
24+
*/
25+
function set_merge( \JakeWhiteley\PhpSets\Set $set, \JakeWhiteley\PhpSets\Set $additionalSet )
26+
{
27+
$iterator = $additionalSet->entries();
28+
29+
// create a copy of $set
30+
$merged = new \JakeWhiteley\PhpSets\Set();
31+
$merged->exchangeArray( $set->values() );
32+
33+
// add values from $additionalSet if not present
34+
while ( $iterator->valid() ) {
35+
if ( ! $set->has( $iterator->current() ) )
36+
$merged->add( $iterator->current() );
37+
$iterator->next();
38+
}
39+
$iterator->rewind();
40+
41+
return $merged;
42+
}
43+
44+
/**
45+
* Returns a new Set object containing the common elements between two given sets
46+
*
47+
* @param \JakeWhiteley\PhpSets\Set $set
48+
* @param \JakeWhiteley\PhpSets\Set $additionalSet
49+
* @return \JakeWhiteley\PhpSets\Set
50+
*/
51+
function set_intersect( \JakeWhiteley\PhpSets\Set $set, \JakeWhiteley\PhpSets\Set $additionalSet )
52+
{
53+
$iterator = $additionalSet->entries();
54+
$intersect = new \JakeWhiteley\PhpSets\Set;
55+
56+
while ( $iterator->valid() ) {
57+
if ( $set->has( $iterator->current() ) )
58+
$intersect->add( $iterator->current() );
59+
$iterator->next();
60+
}
61+
$iterator->rewind();
62+
63+
return $intersect;
64+
}
65+
66+
/**
67+
* Returns a new Set containing all uncommon items between the two given Sets
68+
*
69+
* @todo This is not very efficient as it iterates both Sets completely
70+
*
71+
* @param \JakeWhiteley\PhpSets\Set $set
72+
* @param \JakeWhiteley\PhpSets\Set $additionalSet
73+
* @return \JakeWhiteley\PhpSets\Set
74+
*/
75+
function set_diff( \JakeWhiteley\PhpSets\Set $set, \JakeWhiteley\PhpSets\Set $additionalSet )
76+
{
77+
$originalArray = $set->entries();
78+
$iterator = $additionalSet->entries();
79+
$intersect = new \JakeWhiteley\PhpSets\Set;
80+
81+
// check $set values
82+
while ( $originalArray->valid() ) {
83+
if ( ! $additionalSet->has( $originalArray->current() ) )
84+
$intersect->add( $originalArray->current() );
85+
$originalArray->next();
86+
}
87+
88+
// check $additionalSet values
89+
while ( $iterator->valid() ) {
90+
if ( ! $set->has( $iterator->current() ) )
91+
$intersect->add( $iterator->current() );
92+
$iterator->next();
93+
}
94+
// reset both Set internal pointers
95+
$originalArray->rewind();
96+
$iterator->rewind();
97+
98+
return $intersect;
99+
}
100+
101+
102+
/**
103+
* Checks if a given $additionalSet is a subset of $set
104+
* All values should be present, but ordinality does not matter
105+
*
106+
* @param \JakeWhiteley\PhpSets\Set $set The original Set to check against
107+
* @param \JakeWhiteley\PhpSets\Set $additionalSet The subset
108+
* @return bool Whether $additionalSet was a subset of $set
109+
*/
110+
function set_subset( \JakeWhiteley\PhpSets\Set $set, \JakeWhiteley\PhpSets\Set $additionalSet )
111+
{
112+
$iterator = $additionalSet->entries();
113+
114+
// iterate through $additionalSet and return false is an uncommon value is present
115+
while ( $iterator->valid() ) {
116+
if ( ! $set->has( $iterator->current() ) )
117+
return false;
118+
$iterator->next();
119+
}
120+
$iterator->rewind();
121+
122+
return true;
123+
}

0 commit comments

Comments
 (0)