forked from mystiq/hydrogen-web
move sortedIndex out of observable as other code will want to use it too
This commit is contained in:
parent
95bef00054
commit
6940e14b18
2 changed files with 27 additions and 30 deletions
|
@ -1,5 +1,5 @@
|
||||||
import BaseObservableList from "./BaseObservableList.js";
|
import BaseObservableList from "./BaseObservableList.js";
|
||||||
|
import sortedIndex from "../../utils/sortedIndex";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -25,35 +25,6 @@ so key -> Map<key,value> -> value -> node -> *parentNode -> rootNode
|
||||||
with a node containing {value, leftCount, rightCount, leftNode, rightNode, parentNode}
|
with a node containing {value, leftCount, rightCount, leftNode, rightNode, parentNode}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @license
|
|
||||||
* Based off baseSortedIndex function in Lodash <https://lodash.com/>
|
|
||||||
* Copyright JS Foundation and other contributors <https://js.foundation/>
|
|
||||||
* Released under MIT license <https://lodash.com/license>
|
|
||||||
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
|
||||||
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
||||||
*/
|
|
||||||
function sortedIndex(array, value, comparator) {
|
|
||||||
let low = 0;
|
|
||||||
let high = array.length;
|
|
||||||
|
|
||||||
while (low < high) {
|
|
||||||
let mid = (low + high) >>> 1;
|
|
||||||
let cmpResult = comparator(value, array[mid]);
|
|
||||||
|
|
||||||
if (cmpResult > 0) {
|
|
||||||
low = mid + 1;
|
|
||||||
} else if (cmpResult < 0) {
|
|
||||||
high = mid;
|
|
||||||
} else {
|
|
||||||
low = high = mid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return high;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// does not assume whether or not the values are reference
|
// does not assume whether or not the values are reference
|
||||||
// types modified outside of the collection (and affecting sort order) or not
|
// types modified outside of the collection (and affecting sort order) or not
|
||||||
|
|
||||||
|
|
26
src/utils/sortedIndex.js
Normal file
26
src/utils/sortedIndex.js
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Based off baseSortedIndex function in Lodash <https://lodash.com/>
|
||||||
|
* Copyright JS Foundation and other contributors <https://js.foundation/>
|
||||||
|
* Released under MIT license <https://lodash.com/license>
|
||||||
|
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
||||||
|
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
||||||
|
*/
|
||||||
|
export default function sortedIndex(array, value, comparator) {
|
||||||
|
let low = 0;
|
||||||
|
let high = array.length;
|
||||||
|
|
||||||
|
while (low < high) {
|
||||||
|
let mid = (low + high) >>> 1;
|
||||||
|
let cmpResult = comparator(value, array[mid]);
|
||||||
|
|
||||||
|
if (cmpResult > 0) {
|
||||||
|
low = mid + 1;
|
||||||
|
} else if (cmpResult < 0) {
|
||||||
|
high = mid;
|
||||||
|
} else {
|
||||||
|
low = high = mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return high;
|
||||||
|
}
|
Loading…
Reference in a new issue