|
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\Hashable as HashableInterface; |
|
13
|
|
|
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface; |
|
14
|
|
|
use Traversable; |
|
15
|
|
|
use ArrayAccess; |
|
16
|
|
|
use IteratorAggregate; |
|
17
|
|
|
use OutOfBoundsException; |
|
18
|
|
|
use OutOfRangeException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A Map is a sequential collection of key-value pairs, almost identical to an |
|
22
|
|
|
* array used in a similar context. Keys can be any type, but must be unique. |
|
23
|
|
|
* |
|
24
|
|
|
* @package structures |
|
25
|
|
|
* @author Ulises Jeremias Cornejo Fandos <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class Map implements ArrayAccess, CollectionInterface, IteratorAggregate |
|
28
|
|
|
{ |
|
29
|
|
|
use Traits\Collection; |
|
30
|
|
|
use Traits\Functional; |
|
31
|
|
|
use Traits\SquaredCapacity; |
|
32
|
|
|
|
|
33
|
|
|
const MIN_CAPACITY = 8.0; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var FixedArray internal array to store pairs |
|
37
|
|
|
*/ |
|
38
|
|
|
private $pairs; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Creates a new instance. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array|Traversable $pairs |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($pairs = []) |
|
46
|
|
|
{ |
|
47
|
|
|
FixedArray::fromArray([]); |
|
48
|
|
|
|
|
49
|
|
|
$this->pushAll($pairs); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function clear() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->pairs->clear(); |
|
58
|
|
|
$this->capacity = self::MIN_CAPACITY; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritDoc |
|
63
|
|
|
*/ |
|
64
|
|
|
public function count(): int |
|
65
|
|
|
{ |
|
66
|
|
|
return count($this->pairs); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns whether an association a given key exists. |
|
71
|
|
|
* |
|
72
|
|
|
* @param mixed $key |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function hasKey($key): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->lookupKey($key) !== null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns whether an association for a given value exists. |
|
83
|
|
|
* |
|
84
|
|
|
* @param mixed $value |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public function hasValue($value): bool |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->lookupValue($value) !== null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns a set of all the keys in the map. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Set |
|
97
|
|
|
*/ |
|
98
|
|
|
public function keys(): Set |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
return new static($this->pairs->map(function ($pair) { |
|
|
|
|
|
|
101
|
|
|
return $pair->key; |
|
102
|
|
|
})); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Determines whether two keys are equal. |
|
107
|
|
|
* |
|
108
|
|
|
* @param mixed $a |
|
109
|
|
|
* @param mixed $b |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
private function keysAreEqual($a, $b): bool |
|
114
|
|
|
{ |
|
115
|
|
|
if (is_object($a) && $a instanceof HashableInterface) { |
|
116
|
|
|
return get_class($a) === get_class($b) && $a->equals($b); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $a === $b; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Attempts to look up a key in the table. |
|
124
|
|
|
* |
|
125
|
|
|
* @param $key |
|
126
|
|
|
* |
|
127
|
|
|
* @return Pair|null |
|
128
|
|
|
*/ |
|
129
|
|
|
private function lookupKey($key) |
|
130
|
|
|
{ |
|
131
|
|
|
foreach ($this->pairs as $pair) { |
|
132
|
|
|
if ($this->keysAreEqual($pair->key, $key)) { |
|
133
|
|
|
return $pair; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Attempts to look up a key in the table. |
|
140
|
|
|
* |
|
141
|
|
|
* @param $value |
|
142
|
|
|
* |
|
143
|
|
|
* @return Pair|null |
|
144
|
|
|
*/ |
|
145
|
|
|
private function lookupValue($value) |
|
146
|
|
|
{ |
|
147
|
|
|
foreach ($this->pairs as $pair) { |
|
148
|
|
|
if ($pair->value === $value) { |
|
149
|
|
|
return $pair; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Associates a key with a value, replacing a previous association if there |
|
156
|
|
|
* was one. |
|
157
|
|
|
* |
|
158
|
|
|
* @param mixed $key |
|
159
|
|
|
* @param mixed $value |
|
160
|
|
|
*/ |
|
161
|
|
|
public function put($key, $value) |
|
162
|
|
|
{ |
|
163
|
|
|
$pair = $this->lookupKey($key); |
|
164
|
|
|
if ($pair) { |
|
165
|
|
|
$pair->value = $value; |
|
166
|
|
|
} else { |
|
167
|
|
|
$this->checkCapacity(); |
|
168
|
|
|
$this->pairs[] = new Pair($key, $value); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Creates associations for all keys and corresponding values of either an |
|
174
|
|
|
* array or iterable object. |
|
175
|
|
|
* |
|
176
|
|
|
* @param Traversable|array $values |
|
177
|
|
|
*/ |
|
178
|
|
|
public function putAll($values) |
|
179
|
|
|
{ |
|
180
|
|
|
foreach ($values as $key => $value) { |
|
181
|
|
|
$this->put($key, $value); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Returns a sequence of all the associated values in the Map. |
|
187
|
|
|
* |
|
188
|
|
|
* @return SequenceableInterface |
|
189
|
|
|
*/ |
|
190
|
|
|
public function values(): SequenceableInterface |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->pairs->map(function ($pair) { |
|
193
|
|
|
return $pair->value; |
|
194
|
|
|
}); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.