Skip to content

Commit 6d2636f

Browse files
committed
add hashable Objects
1 parent 9825f53 commit 6d2636f

File tree

4 files changed

+115
-10
lines changed

4 files changed

+115
-10
lines changed

src/Common/Interfaces/IHashable.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* MIT License
5+
*
6+
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
namespace doganoo\PHPAlgorithms\Common\Interfaces;
28+
29+
interface IHashable {
30+
31+
public function getHash(): string;
32+
33+
}

src/Common/Util/MapUtil.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828

2929
use doganoo\PHPAlgorithms\Common\Exception\InvalidKeyTypeException;
3030
use doganoo\PHPAlgorithms\Common\Exception\UnsupportedKeyTypeException;
31+
use doganoo\PHPAlgorithms\Common\Interfaces\IHashable;
3132
use function array_walk_recursive;
3233
use function ceil;
34+
use function doubleval;
3335
use function gettype;
3436
use function is_array;
3537
use function is_bool;
@@ -83,20 +85,23 @@ public static function normalizeKey($key): int {
8385
*/
8486
if (is_string($key)) {
8587
$key = MapUtil::stringToKey($key);
86-
} else if (is_object($key)) {
87-
$objectString = MapUtil::objectToString($key);
88-
$key = MapUtil::stringToKey($objectString);
88+
} else if ($key instanceof IHashable) {
89+
$key = $key->getHash();
90+
$key = MapUtil::stringToKey($key);
91+
} else if (is_object($key) && !($key instanceof IHashable)) {
92+
$objectString = MapUtil::objectToString($key);
93+
$key = MapUtil::stringToKey($objectString);
8994
} else if (is_array($key)) {
90-
$arrayString = MapUtil::arrayToKey($key);
91-
$key = MapUtil::stringToKey($arrayString);
95+
$arrayString = MapUtil::arrayToKey($key);
96+
$key = MapUtil::stringToKey($arrayString);
9297
} else if (is_double($key)) {
93-
$key = MapUtil::doubleToKey($key);
98+
$key = MapUtil::doubleToKey($key);
9499
} else if (is_float($key)) {
95-
$key = MapUtil::doubleToKey(\doubleval($key));
100+
$key = MapUtil::doubleToKey(doubleval($key));
96101
} else if (is_bool($key)) {
97-
$key = MapUtil::booleanToKey($key);
102+
$key = MapUtil::booleanToKey($key);
98103
} else if (is_resource($key) || $key === null) {
99-
$key = MapUtil::booleanToKey(true);
104+
$key = MapUtil::booleanToKey(true);
100105
} else if (is_int($key)) {
101106
return $key;
102107
} else {

tests/Table/Entity/HashableObject.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* MIT License
5+
*
6+
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
namespace doganoo\PHPAlgorithmsTest\Table\Entity;
28+
29+
use doganoo\PHPAlgorithms\Common\Interfaces\IHashable;
30+
31+
class HashableObject implements IHashable {
32+
33+
private $id;
34+
35+
public function __construct($id) {
36+
$this->id = $id;
37+
}
38+
39+
public function getHash(): string {
40+
return $this->id;
41+
}
42+
43+
}

tests/Table/HashTableTest.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
namespace doganoo\PHPAlgorithmsTest\Table;
2828

29+
use doganoo\PHPAlgorithms\Common\Interfaces\IHashable;
2930
use doganoo\PHPAlgorithms\Datastructure\Table\HashTable;
31+
use doganoo\PHPAlgorithmsTest\Table\Entity\HashableObject;
3032
use doganoo\PHPAlgorithmsTest\Util\HashTableUtil;
3133
use PHPUnit\Framework\TestCase;
3234
use stdClass;
@@ -160,11 +162,33 @@ public function testClosure(): void {
160162
$this->assertTrue($added);
161163
$added = $hashMap->add("test2", new class {
162164

163-
public function x():void {
165+
public function x(): void {
164166
}
165167

166168
});
167169
$this->assertTrue($added);
168170
}
169171

172+
public function testHashableObject(): void {
173+
$obj = new HashableObject("1");
174+
$table = new HashTable();
175+
$table->put($obj, "1");
176+
177+
$this->assertTrue($table->size() === 1 && $table->get($obj) === "1");
178+
179+
$obj2 = new HashableObject("1");
180+
$table->put($obj2, "2");
181+
$this->assertTrue($table->size() === 1 && $table->get($obj) === "2");
182+
183+
$table->put("1", "3");
184+
$this->assertTrue(
185+
$table->size() === 2
186+
&& $table->get($obj) === "2"
187+
&& $table->get("1") === "3"
188+
);
189+
190+
$this->assertInstanceOf(IHashable::class, $table->keySet()[0]);
191+
$this->assertTrue(is_string($table->keySet()[1]));
192+
}
193+
170194
}

0 commit comments

Comments
 (0)