1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace KDTree\Structure; |
6
|
|
|
|
7
|
|
|
use KDTree\{Exceptions\InvalidDimensionsCount, |
8
|
|
|
Exceptions\InvalidPointProvided, |
9
|
|
|
Exceptions\PointNotFound, |
10
|
|
|
Interfaces\PointInterface, |
11
|
|
|
Interfaces\PointsListInterface}; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PointsList |
15
|
|
|
* |
16
|
|
|
* @package KDTree\Structure |
17
|
|
|
*/ |
18
|
|
|
final class PointsList implements PointsListInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $dimensions; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var PointInterface[] |
27
|
|
|
*/ |
28
|
|
|
private $container = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param int $dimensions |
32
|
|
|
* |
33
|
|
|
* @throws InvalidDimensionsCount |
34
|
|
|
*/ |
35
|
|
|
public function __construct(int $dimensions) |
36
|
|
|
{ |
37
|
|
|
if ($dimensions < 1) { |
38
|
|
|
throw new InvalidDimensionsCount(); |
39
|
|
|
} |
40
|
|
|
$this->dimensions = $dimensions; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
|
|
public function next(): void |
47
|
|
|
{ |
48
|
|
|
next($this->container); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
*/ |
54
|
|
|
public function key(): ?string |
55
|
|
|
{ |
56
|
|
|
return key($this->container); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
*/ |
62
|
|
|
public function valid(): bool |
63
|
|
|
{ |
64
|
|
|
return null !== $this->current(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
*/ |
70
|
|
|
public function current(): ?PointInterface |
71
|
|
|
{ |
72
|
|
|
return current($this->container) instanceof PointInterface ? current($this->container) : null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
|
|
public function rewind(): void |
79
|
|
|
{ |
80
|
|
|
reset($this->container); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritDoc |
85
|
|
|
*/ |
86
|
|
|
public function count(): int |
87
|
|
|
{ |
88
|
|
|
return count($this->container); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
public function getDimensions(): int |
95
|
|
|
{ |
96
|
|
|
return $this->dimensions; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param PointInterface $point |
101
|
|
|
* |
102
|
|
|
* @return PointsListInterface<PointInterface> |
103
|
|
|
* @throws InvalidPointProvided |
104
|
|
|
*/ |
105
|
|
|
public function addPoint(PointInterface $point): PointsListInterface |
106
|
|
|
{ |
107
|
|
|
if ($point->getDimensions() !== $this->dimensions) { |
108
|
|
|
throw new InvalidPointProvided(); |
109
|
|
|
} |
110
|
|
|
$this->container[$this->prepareKey($point)] = $point; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param PointInterface $point |
117
|
|
|
* |
118
|
|
|
* @return PointsListInterface<PointInterface> |
119
|
|
|
* @throws PointNotFound |
120
|
|
|
*/ |
121
|
|
|
public function removePoint(PointInterface $point): PointsListInterface |
122
|
|
|
{ |
123
|
|
|
if (!$this->pointExists($point)) { |
124
|
|
|
throw new PointNotFound(); |
125
|
|
|
} |
126
|
|
|
unset($this->container[$this->prepareKey($point)]); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param PointInterface $point |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function pointExists(PointInterface $point): bool |
137
|
|
|
{ |
138
|
|
|
return isset($this->container[$this->prepareKey($point)]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param PointInterface $point |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
private function prepareKey(PointInterface $point): string |
147
|
|
|
{ |
148
|
|
|
return implode('_', $point->getAxises()); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|