/** * Tests whether this multiset is a subset of the specified multiset. * It is assumed that the specified multiset is an instance of * the MultisetAsLinkedList class. * * @param IMultiset $multiset The multiset to compare with this multiset. * @return boolean * True if the this multiset is a subset of the specified multiset; * false otherwise. */ public function isSubset(IMultiset $set) { if ($this->getClass() != $set->getClass()) { throw new TypeError(); } if ($this->universeSize != $set->universeSize) { throw new ArgumentError(); } $p = $this->list->getHead(); $q = $set->list->getHead(); while ($p !== NULL && $q !== NULL) { $diff = $p->getDatum() . compare($q->getDatum()); if ($diff == 0) { $p = $p->getNext(); $q = $q->getNext(); } elseif ($diff > 0) { $q = $q->getNext(); } else { return false; } } if ($p !== NULL) { return false; } else { return true; } }
/** * Tests whether this set is a subset of the specified set. * It is assumed that the specified set is an instance of * the MultisetAsArray class. * * @param object IMultiset $set The set to compare with this set. * @return boolean True if the this set is a subset of the specified set; * false otherwise. */ public function isSubset(IMultiset $set) { if ($this->getClass() != $set->getClass()) { throw new TypeError(); } if ($this->universeSize != $set->universeSize) { throw new ArgumentError(); } for ($item = 0; $item < $this->universeSize; ++$item) { if ($this->array[$item] <= $set->array[$item]) { return false; } } return true; }
/** * Multiset test method. * * @param object IMultiset $s1 A set to test. * @param object IMultiset $s2 A set to test. * @param object IMultiset $s3 A set to test. */ public static function test(IMultiset $s1, IMultiset $s2, IMultiset $s3) { printf("AbstractMultiset test program.\n"); for ($i = 0; $i < 4; ++$i) { $s1->insert(box($i)); } for ($i = 2; $i < 6; ++$i) { $s2->insert(box($i)); } $s3->insert(box(0)); $s3->insert(box(2)); printf("%s\n", str($s1)); printf("%s\n", str($s2)); printf("%s\n", str($s3)); printf("%s\n", str($s1->union($s2))); # union printf("%s\n", str($s1->intersection($s3))); # intersection printf("%s\n", str($s1->difference($s3))); # difference printf("Using foreach\n"); foreach ($s3 as $obj) { printf("%s\n", str($obj)); } printf("Using reduce\n"); $s3->reduce(create_function('$sum,$obj', 'printf("%s\\n", str($obj));'), ''); }