Skip to content

Commit 898f269

Browse files
committed
rewrite code for using iterators
1 parent c514568 commit 898f269

File tree

4 files changed

+226
-159
lines changed

4 files changed

+226
-159
lines changed

examples/index.php

+25-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require_once 'vendor/autoload.php';
33

44
use Danon\IntervalTree\IntervalTree;
5+
use Danon\IntervalTree\Interval;
6+
use Danon\IntervalTree\Node;
57

68
$tree = new IntervalTree();
79
$intervals = [[6,8],[1,4],[2,3],[5,12],[1,1],[3,5],[5,7]];
@@ -15,11 +17,30 @@
1517
$sorted_intervals = $tree->getKeys(); // expected array [[1,1],[1,4],[5,7],[5,12],[6,8]]
1618

1719
// Search items which keys intersect with given interval, and return array of values
18-
$valuesInRange = $tree->search([2,3], function($value, $key) {
19-
return $value;
20-
});
20+
$valuesInRange = $tree->iterateIntersections([2,3]);
2121

22-
print_r($valuesInRange);
22+
foreach($valuesInRange as $node) {
23+
echo $node->getValue() . "\n";
24+
}
25+
26+
echo $tree->hasIntersect([66,83]);
27+
echo $tree->countIntersections([2,3]);
28+
29+
print_r($tree->getIntersections([2,3]));
30+
31+
32+
// $tree = new IntervalTree();
33+
// $intervals = [[6,8],[1,4],[2,3],[5,12],[1,1],[3,5],[5,7]];
34+
35+
// // Insert interval as a key and string "val0", "val1" etc. as a value
36+
// for ($i=0; $i < count($intervals); $i++) {
37+
// $tree->insert($intervals[$i], "val" . $i);
38+
// }
39+
// $iterator = $tree->searchIterator(new Node(new Interval(2,3)), null);
40+
// foreach($iterator as $node) {
41+
// echo "123\n";
42+
// //print_r($node->getValue());
43+
// }
2344

2445
// Array
2546
// (

src/Interval.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
<?php
22
namespace Danon\IntervalTree;
3+
use InvalidArgumentException;
34

45
class Interval {
56

67
public $low;
78
public $high;
89

9-
public function __construct($low, $high)
10+
public function __construct(int $low, int $high)
1011
{
12+
if($low > $high) {
13+
throw new InvalidArgumentException('Low interval cannot be greater than high');
14+
}
15+
1116
$this->low = $low;
1217
$this->high = $high;
1318
}

0 commit comments

Comments
 (0)