mirror of
https://github.com/revarbat/BOSL2.git
synced 2025-08-06 19:56:36 +02:00
Added array_unique(). Renamed quicksort() to array_sort()
This commit is contained in:
28
math.scad
28
math.scad
@@ -578,9 +578,9 @@ function compare_vals(a, b, n=0) =
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Function: quicksort()
|
// Function: array_sort()
|
||||||
// Usage:
|
// Usage:
|
||||||
// quicksort(arr, [idx])
|
// array_sort(arr, [idx])
|
||||||
// Description:
|
// Description:
|
||||||
// Sorts the given list using `compare_vals()`.
|
// Sorts the given list using `compare_vals()`.
|
||||||
// Arguments:
|
// Arguments:
|
||||||
@@ -588,8 +588,8 @@ function compare_vals(a, b, n=0) =
|
|||||||
// idx = If given, the index, range, or list of indices of sublist items to compare.
|
// idx = If given, the index, range, or list of indices of sublist items to compare.
|
||||||
// Example:
|
// Example:
|
||||||
// l = [45,2,16,37,8,3,9,23,89,12,34];
|
// l = [45,2,16,37,8,3,9,23,89,12,34];
|
||||||
// sorted = quicksort(l); // Returns [2,3,8,9,12,16,23,34,37,45,89]
|
// sorted = array_sort(l); // Returns [2,3,8,9,12,16,23,34,37,45,89]
|
||||||
function quicksort(arr, idx=undef) =
|
function array_sort(arr, idx=undef) =
|
||||||
(len(arr)<=1) ? arr :
|
(len(arr)<=1) ? arr :
|
||||||
let(
|
let(
|
||||||
pivot = arr[floor(len(arr)/2)],
|
pivot = arr[floor(len(arr)/2)],
|
||||||
@@ -603,7 +603,25 @@ function quicksort(arr, idx=undef) =
|
|||||||
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] ],
|
||||||
greater = [ for (i = [0:len(arr)-1]) if (compare[i] > 0) arr[i] ]
|
greater = [ for (i = [0:len(arr)-1]) if (compare[i] > 0) arr[i] ]
|
||||||
)
|
)
|
||||||
concat(quicksort(lesser,idx), equal, quicksort(greater,idx));
|
concat(array_sort(lesser,idx), equal, array_sort(greater,idx));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Function: array_unique()
|
||||||
|
// Usage:
|
||||||
|
// array_unique(arr);
|
||||||
|
// Description:
|
||||||
|
// Returns a sorted list with all repeated items removed.
|
||||||
|
// Arguments:
|
||||||
|
// arr = The list to uniquify.
|
||||||
|
function array_unique(arr) =
|
||||||
|
len(arr)<=1? arr : let(
|
||||||
|
sorted = array_sort(arr)
|
||||||
|
) [
|
||||||
|
for (i=[0:len(sorted)-1])
|
||||||
|
if (i==0 || (sorted[i] != sorted[i-1]))
|
||||||
|
sorted[i]
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user