From 229b9e1e3ce7cbf39fd8723b02d6eaed63cace48 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Mon, 1 Apr 2019 23:40:15 -0700 Subject: [PATCH] Optimizations to compare_vals() --- math.scad | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/math.scad b/math.scad index 71de4ce..5b33580 100644 --- a/math.scad +++ b/math.scad @@ -245,32 +245,24 @@ function sum_of_sines(a, sines) = function mean(v) = sum(v)/len(v); -// Section: Comparisons and Logic - // Function: compare_vals() // Usage: // compare_vals(a, b); // Description: -// Compares two values. If types don't match, then -// undef < boolean < scalar < string < list -// Lists are compared recursively. +// Compares two values. Lists are compared recursively. // Arguments: // a = First value to compare. // b = Second value to compare. function compare_vals(a, b, n=0) = - (a == undef && b == undef)? 0 : - (a == undef)? -1 : - (b == undef)? 1 : - (is_boolean(a) && is_boolean(b))? ((a?1:0)-(b?1:0)) : - is_boolean(a)? -1 : - is_boolean(b)? 1 : - (is_scalar(a) && is_scalar(b))? (a-b) : - is_scalar(a)? -1 : - is_scalar(b)? 1 : - (is_str(a) && is_str(b))? ((ab)? 1 : 0)) : - is_str(a)? -1 : - is_str(b)? 1 : - compare_lists(a,b); + (a==b)? 0 : + (a==undef)? -1 : + (b==undef)? 1 : + ((a==[] || a=="" || a[0]!=undef) && (b==[] || b=="" || b[0]!=undef))? ( + let (cmp = compare_vals(a[n], b[n])) + (cmp==0)? compare_vals(a,b,n+1) : + cmp + ) : (ab)? 1 : 0; // Function: compare_lists()