|
1
|
|
|
<?php namespace Mbh\Collection; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MBHFramework |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
|
7
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
|
8
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
use Mbh\Collection\Interfaces\Collection as CollectionInterface; |
|
12
|
|
|
use Mbh\Interfaces\Allocated as AllocatedInterface; |
|
13
|
|
|
use Traversable; |
|
14
|
|
|
use ArrayAccess; |
|
15
|
|
|
use IteratorAggregate; |
|
16
|
|
|
use Error; |
|
17
|
|
|
use OutOfBoundsException; |
|
18
|
|
|
use OutOfRangeException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A sequence of unique values. |
|
22
|
|
|
* |
|
23
|
|
|
* @package structures |
|
24
|
|
|
* @author Ulises Jeremias Cornejo Fandos <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class Set implements AllocatedInterface, ArrayAccess, CollectionInterface, IteratorAggregate |
|
27
|
|
|
{ |
|
28
|
|
|
use Traits\Collection; |
|
29
|
|
|
|
|
30
|
|
|
const MIN_CAPACITY = Map::MIN_CAPACITY; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Map internal map to store the values. |
|
34
|
|
|
*/ |
|
35
|
|
|
private $table; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Creates a new set using the values of an array or Traversable object. |
|
39
|
|
|
* The keys of either will not be preserved. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array|Traversable|null $values |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($values = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->table = new Map(); |
|
46
|
|
|
|
|
47
|
|
|
if (func_num_args()) { |
|
48
|
|
|
$this->add(...$values); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Adds zero or more values to the set. |
|
54
|
|
|
* |
|
55
|
|
|
* @param mixed ...$values |
|
56
|
|
|
*/ |
|
57
|
|
|
public function add(...$values) |
|
58
|
|
|
{ |
|
59
|
|
|
foreach ($values as $value) { |
|
60
|
|
|
$this->table->put($value, null); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Ensures that enough memory is allocated for a specified capacity. This |
|
66
|
|
|
* potentially reduces the number of reallocations as the size increases. |
|
67
|
|
|
* |
|
68
|
|
|
* @param int $capacity The number of values for which capacity should be |
|
69
|
|
|
* allocated. Capacity will stay the same if this value |
|
70
|
|
|
* is less than or equal to the current capacity. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function allocate(int $capacity) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->table->allocate($capacity); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the current capacity of the set. |
|
79
|
|
|
* |
|
80
|
|
|
* @return int |
|
81
|
|
|
*/ |
|
82
|
|
|
public function capacity(): int |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->table->capacity(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Clear all elements in the Set |
|
89
|
|
|
*/ |
|
90
|
|
|
public function clear() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->table->clear(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Determines whether the set contains all of zero or more values. |
|
97
|
|
|
* |
|
98
|
|
|
* @param mixed ...$values |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool true if at least one value was provided and the set |
|
101
|
|
|
* contains all given values, false otherwise. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function contains(...$values): bool |
|
104
|
|
|
{ |
|
105
|
|
|
foreach ($values as $value) { |
|
106
|
|
|
if (!$this->table->hasKey($value)) { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritDoc |
|
116
|
|
|
*/ |
|
117
|
|
|
public function copy() |
|
118
|
|
|
{ |
|
119
|
|
|
return new self($this); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns the number of elements in the Stack |
|
124
|
|
|
* |
|
125
|
|
|
* @return int |
|
126
|
|
|
*/ |
|
127
|
|
|
public function count(): int |
|
128
|
|
|
{ |
|
129
|
|
|
return count($this->table); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns the first value in the set. |
|
134
|
|
|
* |
|
135
|
|
|
* @return mixed the first value in the set. |
|
136
|
|
|
*/ |
|
137
|
|
|
public function first() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->table->first()->key; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the value at a specified position in the set. |
|
144
|
|
|
* |
|
145
|
|
|
* @param int $position |
|
146
|
|
|
* |
|
147
|
|
|
* @return mixed|null |
|
148
|
|
|
* |
|
149
|
|
|
* @throws OutOfRangeException |
|
150
|
|
|
*/ |
|
151
|
|
|
public function get(int $position) |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->table->skip($position)->key; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @inheritDoc |
|
158
|
|
|
*/ |
|
159
|
|
|
public function isEmpty(): bool |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->table->isEmpty(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns the last value in the set. |
|
166
|
|
|
* |
|
167
|
|
|
* @return mixed the last value in the set. |
|
168
|
|
|
*/ |
|
169
|
|
|
public function last() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->table->last()->key; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Removes zero or more values from the set. |
|
176
|
|
|
* |
|
177
|
|
|
* @param mixed ...$values |
|
178
|
|
|
*/ |
|
179
|
|
|
public function remove(...$values) |
|
180
|
|
|
{ |
|
181
|
|
|
foreach ($values as $value) { |
|
182
|
|
|
$this->table->remove($value, null); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @inheritDoc |
|
188
|
|
|
*/ |
|
189
|
|
|
public function toArray(): array |
|
190
|
|
|
{ |
|
191
|
|
|
return iterator_to_array($this); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Get iterator |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getIterator() |
|
198
|
|
|
{ |
|
199
|
|
|
foreach ($this->table as $key => $value) { |
|
200
|
|
|
yield $key; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @inheritdoc |
|
206
|
|
|
* |
|
207
|
|
|
* @throws OutOfBoundsException |
|
208
|
|
|
*/ |
|
209
|
|
|
public function offsetSet($offset, $value) |
|
210
|
|
|
{ |
|
211
|
|
|
if ($offset === null) { |
|
212
|
|
|
$this->add($value); |
|
213
|
|
|
return; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
throw new OutOfBoundsException(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @inheritdoc |
|
221
|
|
|
*/ |
|
222
|
|
|
public function offsetGet($offset) |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->table->skip($offset)->key; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @inheritdoc |
|
229
|
|
|
* |
|
230
|
|
|
* @throws Error |
|
231
|
|
|
*/ |
|
232
|
|
|
public function offsetExists($offset) |
|
233
|
|
|
{ |
|
234
|
|
|
throw new Error(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @inheritdoc |
|
239
|
|
|
* |
|
240
|
|
|
* @throws Error |
|
241
|
|
|
*/ |
|
242
|
|
|
public function offsetUnset($offset) |
|
243
|
|
|
{ |
|
244
|
|
|
throw new Error(); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|