@graffy/common
Version:
Common libraries that used by various Graffy modules.
41 lines (36 loc) • 1.01 kB
JavaScript
export function find(items, compare, first, last) {
if (first === void 0) {
first = 0;
}
if (last === void 0) {
last = items.length;
}
var currentFirst = first;
var currentLast = last;
while (currentFirst < currentLast) {
// console.log(currentFirst, currentLast);
var ix = (currentFirst + currentLast) / 2 | 0;
var d = compare(items[ix]); // console.log(ix, items[ix], d);
if (d < 0) {
currentFirst = ix + 1;
} else if (d > 0) {
currentLast = ix;
} else {
return ix;
}
}
return currentFirst;
}
export function findFirst(children, target) {
return find(children, function (_ref) {
var key = _ref.key,
end = _ref.end;
if (key === target || end && key < target && end >= target) return 0;
if (key < target) return -1;
return 1;
});
}
export function findLast(children, end, first, last) {
var ix = findFirst(children, end, first, last);
return children[ix] && children[ix].key <= end ? ix + 1 : ix;
}