trueskill/src/HashMap.php

59 lines
1013 B
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 13:55:47 +00:00
private $_hashToValue = [];
private $_hashToKey = [];
public function getValue($key)
{
$hash = self::getHash($key);
$hashValue = $this->_hashToValue[$hash];
2022-07-05 13:55:47 +00:00
2010-09-30 12:25:31 +00:00
return $hashValue;
}
public function setValue($key, $value)
{
$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()
{
$keys = array_values($this->_hashToKey);
2022-07-05 13:55:47 +00:00
return $keys;
}
public function getAllValues()
{
$values = array_values($this->_hashToValue);
2022-07-05 13:55:47 +00:00
return $values;
}
public function count()
{
return count($this->_hashToKey);
}
private static function getHash($key)
{
if (is_object($key)) {
return spl_object_hash($key);
}
return $key;
}
2022-07-05 13:55:47 +00:00
}