"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createInsert = exports.binSearchInsertAt = void 0;
function binSearchInsertAt(list, item, compare) {
    switch (list.length) {
        case 0: return 0;
        case 1: return compare(item, list[0]) < 0 ? 0 : 1;
    }
    let start = 0;
    let end = list.length;
    let pivot;
    let cmpResult;
    while (start < end) {
        pivot = start + Math.floor((end - start) / 2);
        cmpResult = compare(item, list[pivot]);
        if (cmpResult === 0)
            return pivot + 1;
        if (cmpResult < 0) {
            end = pivot;
        }
        if (cmpResult > 0) {
            start = pivot + 1;
        }
    }
    if (start >= list.length)
        return list.length;
    return compare(item, list[start]) < 0 ? start : start + 1;
}
exports.binSearchInsertAt = binSearchInsertAt;
function createInsert(start, compare) {
    return (list, item) => {
        const pos = start(list, item, compare);
        list.splice(pos, 0, item);
    };
}
exports.createInsert = createInsert;
