vtm/source/helpers/hashmap.ts

111 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

2017-11-09 14:06:35 +01:00
/*
* Verrückte Turing-Maschinen A turing complete game
2018-03-29 22:00:42 +02:00
* Copyright (C) 2016-2018 kcf <vidofnir@folksprak.org>
2017-11-09 14:06:35 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2017-11-08 11:30:34 +01:00
2025-09-23 12:13:49 +02:00
namespace lib_hashmap
2017-11-08 11:30:34 +01:00
{
2018-03-27 19:21:40 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export type type_hashmap<type_key, type_value> =
2017-11-08 11:30:34 +01:00
{
2018-03-28 13:59:29 +02:00
hashfunction : (key : type_key)=>string;
store : {[hashvalue : string] : {key : type_key; value : type_value;}};
2018-03-27 19:21:40 +02:00
}
;
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function create<type_key, type_value>
2018-03-27 19:21:40 +02:00
(
2018-03-28 13:59:29 +02:00
hashfunction : (key : type_key)=>string
2018-03-27 19:21:40 +02:00
)
2018-03-28 13:59:29 +02:00
: type_hashmap<type_key, type_value>
2018-03-27 19:21:40 +02:00
{
return {
"hashfunction": hashfunction,
2018-03-28 13:59:29 +02:00
"store": {},
2018-03-27 19:21:40 +02:00
};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function set<type_key, type_value>
2018-03-27 19:21:40 +02:00
(
2018-03-28 13:59:29 +02:00
hashmap : type_hashmap<type_key, type_value>,
key : type_key,
value : type_value
2018-03-27 19:21:40 +02:00
)
: void
{
2018-03-28 13:59:29 +02:00
let hashvalue : string = hashmap.hashfunction(key);
hashmap.store[hashvalue] = {"key": key, "value": value};
2018-03-27 19:21:40 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function get<type_key, type_value>
2018-03-27 19:21:40 +02:00
(
2018-03-28 13:59:29 +02:00
hashmap : type_hashmap<type_key, type_value>,
key : type_key
2018-03-27 19:21:40 +02:00
)
2018-03-28 13:59:29 +02:00
: lib_errormonade.type_errormonade<type_value>
2018-03-27 19:21:40 +02:00
{
2018-03-28 13:59:29 +02:00
let hashvalue : string = hashmap.hashfunction(key);
if (hashvalue in hashmap.store)
2017-11-08 11:30:34 +01:00
{
2018-03-28 13:59:29 +02:00
let value : type_value = hashmap.store[hashvalue].value;
return (lib_errormonade.create_just<type_value>(value));
2018-03-27 19:21:40 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
return (lib_errormonade.create_nothing<type_value>());
2018-03-27 19:21:40 +02:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function iterate<type_key, type_value>
2018-03-27 19:21:40 +02:00
(
2018-03-28 13:59:29 +02:00
hashmap : type_hashmap<type_key, type_value>,
procedure : (key ?: type_key, value ?: type_value)=>void
2018-03-27 19:21:40 +02:00
)
: void
{
2018-03-28 13:59:29 +02:00
Object.keys(hashmap.store).forEach
2018-03-27 19:21:40 +02:00
(
2018-03-28 13:59:29 +02:00
(hashvalue) =>
2018-03-27 17:24:31 +02:00
{
2018-03-28 13:59:29 +02:00
let paar : {key : type_key; value : type_value;} = hashmap.store[hashvalue];
procedure(paar.key, paar.value);
2018-03-27 17:24:31 +02:00
}
2018-03-27 19:21:40 +02:00
)
;
2017-11-08 11:30:34 +01:00
}
}
2018-03-27 19:21:40 +02:00