/
HashArray.php
329 lines (288 loc) · 7.24 KB
/
HashArray.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
namespace Wikibase\DataModel;
use ArrayObject;
use Hashable;
use InvalidArgumentException;
use Traversable;
/**
* Generic array object with lookups based on hashes of the elements.
*
* Elements need to implement Hashable.
*
* Note that by default the getHash method uses @see MapValueHashesr
* which returns a hash based on the contents of the list, regardless
* of order and keys.
*
* Also note that if the Hashable elements are mutable, any modifications
* made to them via their mutator methods will not cause an update of
* their associated hash in this array.
*
* @since 0.1
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
abstract class HashArray extends ArrayObject {
/**
* Maps element hashes to their offsets.
*
* @since 0.1
*
* @var array [ element hash (string) => element offset (string|int) ]
*/
protected $offsetHashes = [];
/**
* @var integer
*/
protected $indexOffset = 0;
/**
* Returns the name of an interface/class that the element should implement/extend.
*
* @since 0.4
*
* @return string
*/
abstract public function getObjectType();
/**
* @see ArrayObject::__construct
*
* @param array|Traversable|null $input
* @param int $flags
* @param string $iteratorClass
*
* @throws InvalidArgumentException
*/
public function __construct( $input = null, $flags = 0, $iteratorClass = 'ArrayIterator' ) {
parent::__construct( [], $flags, $iteratorClass );
if ( $input !== null ) {
if ( !is_array( $input ) && !( $input instanceof Traversable ) ) {
throw new InvalidArgumentException( '$input must be an array or Traversable' );
}
foreach ( $input as $offset => $value ) {
$this->offsetSet( $offset, $value );
}
}
}
/**
* Finds a new offset for when appending an element.
* The base class does this, so it would be better to integrate,
* but there does not appear to be any way to do this...
*
* @return integer
*/
protected function getNewOffset() {
while ( $this->offsetExists( $this->indexOffset ) ) {
$this->indexOffset++;
}
return $this->indexOffset;
}
/**
* Gets called before a new element is added to the ArrayObject.
*
* At this point the index is always set (ie not null) and the
* value is always of the type returned by @see getObjectType.
*
* Should return a boolean. When false is returned the element
* does not get added to the ArrayObject.
*
* @since 0.1
*
* @param int|string $index
* @param Hashable $hashable
*
* @return bool
*/
protected function preSetElement( $index, $hashable ) {
$hash = $hashable->getHash();
$hasHash = $this->hasElementHash( $hash );
if ( $hasHash ) {
return false;
} else {
$this->offsetHashes[$hash] = $index;
return true;
}
}
/**
* Returns if there is an element with the provided hash.
*
* @since 0.1
*
* @param string $elementHash
*
* @return bool
*/
public function hasElementHash( $elementHash ) {
return array_key_exists( $elementHash, $this->offsetHashes );
}
/**
* Returns if there is an element with the same hash as the provided element in the list.
*
* @since 0.1
*
* @param Hashable $element
*
* @return bool
*/
public function hasElement( Hashable $element ) {
return $this->hasElementHash( $element->getHash() );
}
/**
* Removes the element with the hash of the provided element, if there is such an element in the list.
*
* @since 0.1
*
* @param Hashable $element
*/
public function removeElement( Hashable $element ) {
$this->removeByElementHash( $element->getHash() );
}
/**
* Removes the element with the provided hash, if there is such an element in the list.
*
* @since 0.1
*
* @param string $elementHash
*/
public function removeByElementHash( $elementHash ) {
if ( $this->hasElementHash( $elementHash ) ) {
$offset = $this->offsetHashes[$elementHash];
$this->offsetUnset( $offset );
}
}
/**
* Adds the provided element to the list if there is no element with the same hash yet.
*
* @since 0.1
*
* @param Hashable $element
*
* @return bool Indicates if the element was added or not.
*/
public function addElement( Hashable $element ) {
$append = !$this->hasElementHash( $element->getHash() );
if ( $append ) {
$this->append( $element );
}
return $append;
}
/**
* Returns the element with the provided hash or false if there is no such element.
*
* @since 0.1
*
* @param string $elementHash
*
* @return mixed|bool
*/
public function getByElementHash( $elementHash ) {
if ( $this->hasElementHash( $elementHash ) ) {
$offset = $this->offsetHashes[$elementHash];
return $this->offsetGet( $offset );
}
return false;
}
/**
* @see ArrayObject::offsetUnset
*
* @since 0.1
*
* @param mixed $index
*/
public function offsetUnset( $index ) {
if ( $this->offsetExists( $index ) ) {
/**
* @var Hashable $element
*/
$element = $this->offsetGet( $index );
$hash = $element->getHash();
unset( $this->offsetHashes[$hash] );
parent::offsetUnset( $index );
}
}
/**
* @see ArrayObject::append
*
* @param mixed $value
*/
public function append( $value ) {
$this->setElement( null, $value );
}
/**
* @see ArrayObject::offsetSet()
*
* @param mixed $index
* @param mixed $value
*/
public function offsetSet( $index, $value ) {
$this->setElement( $index, $value );
}
/**
* Returns if the provided value has the same type as the elements
* that can be added to this ArrayObject.
*
* @param mixed $value
*
* @return bool
*/
protected function hasValidType( $value ) {
$class = $this->getObjectType();
return $value instanceof $class;
}
/**
* Method that actually sets the element and holds
* all common code needed for set operations, including
* type checking and offset resolving.
*
* If you want to do additional indexing or have code that
* otherwise needs to be executed whenever an element is added,
* you can overload @see preSetElement.
*
* @param mixed $index
* @param mixed $value
*
* @throws InvalidArgumentException
*/
protected function setElement( $index, $value ) {
if ( !$this->hasValidType( $value ) ) {
$type = is_object( $value ) ? get_class( $value ) : gettype( $value );
throw new InvalidArgumentException( '$value must be an instance of ' . $this->getObjectType() . '; got ' . $type );
}
if ( $index === null ) {
$index = $this->getNewOffset();
}
if ( $this->preSetElement( $index, $value ) ) {
parent::offsetSet( $index, $value );
}
}
/**
* @see Serializable::serialize
*
* @return string
*/
public function serialize() {
return serialize( [
'data' => $this->getArrayCopy(),
'index' => $this->indexOffset,
] );
}
/**
* @see Serializable::unserialize
*
* @param string $serialized
*/
public function unserialize( $serialized ) {
$serializationData = unserialize( $serialized );
foreach ( $serializationData['data'] as $offset => $value ) {
// Just set the element, bypassing checks and offset resolving,
// as these elements have already gone through this.
parent::offsetSet( $offset, $value );
}
$this->indexOffset = $serializationData['index'];
}
/**
* @return bool
*/
public function isEmpty() {
return !$this->getIterator()->valid();
}
}