Reputation: 1888
Many languages have a "Set" data structure which typically only contain unique values from a particular collection of objects. For instance, if you add the integers 1, 4, 5, 1, 2, and 6 to the set, in the end the set would actually only contain 1, 4, 5, 2 and 6.
I haven't observed any kind of collection like this in PHP. Is there such a collection built into the language, or does something need to be custom rolled?
Upvotes: 2
Views: 177
Reputation: 1
In the old days, Turbo Pascal supported a SET-type for storing values upto 255. The mechanism used was quite simple, using an array of 32 bytes, each value in the set was stored in one of the 32*8=256 bits in this array. Value 0 stored in Bit 0 of array[0], value 1 stored in Bit 1 of array[0] .. value 255 stored in bit 7 of array[31] :
Locating the value byte and bit :
BYTEPOS = $value >> 3 (0..7 => 0, 8..15 => 1 etc)
BITPOS = $value & 7 (0,8,16 ... => bit 0 1,9,17 => bit 1 etc)
Empty set [0..255];
$theSET = array( 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 );
Adding a value :
$theSET[ $value >> 3 ] = $theSET [ $value >> 3 ] | (1 << ($value & 7));
Removing :
$theSET[ $value >> 3 ] = $theSET [ $value >> 3 ] & ~(1 << ($value & 7));
Checking :
inSET = ($theSET [ $value >> 3 ] & (1 << ($value & 7)) == (1 << ($value & 7));
Operations on two sets can be done as iterations,
Combining:
$SetA[$i] = $SetA[$i] | $SetB[$i]
Only values not in SetB:
$SetA[$i] = $SetA[$i] & (~$SetB[$i])
ect
Upvotes: 0
Reputation: 31621
There isn't a collection like this that is part of PHP core. You can get pretty close by using array keys or using SplObjectStorage
.
Upvotes: 1
Reputation: 12996
Doubt there is anything built in, but it's very simple to achieve:
<?
$set = array(1, 4, 5, 1, 2, 6); // original set
$set = array_unique($set);
//$set = array_values($set); // add this to reset the keys
print_r($set); // returns 1, 4, 5, 2 and 6.
Upvotes: 1
Reputation: 1888
I haven't noticed any type of "Set" collection in the PHP language, so I've rolled my own method for simulating a set. I approach a set like a hash table. The keys in a hash table by default belong to a set. You can use this to your advantage.
For example, let's say I have a randomly generated array of integers $random_integers
. The following would yield a unique array of values contained in $random_integers
:
$random = array(1,1,1,3,4,5,3,45,7);
$set = array();
foreach($random as $key => $value) {
$set[$value] = 1;
}
$set = array_keys($set);
print_r($random);
print_r($set);
array_keys
is a function included in PHP. You can use this to extract all of the keys from your hash table, which are equivalent to a set.
A pitfall here is that only integers and strings can be used as array keys. So, any objects that you are using will need some kind of string or integer representation if you wish to include them in your set.
Also note in my example, you don't need to take the extra step to extract the array keys. $set
is already a set. Calling array_keys
simply gives you the unique values if you happen to need them.
Upvotes: 2