1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-11 01:04:07 +02:00

refactor: sort and get elem

This commit is contained in:
Justin Lin
2022-04-11 17:13:27 +08:00
parent ad760d1f4a
commit 8a8e55d54f
2 changed files with 15 additions and 6 deletions

View File

@@ -32,3 +32,14 @@ _dedup_add_search = function(buckets, i_elem, eq, hash, bucket_numbers)
function _add(buckets, bucket, i_elem, b_idx) =
let(leng = len(buckets))
[for(i = 0; i < leng; i = i + 1) i == b_idx ? [each bucket, i_elem] : buckets[i]];
function _sort(lt) =
let(leng = len(lt))
leng == 0 ? [] :
leng == 1 ? [lt[0][1]] :
let(
pivot = lt[0],
before = [for(j = 1; j < leng; j = j + 1) if(lt[j][0] < pivot[0]) lt[j]],
after = [for(j = 1; j < leng; j = j + 1) if(lt[j][0] >= pivot[0]) lt[j]]
)
[each _sort(before), pivot[1], each _sort(after)];

View File

@@ -10,7 +10,6 @@
use <../__comm__/_str_hash.scad>;
use <_impl/_dedup_impl.scad>;
use <sort.scad>;
function dedup(lt, eq = undef, hash = function(e) _str_hash(e), number_of_buckets) =
let(leng_lt = len(lt))
@@ -18,7 +17,6 @@ function dedup(lt, eq = undef, hash = function(e) _str_hash(e), number_of_bucket
let(
b_numbers = is_undef(number_of_buckets) ? ceil(sqrt(leng_lt)) : number_of_buckets,
buckets = [for(i = [0:b_numbers - 1]) []],
deduped = _dedup(lt, leng_lt, buckets, eq, hash, b_numbers),
sorted = sort([for(bucket = deduped) each bucket], by = function(e1, e2) e1[0] - e2[0])
deduped = _dedup(lt, leng_lt, buckets, eq, hash, b_numbers)
)
[for(i_elem = sorted) i_elem[1]];
_sort([for(bucket = deduped) each bucket]);