UNPKG

boundit

Version:

🔍 JavaScript and TypeScript implementation of lower_bound and upper_bound for efficient data searching.

80 lines (75 loc) 2.1 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { binarySearch: () => binarySearch, lowerBound: () => lowerBound, upperBound: () => upperBound }); module.exports = __toCommonJS(index_exports); // src/binarySearch.ts var binarySearch = (array, target) => { let left = 0; let right = array.length - 1; while (left <= right) { const mid = left + Math.floor((right - left) / 2); if (array[mid] === target) { return mid; } else if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; }; // src/lowerBound.ts var lowerBound = (array, target) => { let left = 0; let right = array.length; while (left < right) { const mid = left + Math.floor((right - left) / 2); if (array[mid] < target) { left = mid + 1; } else { right = mid; } } return left; }; // src/upperBound.ts var upperBound = (array, target) => { let left = 0; let right = array.length; while (left < right) { const mid = left + Math.floor((right - left) / 2); if (array[mid] <= target) { left = mid + 1; } else { right = mid; } } return left; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { binarySearch, lowerBound, upperBound });