qqq
This commit is contained in:
+109
-109
@@ -10,55 +10,55 @@ use Traversable;
|
|||||||
class HashMap implements \ArrayAccess, \IteratorAggregate
|
class HashMap implements \ArrayAccess, \IteratorAggregate
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private array $lists = [];
|
private array $lists = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Traversable
|
* @return Traversable
|
||||||
*/
|
*/
|
||||||
public function getIterator(): Traversable
|
public function getIterator(): Traversable
|
||||||
{
|
{
|
||||||
return new \ArrayIterator($this->lists);
|
return new \ArrayIterator($this->lists);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasItem(): bool
|
public function hasItem(): bool
|
||||||
{
|
{
|
||||||
return count($this->lists) > 0;
|
return count($this->lists) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function put(string $key, $value)
|
public function put(string $key, $value)
|
||||||
{
|
{
|
||||||
$this->lists[$key] = $value;
|
$this->lists[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function append(string $key, $value): void
|
public function append(string $key, $value): void
|
||||||
{
|
{
|
||||||
if (!$this->has($key)) {
|
if (!$this->has($key)) {
|
||||||
$this->lists[$key] = [];
|
$this->lists[$key] = [];
|
||||||
} else if (!is_array($this->lists[$key])) {
|
} else if (!is_array($this->lists[$key])) {
|
||||||
throw new Exception('Source must a array.');
|
throw new Exception('Source must a array.');
|
||||||
}
|
}
|
||||||
$this->lists[$key][] = $value;
|
$this->lists[$key][] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,87 +66,87 @@ class HashMap implements \ArrayAccess, \IteratorAggregate
|
|||||||
* @param mixed|null $default
|
* @param mixed|null $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
#[Pure] public function get(string $key, mixed $default = null): mixed
|
#[Pure] public function get(string $key, mixed $default = null): mixed
|
||||||
{
|
{
|
||||||
if (!$this->has($key)) {
|
if (!$this->has($key)) {
|
||||||
return $default;
|
return $default;
|
||||||
}
|
}
|
||||||
return $this->lists[$key];
|
return $this->lists[$key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
*/
|
*/
|
||||||
public function del(string $key)
|
public function del(string $key)
|
||||||
{
|
{
|
||||||
if (!$this->has($key)) {
|
if (!$this->has($key)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
unset($this->lists[$key]);
|
unset($this->lists[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function has(string $key): bool
|
public function has(string $key): bool
|
||||||
{
|
{
|
||||||
return array_key_exists($key, $this->lists);
|
return array_key_exists($key, $this->lists);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $offset
|
* @param mixed $offset
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function offsetExists(mixed $offset): bool
|
public function offsetExists(mixed $offset): bool
|
||||||
{
|
{
|
||||||
return isset($this->lists[$offset]);
|
return isset($this->lists[$offset]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $offset
|
* @param mixed $offset
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
#[Pure] public function offsetGet(mixed $offset): mixed
|
#[Pure] public function offsetGet(mixed $offset): mixed
|
||||||
{
|
{
|
||||||
return $this->get($offset);
|
return $this->get($offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $offset
|
* @param mixed $offset
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*/
|
*/
|
||||||
#[ReturnTypeWillChange]
|
#[ReturnTypeWillChange]
|
||||||
public function offsetSet(mixed $offset, mixed $value)
|
public function offsetSet(mixed $offset, mixed $value)
|
||||||
{
|
{
|
||||||
$this->put($offset, $value);
|
$this->put($offset, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $offset
|
* @param mixed $offset
|
||||||
*/
|
*/
|
||||||
#[ReturnTypeWillChange]
|
#[ReturnTypeWillChange]
|
||||||
public function offsetUnset(mixed $offset)
|
public function offsetUnset(mixed $offset)
|
||||||
{
|
{
|
||||||
unset($this->lists[$offset]);
|
unset($this->lists[$offset]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function Tree(HashMap $root, string $leaf)
|
public static function Tree(HashMap $root, string $leaf)
|
||||||
{
|
{
|
||||||
if ($root->has($leaf)) {
|
if ($root->has($leaf)) {
|
||||||
$hashMap = $root->get($leaf);
|
$hashMap = $root->get($leaf);
|
||||||
} else {
|
} else {
|
||||||
$hashMap = new HashMap();
|
$hashMap = new HashMap();
|
||||||
$root->put($leaf, $hashMap);
|
$root->put($leaf, $hashMap);
|
||||||
}
|
}
|
||||||
return $hashMap;
|
return $hashMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user