Skip to content

Commit

Permalink
Fixed compare_vals for lists with single lists.
Browse files Browse the repository at this point in the history
  • Loading branch information
revarbat committed Apr 9, 2019
1 parent 5888047 commit b7e3644
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
27 changes: 12 additions & 15 deletions math.scad
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
(a[0]>b[0])? 1 : 0
) :
compare_lists(a, b)
) : (a<b)? -1 :
(a>b)? 1 : 0;
Expand All @@ -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])? (
a<b? -1 : a>b? 1 : 0
) : compare_vals(a[n], b[n])
)
(cmp != 0 || a==b)? cmp :
compare_lists(a, b, n+1);
Expand Down Expand Up @@ -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] ],
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion tests/test_math.scad
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down

0 comments on commit b7e3644

Please sign in to comment.