Skip to content

Commit d7f2a36

Browse files
committed
iterable -> iterator
1 parent e9e21a2 commit d7f2a36

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/Interval.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Danon\IntervalTree;
34

45
use InvalidArgumentException;
@@ -83,4 +84,4 @@ public static function comparableLessThan($val1, $val2): bool
8384
{
8485
return $val1 < $val2;
8586
}
86-
}
87+
}

src/IntervalTree.php

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
2+
23
namespace Danon\IntervalTree;
34

5+
use Iterator;
6+
47
class IntervalTree
58
{
69
public $root;
@@ -11,7 +14,6 @@ class IntervalTree
1114
*/
1215
public function __construct()
1316
{
14-
$this->root = null;
1517
$this->nilNode = new Node();
1618
}
1719

@@ -88,9 +90,9 @@ public function isEmpty()
8890
* Iterator of nodes which keys intersect with given interval
8991
* If no values stored in the tree, returns array of keys which intersect given interval
9092
* @param array $interval
91-
* @return iterable
93+
* @return Iterator
9294
*/
93-
public function iterateIntersections(array $interval): iterable
95+
public function iterateIntersections(array $interval): Iterator
9496
{
9597
$searchNode = new Node($interval);
9698
yield from $this->treeSearchInterval($this->root, $searchNode);
@@ -179,7 +181,7 @@ public function remove($key, $value): bool
179181
*/
180182
public function foreach($visitor)
181183
{
182-
$this->treeWalk($this->root, function ($node) {
184+
$this->treeWalk($this->root, function ($node) use ($visitor) {
183185
return $visitor($node->item->key, $node->item->value);
184186
});
185187
}
@@ -237,7 +239,7 @@ public function treeInsert($insertNode)
237239
}
238240

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

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

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

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

315314
if ($cutNode === $this->root) {
316315
$this->root = $fixNode;
@@ -341,7 +340,6 @@ public function treeDelete($deleteNode)
341340
public function deleteFixup($fixNode)
342341
{
343342
$currentNode = $fixNode;
344-
$brotherNode;
345343

346344
while ($currentNode !== $this->root && $currentNode->parent !== null && $currentNode->color === Node::COLOR_BLACK) {
347345
if ($currentNode === $currentNode->parent->left) { // fix node is left child
@@ -353,8 +351,10 @@ public function deleteFixup($fixNode)
353351
$brotherNode = $currentNode->parent->right; // update brother
354352
}
355353
// Derive to cases 2..4: brother is black
356-
if ($brotherNode->left->color === Node::COLOR_BLACK &&
357-
$brotherNode->right->color === Node::COLOR_BLACK) { // case 2: both nephews black
354+
if (
355+
$brotherNode->left->color === Node::COLOR_BLACK &&
356+
$brotherNode->right->color === Node::COLOR_BLACK
357+
) { // case 2: both nephews black
358358
$brotherNode->color = Node::COLOR_RED; // re-color brother
359359
$currentNode = $currentNode->parent; // continue iteration
360360
} else {
@@ -381,8 +381,10 @@ public function deleteFixup($fixNode)
381381
$brotherNode = $currentNode->parent->left; // update brother
382382
}
383383
// Go to cases 2..4
384-
if ($brotherNode->left->color === Node::COLOR_BLACK &&
385-
$brotherNode->right->color === Node::COLOR_BLACK) { // case 2
384+
if (
385+
$brotherNode->left->color === Node::COLOR_BLACK &&
386+
$brotherNode->right->color === Node::COLOR_BLACK
387+
) { // case 2
386388
$brotherNode->color = Node::COLOR_RED; // re-color brother
387389
$currentNode = $currentNode->parent; // continue iteration
388390
} else {
@@ -464,10 +466,6 @@ public function localMaximum($node)
464466

465467
public function treeSuccessor($node)
466468
{
467-
$nodeSuccessor;
468-
$currentNode;
469-
$parentNode;
470-
471469
if ($node->right !== $this->nilNode) {
472470
$nodeSuccessor = $this->localMinimum($node->right);
473471
} else {
@@ -599,7 +597,7 @@ public function testBlackHeightProperty($node)
599597
$heightRight = 1;
600598
}
601599
if ($heightLeft !== $heightRight) {
602-
throw new Error('Red-black height property violated');
600+
throw new \Exception('Red-black height property violated');
603601
}
604602
$height += $heightLeft;
605603
return $height;

0 commit comments

Comments
 (0)