mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-01-29 11:58:39 +01:00
add hashmap keys values
This commit is contained in:
parent
139df57f1a
commit
bfe85f4266
5
src/util/map/hashmap_keys.scad
Normal file
5
src/util/map/hashmap_keys.scad
Normal file
@ -0,0 +1,5 @@
|
||||
function hashmap_keys(map) = [
|
||||
for(bucket = map)
|
||||
for(kv = bucket)
|
||||
kv[0]
|
||||
];
|
5
src/util/map/hashmap_values.scad
Normal file
5
src/util/map/hashmap_values.scad
Normal file
@ -0,0 +1,5 @@
|
||||
function hashmap_values(map) = [
|
||||
for(bucket = map)
|
||||
for(kv = bucket)
|
||||
kv[1]
|
||||
];
|
@ -1,5 +1,7 @@
|
||||
use <util/map/hashmap.scad>;
|
||||
use <util/map/hashmap_entries.scad>;
|
||||
use <util/map/hashmap_keys.scad>;
|
||||
use <util/map/hashmap_values.scad>;
|
||||
use <util/map/hashmap_put.scad>;
|
||||
use <util/map/hashmap_len.scad>;
|
||||
use <util/map/hashmap_del.scad>;
|
||||
@ -8,29 +10,32 @@ use <util/map/hashmap_get.scad>;
|
||||
module test_hashmap() {
|
||||
echo("==== test_hashmap ====");
|
||||
|
||||
s = hashmap([
|
||||
m = hashmap([
|
||||
["k1234", 1],
|
||||
["k5678", 2],
|
||||
["k9876", 3],
|
||||
["k4444", 3],
|
||||
]);
|
||||
|
||||
assert(hashmap_len(s) == 4);
|
||||
assert(hashmap_len(m) == 4);
|
||||
|
||||
assert(hashmap_entries(s) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 2]]);
|
||||
assert(hashmap_keys(m) == ["k9876", "k4444", "k1234", "k5678"]);
|
||||
assert(hashmap_values(m) == [3, 3, 1, 2]);
|
||||
|
||||
assert(hashmap_entries(m) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 2]]);
|
||||
|
||||
s2 = hashmap_put(s, "k1357", 100);
|
||||
assert(hashmap_entries(s2) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 2], ["k1357", 100]]);
|
||||
m2 = hashmap_put(m, "k1357", 100);
|
||||
assert(hashmap_entries(m2) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 2], ["k1357", 100]]);
|
||||
|
||||
s3 = hashmap_put(s2, "k5678", 200);
|
||||
assert(hashmap_entries(s3) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 200], ["k1357", 100]]);
|
||||
m3 = hashmap_put(m2, "k5678", 200);
|
||||
assert(hashmap_entries(m3) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 200], ["k1357", 100]]);
|
||||
|
||||
s4 = hashmap_del(s3, "k4444");
|
||||
assert(hashmap_entries(s4) == [["k9876", 3], ["k1234", 1], ["k5678", 200], ["k1357", 100]]);
|
||||
assert(hashmap_entries(hashmap_del(s4, "k4444")) == [["k9876", 3], ["k1234", 1], ["k5678", 200], ["k1357", 100]]);
|
||||
m4 = hashmap_del(m3, "k4444");
|
||||
assert(hashmap_entries(m4) == [["k9876", 3], ["k1234", 1], ["k5678", 200], ["k1357", 100]]);
|
||||
assert(hashmap_entries(hashmap_del(m4, "k4444")) == [["k9876", 3], ["k1234", 1], ["k5678", 200], ["k1357", 100]]);
|
||||
|
||||
assert(hashmap_get(s4, "k1234") == 1);
|
||||
assert(hashmap_get(s4, "k0000") == undef);
|
||||
assert(hashmap_get(m4, "k1234") == 1);
|
||||
assert(hashmap_get(m4, "k0000") == undef);
|
||||
}
|
||||
|
||||
test_hashmap();
|
Loading…
x
Reference in New Issue
Block a user