/** * This method unsubscribes a listener. * * @access public * @static * @param IHashMap\Type $xs the left operand * @param ITuple\Type $entry a tuple containing the key and listener * @return IHashMap\Type the map */ public static function unsubscribe(IHashMap\Type $xs, ITuple\Type $entry) : IHashMap\Type { $k = $entry->first(); if ($xs->__hasKey($k)) { $ys = ILinkedList\Module::delete($xs->item($k), $entry->second()); $zs = IHashMap\Module::putEntry($xs, ITuple\Type::box2($k, $ys)); return $zs; } return $xs; }
/** * This method tests the "split" method. * * @dataProvider data_split */ public function test_split(array $provided, array $expected) { if (is_array($provided[1])) { $p0 = IRegex\Module::split(IRegex\Type::box($provided[0]), ITuple\Type::box2(IString\Type::box($provided[1][0]), IInt32\Type::box($provided[1][1]))); } else { $p0 = IRegex\Module::split(IRegex\Type::box($provided[0]), IString\Type::box($provided[1])); } $e0 = $expected[0]; $this->assertInstanceOf('\\Saber\\Data\\IArrayList\\Type', $p0); $this->assertSame($e0, $p0->unbox(1)); }
/** * This method adds the item with the specified key to the collection (if it doesn't * already exist). * * @access public * @final * @param Core\Type $key the key to associate with * the item * @param Core\Type $item the item to be stored * @return IHashMap\Type the hash map * * @see http://stackoverflow.com/questions/4980757/how-do-hashtables-deal-with-collisions */ public final function putEntry(Core\Type $key, Core\Type $item) : IHashMap\Type { $hashCode = $key->__hashCode(); if (array_key_exists($hashCode, $this->value)) { $bucket = $this->value[$hashCode]; foreach ($bucket as $entry) { if ($entry->first()->__eq($key)) { return $this; } } } $this->value[$hashCode][] = ITuple\Type::box2($key, $item); return $this; }
/** * This method returns a tuple where the first item contains the first "n" characters * in the string and the second item contains the remainder. * * @access public * @static * @param IString\Type $xs the string * @param IInt32\Type $n the number of characters to take * @return ITuple\Type the tuple */ public static function split(IString\Type $xs, IInt32\Type $n) : ITuple\Type { return ITuple\Type::box2(IString\Module::take($xs, $n), IString\Module::drop($xs, $n)); }
/** * This method tests the "item" related methods. */ public function test_items() { $p0 = ITuple\Type::box2(IInt32\Type::zero(), IInt32\Type::one(), IInt32\Type::box(2)); $this->assertSame(0, $p0->item(IInt32\Type::zero())->unbox()); $this->assertSame(1, $p0->item(IInt32\Type::one())->unbox()); $this->assertSame(2, $p0->item(IInt32\Type::box(2))->unbox()); $this->assertSame(0, $p0->first()->unbox()); $this->assertSame(1, $p0->second()->unbox()); }
/** * This method returns a pair of hash maps: those items that satisfy the predicate and * those items that do not satisfy the predicate. * * @access public * @static * @param IHashMap\Type $xs the hash map to be partitioned * @param callable $predicate the predicate function to be used * @return ITuple\Type the results */ public static function partition(IHashMap\Type $xs, callable $predicate) : ITuple\Type { $passed = IHashMap\Type::empty_(); $failed = IHashMap\Type::empty_(); $xi = IHashMap\Module::iterator($xs); $i = IInt32\Type::zero(); foreach ($xi as $k => $v) { $entry = ITuple\Type::box2($k, $v); if ($predicate($entry, $i)->unbox()) { $passed->putEntry($entry->first(), $entry->second()); } else { $failed->putEntry($entry->first(), $entry->second()); } } return ITuple\Type::box2($passed, $failed); }
/** * This method returns a pair of hash sets: those items that satisfy the predicate and * those items that do not satisfy the predicate. * * @access public * @static * @param IHashSet\Type $xs the hash set to be partitioned * @param callable $predicate the predicate function to be used * @return ITuple\Type the results */ public static function partition(IHashSet\Type $xs, callable $predicate) : ITuple\Type { $passed = IHashSet\Type::empty_(); $failed = IHashSet\Type::empty_(); $xi = IHashSet\Module::iterator($xs); foreach ($xi as $i => $x) { if ($predicate($x, $i)->unbox()) { $passed->putItem($x); } else { $failed->putItem($x); } } return ITuple\Type::box2($passed, $failed); }
/** * This method returns a tuple with the items swapped. * * @access public * @static * @param ITuple\Type $xs the left operand * @return ITuple\Type a tuple with the item swapped */ public static function swap(ITuple\Type $xs) : ITuple\Type { return ITuple\Type::box2($xs->second(), $xs->first()); }
/** * This method provides the data for testing that a value is of a particular size. * * @return array */ public function dataSize() { $data = array(array(array(), array(0)), array(array(ITuple\Type::box2(IString\Type::box('key0'), IInt32\Type::zero())), array(1)), array(array(ITuple\Type::box2(IString\Type::box('key0'), IInt32\Type::zero()), ITuple\Type::box2(IString\Type::box('key1'), IInt32\Type::one())), array(2))); return $data; }
/** * This method returns a new list of tuple pairings. * * @access public * @static * @param IArrayList\Type $xs the left operand * @param IArrayList\Type $ys the right operand * @return IArrayList\Type a new list of tuple pairings */ public static function zip(IArrayList\Type $xs, IArrayList\Type $ys) : IArrayList\Type { $buffer = array(); $length = IInt32\Module::min($xs->length(), $ys->length()); for ($i = IInt32\Type::zero(); IInt32\Module::lt($i, $length)->unbox(); $i = IInt32\Module::increment($i)) { $buffer[] = ITuple\Type::box2($xs->item($i), $ys->item($i)); } return IArrayList\Type::box($buffer); }
/** * This method returns a new list of tuple pairings. * * @access public * @static * @param ILinkedList\Type $xs the left operand * @param ILinkedList\Type $ys the right operand * @return ILinkedList\Type a new list of tuple pairings */ public static function zip(ILinkedList\Type $xs, ILinkedList\Type $ys) : ILinkedList\Type { $start = ILinkedList\Type::nil(); $tail = null; for ($as = $xs, $bs = $ys; !$as->__isEmpty() && !$bs->__isEmpty(); $as = $as->tail(), $bs = $bs->tail()) { $tuple = ITuple\Type::box2($as->head(), $bs->head()); $cons = ILinkedList\Type::cons($tuple); if ($tail !== null) { $tail->tail = $cons; } else { $start = $cons; } $tail = $cons; } return $start; }