Files
trueskill/src/HashMap.php

59 lines
1.0 KiB
PHP
Raw Normal View History

2022-07-05 15:55:47 +02:00
<?php
declare(strict_types=1);
2022-07-05 15:55:47 +02:00
namespace DNW\Skills;
/**
* Basic hashmap that supports object keys.
*/
class HashMap
{
2023-08-02 13:29:14 +00:00
/**
* @var mixed[] $hashToValue
*/
2023-08-01 13:35:44 +00:00
private array $hashToValue = [];
2022-07-05 15:55:47 +02:00
2023-08-02 13:29:14 +00:00
/**
* @var mixed[] $hashToKey
*/
2023-08-01 13:35:44 +00:00
private array $hashToKey = [];
2024-05-14 08:46:43 +00:00
public function getValue(object $key): mixed
{
2024-03-19 12:57:56 +00:00
$hash = spl_object_id($key);
2022-07-05 15:55:47 +02:00
2023-08-01 13:35:44 +00:00
return $this->hashToValue[$hash];
}
2024-03-19 12:57:56 +00:00
public function setValue(object $key, mixed $value): self
{
2024-03-19 12:57:56 +00:00
$hash = spl_object_id($key);
2023-08-01 13:35:44 +00:00
$this->hashToKey[$hash] = $key;
$this->hashToValue[$hash] = $value;
2022-07-05 15:55:47 +02:00
return $this;
}
2023-08-02 13:29:14 +00:00
/**
* @return mixed[]
*/
public function getAllKeys(): array
{
2023-08-01 13:35:44 +00:00
return array_values($this->hashToKey);
}
2023-08-02 13:29:14 +00:00
/**
* @return mixed[]
*/
public function getAllValues(): array
{
2023-08-01 13:35:44 +00:00
return array_values($this->hashToValue);
}
public function count(): int
{
2023-08-01 13:35:44 +00:00
return count($this->hashToKey);
}
2022-07-05 15:55:47 +02:00
}