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\Collection\Interfaces\Sequenceable as SequenceableInterface; |
13
|
|
|
use Mbh\Collection\CallbackHeap; |
14
|
|
|
use Mbh\Iterator\SliceIterator; |
15
|
|
|
use Mbh\Iterator\ConcatIterator; |
16
|
|
|
use SplFixedArray; |
17
|
|
|
use SplHeap; |
18
|
|
|
use SplStack; |
19
|
|
|
use LimitIterator; |
20
|
|
|
use Iterator; |
21
|
|
|
use ArrayAccess; |
22
|
|
|
use Countable; |
23
|
|
|
use CallbackFilterIterator; |
24
|
|
|
use JsonSerializable; |
25
|
|
|
use RuntimeException; |
26
|
|
|
use Traversable; |
27
|
|
|
use ReflectionClass; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The Fixed Array |
31
|
|
|
* |
32
|
|
|
* A FixedArray is a sequence of values in a contiguous buffer that grows and |
33
|
|
|
* shrinks automatically. It’s the most efficient sequential structure because |
34
|
|
|
* a value’s index is a direct mapping to its index in the buffer, and the |
35
|
|
|
* growth factor isn't bound to a specific multiple or exponent. |
36
|
|
|
* |
37
|
|
|
* @package structures |
38
|
|
|
* @author Ulises Jeremias Cornejo Fandos <[email protected]> |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
class FixedArray implements SequenceableInterface |
42
|
|
|
{ |
43
|
|
|
use Traits\Sequenceable; |
44
|
|
|
|
45
|
|
|
// The secondary flash array - fixed array |
46
|
|
|
protected $sfa = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create an fixed array |
50
|
|
|
* |
51
|
|
|
* @param Traversable $fixed data guaranteed to be immutable |
52
|
|
|
*/ |
53
|
|
|
protected function __construct(Traversable $fixed) |
54
|
|
|
{ |
55
|
|
|
$this->sfa = $fixed; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Fallback behaviour to use the builtin array sort functions |
60
|
|
|
* |
61
|
|
|
* @param callable $callback The callback for comparison |
62
|
|
|
* @return SequenceableInterface |
63
|
|
|
*/ |
64
|
|
|
public function arraySort(callable $callback = null): SequenceableInterface |
65
|
|
|
{ |
66
|
|
|
$array = $this->toArray(); |
67
|
|
|
|
68
|
|
|
if ($callback) { |
69
|
|
|
usort($array, $callback); |
70
|
|
|
} else { |
71
|
|
|
sort($array); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->sfa = static::fromArray($array); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function toArray(): array |
80
|
|
|
{ |
81
|
|
|
return $this->sfa->toArray(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function validIndex(int $index) |
85
|
|
|
{ |
86
|
|
|
return $index >= 0 && $index < count($this); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Countable |
91
|
|
|
*/ |
92
|
|
|
public function count(): int |
93
|
|
|
{ |
94
|
|
|
return count($this->sfa); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Iterator |
99
|
|
|
*/ |
100
|
|
|
public function current() |
101
|
|
|
{ |
102
|
|
|
return $this->sfa->current(); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function key(): int |
106
|
|
|
{ |
107
|
|
|
return $this->sfa->key(); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function next() |
111
|
|
|
{ |
112
|
|
|
return $this->sfa->next(); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function rewind() |
116
|
|
|
{ |
117
|
|
|
return $this->sfa->rewind(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function valid() |
121
|
|
|
{ |
122
|
|
|
return $this->sfa->valid(); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* ArrayAccess |
127
|
|
|
*/ |
128
|
|
|
public function offsetExists($offset): bool |
129
|
|
|
{ |
130
|
|
|
return is_integer($offset) |
131
|
|
|
&& $this->validIndex($offset) |
132
|
|
|
&& $this->sfa->offsetExists($offset); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function offsetGet($offset) |
136
|
|
|
{ |
137
|
|
|
return $this->sfa->offsetGet($offset); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function offsetSet($offset, $value) |
141
|
|
|
{ |
142
|
|
|
return is_integer($offset) |
|
|
|
|
143
|
|
|
&& $this->validIndex($offset) |
144
|
|
|
&& $this->sfa->offsetSet($offset, $value); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function offsetUnset($offset) |
148
|
|
|
{ |
149
|
|
|
return is_integer($offset) |
|
|
|
|
150
|
|
|
&& $this->validIndex($offset) |
151
|
|
|
&& $this->sfa->offsetUnset($offset); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function clear() |
155
|
|
|
{ |
156
|
|
|
return $this->sfa->clear(); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|