Skip to content

Commit d80ca5f

Browse files
committed
add toArray method to HashTable
1 parent cf8bdba commit d80ca5f

File tree

1 file changed

+82
-58
lines changed

1 file changed

+82
-58
lines changed

src/Datastructure/Table/HashTable.php

Lines changed: 82 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ private function initializeBucket() {
8383
* adds a node to the hash map
8484
*
8585
* @param Node $node
86+
*
8687
* @return bool
8788
* @throws InvalidKeyTypeException
8889
* @throws UnsupportedKeyTypeException
@@ -98,6 +99,7 @@ public function addNode(Node $node): bool {
9899
*
99100
* @param $key
100101
* @param $value
102+
*
101103
* @return bool
102104
* @throws InvalidKeyTypeException
103105
* @throws UnsupportedKeyTypeException
@@ -128,6 +130,7 @@ public function add($key, $value): bool {
128130
* Solution: use universal hashing
129131
*
130132
* @param $key
133+
*
131134
* @return int
132135
* @throws InvalidKeyTypeException
133136
* @throws UnsupportedKeyTypeException
@@ -150,6 +153,7 @@ private function getBucketIndex($key) {
150153
* bucket index.
151154
*
152155
* @param $key
156+
*
153157
* @return int
154158
* @throws InvalidKeyTypeException
155159
* @throws UnsupportedKeyTypeException
@@ -163,6 +167,7 @@ private function getHash($key): int {
163167
* calculates the bucket index for a given hash
164168
*
165169
* @param int $hash
170+
*
166171
* @return int
167172
*/
168173
private function getArrayIndex(int $hash): int {
@@ -175,6 +180,7 @@ private function getArrayIndex(int $hash): int {
175180
*
176181
* @param $key
177182
* @param $value
183+
*
178184
* @return bool
179185
* @throws InvalidKeyTypeException
180186
* @throws UnsupportedKeyTypeException
@@ -183,44 +189,6 @@ public function put($key, $value): bool {
183189
return $this->add($key, $value);
184190
}
185191

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-
224192
/**
225193
* returns the number of elements in the map
226194
*
@@ -239,10 +207,22 @@ public function size(): int {
239207
return $size;
240208
}
241209

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+
242221
/**
243222
* determines whether the HashMap contains a value.
244223
*
245224
* @param $value
225+
*
246226
* @return bool
247227
*/
248228
public function containsValue($value): bool {
@@ -268,20 +248,11 @@ public function containsValue($value): bool {
268248
return false;
269249
}
270250

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-
281251
/**
282252
* determines whether the HashMap contains a key.
283253
*
284254
* @param $key
255+
*
285256
* @return bool
286257
*/
287258
public function containsKey($key): bool {
@@ -315,6 +286,7 @@ public function containsKey($key): bool {
315286
*
316287
*
317288
* @param $value
289+
*
318290
* @return Node|null
319291
*/
320292
public function getNodeByValue($value): ?Node {
@@ -344,6 +316,7 @@ public function getNodeByValue($value): ?Node {
344316
* removes a node by a given key
345317
*
346318
* @param $key
319+
*
347320
* @return bool
348321
* @throws InvalidKeyTypeException
349322
* @throws UnsupportedKeyTypeException
@@ -391,6 +364,32 @@ public function clear() {
391364
$this->initializeBucket();
392365
}
393366

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+
394393
/**
395394
* basic implementation of Java-like keySet().
396395
* The method returns an array containing the node keys.
@@ -417,20 +416,45 @@ public function keySet(): array {
417416
}
418417

419418
/**
420-
* @return array
419+
* @param $key
420+
*
421+
* @return mixed|null
422+
* @throws InvalidKeyTypeException
423+
* @throws UnsupportedKeyTypeException
421424
*/
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+
*/
425446
/** @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;
429449
}
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;
431456
}
432457

433-
434458
/**
435459
* Specify data which should be serialized to JSON
436460
*
@@ -446,4 +470,4 @@ public function jsonSerialize() {
446470
];
447471
}
448472

449-
}
473+
}

0 commit comments

Comments
 (0)