1 | <?php |
||
16 | class HashMap extends AbstractMap |
||
17 | { |
||
18 | /** |
||
19 | * An array of [@see MapEntry] elements |
||
20 | * |
||
21 | * @var MapEntry[] |
||
22 | */ |
||
23 | protected $elements = []; |
||
24 | |||
25 | /** |
||
26 | * Constructor |
||
27 | * |
||
28 | * @param array $map |
||
29 | */ |
||
30 | 15 | public function __construct(array $map = []) |
|
36 | |||
37 | /** |
||
38 | * Removes all mappings from map |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | 2 | public function clear() |
|
46 | |||
47 | /** |
||
48 | * Returns true if the key exists in the lookup table |
||
49 | * |
||
50 | * @param mixed $key |
||
51 | * @return bool |
||
52 | */ |
||
53 | 43 | public function containsKey($key): bool |
|
57 | |||
58 | /** |
||
59 | * Returns true if the value exists in the map |
||
60 | * |
||
61 | * @param mixed $value |
||
62 | * @return bool |
||
63 | */ |
||
64 | 2 | public function containsValue($value): bool |
|
70 | |||
71 | /** |
||
72 | * Return a set representation of map |
||
73 | * |
||
74 | * If a set is passed in, that set will be populated, otherwise |
||
75 | * a default set will be used. |
||
76 | * |
||
77 | * @param SetInterface $set |
||
78 | * @return SetInterface |
||
79 | */ |
||
80 | 11 | public function entrySet(SetInterface $set = null): SetInterface |
|
87 | |||
88 | /** |
||
89 | * Get the value at the specified key |
||
90 | * |
||
91 | * @param mixed $key |
||
92 | * @return mixed |
||
93 | * @throws \OutOfRangeException if the key doesn't exist |
||
94 | */ |
||
95 | 5 | public function get($key) |
|
99 | |||
100 | /** |
||
101 | * Returns true if the map is empty |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | 2 | public function isEmpty(): bool |
|
109 | |||
110 | /** |
||
111 | * Returns a set of the keys in the map |
||
112 | * |
||
113 | * If a set is passed in, that set will be populated, otherwise |
||
114 | * a default set will be used. |
||
115 | * |
||
116 | * @param SetInterface $set |
||
117 | * @return SetInterface |
||
118 | */ |
||
119 | 2 | public function keySet(SetInterface $set = null): SetInterface |
|
129 | |||
130 | /** |
||
131 | * Returns the previous value or null if there was no value |
||
132 | * |
||
133 | * @param mixed $key |
||
134 | * @param mixed $value |
||
135 | * @return mixed |
||
136 | */ |
||
137 | 57 | public function put($key, $value) |
|
141 | |||
142 | /** |
||
143 | * Remove the mapping for the key and returns the previous value |
||
144 | * or null |
||
145 | * |
||
146 | * @param mixed $key |
||
147 | * @return mixed |
||
148 | */ |
||
149 | 10 | public function remove($key) |
|
153 | |||
154 | /** |
||
155 | * Returns the number of mappings in the map |
||
156 | * |
||
157 | * @return int |
||
158 | */ |
||
159 | 13 | public function count(): int |
|
163 | |||
164 | /** |
||
165 | * Returns the keys as a collection |
||
166 | * |
||
167 | * @param CollectionInterface $collection |
||
168 | * @return CollectionInterface |
||
169 | */ |
||
170 | 3 | public function keys(CollectionInterface $collection = null): CollectionInterface |
|
180 | |||
181 | /** |
||
182 | * Returns the values as a collection |
||
183 | * |
||
184 | * @param CollectionInterface $collection |
||
185 | * @return CollectionInterface |
||
186 | */ |
||
187 | 42 | public function values(CollectionInterface $collection = null): CollectionInterface |
|
197 | |||
198 | /** |
||
199 | * Filter the collection using closure |
||
200 | * |
||
201 | * The closure will get passed a [@see MapEntry]. Returning true from the |
||
202 | * closure will include that entry in the new map. |
||
203 | * |
||
204 | * @param callable $filter |
||
205 | * @return MapInterface |
||
206 | */ |
||
207 | 1 | public function filter(callable $filter): MapInterface |
|
220 | |||
221 | /** |
||
222 | * Generate a hashcode for a php value |
||
223 | * |
||
224 | * @param mixed $value |
||
225 | * @return string |
||
226 | */ |
||
227 | 57 | protected function hashCode($value): string |
|
239 | |||
240 | /** |
||
241 | * Returns an array of [@see MapEntry] keys |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | 5 | private function getKeys() |
|
251 | |||
252 | /** |
||
253 | * Returns an array of [@see MapEntry] values |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | private function getValues() |
||
263 | |||
264 | /** |
||
265 | * Internal implementation for containsKey() |
||
266 | * |
||
267 | * @param string $hashCode |
||
268 | * @return bool |
||
269 | */ |
||
270 | 57 | private function doContainsKey(string $hashCode): bool |
|
274 | |||
275 | /** |
||
276 | * Internal implementation for get() |
||
277 | * |
||
278 | * @param string $hashCode |
||
279 | * @return mixed |
||
280 | */ |
||
281 | 14 | private function doGet(string $hashCode) |
|
289 | |||
290 | /** |
||
291 | * Internal implementation for put() |
||
292 | * |
||
293 | * @param string $hashCode |
||
294 | * @param mixed $key |
||
295 | * @param mixed $value |
||
296 | * @return mixed|null |
||
297 | */ |
||
298 | 57 | private function doPut(string $hashCode, $key, $value) |
|
305 | |||
306 | /** |
||
307 | * Internal implementation for remove() |
||
308 | * |
||
309 | * @param string $hashCode |
||
310 | * @return mixed|null |
||
311 | */ |
||
312 | 57 | private function doRemove(string $hashCode) |
|
323 | } |
||
324 |