From d92ca8161ae0d1dafa96587d9b2fb26eb32826af Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Mon, 3 May 2021 17:25:45 +0800 Subject: [PATCH] don't cal len repeatly --- src/util/set/_impl/_hashset_add_impl.scad | 13 ++++++------- src/util/set/_impl/_hashset_impl.scad | 5 +++-- src/util/set/hashset.scad | 2 +- src/util/set/hashset_add.scad | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/util/set/_impl/_hashset_add_impl.scad b/src/util/set/_impl/_hashset_add_impl.scad index 76aa2993..42355163 100644 --- a/src/util/set/_impl/_hashset_add_impl.scad +++ b/src/util/set/_impl/_hashset_add_impl.scad @@ -1,12 +1,11 @@ use <../../some.scad>; -function _hashset_add(set, elem, eq, hash) = +function _hashset_add(buckets, b_numbers, elem, eq, hash) = let( - idx = hash(elem) % len(set), - bucket = set[idx] + idx = hash(elem) % b_numbers, + bucket = buckets[idx] ) - some(bucket, function(e) eq(e, elem)) ? set : _add(set, bucket, elem, idx); + some(bucket, function(e) eq(e, elem)) ? buckets : _add(buckets, b_numbers, bucket, elem, idx); -function _add(set, bucket, elem, idx) = - let(leng = len(set)) - [for(i = 0; i < leng; i = i + 1) i == idx ? concat(bucket, [elem]) : set[i]]; \ No newline at end of file +function _add(buckets, b_numbers, bucket, elem, idx) = + [for(i = 0; i < b_numbers; i = i + 1) i == idx ? concat(bucket, [elem]) : buckets[i]]; \ No newline at end of file diff --git a/src/util/set/_impl/_hashset_impl.scad b/src/util/set/_impl/_hashset_impl.scad index 4209e6b1..7e0e5786 100644 --- a/src/util/set/_impl/_hashset_impl.scad +++ b/src/util/set/_impl/_hashset_impl.scad @@ -1,5 +1,6 @@ use <_hashset_add_impl.scad>; -function _hashset(lt, leng, buckets, eq, hash, i = 0) = +function _hashset(lt, leng, buckets, b_numbers, eq, hash, i = 0) = i == leng ? buckets : - _hashset(lt, leng, _hashset_add(buckets, lt[i], eq, hash), eq, hash, i + 1); \ No newline at end of file + let(n_buckets = _hashset_add(buckets, b_numbers, lt[i], eq, hash)) + _hashset(lt, leng, n_buckets, b_numbers, eq, hash, i + 1); \ No newline at end of file diff --git a/src/util/set/hashset.scad b/src/util/set/hashset.scad index 6ce5f9fc..4326fa6a 100644 --- a/src/util/set/hashset.scad +++ b/src/util/set/hashset.scad @@ -21,4 +21,4 @@ function hashset(lt, eq = function(e1, e2) e1 == e2, hash = function(e) _str_has (lt_undef || leng_lt < 256 ? 16 : ceil(sqrt(leng_lt))) : number_of_buckets, buckets = [for(i = [0:b_numbers - 1]) []] ) - lt_undef ? buckets : _hashset(lt, leng_lt, buckets, eq, hash); \ No newline at end of file + lt_undef ? buckets : _hashset(lt, leng_lt, buckets, b_numbers, eq, hash); \ No newline at end of file diff --git a/src/util/set/hashset_add.scad b/src/util/set/hashset_add.scad index d51ce266..6cc4759e 100644 --- a/src/util/set/hashset_add.scad +++ b/src/util/set/hashset_add.scad @@ -12,4 +12,4 @@ use <../../__comm__/_str_hash.scad>; use <_impl/_hashset_add_impl.scad>; function hashset_add(set, elem, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) = - _hashset_add(set, elem, eq, hash); \ No newline at end of file + _hashset_add(set, len(set), elem, eq, hash); \ No newline at end of file