2022-07-05 15:55:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DNW\Skills;
|
2010-09-25 18:25:56 -04:00
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* Basic hashmap that supports object keys.
|
|
|
|
*/
|
2010-09-18 11:11:44 -04:00
|
|
|
class HashMap
|
|
|
|
{
|
2022-07-05 15:55:47 +02:00
|
|
|
private $_hashToValue = [];
|
|
|
|
|
|
|
|
private $_hashToKey = [];
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
public function getValue($key)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2010-09-28 08:12:06 -04:00
|
|
|
$hash = self::getHash($key);
|
2016-05-24 16:31:21 +02:00
|
|
|
$hashValue = $this->_hashToValue[$hash];
|
2022-07-05 15:55:47 +02:00
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
return $hashValue;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
public function setValue($key, $value)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
$hash = self::getHash($key);
|
2016-05-24 16:31:21 +02:00
|
|
|
$this->_hashToKey[$hash] = $key;
|
|
|
|
$this->_hashToValue[$hash] = $value;
|
2022-07-05 15:55:47 +02:00
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
public function getAllKeys()
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$keys = array_values($this->_hashToKey);
|
2022-07-05 15:55:47 +02:00
|
|
|
|
2010-10-02 21:15:47 -04:00
|
|
|
return $keys;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllValues()
|
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$values = array_values($this->_hashToValue);
|
2022-07-05 15:55:47 +02:00
|
|
|
|
2010-10-02 21:15:47 -04:00
|
|
|
return $values;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function count()
|
2016-05-24 14:10:39 +02:00
|
|
|
{
|
|
|
|
return count($this->_hashToKey);
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
private static function getHash($key)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2016-05-24 14:10:39 +02:00
|
|
|
if (is_object($key)) {
|
|
|
|
return spl_object_hash($key);
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
2022-07-05 15:55:47 +02:00
|
|
|
}
|