1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MartanLV\Koki; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Node |
8
|
|
|
* @author yourname |
9
|
|
|
*/ |
10
|
|
|
class Node |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Interval |
14
|
|
|
*/ |
15
|
|
|
public $interval; |
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
public $max; |
20
|
|
|
/** |
21
|
|
|
* @var Node |
22
|
|
|
*/ |
23
|
|
|
public $left; |
24
|
|
|
/** |
25
|
|
|
* @var Node |
26
|
|
|
*/ |
27
|
|
|
public $right; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* undocumented function |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function __construct(IntervalInterface $interval, $left = null, $right = null, $max = 0) |
35
|
|
|
{ |
36
|
|
|
$this->interval = $interval; |
|
|
|
|
37
|
|
|
$this->left = $left; |
38
|
|
|
$this->right = $right; |
39
|
|
|
$this->max = $max ? $max : $interval->high; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* returns intervals that fall within interval range |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function all(): array |
48
|
|
|
{ |
49
|
|
|
return iterator_to_array($this->yieldAll(), false); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* undocumented function |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function yieldAll() |
58
|
|
|
{ |
59
|
|
|
return $this->yieldSelect(-1, $this->max + 1); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* returns intervals that touches a given range |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function interSelect(int $low, int $high): array |
68
|
|
|
{ |
69
|
|
|
return iterator_to_array($this->yieldInterSelect($low, $high), false); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* returns intervals that fall within interval range |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function select(int $low, int $high): array |
78
|
|
|
{ |
79
|
|
|
return iterator_to_array($this->yieldSelect($low, $high), false); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* returns intervals that touches a given range |
84
|
|
|
* |
85
|
|
|
* @return generator |
|
|
|
|
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function yieldInterSelect(int $low, int $high) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
/** |
90
|
|
|
* does current node matches? |
91
|
|
|
*/ |
92
|
|
|
if ($this->interval->getEnd() - $low < $high && $this->interval->getStart() + $high > $low) { |
93
|
|
|
yield $this->interval; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* since the node's low value is less than the "select end" value, |
98
|
|
|
* we must search in the right subtree. If it exists. |
99
|
|
|
*/ |
100
|
|
|
if ($this->right && $this->interval->getStart() < $high) { |
101
|
|
|
yield from $this->right->yieldInterSelect($low, $high); |
102
|
|
|
} |
103
|
|
|
/** |
104
|
|
|
* If the left subtree's max exceeds the quiery's low value, |
105
|
|
|
* so we must search the left subtree as well. |
106
|
|
|
*/ |
107
|
|
|
if ($this->left && $this->left->max > $low) { |
108
|
|
|
yield from $this->left->yieldSelect($low, $high); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* returns intervals that fall within interval range |
115
|
|
|
* |
116
|
|
|
* @return generator |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function yieldSelect(int $low, int $high) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
/** |
121
|
|
|
* does current node matches? |
122
|
|
|
*/ |
123
|
|
|
if ($this->interval->getEnd() < $high && $this->interval->getStart() > $low) { |
124
|
|
|
yield $this->interval; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* since the node's low value is less than the "select end" value, |
129
|
|
|
* we must search in the right subtree. If it exists. |
130
|
|
|
*/ |
131
|
|
|
if ($this->right && $this->interval->getStart() < $high) { |
132
|
|
|
yield from $this->right->yieldSelect($low, $high); |
133
|
|
|
} |
134
|
|
|
/** |
135
|
|
|
* If the left subtree's max exceeds the quiery's low value, |
136
|
|
|
* so we must search the left subtree as well. |
137
|
|
|
*/ |
138
|
|
|
if ($this->left && $this->left->max > $low) { |
139
|
|
|
yield from $this->left->yieldSelect($low, $high); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.