Fixed compare_vals for lists with single lists.

This commit is contained in:
Revar Desmera 2019-04-08 20:51:29 -07:00
parent 58880473da
commit b7e3644df3
2 changed files with 20 additions and 16 deletions

View file

@ -265,10 +265,6 @@ function compare_vals(a, b) =
(a==undef)? -1 : (a==undef)? -1 :
(b==undef)? 1 : (b==undef)? 1 :
((a==[] || a=="" || a[0]!=undef) && (b==[] || b=="" || b[0]!=undef))? ( ((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) compare_lists(a, b)
) : (a<b)? -1 : ) : (a<b)? -1 :
(a>b)? 1 : 0; (a>b)? 1 : 0;
@ -292,7 +288,9 @@ function compare_lists(a, b, n=0) =
cmp = (a==b)? 0 : cmp = (a==b)? 0 :
(len(a)<=n)? -1 : (len(a)<=n)? -1 :
(len(b)<=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 : (cmp != 0 || a==b)? cmp :
compare_lists(a, b, n+1); compare_lists(a, b, n+1);
@ -702,8 +700,9 @@ function sort(arr, idx=undef) =
pivotval = idx==undef? pivot : [for (i=idx) pivot[i]], pivotval = idx==undef? pivot : [for (i=idx) pivot[i]],
compare = [ compare = [
for (entry = arr) let( for (entry = arr) let(
val = idx==undef? entry : [for (i=idx) entry[i]] val = idx==undef? entry : [for (i=idx) entry[i]],
) compare_vals(val, pivotval) cmp = compare_vals(val, pivotval)
) cmp
], ],
lesser = [ for (i = [0:len(arr)-1]) if (compare[i] < 0) arr[i] ], 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] ], 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] // idxs2 = sortidx(lst, idx=0); // Returns: [1,2,0,3]
// idxs3 = sortidx(lst, idx=[1,3]); // Returns: [3,0,2,1] // idxs3 = sortidx(lst, idx=[1,3]); // Returns: [3,0,2,1]
function sortidx(l, idx=undef) = function sortidx(l, idx=undef) =
let(ll=enumerate(l,idx=idx)) (l==[])? [] :
array_subindex( let(
sort(ll, idx=( ll=enumerate(l,idx=idx),
idx==undef? 1 : sidx = [1:len(ll[0])-1]
(ll==[])? 1 : )
[1:len(ll[0])-1] array_subindex(sort(ll, idx=sidx), 0);
)),
0);
// Function: unique() // Function: unique()

View file

@ -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,5]) == 0);
assert(compare_vals([2,3,4,5], [2,3,4]) == 1); 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,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], [3,4,5]]) == 0);
assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) == -1); 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,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,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([[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(); test_compare_lists();
@ -447,7 +451,8 @@ test_flatten();
module test_sort() { module test_sort() {
assert(sort([7,3,9,4,3,1,8]) == [1,3,3,4,7,8,9]); 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(); test_sort();
@ -464,6 +469,8 @@ module test_sortidx() {
assert(sortidx(lst2, idx=1) == [3,0,2,1]); assert(sortidx(lst2, idx=1) == [3,0,2,1]);
assert(sortidx(lst2, idx=0) == [1,2,0,3]); assert(sortidx(lst2, idx=0) == [1,2,0,3]);
assert(sortidx(lst2, idx=[1,3]) == [3,0,2,1]); 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(); test_sortidx();