Quicker hash

This commit is contained in:
Jens True 2024-03-19 12:57:56 +00:00
parent fd91e9b0c1
commit 73ef2f45c8

@ -19,16 +19,16 @@ class HashMap
*/ */
private array $hashToKey = []; private array $hashToKey = [];
public function getValue(string|object $key): mixed public function getValue(object $key): object
{ {
$hash = self::getHash($key); $hash = spl_object_id($key);
return $this->hashToValue[$hash]; return $this->hashToValue[$hash];
} }
public function setValue(string|object $key, mixed $value): self public function setValue(object $key, mixed $value): self
{ {
$hash = self::getHash($key); $hash = spl_object_id($key);
$this->hashToKey[$hash] = $key; $this->hashToKey[$hash] = $key;
$this->hashToValue[$hash] = $value; $this->hashToValue[$hash] = $value;
@ -55,13 +55,4 @@ class HashMap
{ {
return count($this->hashToKey); return count($this->hashToKey);
} }
private static function getHash(string|object $key): string
{
if (is_object($key)) {
return spl_object_hash($key);
}
return $key;
}
} }