Skip to content

Add HashMap Implementation and Comprehensive Unit Tests #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Collection/LinkedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ public function set(int $index, mixed $element): void
}

/**
* Clone method to ensure deep copy of nodes
*/
* Clone method to ensure deep copy of nodes.
*/
public function __clone()
{
$newList = new LinkedList();
$current = $this->head;
while ($current !== null) {
while (null !== $current) {
$newList->add($current->data);
$current = $current->next;
}
Expand Down
71 changes: 71 additions & 0 deletions src/Map/HashMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace KaririCode\DataStructure\Map;

use KaririCode\Contract\DataStructure\Map;

/**
* HashMap implementation.
*
* This class implements a hash map using PHP's built-in array as the underlying storage.
* It provides O(1) average time complexity for put, get, and remove operations.
*
* @category Maps
*
* @author Walmir Silva <walmir.silva@kariricode.org>
* @license MIT
*
* @see https://kariricode.org/
*/
class HashMap implements Map
{
private array $map = [];

public function put(mixed $key, mixed $value): void
{
$this->map[$key] = $value;
}

public function get(mixed $key): mixed
{
return $this->map[$key] ?? null;
}

public function remove(mixed $key): bool
{
if (array_key_exists($key, $this->map)) {
unset($this->map[$key]);

return true;
}

return false;
}

public function containsKey(mixed $key): bool
{
return array_key_exists($key, $this->map);
}

public function size(): int
{
return count($this->map);
}

public function clear(): void
{
$this->map = [];
}

public function keys(): array
{
return array_keys($this->map);
}

public function values(): array
{
return array_values($this->map);
}
}
107 changes: 107 additions & 0 deletions tests/Map/HashMapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace KaririCode\DataStructure\Tests\Map;

use KaririCode\DataStructure\Map\HashMap;
use PHPUnit\Framework\TestCase;

final class HashMapTest extends TestCase
{
// Test adding a key-value pair to the map
public function testPutAddsKeyValuePairToMap(): void
{
$map = new HashMap();
$map->put('key1', 'value1');
$this->assertSame('value1', $map->get('key1'));
}

// Test retrieving a value by key
public function testGetReturnsValueForKey(): void
{
$map = new HashMap();
$map->put('key1', 'value1');
$this->assertSame('value1', $map->get('key1'));
$this->assertNull($map->get('key2'));
}

// Test removing a key-value pair by key
public function testRemoveDeletesKeyValuePairFromMap(): void
{
$map = new HashMap();
$map->put('key1', 'value1');
$this->assertTrue($map->remove('key1'));
$this->assertFalse($map->remove('key2'));
$this->assertNull($map->get('key1'));
}

// Test checking if the map contains a specific key
public function testContainsKeyReturnsTrueIfKeyExists(): void
{
$map = new HashMap();
$map->put('key1', 'value1');
$this->assertTrue($map->containsKey('key1'));
$this->assertFalse($map->containsKey('key2'));
}

// Test clearing all key-value pairs from the map
public function testClearRemovesAllKeyValuePairsFromMap(): void
{
$map = new HashMap();
$map->put('key1', 'value1');
$map->put('key2', 'value2');
$map->clear();
$this->assertSame(0, $map->size());
$this->assertNull($map->get('key1'));
$this->assertNull($map->get('key2'));
}

// Test getting all keys from the map
public function testKeysReturnsAllKeysInMap(): void
{
$map = new HashMap();
$map->put('key1', 'value1');
$map->put('key2', 'value2');
$this->assertSame(['key1', 'key2'], $map->keys());
}

// Test getting all values from the map
public function testValuesReturnsAllValuesInMap(): void
{
$map = new HashMap();
$map->put('key1', 'value1');
$map->put('key2', 'value2');
$this->assertSame(['value1', 'value2'], $map->values());
}

// Test getting the size of the map
public function testSizeReturnsNumberOfKeyValuePairsInMap(): void
{
$map = new HashMap();
$this->assertSame(0, $map->size());
$map->put('key1', 'value1');
$map->put('key2', 'value2');
$this->assertSame(2, $map->size());
}

// Test handling null values in the map
public function testHandlingNullValuesCorrectly(): void
{
$map = new HashMap();
$map->put('key1', null);
$this->assertTrue($map->containsKey('key1'));
$this->assertNull($map->get('key1'));
$this->assertTrue($map->remove('key1'));
$this->assertFalse($map->containsKey('key1'));
}

// Test replacing a value for an existing key
public function testPutReplacesValueForExistingKey(): void
{
$map = new HashMap();
$map->put('key1', 'value1');
$map->put('key1', 'value2');
$this->assertSame('value2', $map->get('key1'));
}
}
Loading