@@ -83,6 +83,7 @@ private function initializeBucket() {
83
83
* adds a node to the hash map
84
84
*
85
85
* @param Node $node
86
+ *
86
87
* @return bool
87
88
* @throws InvalidKeyTypeException
88
89
* @throws UnsupportedKeyTypeException
@@ -98,6 +99,7 @@ public function addNode(Node $node): bool {
98
99
*
99
100
* @param $key
100
101
* @param $value
102
+ *
101
103
* @return bool
102
104
* @throws InvalidKeyTypeException
103
105
* @throws UnsupportedKeyTypeException
@@ -128,6 +130,7 @@ public function add($key, $value): bool {
128
130
* Solution: use universal hashing
129
131
*
130
132
* @param $key
133
+ *
131
134
* @return int
132
135
* @throws InvalidKeyTypeException
133
136
* @throws UnsupportedKeyTypeException
@@ -150,6 +153,7 @@ private function getBucketIndex($key) {
150
153
* bucket index.
151
154
*
152
155
* @param $key
156
+ *
153
157
* @return int
154
158
* @throws InvalidKeyTypeException
155
159
* @throws UnsupportedKeyTypeException
@@ -163,6 +167,7 @@ private function getHash($key): int {
163
167
* calculates the bucket index for a given hash
164
168
*
165
169
* @param int $hash
170
+ *
166
171
* @return int
167
172
*/
168
173
private function getArrayIndex (int $ hash ): int {
@@ -175,6 +180,7 @@ private function getArrayIndex(int $hash): int {
175
180
*
176
181
* @param $key
177
182
* @param $value
183
+ *
178
184
* @return bool
179
185
* @throws InvalidKeyTypeException
180
186
* @throws UnsupportedKeyTypeException
@@ -183,44 +189,6 @@ public function put($key, $value): bool {
183
189
return $ this ->add ($ key , $ value );
184
190
}
185
191
186
- /**
187
- * @param $key
188
- * @return mixed|null
189
- * @throws InvalidKeyTypeException
190
- * @throws UnsupportedKeyTypeException
191
- */
192
- public function get ($ key ) {
193
- $ node = $ this ->getNodeByKey ($ key );
194
- if (null === $ node ) return null ;
195
- return $ node ->getValue ();
196
- }
197
-
198
- /**
199
- * searches the hash map for a node by a given key.
200
- *
201
- * @param $key
202
- * @return Node|null
203
- * @throws InvalidKeyTypeException
204
- * @throws UnsupportedKeyTypeException
205
- */
206
- public function getNodeByKey ($ key ): ?Node {
207
- $ arrayIndex = $ this ->getBucketIndex ($ key );
208
- /*
209
- * the list is requested from the array based on
210
- * the array index hash.
211
- */
212
- /** @var SinglyLinkedList $list */
213
- if (!isset ($ this ->bucket [$ arrayIndex ])) {
214
- return null ;
215
- }
216
- $ list = $ this ->bucket [$ arrayIndex ];
217
- if (!$ list ->containsKey ($ key )) {
218
- return null ;
219
- }
220
- $ node = $ list ->getNodeByKey ($ key );
221
- return $ node ;
222
- }
223
-
224
192
/**
225
193
* returns the number of elements in the map
226
194
*
@@ -239,10 +207,22 @@ public function size(): int {
239
207
return $ size ;
240
208
}
241
209
210
+ /**
211
+ * wrapper method for containsValue()
212
+ *
213
+ * @param $value
214
+ *
215
+ * @return bool
216
+ */
217
+ public function contains ($ value ): bool {
218
+ return $ this ->containsValue ($ value );
219
+ }
220
+
242
221
/**
243
222
* determines whether the HashMap contains a value.
244
223
*
245
224
* @param $value
225
+ *
246
226
* @return bool
247
227
*/
248
228
public function containsValue ($ value ): bool {
@@ -268,20 +248,11 @@ public function containsValue($value): bool {
268
248
return false ;
269
249
}
270
250
271
- /**
272
- * wrapper method for containsValue()
273
- *
274
- * @param $value
275
- * @return bool
276
- */
277
- public function contains ($ value ):bool {
278
- return $ this ->containsValue ($ value );
279
- }
280
-
281
251
/**
282
252
* determines whether the HashMap contains a key.
283
253
*
284
254
* @param $key
255
+ *
285
256
* @return bool
286
257
*/
287
258
public function containsKey ($ key ): bool {
@@ -315,6 +286,7 @@ public function containsKey($key): bool {
315
286
*
316
287
*
317
288
* @param $value
289
+ *
318
290
* @return Node|null
319
291
*/
320
292
public function getNodeByValue ($ value ): ?Node {
@@ -344,6 +316,7 @@ public function getNodeByValue($value): ?Node {
344
316
* removes a node by a given key
345
317
*
346
318
* @param $key
319
+ *
347
320
* @return bool
348
321
* @throws InvalidKeyTypeException
349
322
* @throws UnsupportedKeyTypeException
@@ -391,6 +364,32 @@ public function clear() {
391
364
$ this ->initializeBucket ();
392
365
}
393
366
367
+ /**
368
+ * @return array
369
+ */
370
+ public function countPerBucket () {
371
+ $ i = 0 ;
372
+ $ array = [];
373
+ /** @var SinglyLinkedList $list */
374
+ foreach ($ this ->bucket as $ list ) {
375
+ $ array [$ i ] = $ list ->size ();
376
+ $ i ++;
377
+ }
378
+ return $ array ;
379
+ }
380
+
381
+ /**
382
+ * returns the hash table as an array
383
+ * @return array
384
+ */
385
+ public function toArray (): array {
386
+ $ array = [];
387
+ foreach ($ this ->keySet () as $ key ) {
388
+ $ array [$ key ] = $ this ->get ($ key );
389
+ }
390
+ return $ array ;
391
+ }
392
+
394
393
/**
395
394
* basic implementation of Java-like keySet().
396
395
* The method returns an array containing the node keys.
@@ -417,20 +416,45 @@ public function keySet(): array {
417
416
}
418
417
419
418
/**
420
- * @return array
419
+ * @param $key
420
+ *
421
+ * @return mixed|null
422
+ * @throws InvalidKeyTypeException
423
+ * @throws UnsupportedKeyTypeException
421
424
*/
422
- public function countPerBucket () {
423
- $ i = 0 ;
424
- $ array = [];
425
+ public function get ($ key ) {
426
+ $ node = $ this ->getNodeByKey ($ key );
427
+ if (null === $ node ) return null ;
428
+ return $ node ->getValue ();
429
+ }
430
+
431
+ /**
432
+ * searches the hash map for a node by a given key.
433
+ *
434
+ * @param $key
435
+ *
436
+ * @return Node|null
437
+ * @throws InvalidKeyTypeException
438
+ * @throws UnsupportedKeyTypeException
439
+ */
440
+ public function getNodeByKey ($ key ): ?Node {
441
+ $ arrayIndex = $ this ->getBucketIndex ($ key );
442
+ /*
443
+ * the list is requested from the array based on
444
+ * the array index hash.
445
+ */
425
446
/** @var SinglyLinkedList $list */
426
- foreach ($ this ->bucket as $ list ) {
427
- $ array [$ i ] = $ list ->size ();
428
- $ i ++;
447
+ if (!isset ($ this ->bucket [$ arrayIndex ])) {
448
+ return null ;
429
449
}
430
- return $ array ;
450
+ $ list = $ this ->bucket [$ arrayIndex ];
451
+ if (!$ list ->containsKey ($ key )) {
452
+ return null ;
453
+ }
454
+ $ node = $ list ->getNodeByKey ($ key );
455
+ return $ node ;
431
456
}
432
457
433
-
434
458
/**
435
459
* Specify data which should be serialized to JSON
436
460
*
@@ -446,4 +470,4 @@ public function jsonSerialize() {
446
470
];
447
471
}
448
472
449
- }
473
+ }
0 commit comments