|
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\Functional as FunctionalInterface; |
|
12
|
|
|
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface; |
|
13
|
|
|
use Mbh\Interfaces\Allocated as AllocatedInterface; |
|
14
|
|
|
use Mbh\Exceptions\ImmutableException; |
|
15
|
|
|
use Mbh\Traits\Capacity; |
|
16
|
|
|
use Mbh\Traits\EmptyGuard; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The Immutable Array |
|
20
|
|
|
* |
|
21
|
|
|
* This provides special methods for quickly creating an immutable array, |
|
22
|
|
|
* either from any Traversable, or using a C-optimized fromArray() to directly |
|
23
|
|
|
* instantiate from. Also includes methods fundamental to functional |
|
24
|
|
|
* programming, e.g. map, filter, join, and sort. |
|
25
|
|
|
* |
|
26
|
|
|
* @package structures |
|
27
|
|
|
* @author Ulises Jeremias Cornejo Fandos <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class ImmutableArray implements AllocatedInterface, FunctionalInterface, SequenceableInterface |
|
30
|
|
|
{ |
|
31
|
|
|
use Capacity; |
|
32
|
|
|
use EmptyGuard; |
|
33
|
|
|
use Traits\Collection; |
|
34
|
|
|
use Traits\ImmutableFunctional; |
|
35
|
|
|
use Traits\Sequenceable; |
|
36
|
|
|
use Traits\Sequenceable\Arrayed; |
|
37
|
|
|
|
|
38
|
|
|
const MIN_CAPACITY = 8.0; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* ArrayAccess |
|
42
|
|
|
*/ |
|
43
|
|
|
public function offsetExists($offset) |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->sfa->offsetExists($offset); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function offsetGet($offset) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->sfa->offsetGet($offset); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function offsetSet($offset, $value) |
|
54
|
|
|
{ |
|
55
|
|
|
throw ImmutableException::cannotModify(__CLASS__, __METHOD__); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function offsetUnset($offset) |
|
59
|
|
|
{ |
|
60
|
|
|
throw ImmutableException::cannotModify(__CLASS__, __METHOD__); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritDoc |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getGrowthFactor(): float |
|
67
|
|
|
{ |
|
68
|
|
|
return 1.5; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function shouldIncreaseCapacity(): bool |
|
75
|
|
|
{ |
|
76
|
|
|
return count($this) > $this->getSize(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|