mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-15 11:14:17 +02:00
refactor: use search if eq undef
This commit is contained in:
12
src/util/_impl/_find_eq.scad
Normal file
12
src/util/_impl/_find_eq.scad
Normal file
@@ -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
|
||||
);
|
@@ -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) =
|
||||
|
@@ -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),
|
||||
|
@@ -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]] :
|
||||
|
@@ -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];
|
||||
let(i = _find_eq(bucket, key, eq))
|
||||
i == -1 ? undef : bucket[i][1];
|
@@ -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);
|
@@ -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]];
|
@@ -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),
|
||||
|
@@ -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);
|
@@ -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]] :
|
||||
|
@@ -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));
|
||||
function hashset_has(set, elem, eq = undef, hash = function(e) _str_hash(e)) =
|
||||
_find_eq(set[hash(elem) % len(set)], elem, eq) != -1;
|
Reference in New Issue
Block a user