mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-01-17 22:28:16 +01:00
add hashmap add len list
This commit is contained in:
parent
65c7ba3ae2
commit
1cd2af5760
32
src/collection/_impl/_hashmap_add_impl.scad
Normal file
32
src/collection/_impl/_hashmap_add_impl.scad
Normal file
@ -0,0 +1,32 @@
|
||||
use <../../util/slice.scad>;
|
||||
use <../../util/some.scad>;
|
||||
use <../../util/find_index.scad>;
|
||||
|
||||
function _hashmap_add(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);
|
||||
|
||||
function _replace(map, bucket, key, value, b_idx, k_idx) =
|
||||
let(
|
||||
n_bucket = concat(
|
||||
slice(bucket, 0, k_idx),
|
||||
[[key, value]],
|
||||
slice(bucket, k_idx + 1)
|
||||
)
|
||||
)
|
||||
concat(
|
||||
slice(map, 0, b_idx),
|
||||
[n_bucket],
|
||||
slice(map, b_idx + 1)
|
||||
);
|
||||
|
||||
function _add(map, bucket, key, value, b_idx) = concat(
|
||||
slice(map, 0, b_idx),
|
||||
[concat(bucket, [[key, value]])],
|
||||
slice(map, b_idx + 1)
|
||||
);
|
5
src/collection/_impl/_hashmap_impl.scad
Normal file
5
src/collection/_impl/_hashmap_impl.scad
Normal file
@ -0,0 +1,5 @@
|
||||
use <_hashmap_add_impl.scad>;
|
||||
|
||||
function _hashmap(kv_lt, leng, buckets, eq, hash, i = 0) =
|
||||
i == leng ? buckets :
|
||||
_hashmap(kv_lt, leng, _hashmap_add(buckets, kv_lt[i][0], kv_lt[i][1], eq, hash), eq, hash, i + 1);
|
14
src/collection/hashmap.scad
Normal file
14
src/collection/hashmap.scad
Normal file
@ -0,0 +1,14 @@
|
||||
use <../__comm__/_str_hash.scad>;
|
||||
use <_impl/_hashmap_impl.scad>;
|
||||
use <_impl/_hashmap_add_impl.scad>;
|
||||
|
||||
function hashmap(kv_lt, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e), bucket_numbers) =
|
||||
let(
|
||||
kv_lt_undef = is_undef(kv_lt),
|
||||
leng_kv_lt = kv_lt_undef ? -1 : len(kv_lt),
|
||||
bucket_numbers_undef = is_undef(bucket_numbers),
|
||||
b_numbers = bucket_numbers_undef ?
|
||||
(kv_lt_undef || leng_kv_lt < 256 ? 16 : ceil(sqrt(leng_kv_lt))) : bucket_numbers,
|
||||
buckets = [for(i = [0:b_numbers - 1]) []]
|
||||
)
|
||||
kv_lt_undef ? buckets : _hashmap(kv_lt, leng_kv_lt, buckets, eq, hash);
|
5
src/collection/hashmap_add.scad
Normal file
5
src/collection/hashmap_add.scad
Normal file
@ -0,0 +1,5 @@
|
||||
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);
|
6
src/collection/hashmap_len.scad
Normal file
6
src/collection/hashmap_len.scad
Normal file
@ -0,0 +1,6 @@
|
||||
use <../util/sum.scad>;
|
||||
|
||||
function hashmap_len(map) = sum([
|
||||
for(bucket = map)
|
||||
len(bucket)
|
||||
]);
|
5
src/collection/hashmap_list.scad
Normal file
5
src/collection/hashmap_list.scad
Normal file
@ -0,0 +1,5 @@
|
||||
function hashmap_list(map) = [
|
||||
for(bucket = map)
|
||||
for(kv = bucket)
|
||||
kv
|
||||
];
|
28
test/collection/test_hashmap.scad
Normal file
28
test/collection/test_hashmap.scad
Normal file
@ -0,0 +1,28 @@
|
||||
use <unittest.scad>;
|
||||
use <collection/hashmap.scad>;
|
||||
use <collection/hashmap_list.scad>;
|
||||
use <collection/hashmap_add.scad>;
|
||||
use <collection/hashmap_len.scad>;
|
||||
|
||||
module test_hashmap() {
|
||||
echo("==== test_hashmap ====");
|
||||
|
||||
s = hashmap([
|
||||
["k1234", 1],
|
||||
["k5678", 2],
|
||||
["k9876", 3],
|
||||
["k4444", 3],
|
||||
]);
|
||||
|
||||
assert(hashmap_len(s) == 4);
|
||||
|
||||
assert(hashmap_list(s) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 2]]);
|
||||
|
||||
s2 = hashmap_add(s, "k1357", 100);
|
||||
assert(hashmap_list(s2) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 2], ["k1357", 100]]);
|
||||
|
||||
s3 = hashmap_add(s2, "k5678", 200);
|
||||
assert(hashmap_list(s3) == [["k9876", 3], ["k4444", 3], ["k1234", 1], ["k5678", 200], ["k1357", 100]]);
|
||||
}
|
||||
|
||||
test_hashmap();
|
Loading…
x
Reference in New Issue
Block a user