UNPKG

@picosearch/trie

Version:

Simple, zero dependency, type-safe implementation of a trie data structure.

26 lines (25 loc) 716 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sortedInsert = void 0; /** * Insert a value into a sorted array efficiently. * * Reference: https://stackoverflow.com/a/21822316 * * @param array - The array to insert into. Must be sorted. * @param value - The value to insert. * @param compare - The comparison function. */ const sortedInsert = (array, value, compare) => { let low = 0; let high = array.length; while (low < high) { const mid = (low + high) >>> 1; if (compare(array[mid], value) < 0) low = mid + 1; else high = mid; } array.splice(low, 0, value); }; exports.sortedInsert = sortedInsert;