UNPKG

@picosearch/trie

Version:

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

22 lines (21 loc) 578 B
/** * 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. */ export 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); };