lang/php/ DataStructures
Sets
<?php
require_once("Set.php"); // from: https://github.com/jakewhiteley/php-sets
$a = new PhpSets\Set(1,2,3,4,5);
$b = new PhpSets\Set(4,5,6,7,8);
$aub = $a->union($b);
$anb = $a->intersect($b);
$a_b = $a->difference($b);
$b_a = $b->difference($a);
$axb = $a->symmetricDifference($b);
to construct from an array, we need to use ReflectionClass:
<?php
require_once("Set.php"); // from: https://github.com/jakewhiteley/php-sets
use PhpSets\Set;
$set_reflector = new ReflectionClass("PhpSets\Set"); # note that we need the full namespace\class here
$a = new Set(1,2,3,4,5);
$x = $a->union($set_reflector->newInstanceArgs([10,11,12]));
var_dump($x);
# for convenience
function newset($elements) {
$set_reflector = new ReflectionClass("PhpSets\Set"); # note that we need the full namespace\class here
return $set_reflector->newInstanceArgs($elements);
}
# and then we can do
$x = $a->union(newset([10,11,12]));
var_dump($x);