mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-01 09:49:45 +00:00
Added list_set() optimization for single scalar indices.
This commit is contained in:
parent
00b11d130c
commit
1e0ca09182
1 changed files with 9 additions and 2 deletions
11
arrays.scad
11
arrays.scad
|
@ -183,15 +183,22 @@ function deduplicate(list, closed=false, eps=EPSILON) =
|
||||||
// then the list is extended and filled in with the `dflt` value. If you set `minlen` then the list is
|
// then the list is extended and filled in with the `dflt` value. If you set `minlen` then the list is
|
||||||
// lengthed, if necessary, by padding with `dflt` to that length. The `indices` list can be in any
|
// lengthed, if necessary, by padding with `dflt` to that length. The `indices` list can be in any
|
||||||
// order but run time will be (much) faster for long lists if it is already sorted. Reptitions are
|
// order but run time will be (much) faster for long lists if it is already sorted. Reptitions are
|
||||||
// not allowed.
|
// not allowed. If `indices` is given as a non-list scalar, then that index of the given `list` will
|
||||||
|
// be set to the value of `values`.
|
||||||
// Arguments:
|
// Arguments:
|
||||||
// list = List to set items in. Default: []
|
// list = List to set items in. Default: []
|
||||||
// indices = List of indices into `list` to set.
|
// indices = List of indices into `list` to set.
|
||||||
// values = List of values to set.
|
// values = List of values to set.
|
||||||
// dflt = Default value to store in sparse skipped indices.
|
// dflt = Default value to store in sparse skipped indices.
|
||||||
// minlen = Minimum length to expand list to.
|
// minlen = Minimum length to expand list to.
|
||||||
|
// Examples:
|
||||||
|
// list_set([2,3,4,5], 2, 21); // Returns: [2,3,21,5]
|
||||||
|
// list_set([2,3,4,5], [1,3], [81,47]); // Returns: [2,81,4,47]
|
||||||
function list_set(list=[],indices,values,dflt=0,minlen=0) =
|
function list_set(list=[],indices,values,dflt=0,minlen=0) =
|
||||||
!is_list(indices) ? list_set(list,[indices],[values],dflt) :
|
!is_list(indices)? (
|
||||||
|
(is_num(indices) && indices<len(list))? [for (i=idx(list)) i==indices? values : list[i]] :
|
||||||
|
list_set(list,[indices],[values],dflt)
|
||||||
|
) :
|
||||||
assert(len(indices)==len(values),"Index list and value list must have the same length")
|
assert(len(indices)==len(values),"Index list and value list must have the same length")
|
||||||
let(
|
let(
|
||||||
sortind = list_increasing(indices) ? list_range(len(indices)) : sortidx(indices),
|
sortind = list_increasing(indices) ? list_range(len(indices)) : sortidx(indices),
|
||||||
|
|
Loading…
Reference in a new issue