mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-06 06:47:46 +02:00
refactor
This commit is contained in:
7
src/util/_impl/_bsearch_impl.scad
Normal file
7
src/util/_impl/_bsearch_impl.scad
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
use <util/_impl/_vt_default_comparator.scad>;
|
||||||
|
|
||||||
|
function _binary_search(sorted, elem, low, upper) =
|
||||||
|
low > upper ? -1 :
|
||||||
|
let(mid = floor((low + upper) / 2))
|
||||||
|
lessThan(sorted[mid], elem) ? _binary_search(sorted, elem, mid + 1, upper) :
|
||||||
|
greaterThan(sorted[mid], elem) ? _binary_search(sorted, elem, low, mid - 1) : mid;
|
@@ -1,10 +1,4 @@
|
|||||||
use <util/_impl/_vt_default_comparator.scad>;
|
use <util/_impl/_bsearch_impl.scad>;
|
||||||
|
|
||||||
function _binary_search(sorted, elem, low, upper) =
|
|
||||||
low > upper ? -1 :
|
|
||||||
let(mid = floor((low + upper) / 2))
|
|
||||||
lessThan(sorted[mid], elem) ? _binary_search(sorted, elem, mid + 1, upper) :
|
|
||||||
greaterThan(sorted[mid], elem) ? _binary_search(sorted, elem, low, mid - 1) : mid;
|
|
||||||
|
|
||||||
// for example, `sorted` is by zyx
|
// for example, `sorted` is by zyx
|
||||||
function bsearch(sorted, elem) = _binary_search(sorted, elem, 0, len(sorted) - 1);
|
function bsearch(sorted, elem) = _binary_search(sorted, elem, 0, len(sorted) - 1);
|
Reference in New Issue
Block a user