From baf64e47b86cb0d5999a8f73fa765e016161082b Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Fri, 23 Apr 2021 21:56:13 +0800 Subject: [PATCH] use for directly --- src/matrix/_impl/_m_determinant_impl.scad | 8 ++++++-- src/util/_impl/_dedup_impl.scad | 10 +++------- src/util/map/_impl/_hashmap_put_impl.scad | 23 +++++++---------------- src/util/map/hashmap_del.scad | 16 ++++++++-------- src/util/map/hashmap_get.scad | 1 - src/util/set/_impl/_hashset_add_impl.scad | 9 +++------ src/util/set/hashset_del.scad | 1 - 7 files changed, 27 insertions(+), 41 deletions(-) diff --git a/src/matrix/_impl/_m_determinant_impl.scad b/src/matrix/_impl/_m_determinant_impl.scad index 7e99d3a4..24e14f5e 100644 --- a/src/matrix/_impl/_m_determinant_impl.scad +++ b/src/matrix/_impl/_m_determinant_impl.scad @@ -1,11 +1,15 @@ -use <../../util/slice.scad>; use <../../util/sum.scad>; function _m_determinant_sub(matrix, leng, fc) = let( init_sub_m = [for(i = [1:leng - 1]) matrix[i]], sub_m = [for(i = [0:len(init_sub_m) - 1]) - concat(slice(init_sub_m[i], 0, fc), slice(init_sub_m[i], fc + 1)) + let( + mi = init_sub_m[i], + leng_mi = len(mi) + ) + [for(j = 0; j < leng_mi; j = j + 1) if(j != fc) mi[j]] + ], sgn = pow(-1, fc % 2) ) diff --git a/src/util/_impl/_dedup_impl.scad b/src/util/_impl/_dedup_impl.scad index 7bef90da..ecf7a7b5 100644 --- a/src/util/_impl/_dedup_impl.scad +++ b/src/util/_impl/_dedup_impl.scad @@ -1,5 +1,3 @@ - -use <../slice.scad>; use <../some.scad>; function _dedup(elems, leng, buckets, eq, hash, bucket_numbers, i = 0) = @@ -15,8 +13,6 @@ function _dedup_add(buckets, i_elem, eq, hash, bucket_numbers) = ) some(bucket, function(i_e) eq(i_e[1], elem)) ? buckets : _add(buckets, bucket, i_elem, b_idx); -function _add(buckets, bucket, i_elem, b_idx) = concat( - slice(buckets, 0, b_idx), - [concat(bucket, [i_elem])], - slice(buckets, b_idx + 1) -); \ No newline at end of file +function _add(buckets, bucket, i_elem, b_idx) = + let(leng = len(buckets)) + [for(i = 0; i < leng; i = i + 1) i == b_idx ? concat(bucket, [i_elem]) : buckets[i]]; \ 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 f58c3a8b..56c183b0 100644 --- a/src/util/map/_impl/_hashmap_put_impl.scad +++ b/src/util/map/_impl/_hashmap_put_impl.scad @@ -1,4 +1,3 @@ -use <../../slice.scad>; use <../../some.scad>; use <../../find_index.scad>; @@ -13,20 +12,12 @@ function _hashmap_put(map, key, value, eq, hash) = 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) - ) + leng_bucket = len(bucket), + n_bucket = [for(i = 0; i < leng_bucket; i = i + 1) i == k_idx ? [key, value] : bucket[i]], + leng_map = len(map) ) - concat( - slice(map, 0, b_idx), - [n_bucket], - slice(map, b_idx + 1) - ); + [for(i = 0; i < leng_map; i = i + 1) i == b_idx ? n_bucket : map[i]]; -function _put(map, bucket, key, value, b_idx) = concat( - slice(map, 0, b_idx), - [concat(bucket, [[key, value]])], - slice(map, b_idx + 1) -); \ No newline at end of file +function _put(map, bucket, key, value, b_idx) = + let(leng_map = len(map)) + [for(i = 0; i < leng_map; i = i + 1) i == b_idx ? concat(bucket, [[key, value]]) : map[i]]; \ No newline at end of file diff --git a/src/util/map/hashmap_del.scad b/src/util/map/hashmap_del.scad index 1c3be6e5..474e3785 100644 --- a/src/util/map/hashmap_del.scad +++ b/src/util/map/hashmap_del.scad @@ -9,20 +9,20 @@ **/ use <../../__comm__/_str_hash.scad>; -use <../slice.scad>; use <../find_index.scad>; function hashmap_del(map, key, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) = let( bidx = hash(key) % len(map), bucket = map[bidx], - leng = len(bucket) + leng_bucket = len(bucket), + leng_map = len(map) ) - leng == 0 ? map : + leng_bucket == 0 ? map : let(i = find_index(bucket, function(e) eq(e[0], key))) i == -1 ? map : - concat( - slice(map, 0, bidx), - [concat(slice(bucket, 0, i), slice(bucket, i + 1))], - slice(map, bidx + 1) - ); \ No newline at end of file + [ + 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]] : + map[j] + ]; \ No newline at end of file diff --git a/src/util/map/hashmap_get.scad b/src/util/map/hashmap_get.scad index dbe90181..39b5d877 100644 --- a/src/util/map/hashmap_get.scad +++ b/src/util/map/hashmap_get.scad @@ -9,7 +9,6 @@ **/ use <../../__comm__/_str_hash.scad>; -use <../slice.scad>; use <../find_index.scad>; function hashmap_get(map, key, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) = diff --git a/src/util/set/_impl/_hashset_add_impl.scad b/src/util/set/_impl/_hashset_add_impl.scad index 5803f5ae..76aa2993 100644 --- a/src/util/set/_impl/_hashset_add_impl.scad +++ b/src/util/set/_impl/_hashset_add_impl.scad @@ -1,4 +1,3 @@ -use <../../slice.scad>; use <../../some.scad>; function _hashset_add(set, elem, eq, hash) = @@ -8,8 +7,6 @@ function _hashset_add(set, elem, eq, hash) = ) some(bucket, function(e) eq(e, elem)) ? set : _add(set, bucket, elem, idx); -function _add(set, bucket, elem, idx) = concat( - slice(set, 0, idx), - [concat(bucket, [elem])], - slice(set, idx + 1) -); +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 diff --git a/src/util/set/hashset_del.scad b/src/util/set/hashset_del.scad index 08425953..85931f70 100644 --- a/src/util/set/hashset_del.scad +++ b/src/util/set/hashset_del.scad @@ -9,7 +9,6 @@ **/ use <../../__comm__/_str_hash.scad>; -use <../slice.scad>; use <../find_index.scad>; function hashset_del(set, elem, eq = function(e1, e2) e1 == e2, hash = function(e) _str_hash(e)) =