trueskill/src/HashMap.php
2023-08-02 13:29:14 +00:00

66 lines
1.2 KiB
PHP

<?php
namespace DNW\Skills;
/**
* Basic hashmap that supports object keys.
*/
class HashMap
{
/**
* @var mixed[] $hashToValue
*/
private array $hashToValue = [];
/**
* @var mixed[] $hashToKey
*/
private array $hashToKey = [];
public function getValue(string|object $key): mixed
{
$hash = self::getHash($key);
return $this->hashToValue[$hash];
}
public function setValue(string|object $key, mixed $value): self
{
$hash = self::getHash($key);
$this->hashToKey[$hash] = $key;
$this->hashToValue[$hash] = $value;
return $this;
}
/**
* @return mixed[]
*/
public function getAllKeys(): array
{
return array_values($this->hashToKey);
}
/**
* @return mixed[]
*/
public function getAllValues(): array
{
return array_values($this->hashToValue);
}
public function count(): int
{
return count($this->hashToKey);
}
private static function getHash(string|object $key): string
{
if (is_object($key)) {
return spl_object_hash($key);
}
return $key;
}
}