1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-01-17 14:18:13 +01:00
dotSCAD/docs/lib3x-hashmap.md
2021-03-09 17:11:02 +08:00

2.3 KiB

hashmap

This function maps keys to values. You can use the following to process the returned 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]