public function compareTo(IComparable $o) { $rv = 0; if ($this->getValue() < $o->getValue()) { $rv = -1; } elseif ($this->getValue() > $o->getValue()) { $rv = 1; } return $rv; }
/** * Tests whether this set is equal to the specified set. * It is assumed that the specified set is an instance of * the <code>SetAsBitVector</code> class. * * @param object ISet $set The set to compare with this set. * @return boolean True if the sets are equal; false otherwise. */ public function eq(IComparable $set) { if ($this->getClass() != $set->getClass()) { throw new TypeError(); } if ($this->universeSize != $set->universeSize) { throw new ArgumentError(); } for ($i = 0; $i < $this->vector->getLength(); ++$i) { if ($this->vector[$i] != $set->vector[$i]) { return false; } } return true; }
/** * Tests whether this set is equal to the specified set. * It is assumed that the specified set is an instance of * the SetAsArray class. * * @param object ISet $set The set to compare with this set. * @return boolean True if the sets are equal; false otherwise. */ public function eq(IComparable $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; }
/** * Finds an object in this M-way tree that matches * the specified object. * Uses linear search. * * @param object IComparable $obj The object to match. * @return mixed An object that matches the specified one; * NULL if the tree is empty. */ public function find(IComparable $obj) { if ($this->isEmpty()) { return NULL; } for ($i = $this->count; $i > 0; --$i) { $diff = $obj->compare($this->key[$i]); if ($diff == 0) { return $this->key[$i]; } if ($diff > 0) { break; } } return $this->subtree[$i]->find($obj); }
/** * Compares this association with the specified comparable object. * The specified comparable object is assumed to be an Association instance. * The two associations are compared by comparing just their keys. * * @param object IComparable $obj * The object with which this object is compared. * @return integer A number less than zero if the key of this association * is less than the key of the specified one; * a number greater than zero if the key of this association * is greater than the key of the specified one; * zero if the keys of both associations are equal. */ protected function compareTo(IComparable $obj) { return $this->key->compare($obj->getKey()); }
/** * Remove HashTable item * * @param IComparable $pSource Item to remove * @throws Exception */ public function remove(IComparable $pSource = null) { if (isset($this->_items[$pSource->getHashCode()])) { unset($this->_items[$pSource->getHashCode()]); $deleteKey = -1; foreach ($this->_keyMap as $key => $value) { if ($deleteKey >= 0) { $this->_keyMap[$key - 1] = $value; } if ($value == $pSource->getHashCode()) { $deleteKey = $key; } } unset($this->_keyMap[count($this->_keyMap) - 1]); } }
/** * Compares this binary tree with the specified comparable object. * The specified comparable object is assumed to be a BinaryTree instance. * * @param object IComparable $obj * The comparable object with which to compare this * binary tree. * @return integer A number less than zero if this binary tree is less * than the specified binary tree; * greater than zero if this binary tree is greater * than the specified binary tree; * zero if the two trees are identical. */ protected function compareTo(IComparable $obj) { if ($this->isEmpty()) { return $obj->isEmpty() ? 0 : -1; } elseif ($obj->isEmpty()) { return 1; } else { $result = $this->getKey()->compare($obj->getKey()); if ($result == 0) { $result = $this->getLeft()->compare($obj->getLeft()); } if ($result == 0) { $result = $this->getRight()->compare($obj->getRight()); } return $result; } }
/** * Withdraws the specified object from this binary search tree. * * @param object IObject $obj The object to be withdrawn from this tree. */ public function withdraw(IComparable $obj) { if ($this->isEmpty()) { throw new ArgumentError(); } $diff = $obj->compare($this->getKey()); if ($diff == 0) { if (!$this->getLeft()->isEmpty()) { $max = $this->getLeft()->findMax(); $this->key = $max; $this->getLeft()->withdraw($max); } elseif (!$this->getRight()->isEmpty()) { $min = $this->getRight()->findMin(); $this->key = $min; $this->getRight()->withdraw($min); } else { $this->detachKey(); } } else { if ($diff < 0) { $this->getLeft()->withdraw($obj); } else { $this->getRight()->withdraw($obj); } } $this->balance(); }
/** * Compares this object with the given object. * * @param object IComparable $object A comparable object. * @return integer A number less than zero * if this object is less than the given object, * zero if this object equals the given object, and * a number greater than zero * if this object is greater than the given object. */ public function compare(IComparable $object) { $result = 0; if ($this->getClass() == $object->getClass()) { $result = $this->compareTo($object); } else { $result = strcmp($this->getClass()->getName(), $object->getClass()->getName()); } return $result; }
/** * Tests whether this multiset is equal to 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 multisets are equal; false otherwise. */ public function eq(IComparable $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) { if (ne($p->getDatum(), $q->getDatum())) { return false; } $p = $p->getNext(); $q = $q->getNext(); } if ($p !== NULL || $q !== NULL) { return false; } else { return true; } }
/** * Tests whether the given comparable object * is a member of this partition. * * @param object IComparable $obj The object for which to look. * @return boolean True if the object is a member of this partition; * false otherwise. */ public function contains(IComparable $obj) { return $obj->isMemberOf($this); }