mirror of
https://github.com/revarbat/BOSL2.git
synced 2025-08-10 05:57:06 +02:00
42
arrays.scad
42
arrays.scad
@@ -73,6 +73,9 @@ function select(list, start, end=undef) =
|
||||
: concat([for (i = [s:1:l-1]) list[i]], [for (i = [0:1:e]) list[i]]) ;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: slice()
|
||||
// Description:
|
||||
// Returns a slice of a list. The first item is index 0.
|
||||
@@ -98,6 +101,8 @@ function slice(list,start,end) =
|
||||
) [for (i=[s:1:e-1]) if (e>s) list[i]];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: in_list()
|
||||
// Description: Returns true if value `val` is in list `list`. When `val==NAN` the answer will be false for any list.
|
||||
// Arguments:
|
||||
@@ -113,6 +118,7 @@ function in_list(val,list,idx=undef) =
|
||||
s==[] || s[0]==[] ? false
|
||||
: is_undef(idx) ? val==list[s]
|
||||
: val==list[s][idx];
|
||||
|
||||
|
||||
|
||||
// Function: min_index()
|
||||
@@ -203,6 +209,7 @@ function repeat(val, n, i=0) =
|
||||
[for (j=[1:1:n[i]]) repeat(val, n, i+1)];
|
||||
|
||||
|
||||
|
||||
// Function: list_range()
|
||||
// Usage:
|
||||
// list_range(n, [s], [e])
|
||||
@@ -239,6 +246,8 @@ function list_range(n=undef, s=0, e=undef, step=undef) =
|
||||
[for (i=[0:1:n-1]) s+step*i ]
|
||||
: assert( is_vector([s,step,e]), "Start `s`, step `step` and end `e` must be numbers.")
|
||||
[for (v=[s:step:e]) v] ;
|
||||
|
||||
|
||||
|
||||
|
||||
// Section: List Manipulation
|
||||
@@ -306,6 +315,8 @@ 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]];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: deduplicate_indexed()
|
||||
// Usage:
|
||||
// new_idxs = deduplicate_indexed(list, indices, [closed], [eps]);
|
||||
@@ -340,6 +351,8 @@ function deduplicate_indexed(list, indices, closed=false, eps=EPSILON) =
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: repeat_entries()
|
||||
// Usage:
|
||||
// newlist = repeat_entries(list, N)
|
||||
@@ -379,6 +392,8 @@ function repeat_entries(list, N, exact = true) =
|
||||
[for(i=[0:length-1]) each repeat(list[i],reps[i])];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: list_set()
|
||||
// Usage:
|
||||
// list_set(list, indices, values, [dflt], [minlen])
|
||||
@@ -418,6 +433,7 @@ function list_set(list=[],indices,values,dflt=0,minlen=0) =
|
||||
];
|
||||
|
||||
|
||||
|
||||
// Function: list_insert()
|
||||
// Usage:
|
||||
// list_insert(list, indices, values);
|
||||
@@ -449,6 +465,8 @@ function list_insert(list, indices, values, _i=0) =
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: list_remove()
|
||||
// Usage:
|
||||
// list_remove(list, indices)
|
||||
@@ -471,6 +489,8 @@ function list_remove(list, indices) =
|
||||
if ( []==search(i,indices,1) ) list[i] ];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: list_remove_values()
|
||||
// Usage:
|
||||
// list_remove_values(list,values,all=false) =
|
||||
@@ -540,6 +560,8 @@ function list_bset(indexset, valuelist, dflt=0) =
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
// Section: List Length Manipulation
|
||||
|
||||
// Function: list_shortest()
|
||||
@@ -552,6 +574,7 @@ function list_shortest(array) =
|
||||
min([for (v = array) len(v)]);
|
||||
|
||||
|
||||
|
||||
// Function: list_longest()
|
||||
// Description:
|
||||
// Returns the length of the longest sublist in a list of lists.
|
||||
@@ -601,6 +624,7 @@ function list_fit(array, length, fill) =
|
||||
: list_pad(array,length,fill);
|
||||
|
||||
|
||||
|
||||
// Section: List Shuffling and Sorting
|
||||
|
||||
// Function: shuffle()
|
||||
@@ -655,7 +679,6 @@ function _sort_vectors2(arr) =
|
||||
)
|
||||
concat( _sort_vectors2(lesser), equal, _sort_vectors2(greater) );
|
||||
|
||||
|
||||
// Sort a vector of vectors based on the first three entries of each vector
|
||||
// Lexicographic order, remaining entries of vector ignored
|
||||
function _sort_vectors3(arr) =
|
||||
@@ -733,7 +756,6 @@ function _sort_general(arr, idx=undef) =
|
||||
)
|
||||
concat(_sort_general(lesser,idx), equal, _sort_general(greater,idx));
|
||||
|
||||
|
||||
function _sort_general(arr, idx=undef) =
|
||||
(len(arr)<=1) ? arr :
|
||||
let(
|
||||
@@ -752,6 +774,9 @@ function _sort_general(arr, idx=undef) =
|
||||
concat(_sort_general(lesser,idx), equal, _sort_general(greater,idx));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: sort()
|
||||
// Usage:
|
||||
// sort(list, [idx])
|
||||
@@ -784,6 +809,7 @@ function sort(list, idx=undef) =
|
||||
: _sort_general(list);
|
||||
|
||||
|
||||
|
||||
// Function: sortidx()
|
||||
// Description:
|
||||
// Given a list, calculates the sort order of the list, and returns
|
||||
@@ -827,7 +853,6 @@ function sortidx(list, idx=undef) =
|
||||
: // general case
|
||||
subindex(_sort_general(aug, idx=list_range(s=1,n=len(aug)-1)), 0);
|
||||
|
||||
|
||||
function sortidx(list, idx=undef) =
|
||||
list==[] ? [] : let(
|
||||
size = array_dim(list),
|
||||
@@ -866,6 +891,7 @@ function unique(arr) =
|
||||
];
|
||||
|
||||
|
||||
|
||||
// Function: unique_count()
|
||||
// Usage:
|
||||
// unique_count(arr);
|
||||
@@ -882,6 +908,8 @@ function unique_count(arr) =
|
||||
[ select(arr,ind), deltas( concat(ind,[len(arr)]) ) ];
|
||||
|
||||
|
||||
|
||||
|
||||
// Section: List Iteration Helpers
|
||||
|
||||
// Function: idx()
|
||||
@@ -1076,6 +1104,8 @@ function set_union(a, b, get_indices=false) =
|
||||
) [idxs, nset];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: set_difference()
|
||||
// Usage:
|
||||
// s = set_difference(a, b);
|
||||
@@ -1095,6 +1125,7 @@ function set_difference(a, b) =
|
||||
[ for (i=idx(a)) if(found[i]==[]) a[i] ];
|
||||
|
||||
|
||||
|
||||
// Function: set_intersection()
|
||||
// Usage:
|
||||
// s = set_intersection(a, b);
|
||||
@@ -1114,6 +1145,8 @@ function set_intersection(a, b) =
|
||||
[ for (i=idx(a)) if(found[i]!=[]) a[i] ];
|
||||
|
||||
|
||||
|
||||
|
||||
// Section: Array Manipulation
|
||||
|
||||
// Function: add_scalar()
|
||||
@@ -1132,6 +1165,7 @@ 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;
|
||||
|
||||
|
||||
|
||||
// Function: subindex()
|
||||
// Description:
|
||||
// For each array item, return the indexed subitem.
|
||||
@@ -1315,4 +1349,6 @@ function transpose(arr) =
|
||||
: arr;
|
||||
|
||||
|
||||
|
||||
|
||||
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||
|
Reference in New Issue
Block a user