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
|
|
|
public function toArray(): array |
59
|
|
|
{ |
60
|
|
|
return $this->sfa->toArray(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function validIndex(int $index) |
64
|
|
|
{ |
65
|
|
|
return $index >= 0 && $index < count($this); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Countable |
70
|
|
|
*/ |
71
|
|
|
public function count(): int |
72
|
|
|
{ |
73
|
|
|
return count($this->sfa); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Iterator |
78
|
|
|
*/ |
79
|
|
|
public function current() |
80
|
|
|
{ |
81
|
|
|
return $this->sfa->current(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function key(): int |
85
|
|
|
{ |
86
|
|
|
return $this->sfa->key(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function next() |
90
|
|
|
{ |
91
|
|
|
return $this->sfa->next(); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function rewind() |
95
|
|
|
{ |
96
|
|
|
return $this->sfa->rewind(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function valid() |
100
|
|
|
{ |
101
|
|
|
return $this->sfa->valid(); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* ArrayAccess |
106
|
|
|
*/ |
107
|
|
|
public function offsetExists($offset): bool |
108
|
|
|
{ |
109
|
|
|
return is_integer($offset) |
110
|
|
|
&& $this->validIndex($offset) |
111
|
|
|
&& $this->sfa->offsetExists($offset); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function offsetGet($offset) |
115
|
|
|
{ |
116
|
|
|
return $this->sfa->offsetGet($offset); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function offsetSet($offset, $value) |
120
|
|
|
{ |
121
|
|
|
return is_integer($offset) |
|
|
|
|
122
|
|
|
&& $this->validIndex($offset) |
123
|
|
|
&& $this->sfa->offsetSet($offset, $value); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function offsetUnset($offset) |
127
|
|
|
{ |
128
|
|
|
return is_integer($offset) |
|
|
|
|
129
|
|
|
&& $this->validIndex($offset) |
130
|
|
|
&& $this->sfa->offsetUnset($offset); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function clear() |
134
|
|
|
{ |
135
|
|
|
return $this->sfa->clear(); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function getMainTraversable(): Traversable |
139
|
|
|
{ |
140
|
|
|
return $this->sfa; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function setTraversable(Traversable $traversable) |
144
|
|
|
{ |
145
|
|
|
$this->sfa = $traversable; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|