142 lines
2.4 KiB
PHP
142 lines
2.4 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace alveolata\structures;
|
||
|
|
||
|
|
||
|
require_once(DIR_ALVEOLATA . '/structures/pair/functions.php');
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @template type_key
|
||
|
* @template type_value
|
||
|
*/
|
||
|
class struct_subject_map
|
||
|
{
|
||
|
|
||
|
/**
|
||
|
* @var array {list<&struct_subject_pair<§type_key,§type_value>>}
|
||
|
*/
|
||
|
public $pairs;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @param array {list<&struct_subject_pair<§type_key,§type_value>>}
|
||
|
*/
|
||
|
public function __construct(
|
||
|
array $pairs
|
||
|
)
|
||
|
{
|
||
|
$this->pairs = $pairs;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @template type_key
|
||
|
* @template type_value
|
||
|
* @param array {list<record<key:§type_key,value:§type_value>>}
|
||
|
* @return struct_subject_map<type_key,type_value>
|
||
|
*/
|
||
|
function map_make(
|
||
|
array $pairs = []
|
||
|
)
|
||
|
{
|
||
|
return (new struct_subject_map($pairs));
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @template type_key
|
||
|
* @template type_value
|
||
|
* @param struct_subject_map<type_key,type_value> $subject
|
||
|
* @param type_key $key
|
||
|
* @return bool
|
||
|
*/
|
||
|
function map_has(
|
||
|
\Closure $collate_key,
|
||
|
struct_subject_map $subject,
|
||
|
$key
|
||
|
) : bool
|
||
|
{
|
||
|
foreach ($subject->pairs as $pair) {
|
||
|
if ($collate_key($key, $pair->first)) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @template type_key
|
||
|
* @template type_value
|
||
|
* @param struct_subject_map<type_key,type_value> $subject
|
||
|
* @param type_key $key
|
||
|
* @return type_value
|
||
|
* @throws \Exception if not found
|
||
|
*/
|
||
|
function map_get(
|
||
|
\Closure $collate_key,
|
||
|
struct_subject_map $subject,
|
||
|
$key
|
||
|
)
|
||
|
{
|
||
|
foreach ($subject->pairs as $pair) {
|
||
|
if ($collate_key($key, $pair->first)) {
|
||
|
return $pair->second;
|
||
|
}
|
||
|
}
|
||
|
throw (new \Exception('not found'));
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @template type_key
|
||
|
* @template type_value
|
||
|
* @param struct_subject_map<type_key,type_value> $subject
|
||
|
* @param type_key $key
|
||
|
* @param type_value $value
|
||
|
*/
|
||
|
function map_set(
|
||
|
\Closure $collate_key,
|
||
|
struct_subject_map $subject,
|
||
|
$key,
|
||
|
$value
|
||
|
) : void
|
||
|
{
|
||
|
$found = false;
|
||
|
foreach ($subject->pairs as &$pair) {
|
||
|
if ($collate_key($key, $pair->first)) {
|
||
|
$pair->second = $value;
|
||
|
$found = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
unset($pair);
|
||
|
if (! $found) {
|
||
|
$pair = \alveolata\structures\pair_make($key, $value);
|
||
|
array_push($subject->pairs, $pair);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @template type_key
|
||
|
* @template type_value
|
||
|
* @param struct_subject_map<type_key,type_value> $subject
|
||
|
* @param \Closure $procedure {function<§type_key,§type_value,void>}
|
||
|
*/
|
||
|
function map_iterate(
|
||
|
struct_subject_map $subject,
|
||
|
\Closure $procedure
|
||
|
) : void
|
||
|
{
|
||
|
foreach ($subject->pairs as $pair) {
|
||
|
$procedure($pair->first, $pair->second);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
?>
|