1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-22 14:23:23 +02:00
This commit is contained in:
Justin Lin
2021-03-06 15:42:25 +08:00
parent 19a02d8f2b
commit 7158cb36fa
4 changed files with 11 additions and 11 deletions

View File

@@ -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)

View File

@@ -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);

View File

@@ -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);

View File

@@ -1,7 +1,7 @@
use <unittest.scad>;
use <collection/hashmap.scad>;
use <collection/hashmap_list.scad>;
use <collection/hashmap_add.scad>;
use <collection/hashmap_put.scad>;
use <collection/hashmap_len.scad>;
use <collection/hashmap_del.scad>;
use <collection/hashmap_get.scad>;
@@ -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");