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\Sequenceable as SequenceableInterface; |
12
|
|
|
use Mbh\Collection\CallbackHeap; |
13
|
|
|
use Mbh\Iterator\SliceIterator; |
14
|
|
|
use Mbh\Iterator\ConcatIterator; |
15
|
|
|
use SplFixedArray; |
16
|
|
|
use SplHeap; |
17
|
|
|
use SplStack; |
18
|
|
|
use LimitIterator; |
19
|
|
|
use Iterator; |
20
|
|
|
use ArrayAccess; |
21
|
|
|
use Countable; |
22
|
|
|
use CallbackFilterIterator; |
23
|
|
|
use JsonSerializable; |
24
|
|
|
use RuntimeException; |
25
|
|
|
use Traversable; |
26
|
|
|
use ReflectionClass; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The Immutable Array |
30
|
|
|
* |
31
|
|
|
* This provides special methods for quickly creating an immutable array, |
32
|
|
|
* either from any Traversable, or using a C-optimized fromArray() to directly |
33
|
|
|
* instantiate from. Also includes methods fundamental to functional |
34
|
|
|
* programming, e.g. map, filter, join, and sort. |
35
|
|
|
* |
36
|
|
|
* @package structures |
37
|
|
|
* @author Ulises Jeremias Cornejo Fandos <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
class ImmutableArray implements SequenceableInterface |
41
|
|
|
{ |
42
|
|
|
use Traits\Sequenceable; |
43
|
|
|
|
44
|
|
|
// The secondary flash array - fixed array |
45
|
|
|
private $sfa = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create an immutable array |
49
|
|
|
* |
50
|
|
|
* @param Traversable $immute Data guaranteed to be immutable |
51
|
|
|
*/ |
52
|
|
|
private function __construct(Traversable $immute) |
53
|
|
|
{ |
54
|
|
|
$this->sfa = $immute; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Factory for building ImmutableArrays from any traversable |
59
|
|
|
* |
60
|
|
|
* @return ImmutableArray |
61
|
|
|
*/ |
62
|
|
|
public static function fromItems(Traversable $array): self |
63
|
|
|
{ |
64
|
|
|
// We can only do it this way if we can count it |
65
|
|
|
if ($array instanceof Countable) { |
66
|
|
|
$sfa = new SplFixedArray(count($array)); |
67
|
|
|
|
68
|
|
|
foreach ($array as $i => $elem) { |
69
|
|
|
$sfa[$i] = $elem; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new static($sfa); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// If we can't count it, it's simplest to iterate into an array first |
76
|
|
|
return static::fromArray(iterator_to_array($array)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Build from an array |
81
|
|
|
* |
82
|
|
|
* @return ImmutableArray |
83
|
|
|
*/ |
84
|
|
|
public static function fromArray(array $array): self |
85
|
|
|
{ |
86
|
|
|
return new static(SplFixedArray::fromArray($array)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function toArray(): array |
90
|
|
|
{ |
91
|
|
|
return $this->sfa->toArray(); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Countable |
96
|
|
|
*/ |
97
|
|
|
public function count(): int |
98
|
|
|
{ |
99
|
|
|
return count($this->sfa); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Iterator |
104
|
|
|
*/ |
105
|
|
|
public function current() |
106
|
|
|
{ |
107
|
|
|
return $this->sfa->current(); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function key(): int |
111
|
|
|
{ |
112
|
|
|
return $this->sfa->key(); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function next() |
116
|
|
|
{ |
117
|
|
|
return $this->sfa->next(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function rewind() |
121
|
|
|
{ |
122
|
|
|
return $this->sfa->rewind(); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function valid() |
126
|
|
|
{ |
127
|
|
|
return $this->sfa->valid(); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* ArrayAccess |
132
|
|
|
*/ |
133
|
|
|
public function offsetExists($offset): bool |
134
|
|
|
{ |
135
|
|
|
return $this->sfa->offsetExists($offset); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function offsetGet($offset) |
139
|
|
|
{ |
140
|
|
|
return $this->sfa->offsetGet($offset); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function offsetSet($offset, $value) |
144
|
|
|
{ |
145
|
|
|
throw new RuntimeException('Attempt to mutate immutable ' . __CLASS__ . ' object.'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function offsetUnset($offset) |
149
|
|
|
{ |
150
|
|
|
throw new RuntimeException('Attempt to mutate immutable ' . __CLASS__ . ' object.'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function clear() |
154
|
|
|
{ |
155
|
|
|
throw new RuntimeException('Attempt to mutate immutable ' . __CLASS__ . ' object.'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|