diff --git a/src/util/_impl/_find_eq.scad b/src/util/_impl/_find_eq.scad new file mode 100644 index 00000000..35b1ec48 --- /dev/null +++ b/src/util/_impl/_find_eq.scad @@ -0,0 +1,12 @@ +function _find_eq(lt, target, eq) = + is_undef(eq) ? ( + let(found = search([target], lt)[0]) + found == [] ? - 1 : found + ) : ( + let( + leng = len(lt), + indices = [for(i = 0; i < leng && !eq(lt[i], target); i = i + 1) undef], + leng_indices = len(indices) + ) + leng_indices == leng ? -1 : leng_indices + ); \ No newline at end of file diff --git a/src/util/map/_impl/_hashmap_put_impl.scad b/src/util/map/_impl/_hashmap_put_impl.scad index 6b897fd1..61657c76 100644 --- a/src/util/map/_impl/_hashmap_put_impl.scad +++ b/src/util/map/_impl/_hashmap_put_impl.scad @@ -1,12 +1,12 @@ -use <../../some.scad>; +use <../../_impl/_find_eq.scad>; function _hashmap_put(buckets, b_numbers, key, value, eq, hash) = let( b_idx = hash(key) % b_numbers, bucket = buckets[b_idx], - k_idx = search([key], bucket)[0] + k_idx = _find_eq(bucket, key, eq) ) - k_idx != [] ? _replace(buckets, b_numbers, bucket, key, value, b_idx, k_idx) : + k_idx != -1 ? _replace(buckets, b_numbers, bucket, key, value, b_idx, k_idx) : _put(buckets, b_numbers, bucket, key, value, b_idx); function _replace(buckets, b_numbers, bucket, key, value, b_idx, k_idx) = diff --git a/src/util/map/hashmap.scad b/src/util/map/hashmap.scad index ee7f4a94..4bcb8eaf 100644 --- a/src/util/map/hashmap.scad +++ b/src/util/map/hashmap.scad @@ -11,7 +11,7 @@ use <../../__comm__/_str_hash.scad>; use <_impl/_hashmap_impl.scad>; -function hashmap(kv_lt, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e), number_of_buckets) = +function hashmap(kv_lt, eq = undef, hash = function(e) _str_hash(e), number_of_buckets) = let( kv_lt_undef = is_undef(kv_lt), leng_kv_lt = kv_lt_undef ? -1 : len(kv_lt), diff --git a/src/util/map/hashmap_del.scad b/src/util/map/hashmap_del.scad index 045eb4e8..3153acd4 100644 --- a/src/util/map/hashmap_del.scad +++ b/src/util/map/hashmap_del.scad @@ -9,8 +9,9 @@ **/ use <../../__comm__/_str_hash.scad>; +use <../_impl/_find_eq.scad>; -function hashmap_del(map, key, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) = +function hashmap_del(map, key, eq = undef, hash = function(e) _str_hash(e)) = let( bidx = hash(key) % len(map), bucket = map[bidx], @@ -18,8 +19,8 @@ function hashmap_del(map, key, eq = function(e1, e2) e1 == e2, hash = function(e leng_map = len(map) ) leng_bucket == 0 ? map : - let(i = search([key], bucket)[0]) - i == [] ? map : + let(i = _find_eq(bucket, key, eq)) + i == -1 ? map : [ for(j = 0; j < leng_map; j = j + 1) j == bidx ? [for(k = 0; k < leng_bucket; k = k + 1) if(k != i) bucket[k]] : diff --git a/src/util/map/hashmap_get.scad b/src/util/map/hashmap_get.scad index 391a0e68..3b6d4549 100644 --- a/src/util/map/hashmap_get.scad +++ b/src/util/map/hashmap_get.scad @@ -9,11 +9,12 @@ **/ use <../../__comm__/_str_hash.scad>; +use <../_impl/_find_eq.scad>; -function hashmap_get(map, key, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) = +function hashmap_get(map, key, eq = undef, hash = function(e) _str_hash(e)) = let( bidx = hash(key) % len(map), bucket = map[bidx] ) - let(i = search([key], bucket)[0]) - i == [] ? undef : bucket[i][1]; \ No newline at end of file + let(i = _find_eq(bucket, key, eq)) + i == -1 ? undef : bucket[i][1]; \ No newline at end of file diff --git a/src/util/map/hashmap_put.scad b/src/util/map/hashmap_put.scad index f3958020..a2fa1d1c 100644 --- a/src/util/map/hashmap_put.scad +++ b/src/util/map/hashmap_put.scad @@ -11,5 +11,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)) = +function hashmap_put(map, key, value, eq = undef, hash = function(e) _str_hash(e)) = _hashmap_put(map, len(map), key, value, eq, hash); \ No newline at end of file diff --git a/src/util/set/_impl/_hashset_add_impl.scad b/src/util/set/_impl/_hashset_add_impl.scad index 7c53835b..25e1ca28 100644 --- a/src/util/set/_impl/_hashset_add_impl.scad +++ b/src/util/set/_impl/_hashset_add_impl.scad @@ -1,11 +1,11 @@ -use <../../some.scad>; +use <../../_impl/_find_eq.scad>; function _hashset_add(buckets, b_numbers, elem, eq, hash) = let( idx = hash(elem) % b_numbers, bucket = buckets[idx] ) - some(bucket, function(e) eq(e, elem)) ? buckets : _add(buckets, b_numbers, bucket, elem, idx); + _find_eq(bucket, elem, eq) != -1 ? buckets : _add(buckets, b_numbers, bucket, elem, idx); function _add(buckets, b_numbers, bucket, elem, idx) = [for(i = 0; i < b_numbers; i = i + 1) i == idx ? [each bucket, elem] : buckets[i]]; \ No newline at end of file diff --git a/src/util/set/hashset.scad b/src/util/set/hashset.scad index 4326fa6a..6a1e3f64 100644 --- a/src/util/set/hashset.scad +++ b/src/util/set/hashset.scad @@ -12,7 +12,7 @@ use <../../__comm__/_str_hash.scad>; use <_impl/_hashset_impl.scad>; use <_impl/_hashset_add_impl.scad>; -function hashset(lt, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e), number_of_buckets) = +function hashset(lt, eq = undef, hash = function(e) _str_hash(e), number_of_buckets) = let( lt_undef = is_undef(lt), leng_lt = lt_undef ? -1 : len(lt), diff --git a/src/util/set/hashset_add.scad b/src/util/set/hashset_add.scad index 6cc4759e..ee89b71b 100644 --- a/src/util/set/hashset_add.scad +++ b/src/util/set/hashset_add.scad @@ -11,5 +11,5 @@ 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)) = +function hashset_add(set, elem, eq = undef, hash = function(e) _str_hash(e)) = _hashset_add(set, len(set), elem, eq, hash); \ No newline at end of file diff --git a/src/util/set/hashset_del.scad b/src/util/set/hashset_del.scad index e4258219..f4bb03da 100644 --- a/src/util/set/hashset_del.scad +++ b/src/util/set/hashset_del.scad @@ -9,8 +9,9 @@ **/ use <../../__comm__/_str_hash.scad>; +use <../_impl/_find_eq.scad>; -function hashset_del(set, elem, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) = +function hashset_del(set, elem, eq = undef, hash = function(e) _str_hash(e)) = let( leng_set = len(set), bidx = hash(elem) % leng_set, @@ -18,8 +19,8 @@ function hashset_del(set, elem, eq = function(e1, e2) e1 == e2, hash = function( leng_bucket = len(bucket) ) leng_bucket == 0 ? set : - let(i = search([elem], bucket)[0]) - i == [] ? set : + let(i = _find_eq(bucket, elem, eq)) + i == -1 ? set : [ for(j = 0; j < leng_set; j = j + 1) j == bidx ? [for(k = 0; k < leng_bucket; k = k + 1) if(k != i) bucket[k]] : diff --git a/src/util/set/hashset_has.scad b/src/util/set/hashset_has.scad index c2c2b9e0..862dde1c 100644 --- a/src/util/set/hashset_has.scad +++ b/src/util/set/hashset_has.scad @@ -9,7 +9,7 @@ **/ use <../../__comm__/_str_hash.scad>; -use <../some.scad>; +use <../_impl/_find_eq.scad>; -function hashset_has(set, elem, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) = - some(set[hash(elem) % len(set)], function(e) eq(e, elem)); \ No newline at end of file +function hashset_has(set, elem, eq = undef, hash = function(e) _str_hash(e)) = + _find_eq(set[hash(elem) % len(set)], elem, eq) != -1; \ No newline at end of file