trueskill/src/HashMap.php

54 lines
1.0 KiB
PHP
Raw Normal View History

2022-07-05 13:55:47 +00:00
<?php
namespace DNW\Skills;
/**
* Basic hashmap that supports object keys.
*/
class HashMap
{
2022-07-05 14:21:06 +00:00
private array $_hashToValue = [];
2022-07-05 13:55:47 +00:00
2022-07-05 14:21:06 +00:00
private array $_hashToKey = [];
public function getValue(string|object $key): mixed
{
$hash = self::getHash($key);
2022-07-05 13:55:47 +00:00
2022-07-05 14:21:06 +00:00
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;
2022-07-05 13:55:47 +00:00
return $this;
}
public function getAllKeys(): array
{
2022-07-05 14:21:06 +00:00
return array_values($this->_hashToKey);
}
public function getAllValues(): array
{
2022-07-05 14:21:06 +00:00
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;
}
2022-07-05 13:55:47 +00:00
}