From b7e3644df324384c1ecae154bbd1004862547457 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Mon, 8 Apr 2019 20:51:29 -0700 Subject: [PATCH] Fixed compare_vals for lists with single lists. --- math.scad | 27 ++++++++++++--------------- tests/test_math.scad | 9 ++++++++- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/math.scad b/math.scad index f3b2dc0..0860299 100644 --- a/math.scad +++ b/math.scad @@ -265,10 +265,6 @@ function compare_vals(a, b) = (a==undef)? -1 : (b==undef)? 1 : ((a==[] || a=="" || a[0]!=undef) && (b==[] || b=="" || b[0]!=undef))? ( - (len(a)==1 && len(b)==1)? ( - (a[0]b[0])? 1 : 0 - ) : compare_lists(a, b) ) : (ab)? 1 : 0; @@ -292,7 +288,9 @@ function compare_lists(a, b, n=0) = cmp = (a==b)? 0 : (len(a)<=n)? -1 : (len(b)<=n)? 1 : - compare_vals(a[n], b[n]) + (a==a[n] || b==b[n])? ( + ab? 1 : 0 + ) : compare_vals(a[n], b[n]) ) (cmp != 0 || a==b)? cmp : compare_lists(a, b, n+1); @@ -702,8 +700,9 @@ function sort(arr, idx=undef) = pivotval = idx==undef? pivot : [for (i=idx) pivot[i]], compare = [ for (entry = arr) let( - val = idx==undef? entry : [for (i=idx) entry[i]] - ) compare_vals(val, pivotval) + val = idx==undef? entry : [for (i=idx) entry[i]], + cmp = compare_vals(val, pivotval) + ) cmp ], lesser = [ for (i = [0:len(arr)-1]) if (compare[i] < 0) arr[i] ], equal = [ for (i = [0:len(arr)-1]) if (compare[i] ==0) arr[i] ], @@ -734,14 +733,12 @@ function sort(arr, idx=undef) = // idxs2 = sortidx(lst, idx=0); // Returns: [1,2,0,3] // idxs3 = sortidx(lst, idx=[1,3]); // Returns: [3,0,2,1] function sortidx(l, idx=undef) = - let(ll=enumerate(l,idx=idx)) - array_subindex( - sort(ll, idx=( - idx==undef? 1 : - (ll==[])? 1 : - [1:len(ll[0])-1] - )), - 0); + (l==[])? [] : + let( + ll=enumerate(l,idx=idx), + sidx = [1:len(ll[0])-1] + ) + array_subindex(sort(ll, idx=sidx), 0); // Function: unique() diff --git a/tests/test_math.scad b/tests/test_math.scad index c151024..99d39d2 100644 --- a/tests/test_math.scad +++ b/tests/test_math.scad @@ -225,6 +225,7 @@ module test_compare_vals() { assert(compare_vals([2,3,4,5], [2,3,4,5]) == 0); assert(compare_vals([2,3,4,5], [2,3,4]) == 1); assert(compare_vals([2,3,4,5], [2,3,5,5]) == -1); + assert(compare_vals([[2,3,4,5]], [[2,3,5,5]]) == -1); assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0); assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) == -1); @@ -250,6 +251,9 @@ module test_compare_lists() { assert(compare_lists([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) == 1); assert(compare_lists([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) == 1); assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) == -1); + + assert(compare_lists("cat", "bat") == 1); + assert(compare_lists(["cat"], ["bat"]) == 1); } test_compare_lists(); @@ -447,7 +451,8 @@ test_flatten(); module test_sort() { assert(sort([7,3,9,4,3,1,8]) == [1,3,3,4,7,8,9]); - assert(sort(["cat", "oat", "sat", "bat", "vat", "rat", "pat", "mat", "fat", "hat", "eat"])== ["bat", "cat", "eat", "fat", "hat", "mat", "oat", "pat", "rat", "sat", "vat"]); + assert(sort(["cat", "oat", "sat", "bat", "vat", "rat", "pat", "mat", "fat", "hat", "eat"]) == ["bat", "cat", "eat", "fat", "hat", "mat", "oat", "pat", "rat", "sat", "vat"]); + assert(sort(enumerate([[2,3,4],[1,2,3],[2,4,3]]),idx=1)==[[1,[1,2,3]], [0,[2,3,4]], [2,[2,4,3]]]); } test_sort(); @@ -464,6 +469,8 @@ module test_sortidx() { assert(sortidx(lst2, idx=1) == [3,0,2,1]); assert(sortidx(lst2, idx=0) == [1,2,0,3]); assert(sortidx(lst2, idx=[1,3]) == [3,0,2,1]); + lst3 = [[-4, 0, 0], [0, 0, -4], [0, -4, 0], [-4, 0, 0], [0, -4, 0], [0, 0, 4], [0, 0, -4], [0, 4, 0], [4, 0, 0], [0, 0, 4], [0, 4, 0], [4, 0, 0]]; + assert(sortidx(lst3)==[0,3,2,4,1,6,5,9,7,10,8,11]); } test_sortidx();