diff --git a/src/collection/_impl/_hashmap_add_impl.scad b/src/collection/_impl/_hashmap_put_impl.scad similarity index 79% rename from src/collection/_impl/_hashmap_add_impl.scad rename to src/collection/_impl/_hashmap_put_impl.scad index d2d81ff4..42e911d5 100644 --- a/src/collection/_impl/_hashmap_add_impl.scad +++ b/src/collection/_impl/_hashmap_put_impl.scad @@ -2,14 +2,14 @@ use <../../util/slice.scad>; use <../../util/some.scad>; use <../../util/find_index.scad>; -function _hashmap_add(map, key, value, eq, hash) = +function _hashmap_put(map, key, value, eq, hash) = let( b_idx = hash(key) % len(map), bucket = map[b_idx], k_idx = find_index(bucket, function(kv) eq(kv[0], key)) ) k_idx != -1 ? _replace(map, bucket, key, value, b_idx, k_idx) : - _add(map, bucket, key, value, b_idx); + _put(map, bucket, key, value, b_idx); function _replace(map, bucket, key, value, b_idx, k_idx) = let( @@ -25,7 +25,7 @@ function _replace(map, bucket, key, value, b_idx, k_idx) = slice(map, b_idx + 1) ); -function _add(map, bucket, key, value, b_idx) = concat( +function _put(map, bucket, key, value, b_idx) = concat( slice(map, 0, b_idx), [concat(bucket, [[key, value]])], slice(map, b_idx + 1) diff --git a/src/collection/hashmap_add.scad b/src/collection/hashmap_add.scad deleted file mode 100644 index e9be9fec..00000000 --- a/src/collection/hashmap_add.scad +++ /dev/null @@ -1,5 +0,0 @@ -use <../__comm__/_str_hash.scad>; -use <_impl/_hashmap_add_impl.scad>; - -function hashmap_add(map, key, value, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) = - _hashmap_add(map, key, value, eq, hash); \ No newline at end of file diff --git a/src/collection/hashmap_put.scad b/src/collection/hashmap_put.scad new file mode 100644 index 00000000..e4b55d1f --- /dev/null +++ b/src/collection/hashmap_put.scad @@ -0,0 +1,5 @@ +use <../__comm__/_str_hash.scad>; +use <_impl/_hashmap_put_impl.scad>; + +function hashmap_put(map, key, value, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) = + _hashmap_put(map, key, value, eq, hash); \ No newline at end of file diff --git a/test/collection/test_hashmap.scad b/test/collection/test_hashmap.scad index ee46e64d..d97bc64d 100644 --- a/test/collection/test_hashmap.scad +++ b/test/collection/test_hashmap.scad @@ -1,7 +1,7 @@ use ; use ; use ; -use ; +use ; use ; use ; use ; @@ -20,10 +20,10 @@ module test_hashmap() { assert(hashmap_list(s) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 2]]); - s2 = hashmap_add(s, "k1357", 100); + s2 = hashmap_put(s, "k1357", 100); assert(hashmap_list(s2) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 2], ["k1357", 100]]); - s3 = hashmap_add(s2, "k5678", 200); + s3 = hashmap_put(s2, "k5678", 200); assert(hashmap_list(s3) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 200], ["k1357", 100]]); s4 = hashmap_del(s3, "k4444");