2010-09-18 11:11:44 -04:00
|
|
|
<?php
|
|
|
|
|
2010-09-25 18:25:56 -04:00
|
|
|
namespace Moserware\Skills;
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* Basic hashmap that supports object keys.
|
|
|
|
*/
|
2010-09-18 11:11:44 -04:00
|
|
|
class HashMap
|
|
|
|
{
|
|
|
|
private $_hashToValue = array();
|
|
|
|
private $_hashToKey = array();
|
|
|
|
|
2010-10-02 21:15:47 -04: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);
|
2010-09-30 08:25:31 -04:00
|
|
|
$hashValue = &$this->_hashToValue[$hash];
|
|
|
|
return $hashValue;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2010-10-02 21:15:47 -04:00
|
|
|
public function setValue(&$key, &$value)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
$hash = self::getHash($key);
|
2010-10-02 21:15:47 -04:00
|
|
|
$this->_hashToKey[$hash] = &$key;
|
|
|
|
$this->_hashToValue[$hash] = &$value;
|
2010-09-18 11:11:44 -04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-10-02 21:15:47 -04:00
|
|
|
public function &getAllKeys()
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2010-10-02 21:15:47 -04:00
|
|
|
$keys = &\array_values($this->_hashToKey);
|
|
|
|
return $keys;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllValues()
|
|
|
|
{
|
2010-10-02 21:15:47 -04:00
|
|
|
$values = &\array_values($this->_hashToValue);
|
|
|
|
return $values;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function count()
|
2010-10-03 20:17:34 -04:00
|
|
|
{
|
2010-09-18 11:11:44 -04:00
|
|
|
return \count($this->_hashToKey);
|
|
|
|
}
|
|
|
|
|
2010-10-02 21:15:47 -04:00
|
|
|
private static function getHash(&$key)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
if(\is_object($key))
|
|
|
|
{
|
|
|
|
return \spl_object_hash($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|