Minor edits in in_list and transpose, removal of is_simple list

This commit is contained in:
RonaldoCMP 2020-07-31 15:52:35 +01:00
parent 3b1d567219
commit 526c01109c
2 changed files with 16 additions and 70 deletions

View file

@ -18,20 +18,6 @@
// Section: List Query Operations // Section: List Query Operations
// Function: is_simple_list()
// Description:
// Returns true just when all elements of `list` are simple values.
// Usage:
// is_simple_list(list)
// Arguments:
// list = The list to check.
// Example:
// a = is_simple_list([3,4,5,6,7,8,9]); Returns: true
// b = is_simple_list([3,4,5,[6],7,8]); Returns: false
function is_simple_list(list) =
is_list(list)
&& []==[for(e=list) if(is_list(e)) 0];
// Function: select() // Function: select()
// Description: // Description:
@ -73,9 +59,6 @@ function select(list, start, end=undef) =
: concat([for (i = [s:1:l-1]) list[i]], [for (i = [0:1:e]) list[i]]) ; : concat([for (i = [s:1:l-1]) list[i]], [for (i = [0:1:e]) list[i]]) ;
// Function: slice() // Function: slice()
// Description: // Description:
// Returns a slice of a list. The first item is index 0. // Returns a slice of a list. The first item is index 0.
@ -101,24 +84,23 @@ function slice(list,start,end) =
) [for (i=[s:1:e-1]) if (e>s) list[i]]; ) [for (i=[s:1:e-1]) if (e>s) list[i]];
// Function: in_list() // Function: in_list()
// Description: Returns true if value `val` is in list `list`. When `val==NAN` the answer will be false for any list. // Description: Returns true if value `val` is in list `list`. When `val==NAN` the answer will be false for any list.
// Arguments: // Arguments:
// val = The simple value to search for. // val = The simple value to search for.
// list = The list to search. // list = The list to search.
// idx = If given, searches the given subindexes for matches for `val`. // idx = If given, searches the given subindex for matches for `val`.
// Example: // Example:
// in_list("bar", ["foo", "bar", "baz"]); // Returns true. // in_list("bar", ["foo", "bar", "baz"]); // Returns true.
// in_list("bee", ["foo", "bar", "baz"]); // Returns false. // in_list("bee", ["foo", "bar", "baz"]); // Returns false.
// in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1); // Returns true. // in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1); // Returns true.
function in_list(val,list,idx=undef) = function in_list(val,list,idx=undef) =
assert( is_list(list) && (is_undef(idx) || is_finite(idx)),
"Invalid input." )
let( s = search([val], list, num_returns_per_match=1, index_col_num=idx)[0] ) let( s = search([val], list, num_returns_per_match=1, index_col_num=idx)[0] )
s==[] || s[0]==[] ? false s==[] || s==[[]] ? false
: is_undef(idx) ? val==list[s] : is_undef(idx) ? val==list[s]
: val==list[s][idx]; : val==list[s][idx];
// Function: min_index() // Function: min_index()
@ -209,7 +191,6 @@ function repeat(val, n, i=0) =
[for (j=[1:1:n[i]]) repeat(val, n, i+1)]; [for (j=[1:1:n[i]]) repeat(val, n, i+1)];
// Function: list_range() // Function: list_range()
// Usage: // Usage:
// list_range(n, [s], [e]) // list_range(n, [s], [e])
@ -249,7 +230,6 @@ function list_range(n=undef, s=0, e=undef, step=undef) =
// Section: List Manipulation // Section: List Manipulation
// Function: reverse() // Function: reverse()
@ -315,8 +295,6 @@ function deduplicate(list, closed=false, eps=EPSILON) =
: [for (i=[0:1:l-1]) if (i==end || !approx(list[i], list[(i+1)%l], eps)) list[i]]; : [for (i=[0:1:l-1]) if (i==end || !approx(list[i], list[(i+1)%l], eps)) list[i]];
// Function: deduplicate_indexed() // Function: deduplicate_indexed()
// Usage: // Usage:
// new_idxs = deduplicate_indexed(list, indices, [closed], [eps]); // new_idxs = deduplicate_indexed(list, indices, [closed], [eps]);
@ -351,8 +329,6 @@ function deduplicate_indexed(list, indices, closed=false, eps=EPSILON) =
]; ];
// Function: repeat_entries() // Function: repeat_entries()
// Usage: // Usage:
// newlist = repeat_entries(list, N) // newlist = repeat_entries(list, N)
@ -390,8 +366,6 @@ function repeat_entries(list, N, exact = true) =
: [for (val=reps_guess) round(val)] : [for (val=reps_guess) round(val)]
) )
[for(i=[0:length-1]) each repeat(list[i],reps[i])]; [for(i=[0:length-1]) each repeat(list[i],reps[i])];
// Function: list_set() // Function: list_set()
@ -431,7 +405,6 @@ function list_set(list=[],indices,values,dflt=0,minlen=0) =
dflt , dflt ,
each repeat(dflt, minlen-max(indices)) each repeat(dflt, minlen-max(indices))
]; ];
// Function: list_insert() // Function: list_insert()
@ -465,8 +438,6 @@ function list_insert(list, indices, values, _i=0) =
]; ];
// Function: list_remove() // Function: list_remove()
// Usage: // Usage:
// list_remove(list, indices) // list_remove(list, indices)
@ -489,8 +460,6 @@ function list_remove(list, indices) =
if ( []==search(i,indices,1) ) list[i] ]; if ( []==search(i,indices,1) ) list[i] ];
// Function: list_remove_values() // Function: list_remove_values()
// Usage: // Usage:
// list_remove_values(list,values,all=false) = // list_remove_values(list,values,all=false) =
@ -560,8 +529,6 @@ function list_bset(indexset, valuelist, dflt=0) =
); );
// Section: List Length Manipulation // Section: List Length Manipulation
// Function: list_shortest() // Function: list_shortest()
@ -574,7 +541,6 @@ function list_shortest(array) =
min([for (v = array) len(v)]); min([for (v = array) len(v)]);
// Function: list_longest() // Function: list_longest()
// Description: // Description:
// Returns the length of the longest sublist in a list of lists. // Returns the length of the longest sublist in a list of lists.
@ -624,7 +590,6 @@ function list_fit(array, length, fill) =
: list_pad(array,length,fill); : list_pad(array,length,fill);
// Section: List Shuffling and Sorting // Section: List Shuffling and Sorting
// Function: shuffle() // Function: shuffle()
@ -679,6 +644,7 @@ function _sort_vectors2(arr) =
) )
concat( _sort_vectors2(lesser), equal, _sort_vectors2(greater) ); concat( _sort_vectors2(lesser), equal, _sort_vectors2(greater) );
// Sort a vector of vectors based on the first three entries of each vector // Sort a vector of vectors based on the first three entries of each vector
// Lexicographic order, remaining entries of vector ignored // Lexicographic order, remaining entries of vector ignored
function _sort_vectors3(arr) = function _sort_vectors3(arr) =
@ -706,7 +672,6 @@ function _sort_vectors3(arr) =
) concat( _sort_vectors3(lesser), equal, _sort_vectors3(greater) ); ) concat( _sort_vectors3(lesser), equal, _sort_vectors3(greater) );
// Sort a vector of vectors based on the first four entries of each vector // Sort a vector of vectors based on the first four entries of each vector
// Lexicographic order, remaining entries of vector ignored // Lexicographic order, remaining entries of vector ignored
function _sort_vectors4(arr) = function _sort_vectors4(arr) =
@ -737,7 +702,6 @@ function _sort_vectors4(arr) =
&& y[3]>pivot[3] )))))) && y[3]>pivot[3] ))))))
y ] y ]
) concat( _sort_vectors4(lesser), equal, _sort_vectors4(greater) ); ) concat( _sort_vectors4(lesser), equal, _sort_vectors4(greater) );
function _sort_general(arr, idx=undef) = function _sort_general(arr, idx=undef) =
@ -755,7 +719,8 @@ function _sort_general(arr, idx=undef) =
greater = [ for (i = [0:1:len(arr)-1]) if (compare[i] > 0) arr[i] ] greater = [ for (i = [0:1:len(arr)-1]) if (compare[i] > 0) arr[i] ]
) )
concat(_sort_general(lesser,idx), equal, _sort_general(greater,idx)); concat(_sort_general(lesser,idx), equal, _sort_general(greater,idx));
function _sort_general(arr, idx=undef) = function _sort_general(arr, idx=undef) =
(len(arr)<=1) ? arr : (len(arr)<=1) ? arr :
let( let(
@ -774,9 +739,6 @@ function _sort_general(arr, idx=undef) =
concat(_sort_general(lesser,idx), equal, _sort_general(greater,idx)); concat(_sort_general(lesser,idx), equal, _sort_general(greater,idx));
// Function: sort() // Function: sort()
// Usage: // Usage:
// sort(list, [idx]) // sort(list, [idx])
@ -809,7 +771,6 @@ function sort(list, idx=undef) =
: _sort_general(list); : _sort_general(list);
// Function: sortidx() // Function: sortidx()
// Description: // Description:
// Given a list, calculates the sort order of the list, and returns // Given a list, calculates the sort order of the list, and returns
@ -853,6 +814,7 @@ function sortidx(list, idx=undef) =
: // general case : // general case
subindex(_sort_general(aug, idx=list_range(s=1,n=len(aug)-1)), 0); subindex(_sort_general(aug, idx=list_range(s=1,n=len(aug)-1)), 0);
function sortidx(list, idx=undef) = function sortidx(list, idx=undef) =
list==[] ? [] : let( list==[] ? [] : let(
size = array_dim(list), size = array_dim(list),
@ -891,7 +853,6 @@ function unique(arr) =
]; ];
// Function: unique_count() // Function: unique_count()
// Usage: // Usage:
// unique_count(arr); // unique_count(arr);
@ -908,8 +869,6 @@ function unique_count(arr) =
[ select(arr,ind), deltas( concat(ind,[len(arr)]) ) ]; [ select(arr,ind), deltas( concat(ind,[len(arr)]) ) ];
// Section: List Iteration Helpers // Section: List Iteration Helpers
// Function: idx() // Function: idx()
@ -1104,8 +1063,6 @@ function set_union(a, b, get_indices=false) =
) [idxs, nset]; ) [idxs, nset];
// Function: set_difference() // Function: set_difference()
// Usage: // Usage:
// s = set_difference(a, b); // s = set_difference(a, b);
@ -1125,7 +1082,6 @@ function set_difference(a, b) =
[ for (i=idx(a)) if(found[i]==[]) a[i] ]; [ for (i=idx(a)) if(found[i]==[]) a[i] ];
// Function: set_intersection() // Function: set_intersection()
// Usage: // Usage:
// s = set_intersection(a, b); // s = set_intersection(a, b);
@ -1146,7 +1102,6 @@ function set_intersection(a, b) =
// Section: Array Manipulation // Section: Array Manipulation
// Function: add_scalar() // Function: add_scalar()
@ -1165,7 +1120,6 @@ function add_scalar(v,s) =
is_finite(s) ? [for (x=v) is_list(x)? add_scalar(x,s) : is_finite(x) ? x+s: x] : v; is_finite(s) ? [for (x=v) is_list(x)? add_scalar(x,s) : is_finite(x) ? x+s: x] : v;
// Function: subindex() // Function: subindex()
// Description: // Description:
// For each array item, return the indexed subitem. // For each array item, return the indexed subitem.
@ -1341,14 +1295,14 @@ function array_dim(v, depth=undef) =
// Example: // Example:
// transpose([3,4,5]); // Returns: [3,4,5] // transpose([3,4,5]); // Returns: [3,4,5]
function transpose(arr) = function transpose(arr) =
let( a0 = arr[0] ) assert( is_list(arr) && len(arr)>0, "The array is not a vector neither a matrix." )
is_list(a0) is_list(arr[0])
? assert([for(a=arr) if(len(a)!=len(a0)) 1]==[], "The array is not a matrix." ) ? let( l0 = len(arr[0]) )
[for (i=[0:1:len(a0)-1]) assert([for(a=arr) if(!is_list(a) || len(a)!=l0) 1 ]==[], "The array is not a vector neither a matrix." )
[ for (j=[0:1:len(arr)-1]) arr[j][i] ] ] [for (i=[0:1:l0-1])
: arr; [ for (j=[0:1:len(arr)-1]) arr[j][i] ] ]
: assert( is_vector(arr), "The array is not a vector neither a matrix." )
arr;
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap // vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View file

@ -3,14 +3,6 @@ include <../std.scad>
// Section: List Query Operations // Section: List Query Operations
module test_is_simple_list() {
assert(is_simple_list([1,2,3,4]));
assert(is_simple_list([]));
assert(!is_simple_list([1,2,[3,4]]));
}
test_is_simple_list();
module test_select() { module test_select() {
l = [3,4,5,6,7,8,9]; l = [3,4,5,6,7,8,9];
assert(select(l, 5, 6) == [8,9]); assert(select(l, 5, 6) == [8,9]);