|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MartanLV\Koki; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Augmented tree |
|
9
|
|
|
* Class StaticTree |
|
10
|
|
|
* @author yourname |
|
11
|
|
|
*/ |
|
12
|
|
|
class Tree extends Node |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* undocumented function |
|
16
|
|
|
* |
|
17
|
|
|
* @param $intervals array of Interval |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(array $intervals, bool $preSorted = false) |
|
21
|
|
|
{ |
|
22
|
|
|
!$preSorted && usort($intervals, function (IntervalInterface $i0, IntervalInterface $i) { |
|
23
|
|
|
return $i0->getStart() <=> $i->getStart(); |
|
24
|
|
|
}); |
|
25
|
|
|
$this->root = $this->toTree($intervals); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* An augmented tree can be built from a simple ordered tree, |
|
30
|
|
|
* for example a binary search tree or self-balancing binary search tree, |
|
31
|
|
|
* ordered by the 'low' values of the intervals. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function toTree(array $intervals) |
|
34
|
|
|
{ |
|
35
|
|
|
$max = 0; |
|
36
|
|
|
$len = count($intervals); |
|
37
|
|
|
$mid = (int)floor($len / 2); |
|
38
|
|
|
|
|
39
|
|
|
if (!$len) { |
|
40
|
|
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
array_walk($intervals, function (IntervalInterface $el) use (&$max) { |
|
44
|
|
|
if ($max < $el->getEnd()) { |
|
45
|
|
|
$max = $el->getEnd(); |
|
46
|
|
|
} |
|
47
|
|
|
}); |
|
48
|
|
|
$l_intervals = array_splice($intervals, 0, $mid); |
|
49
|
|
|
$r_intervals = array_splice($intervals, -$mid); |
|
50
|
|
|
$interval = $intervals ? reset($intervals) : array_pop($l_intervals); |
|
51
|
|
|
$interval = $interval ?? array_pop($r_intervals); |
|
52
|
|
|
|
|
53
|
|
|
return new Node( |
|
54
|
|
|
$interval, |
|
55
|
|
|
$this->toTree($l_intervals), |
|
56
|
|
|
$this->toTree($r_intervals), |
|
57
|
|
|
$max |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* returns intervals that exclusively fall within range given |
|
63
|
|
|
* to retrieve eather bound inclusevely, just modify parameters |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
public function select(int $low, int $high): array |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->root ? $this->root->select($low, $high) : []; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* understandable var dump |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __debugInfo() |
|
76
|
|
|
{ |
|
77
|
|
|
$arr = $this->all(); |
|
78
|
|
|
|
|
79
|
|
|
usort($arr, function (IntervalInterface $i0, IntervalInterface $i) { |
|
80
|
|
|
return $i0->getStart() <=> $i->getStart(); |
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
return $arr; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function interSelect(int $low, int $high): array |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->root ? $this->root->interSelect($low, $high) : []; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function yieldSelect(int $low, int $high) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->root ? $this->root->yieldSelect($low, $high) : []; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
public function all(): array |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->root ? $this->root->all() : []; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* undocumented function |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function yieldAll() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->root ? $this->root->yieldAll() : []; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|