Skip to content

Commit

Permalink
iterable -> iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-on committed Nov 9, 2020
1 parent e9e21a2 commit d7f2a36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/Interval.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Danon\IntervalTree;

use InvalidArgumentException;
Expand Down Expand Up @@ -83,4 +84,4 @@ public static function comparableLessThan($val1, $val2): bool
{
return $val1 < $val2;
}
}
}
38 changes: 18 additions & 20 deletions src/IntervalTree.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

namespace Danon\IntervalTree;

use Iterator;

class IntervalTree
{
public $root;
Expand All @@ -11,7 +14,6 @@ class IntervalTree
*/
public function __construct()
{
$this->root = null;
$this->nilNode = new Node();
}

Expand Down Expand Up @@ -88,9 +90,9 @@ public function isEmpty()
* Iterator of nodes which keys intersect with given interval
* If no values stored in the tree, returns array of keys which intersect given interval
* @param array $interval
* @return iterable
* @return Iterator
*/
public function iterateIntersections(array $interval): iterable
public function iterateIntersections(array $interval): Iterator
{
$searchNode = new Node($interval);
yield from $this->treeSearchInterval($this->root, $searchNode);
Expand Down Expand Up @@ -179,7 +181,7 @@ public function remove($key, $value): bool
*/
public function foreach($visitor)
{
$this->treeWalk($this->root, function ($node) {
$this->treeWalk($this->root, function ($node) use ($visitor) {
return $visitor($node->item->key, $node->item->value);
});
}
Expand Down Expand Up @@ -237,7 +239,7 @@ public function treeInsert($insertNode)
}

// After insertion insert_node may have red-colored parent, and this is a single possible violation
// Go upwords to the root and re-color until violation will be resolved
// Go upwards to the root and re-color until violation will be resolved
public function insertFixup($insertNode)
{
$currentNode = null;
Expand Down Expand Up @@ -291,8 +293,8 @@ public function insertFixup($insertNode)

public function treeDelete($deleteNode)
{
$cutNode; // node to be cut - either delete_node or successor_node ("y" from 14.4)
$fixNode; // node to fix rb tree property ("x" from 14.4)
$cutNode = null; // node to be cut - either delete_node or successor_node ("y" from 14.4)
$fixNode = null; // node to fix rb tree property ("x" from 14.4)

if ($deleteNode->left === $this->nilNode || $deleteNode->right === $this->nilNode) { // delete_node has less then 2 children
$cutNode = $deleteNode;
Expand All @@ -307,10 +309,7 @@ public function treeDelete($deleteNode)
$fixNode = $cutNode->right;
}

// remove cut_node from parent
/*if (fix_node !== this.nil_node) {*/
$fixNode->parent = $cutNode->parent;
/*}*/

if ($cutNode === $this->root) {
$this->root = $fixNode;
Expand Down Expand Up @@ -341,7 +340,6 @@ public function treeDelete($deleteNode)
public function deleteFixup($fixNode)
{
$currentNode = $fixNode;
$brotherNode;

while ($currentNode !== $this->root && $currentNode->parent !== null && $currentNode->color === Node::COLOR_BLACK) {
if ($currentNode === $currentNode->parent->left) { // fix node is left child
Expand All @@ -353,8 +351,10 @@ public function deleteFixup($fixNode)
$brotherNode = $currentNode->parent->right; // update brother
}
// Derive to cases 2..4: brother is black
if ($brotherNode->left->color === Node::COLOR_BLACK &&
$brotherNode->right->color === Node::COLOR_BLACK) { // case 2: both nephews black
if (
$brotherNode->left->color === Node::COLOR_BLACK &&
$brotherNode->right->color === Node::COLOR_BLACK
) { // case 2: both nephews black
$brotherNode->color = Node::COLOR_RED; // re-color brother
$currentNode = $currentNode->parent; // continue iteration
} else {
Expand All @@ -381,8 +381,10 @@ public function deleteFixup($fixNode)
$brotherNode = $currentNode->parent->left; // update brother
}
// Go to cases 2..4
if ($brotherNode->left->color === Node::COLOR_BLACK &&
$brotherNode->right->color === Node::COLOR_BLACK) { // case 2
if (
$brotherNode->left->color === Node::COLOR_BLACK &&
$brotherNode->right->color === Node::COLOR_BLACK
) { // case 2
$brotherNode->color = Node::COLOR_RED; // re-color brother
$currentNode = $currentNode->parent; // continue iteration
} else {
Expand Down Expand Up @@ -464,10 +466,6 @@ public function localMaximum($node)

public function treeSuccessor($node)
{
$nodeSuccessor;
$currentNode;
$parentNode;

if ($node->right !== $this->nilNode) {
$nodeSuccessor = $this->localMinimum($node->right);
} else {
Expand Down Expand Up @@ -599,7 +597,7 @@ public function testBlackHeightProperty($node)
$heightRight = 1;
}
if ($heightLeft !== $heightRight) {
throw new Error('Red-black height property violated');
throw new \Exception('Red-black height property violated');
}
$height += $heightLeft;
return $height;
Expand Down

0 comments on commit d7f2a36

Please sign in to comment.