mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-01-17 14:18:13 +01:00
2.3 KiB
2.3 KiB
hashmap
This function maps keys to values. You can use the following to process the returned map.
- util/map/hashmap_put : Puts an element to the map. It returns a new map.
- util/map/hashmap_get : Returns the value to which the specified key is mapped, or
undef
if the map contains no mapping for the key. - util/map/hashmap_del : Deletes the mapping for the specified key from a map if present.
- util/map/hashmap_len : Returns the length of the map.
- util/map/hashmap_keys : Returns a list containing all keys in the map.
- util/map/hashmap_values : Returns a list containing all values in the map.
- util/map/hashmap_entries : Returns a list containing all
[key, value]
s in the map.
Since: 3.0
Parameters
kv_lt
: Constructs a new map containing the[key, value]
s in the specified list. It can be ignored if you want an empty map.eq
: A equality function. If it's ignored, use==
to compare elements.hash
: A hash function. If it's ignored, convert each element to a string and hash it.number_of_buckets
: The function uses a hash table internally. Change the number of buckets if you're trying to do optimization.
Examples
use <util/map/hashmap.scad>;
use <util/map/hashmap_len.scad>;
use <util/map/hashmap_put.scad>;
use <util/map/hashmap_get.scad>;
use <util/map/hashmap_del.scad>;
use <util/map/hashmap_keys.scad>;
use <util/map/hashmap_values.scad>;
use <util/map/hashmap_entries.scad>;
m1 = hashmap([["k1", 10], ["k2", 20], ["k3", 30]]);
assert(hashmap_len(m1) == 3);
m2 = hashmap_put(m1, "k4", 40);
assert(hashmap_get(m2, "k4") == 40);
m3 = hashmap_del(m2, "k1");
assert(hashmap_get(m3, "k1") == undef);
echo(hashmap_keys(m3)); // a list contains "k2", "k2", "k3"
echo(hashmap_values(m3)); // a list contains 20, 30, 40
echo(hashmap_entries(m3)); // a list contains ["k2", 20], ["k3", 30], ["k4", 40]