forked from Unicity/Chimera
/
ArrayList.php
executable file
·460 lines (422 loc) · 15.4 KB
/
ArrayList.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
/**
* Copyright 2015 Unicity International
* Copyright 2011-2012 Spadefoot Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Unicity\Common {
use \Unicity\Common;
use \Unicity\Core;
use \Unicity\Throwable;
/**
* This class creates an immutable list using an indexed array.
*
* @access public
* @class
* @package Common
*/
class ArrayList extends Core\Object implements Common\IList {
/**
* This variable stores the elements in the collection.
*
* @access protected
* @var array
*/
protected $elements;
/**
* This variable stores the pointer position.
*
* @access protected
* @var integer
*/
protected $pointer;
/**
* This method initializes the class with the specified values (if any are provided).
*
* @access public
* @param \Traversable $elements a traversable array or collection
* @throws Throwable\InvalidArgument\Exception indicates that the specified argument
* is invalid
*/
public function __construct($elements = null) {
$this->elements = array();
if ($elements !== null) {
if ( ! (is_array($elements) || ($elements instanceof \Traversable))) {
throw new Throwable\InvalidArgument\Exception('Invalid argument specified. Argument must be traversable or null.');
}
foreach ($elements as $value) {
$this->elements[] = $value;
}
}
$this->pointer = 0;
}
/**
* This method returns the number of elements in the collection.
*
* @access public
* @return integer the number of elements
*/
public function count() {
return count($this->elements);
}
/**
* This method returns the current element that is pointed at by the iterator.
*
* @access public
* @return mixed the current element
*/
public function current() {
return current($this->elements);
}
/**
* This destructor ensures that any resources are properly disposed.
*
* @access public
*/
public function __destruct() {
parent::__destruct();
unset($this->elements);
unset($this->pointer);
}
/**
* This method evaluates whether the specified objects is equal to the current object.
*
* @access public
* @param mixed $object the object to be evaluated
* @return boolean whether the specified object is equal
* to the current object
*/
public function __equals($object) {
return (($object !== null) && ($object instanceof Common\ArrayList) && ((string)serialize($object->elements) == (string)serialize($this->elements)));
}
/**
* This method returns the element at the the specified index.
*
* @access public
* @param integer $index the index of the element
* @return mixed the element at the specified index
* @throws Throwable\InvalidArgument\Exception indicates that an index must be an integer
* @throws Throwable\OutOfBounds\Exception indicates that the index is out of bounds
*/
public function __get($index) {
return $this->getValue($index);
}
/**
* This method returns a sublist of all elements between the specified range.
*
* @access public
* @param integer $sIndex the beginning index
* @param integer $eIndex the ending index
* @return Common\IList a sublist of all elements between the specified
* range
* @throws Throwable\InvalidArgument\Exception indicates that an index must be an integer
* @throws Throwable\InvalidRange\Exception indicates that the ending index is less than
* the beginning index
*/
public function getRangeOfValues($sIndex, $eIndex) {
if (is_integer($sIndex) && is_integer($eIndex)) {
if (array_key_exists($sIndex, $this->elements) && ($eIndex >= $sIndex) && ($eIndex <= $this->count())) {
$sublist = new static();
for ($index = $sIndex; $index < $eIndex; $index++) {
$sublist->elements[] = $this->elements[$index];
}
return $sublist;
}
throw new Throwable\InvalidRange\Exception('Unable to get range. Invalid range start from :start and ends at :end', array(':start' => $sIndex, ':end' => $eIndex));
}
throw new Throwable\InvalidArgument\Exception('Unable to get range. Either :start or :end is of the wrong data type.', array(':start' => Core\DataType::info($sIndex)->type, ':end' => Core\DataType::info($eIndex)->type));
}
/**
* This method returns the element at the the specified index.
*
* @access public
* @param integer $index the index of the element
* @return mixed the element at the specified index
* @throws Throwable\InvalidArgument\Exception indicates that an index must be an integer
* @throws Throwable\OutOfBounds\Exception indicates that the index is out of bounds
*/
public function getValue($index) {
if (is_integer($index)) {
if (array_key_exists($index, $this->elements)) {
return $this->elements[$index];
}
throw new Throwable\OutOfBounds\Exception('Unable to get element. Undefined index at ":index" specified', array(':index' => $index));
}
throw new Throwable\InvalidArgument\Exception('Unable to get element. :type is of the wrong data type.', array(':type' => Core\DataType::info($index)->type));
}
/**
* This method determines whether the specified index exits.
*
* @access protected
* @param integer $index the index to be tested
* @return boolean whether the specified index exits
* @throws Throwable\InvalidArgument\Exception indicates that an index must be an integer
*/
public function hasIndex($index) {
if (!is_integer($index)) {
throw new Throwable\InvalidArgument\Exception('Unable to get element. :type is of the wrong data type.', array(':type' => Core\DataType::info($index)->type));
}
return array_key_exists($index, $this->elements);
}
/**
* This method determines whether the specified element is contained within the
* collection.
*
* @access public
* @param mixed $value the value to be tested
* @return boolean whether the specified element is contained
* within the collection
*/
public function hasValue($value) {
return ($this->indexOf($value) >= 0);
}
/**
* This method determines whether all elements in the specified array are contained
* within the collection.
*
* @access public
* @param \Traversable $values the values to be tested
* @return boolean whether all elements are contained within
* the collection
*/
public function hasValues($values) {
$success = 0;
foreach ($values as $value) {
$success += (int) $this->hasValue($value);
}
return ($success == count($values));
}
/**
* This method returns the index of the specified element should it exist within the collection.
*
* @access public
* @param mixed $value the element to be located
* @return integer the index of the element if it exists within
* the collection; otherwise, a value of -1
*/
public function indexOf($value) {
$count = $this->count();
for ($i = 0; $i < $count; $i++) {
if (Core\DataType::info($value)->hash == Core\DataType::info($this->getValue($i))->hash) {
return $i;
}
}
return -1;
}
/**
* This method determines whether there are any elements in the collection.
*
* @access public
* @return boolean whether the collection is empty
*/
public function isEmpty() {
return empty($this->elements);
}
/**
* This method returns the current key that is pointed at by the iterator.
*
* @access public
* @return scaler the key on success or null on failure
*/
public function key() {
return key($this->elements);
}
/**
* This method returns the last index of the specified value in the list.
*
* @access public
* @param mixed $value the value to be located
* @return integer the last index of the specified value
*/
public function lastIndexOf($value) {
for ($i = $this->count(); $i >= 0; $i--) {
if (Core\DataType::info($value)->hash == Core\DataType::info($this->getValue($i))->hash) {
return $i;
}
}
return -1;
}
/**
* This method will iterate to the next element.
*
* @access public
*/
public function next() {
next($this->elements);
$this->pointer++;
}
/**
* This method determines whether an offset exists.
*
* @access public
* @override
* @param integer $offset the offset to be evaluated
* @return boolean whether the requested offset exists
*/
public function offsetExists($offset) {
return $this->hasIndex($offset);
}
/**
* This methods gets value at the specified offset.
*
* @access public
* @override
* @param integer $offset the offset to be fetched
* @return mixed the value at the specified offset
*/
public function offsetGet($offset) {
return $this->getValue($offset);
}
/**
* This methods sets the specified value at the specified offset.
*
* @access public
* @override
* @param integer $offset the offset to be set
* @param mixed $value the value to be set
* @throws Throwable\UnimplementedMethod\Exception indicates the method has not been
* implemented
*/
public function offsetSet($offset, $value) {
throw new Throwable\UnimplementedMethod\Exception('Invalid call to member function. Method has not been implemented.');
}
/**
* This method allows for the specified offset to be unset.
*
* @access public
* @override
* @param integer $offset the offset to be unset
* @throws Throwable\UnimplementedMethod\Exception indicates the method has not been
* implemented
*/
public function offsetUnset($offset) {
throw new Throwable\UnimplementedMethod\Exception('Invalid call to member function. Method has not been implemented.');
}
/**
* This method will resets the iterator.
*
* @access public
*/
public function rewind() {
reset($this->elements);
$this->pointer = 0;
}
/**
* This method seeks for the specified index and moves the pointer to that location
* if found.
*
* @access public
* @param integer $index the index to be seeked
* @throws Throwable\OutOfBounds\Exception indicates that the index is not within
* the bounds of the list
*/
public function seek($index) {
if ( ! array_key_exists($index, $this->elements)) {
throw new Throwable\OutOfBounds\Exception('Invalid seek index. Index :index is not within the bounds of the list.', array(':index' => $index));
}
$this->pointer = $index;
}
/**
* This method returns the collection as an array.
*
* @access public
* @return array an array of the elements
*/
public function toArray() {
return $this->elements;
}
/**
* This method returns the collection as a dictionary.
*
* @access public
* @return array a dictionary of the elements
*/
public function toDictionary() {
return $this->elements;
}
/**
* This method returns the collection as a list.
*
* @access public
* @return \Unicity\Common\IList a list of the elements
*/
public function toList() {
return $this;
}
/**
* This method returns the collection as a map.
*
* @access public
* @return \Unicity\Common\IMap a map of the elements
*/
public function toMap() {
return new Common\HashMap($this->elements);
}
/**
* This method determines whether all elements have been iterated through.
*
* @access public
* @return boolean whether iterator is still valid
*/
public function valid() {
return ($this->key() !== null);
}
/**
* This method returns whether the data type of the specified value is related to the data type
* of this class.
*
* @access public
* @param mixed $value the value to be evaluated
* @return boolean whether the data type of the specified
* value is related to the data type of
* this class
*/
public static function isTypeOf($value) {
if ($value !== null) {
if (is_array($value)) {
$keys = array_keys($value);
return (array_keys($keys) === $keys);
}
return (is_object($value) && ($value instanceof Common\IList));
}
return false;
}
/**
* This method returns the value as an instance of this class.
*
* @access public
* @static
* @param mixed $value the value to be boxed
* @return Common\ArrayList an instance of this class
*/
public static function box($value) {
if (!($value instanceof static)) {
return new static($value);
}
return $value;
}
/**
* This method returns an un-box value equivalent to this class.
*
* @access public
* @static
* @param mixed $value the value to be un-boxed
* @return array an array
* @throws Throwable\Parse\Exception
*/
public static function unbox($value) {
return Core\Convert::toArray($value);
}
}
}