From bfe85f4266a1e8894e084543bd40cc2796abe795 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sat, 6 Mar 2021 22:37:23 +0800 Subject: [PATCH] add hashmap keys values --- src/util/map/hashmap_keys.scad | 5 +++++ src/util/map/hashmap_values.scad | 5 +++++ test/util/map/test_hashmap.scad | 29 +++++++++++++++++------------ 3 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 src/util/map/hashmap_keys.scad create mode 100644 src/util/map/hashmap_values.scad diff --git a/src/util/map/hashmap_keys.scad b/src/util/map/hashmap_keys.scad new file mode 100644 index 00000000..d70ba639 --- /dev/null +++ b/src/util/map/hashmap_keys.scad @@ -0,0 +1,5 @@ +function hashmap_keys(map) = [ + for(bucket = map) + for(kv = bucket) + kv[0] +]; \ No newline at end of file diff --git a/src/util/map/hashmap_values.scad b/src/util/map/hashmap_values.scad new file mode 100644 index 00000000..6b704c75 --- /dev/null +++ b/src/util/map/hashmap_values.scad @@ -0,0 +1,5 @@ +function hashmap_values(map) = [ + for(bucket = map) + for(kv = bucket) + kv[1] +]; \ No newline at end of file diff --git a/test/util/map/test_hashmap.scad b/test/util/map/test_hashmap.scad index ae55d56a..31698111 100644 --- a/test/util/map/test_hashmap.scad +++ b/test/util/map/test_hashmap.scad @@ -1,5 +1,7 @@ use ; use ; +use ; +use ; use ; use ; use ; @@ -8,29 +10,32 @@ use ; 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(); \ No newline at end of file