From 61a1d3d2b15790531d422cafb8f3d07411f1744b Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Wed, 20 Apr 2022 07:51:19 +0800 Subject: [PATCH] refactor: include 1st class hash function --- src/__comm__/_pt2_hash.scad | 2 +- src/__comm__/_pt3_hash.scad | 2 +- src/__comm__/_str_hash.scad | 2 +- src/maze/mz_hamiltonian.scad | 5 +++-- src/util/dedup.scad | 5 +++-- src/util/map/hashmap.scad | 4 ++-- src/util/map/hashmap_del.scad | 4 ++-- src/util/map/hashmap_get.scad | 4 ++-- src/util/map/hashmap_put.scad | 5 +++-- src/util/set/hashset.scad | 5 +++-- src/util/set/hashset_add.scad | 5 +++-- src/util/set/hashset_del.scad | 5 +++-- src/util/set/hashset_has.scad | 5 +++-- src/voronoi/_impl/_convex_intersection.scad | 5 +++-- src/voxel/_impl/_vx_contour_impl.scad | 13 +++++++------ src/voxel/vx_bezier.scad | 7 ++++--- src/voxel/vx_circle.scad | 5 +++-- src/voxel/vx_curve.scad | 7 ++++--- src/voxel/vx_difference.scad | 10 ++++------ src/voxel/vx_intersection.scad | 8 ++++---- src/voxel/vx_polygon.scad | 5 +++-- src/voxel/vx_polyline.scad | 7 ++++--- src/voxel/vx_union.scad | 5 +++-- 23 files changed, 69 insertions(+), 56 deletions(-) diff --git a/src/__comm__/_pt2_hash.scad b/src/__comm__/_pt2_hash.scad index 4f905c45..d1753f01 100644 --- a/src/__comm__/_pt2_hash.scad +++ b/src/__comm__/_pt2_hash.scad @@ -1,4 +1,4 @@ -function _pt2_hash(p) = +_pt2_hash = function(p) let( x = p.x >= 0 ? 2 * p.x : -2 * p.x - 1, y = p.y >= 0 ? 2 * p.y : -2 * p.y - 1 diff --git a/src/__comm__/_pt3_hash.scad b/src/__comm__/_pt3_hash.scad index f2b0b12a..f0769f91 100644 --- a/src/__comm__/_pt3_hash.scad +++ b/src/__comm__/_pt3_hash.scad @@ -1,4 +1,4 @@ -function _pt3_hash(p) = +_pt3_hash = function(p) let( x = p.x >= 0 ? 2 * p.x : -2 * p.x - 1, y = p.y >= 0 ? 2 * p.y : -2 * p.y - 1, diff --git a/src/__comm__/_str_hash.scad b/src/__comm__/_str_hash.scad index c6c93fd0..c5b61d3f 100644 --- a/src/__comm__/_str_hash.scad +++ b/src/__comm__/_str_hash.scad @@ -1,4 +1,4 @@ -function _str_hash(value) = +_str_hash = function(value) let( chars = str(value), end = len(chars) - 1 diff --git a/src/maze/mz_hamiltonian.scad b/src/maze/mz_hamiltonian.scad index 5d4b12c9..f62cde4d 100644 --- a/src/maze/mz_hamiltonian.scad +++ b/src/maze/mz_hamiltonian.scad @@ -8,7 +8,6 @@ * **/ -use <../__comm__/_pt2_hash.scad>; use <_impl/_mz_hamiltonian_impl.scad>; use ; use ; @@ -16,6 +15,8 @@ use ; use <../util/set/hashset.scad>; use <../util/set/hashset_elems.scad>; +include <../__comm__/_pt2_hash.scad>; + function mz_hamiltonian(rows, columns, start = [0, 0], init_cells, seed) = let( init_cells_undef = is_undef(init_cells), @@ -42,7 +43,7 @@ function mz_hamiltonian(rows, columns, start = [0, 0], init_cells, seed) = dot_pts = hashset_elems( hashset( all, - hash = function(p) _pt2_hash(p) + hash = _pt2_hash ) ), falseRow = [for(c = [0:c * 2]) false], diff --git a/src/util/dedup.scad b/src/util/dedup.scad index d11ecfa7..04192140 100644 --- a/src/util/dedup.scad +++ b/src/util/dedup.scad @@ -8,10 +8,11 @@ * **/ -use <../__comm__/_str_hash.scad>; use <_impl/_dedup_impl.scad>; + +include <../__comm__/_str_hash.scad>; -function dedup(lt, eq = undef, hash = function(e) _str_hash(e), number_of_buckets) = +function dedup(lt, eq = undef, hash = _str_hash, number_of_buckets) = let(leng_lt = len(lt)) leng_lt < 2 ? lt : let( diff --git a/src/util/map/hashmap.scad b/src/util/map/hashmap.scad index 36203f7f..7bb4bfc5 100644 --- a/src/util/map/hashmap.scad +++ b/src/util/map/hashmap.scad @@ -8,10 +8,10 @@ * **/ -use <../../__comm__/_str_hash.scad>; use <_impl/_hashmap_impl.scad>; +include <../../__comm__/_str_hash.scad>; -function hashmap(kv_lt, eq = undef, hash = function(e) _str_hash(e), number_of_buckets) = +function hashmap(kv_lt, eq = undef, hash = _str_hash, 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 3153acd4..42f84df3 100644 --- a/src/util/map/hashmap_del.scad +++ b/src/util/map/hashmap_del.scad @@ -8,10 +8,10 @@ * **/ -use <../../__comm__/_str_hash.scad>; use <../_impl/_find_eq.scad>; +include <../../__comm__/_str_hash.scad>; -function hashmap_del(map, key, eq = undef, hash = function(e) _str_hash(e)) = +function hashmap_del(map, key, eq = undef, hash = _str_hash) = let( bidx = hash(key) % len(map), bucket = map[bidx], diff --git a/src/util/map/hashmap_get.scad b/src/util/map/hashmap_get.scad index b3b8f351..958ef48e 100644 --- a/src/util/map/hashmap_get.scad +++ b/src/util/map/hashmap_get.scad @@ -8,10 +8,10 @@ * **/ -use <../../__comm__/_str_hash.scad>; use <../_impl/_find_eq.scad>; +include <../../__comm__/_str_hash.scad>; -function hashmap_get(map, key, eq = undef, hash = function(e) _str_hash(e)) = +function hashmap_get(map, key, eq = undef, hash = _str_hash) = let( bidx = hash(key) % len(map), bucket = map[bidx] diff --git a/src/util/map/hashmap_put.scad b/src/util/map/hashmap_put.scad index a2fa1d1c..0de1825f 100644 --- a/src/util/map/hashmap_put.scad +++ b/src/util/map/hashmap_put.scad @@ -8,8 +8,9 @@ * **/ -use <../../__comm__/_str_hash.scad>; use <_impl/_hashmap_put_impl.scad>; -function hashmap_put(map, key, value, eq = undef, hash = function(e) _str_hash(e)) = +include <../../__comm__/_str_hash.scad>; + +function hashmap_put(map, key, value, eq = undef, hash = _str_hash) = _hashmap_put(map, len(map), key, value, eq, hash); \ No newline at end of file diff --git a/src/util/set/hashset.scad b/src/util/set/hashset.scad index 8bd1a687..4c10a5ee 100644 --- a/src/util/set/hashset.scad +++ b/src/util/set/hashset.scad @@ -8,11 +8,12 @@ * **/ -use <../../__comm__/_str_hash.scad>; use <_impl/_hashset_impl.scad>; use <_impl/_hashset_add_impl.scad>; + +include <../../__comm__/_str_hash.scad>; -function hashset(lt, eq = undef, hash = function(e) _str_hash(e), number_of_buckets) = +function hashset(lt, eq = undef, hash = _str_hash, 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 ee89b71b..f8288c53 100644 --- a/src/util/set/hashset_add.scad +++ b/src/util/set/hashset_add.scad @@ -8,8 +8,9 @@ * **/ -use <../../__comm__/_str_hash.scad>; use <_impl/_hashset_add_impl.scad>; -function hashset_add(set, elem, eq = undef, hash = function(e) _str_hash(e)) = +include <../../__comm__/_str_hash.scad>; + +function hashset_add(set, elem, eq = undef, hash = _str_hash) = _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 f4bb03da..9aff941d 100644 --- a/src/util/set/hashset_del.scad +++ b/src/util/set/hashset_del.scad @@ -8,10 +8,11 @@ * **/ -use <../../__comm__/_str_hash.scad>; use <../_impl/_find_eq.scad>; -function hashset_del(set, elem, eq = undef, hash = function(e) _str_hash(e)) = +include <../../__comm__/_str_hash.scad>; + +function hashset_del(set, elem, eq = undef, hash = _str_hash) = let( leng_set = len(set), bidx = hash(elem) % leng_set, diff --git a/src/util/set/hashset_has.scad b/src/util/set/hashset_has.scad index 84ddaefd..7728c57b 100644 --- a/src/util/set/hashset_has.scad +++ b/src/util/set/hashset_has.scad @@ -8,9 +8,10 @@ * **/ -use <../../__comm__/_str_hash.scad>; use <../_impl/_find_eq.scad>; -function hashset_has(set, elem, eq = undef, hash = function(e) _str_hash(e)) = +include <../../__comm__/_str_hash.scad>; + +function hashset_has(set, elem, eq = undef, hash = _str_hash) = let(bucket = set[hash(elem) % len(set)]) bucket != [] && _find_eq(bucket, elem, eq) != -1; \ No newline at end of file diff --git a/src/voronoi/_impl/_convex_intersection.scad b/src/voronoi/_impl/_convex_intersection.scad index aa27a962..98a84a8d 100644 --- a/src/voronoi/_impl/_convex_intersection.scad +++ b/src/voronoi/_impl/_convex_intersection.scad @@ -1,9 +1,10 @@ -use <../../__comm__/_pt2_hash.scad>; use <../../lines_intersection.scad>; use <_convex_ct_clk_order.scad>; use <../../util/set/hashset.scad>; use <../../util/set/hashset_elems.scad>; +include <../../__comm__/_pt2_hash.scad>; + function _in_convex_r(i, j, preC, convex_pts, pt, leng, convex_pts, pt) = j == leng || ( let(c = cross(convex_pts[i] - pt, convex_pts[j] - pt)) @@ -28,7 +29,7 @@ function _intersection_ps(closed_shape, line_pts, epsilon) = ) leng < 2 ? npts : leng == 2 ? (npts[0] != npts[1] ? npts : [npts[0]]) : - hashset_elems(hashset(npts, hash = function(p) _pt2_hash(p))); + hashset_elems(hashset(npts, hash = _pt2_hash)); function _convex_intersection(shape1, shape2, epsilon = 0.0001) = (shape1 == [] || shape2 == []) ? [] : diff --git a/src/voxel/_impl/_vx_contour_impl.scad b/src/voxel/_impl/_vx_contour_impl.scad index b36ec0c4..408c9ad1 100644 --- a/src/voxel/_impl/_vx_contour_impl.scad +++ b/src/voxel/_impl/_vx_contour_impl.scad @@ -1,15 +1,16 @@ use <../../util/set/hashset.scad>; use <../../util/set/hashset_has.scad>; -use <../../__comm__/_pt2_hash.scad>; + +include <../../__comm__/_pt2_hash.scad>; hash = function(p) _pt2_hash(p); function _vx_contour_corner_value(pts, x, y) = let( - c1 = hashset_has(pts, [x, y - 1], hash = hash) || hashset_has(pts, [x - 1, y - 1], hash = hash) ? 1 : 0, - c2 = hashset_has(pts, [x - 1, y], hash = hash) || hashset_has(pts, [x - 1, y + 1], hash = hash) ? 2 : 0, - c3 = hashset_has(pts, [x, y + 1], hash = hash) || hashset_has(pts, [x + 1, y + 1], hash = hash) ? 4 : 0, - c4 = hashset_has(pts, [x + 1, y], hash = hash) || hashset_has(pts, [x + 1, y - 1], hash = hash) ? 8 : 0 + c1 = hashset_has(pts, [x, y - 1], hash = _pt2_hash) || hashset_has(pts, [x - 1, y - 1], hash = _pt2_hash) ? 1 : 0, + c2 = hashset_has(pts, [x - 1, y], hash = _pt2_hash) || hashset_has(pts, [x - 1, y + 1], hash = _pt2_hash) ? 2 : 0, + c3 = hashset_has(pts, [x, y + 1], hash = _pt2_hash) || hashset_has(pts, [x + 1, y + 1], hash = _pt2_hash) ? 4 : 0, + c4 = hashset_has(pts, [x + 1, y], hash = _pt2_hash) || hashset_has(pts, [x + 1, y - 1], hash = _pt2_hash) ? 8 : 0 ) c1 + c2 + c3 + c4; @@ -27,7 +28,7 @@ function _vx_contour(points) = // always start from the left-bottom pt fst = min(points) + [-1, -1] ) - _vx_contour_travel(hashset(points, hash = function(p) _pt2_hash(p)), fst, fst); + _vx_contour_travel(hashset(points, hash = _pt2_hash), fst, fst); _vx_contour_nxt_offset = [ [1, 0], // RIGHT diff --git a/src/voxel/vx_bezier.scad b/src/voxel/vx_bezier.scad index 9a379443..8663dc96 100644 --- a/src/voxel/vx_bezier.scad +++ b/src/voxel/vx_bezier.scad @@ -8,18 +8,19 @@ * **/ -use <../__comm__/_pt2_hash.scad>; -use <../__comm__/_pt3_hash.scad>; use <../__comm__/__to2d.scad>; use <../__comm__/__to3d.scad>; use <_impl/_vx_bezier_impl.scad>; use <../util/dedup.scad>; +include <../__comm__/_pt2_hash.scad>; +include <../__comm__/_pt3_hash.scad>; + function vx_bezier(p1, p2, p3, p4) = let( is2d = len(p1) == 2, pts = is2d ? _vx_bezier2(__to3d(p1), __to3d(p2), __to3d(p3), __to3d(p4), []) : _vx_bezier3(p1, p2, p3, p4, []), - deduped = dedup(pts, hash = is2d ? function(p) _pt2_hash(p) : function(p) _pt3_hash(p)) + deduped = dedup(pts, hash = is2d ? _pt2_hash : _pt3_hash) ) is2d ? [for(p = deduped) __to2d(p)] : deduped; \ No newline at end of file diff --git a/src/voxel/vx_circle.scad b/src/voxel/vx_circle.scad index f8cd8dac..e1998ca8 100644 --- a/src/voxel/vx_circle.scad +++ b/src/voxel/vx_circle.scad @@ -8,12 +8,13 @@ * **/ -use <../__comm__/_pt2_hash.scad>; use <_impl/_vx_circle_impl.scad>; use <../util/set/hashset.scad>; use <../util/set/hashset_elems.scad>; +include <../__comm__/_pt2_hash.scad>; + function vx_circle(radius, filled = false) = hashset_elems( - hashset(_vx_circle_impl(radius, filled), hash = function(p) _pt2_hash(p)) + hashset(_vx_circle_impl(radius, filled), hash = _pt2_hash) ); \ No newline at end of file diff --git a/src/voxel/vx_curve.scad b/src/voxel/vx_curve.scad index 487cd90b..0f3d9260 100644 --- a/src/voxel/vx_curve.scad +++ b/src/voxel/vx_curve.scad @@ -8,11 +8,12 @@ * **/ -use <../__comm__/_pt2_hash.scad>; -use <../__comm__/_pt3_hash.scad>; use <_impl/_vx_curve_impl.scad>; use <../util/dedup.scad>; +include <../__comm__/_pt2_hash.scad>; +include <../__comm__/_pt3_hash.scad>; + function vx_curve(points, tightness = 0) = let(leng = len(points)) dedup([ @@ -24,4 +25,4 @@ function vx_curve(points, tightness = 0) = for(i = [0:len(pts) - 2]) pts[i] ], points[leng - 2] - ], hash = len(points[0]) == 2 ? function(p) _pt2_hash(p) : function(p) _pt3_hash(p)); \ No newline at end of file + ], hash = len(points[0]) == 2 ? _pt2_hash : _pt3_hash); \ No newline at end of file diff --git a/src/voxel/vx_difference.scad b/src/voxel/vx_difference.scad index 7262bd89..d9f876d6 100644 --- a/src/voxel/vx_difference.scad +++ b/src/voxel/vx_difference.scad @@ -10,11 +10,9 @@ use <../util/set/hashset.scad>; use <../util/set/hashset_has.scad>; -use <../__comm__/_pt3_hash.scad>; + +include <../__comm__/_pt3_hash.scad>; function vx_difference(points1, points2) = - let( - hash = function(p) _pt3_hash(p), - set = hashset(points2, hash = hash) - ) - [for(p = points1) if(!hashset_has(set, p, hash = hash)) p]; \ No newline at end of file + let(set = hashset(points2, hash = _pt3_hash)) + [for(p = points1) if(!hashset_has(set, p, hash = _pt3_hash)) p]; \ No newline at end of file diff --git a/src/voxel/vx_intersection.scad b/src/voxel/vx_intersection.scad index 3960bf06..17ffe7e3 100644 --- a/src/voxel/vx_intersection.scad +++ b/src/voxel/vx_intersection.scad @@ -10,14 +10,14 @@ use <../util/set/hashset.scad>; use <../util/set/hashset_has.scad>; -use <../__comm__/_pt3_hash.scad>; + +include <../__comm__/_pt3_hash.scad>; function vx_intersection(points1, points2) = let( leng1 = len(points1), leng2 = len(points2), pts_pair = leng1 > leng2 ? [points1, points2] : [points2, points1], - hash = function(p) _pt3_hash(p), - set = hashset(pts_pair[1], hash = hash) + set = hashset(pts_pair[1], hash = _pt3_hash) ) - [for(p = pts_pair[0]) if(hashset_has(set, p, hash = hash)) p]; \ No newline at end of file + [for(p = pts_pair[0]) if(hashset_has(set, p, hash = _pt3_hash)) p]; \ No newline at end of file diff --git a/src/voxel/vx_polygon.scad b/src/voxel/vx_polygon.scad index 631ece04..0d33801e 100644 --- a/src/voxel/vx_polygon.scad +++ b/src/voxel/vx_polygon.scad @@ -1,10 +1,11 @@ -use <../__comm__/_pt2_hash.scad>; use <../in_shape.scad>; use <../util/sorted.scad>; use ; use <../util/set/hashset.scad>; use <../util/set/hashset_elems.scad>; +include <../__comm__/_pt2_hash.scad>; + function vx_polygon(points, filled = false) = let(contour = vx_polyline([each points, points[0]])) !filled ? contour : @@ -29,5 +30,5 @@ function vx_polygon(points, filled = false) = ) ) hashset_elems( - hashset(all, hash = function(p) _pt2_hash(p)) + hashset(all, hash = _pt2_hash) ); \ No newline at end of file diff --git a/src/voxel/vx_polyline.scad b/src/voxel/vx_polyline.scad index 69ed5677..e304ac8c 100644 --- a/src/voxel/vx_polyline.scad +++ b/src/voxel/vx_polyline.scad @@ -11,11 +11,12 @@ use <../__comm__/__to3d.scad>; use <../__comm__/__to2d.scad>; use <../__comm__/__lines_from.scad>; -use <../__comm__/_pt2_hash.scad>; -use <../__comm__/_pt3_hash.scad>; use <../util/dedup.scad>; use ; +include <../__comm__/_pt2_hash.scad>; +include <../__comm__/_pt3_hash.scad>; + function vx_polyline(points) = let( is_2d = len(points[0]) == 2, @@ -24,5 +25,5 @@ function vx_polyline(points) = ) dedup( is_2d ? [for(pt = polyline) __to2d(pt)] : polyline, - hash = is_2d ? function(p) _pt2_hash(p) : function(p) _pt3_hash(p) + hash = is_2d ? _pt2_hash : _pt3_hash ); \ No newline at end of file diff --git a/src/voxel/vx_union.scad b/src/voxel/vx_union.scad index ca9e5031..aa8d9d67 100644 --- a/src/voxel/vx_union.scad +++ b/src/voxel/vx_union.scad @@ -8,11 +8,12 @@ * **/ -use <../__comm__/_pt3_hash.scad>; use <../util/set/hashset.scad>; use <../util/set/hashset_elems.scad>; +include <../__comm__/_pt3_hash.scad>; + function vx_union(points1, points2) = hashset_elems( - hashset(concat(points1, points2), hash = function(p) _pt3_hash(p)) + hashset(concat(points1, points2), hash = _pt3_hash) ); \ No newline at end of file