Skip to content

Commit 86e58dc

Browse files
committed
initial
0 parents  commit 86e58dc

File tree

7 files changed

+789
-0
lines changed

7 files changed

+789
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# PHP Interval tree
2+
Is an implementation of interval binary search tree according to Thomas H. Cormen book "Introduction to Algorithms".

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "dan-on/php-interval-tree",
3+
"description": "Is an implementation of interval binary search tree according to Thomas Cormen book \"Introduction to Algorithms\".",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Daniil Akhmetov",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"IntervalTree\\": "src/"
15+
}
16+
},
17+
"require": {
18+
"php": "^5.3 || ^7.0"
19+
}
20+
}

examples/index.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once 'vendor/autoload.php';
3+
4+
use IntervalTree\IntervalTree;
5+
6+
$tree = new IntervalTree();
7+
$intervals = [[6,8],[1,4],[2,3],[5,12],[1,1],[3,5],[5,7]];
8+
9+
// Insert interval as a key and string "val0", "val1" etc. as a value
10+
for ($i=0; $i < count($intervals); $i++) {
11+
$tree->insert($intervals[$i],"val" . $i);
12+
}
13+
14+
// Get array of keys sorted in ascendant order
15+
$sorted_intervals = $tree->getKeys(); // expected array [[1,1],[1,4],[5,7],[5,12],[6,8]]
16+
17+
// Search items which keys intersect with given interval, and return array of values
18+
$valuesInRange = $tree->search([2,3], function($value, $key) {
19+
echo $value . "\n";
20+
});

src/Interval.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
namespace IntervalTree;
3+
4+
class Interval {
5+
6+
public $low;
7+
public $high;
8+
9+
public function __construct($low, $high)
10+
{
11+
$this->low = $low;
12+
$this->high = $high;
13+
}
14+
15+
16+
public function lessThan(Interval $otherInterval)
17+
{
18+
return $this->low < $otherInterval->low ||
19+
$this->low == $otherInterval->low && $this->high < $otherInterval->high;
20+
}
21+
22+
public function equalTo(Interval $otherInterval)
23+
{
24+
return $this->low == $otherInterval->low && $this->high == $otherInterval->high;
25+
}
26+
27+
public function intersect(Interval $otherInterval)
28+
{
29+
return !$this->notIntersect($otherInterval);
30+
}
31+
32+
public function notIntersect(Interval $otherInterval)
33+
{
34+
return ($this->high < $otherInterval->low || $otherInterval->high < $this->low);
35+
}
36+
37+
public function merge(Interval $otherInterval)
38+
{
39+
return new Interval(
40+
$this->low === null ? $otherInterval->low : min($this->low, $otherInterval->low),
41+
$this->high === null ? $otherInterval->high : max($this->high, $otherInterval->high)
42+
);
43+
}
44+
45+
/**
46+
* Returns how key should return
47+
*/
48+
public function output() {
49+
return [$this->low, $this->high];
50+
}
51+
52+
53+
54+
/**
55+
* Function returns maximum between two comparable values
56+
* @param interval1
57+
* @param interval2
58+
* @returns {Interval}
59+
*/
60+
public static function comparableMax($interval1, $interval2) {
61+
return $interval1->merge($interval2);
62+
}
63+
64+
public function getMax()
65+
{
66+
return clone $this;
67+
}
68+
69+
/**
70+
* Predicate returns true if first value less than second value
71+
* @param val1
72+
* @param val2
73+
* @returns {boolean}
74+
*/
75+
public static function comparableLessThan($val1, $val2) {
76+
return $val1 < $val2;
77+
}
78+
}

0 commit comments

Comments
 (0)