| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Mikola Lysenko @mikolalysenko
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /* cspell:disable-next-line */
|
|---|
| 9 | // Refactor: Peter Somogyvari @petermetz
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {">=" | "<=" | "<" | ">" | "-"} BinarySearchPredicate */
|
|---|
| 12 | /** @typedef {"GE" | "GT" | "LT" | "LE" | "EQ"} SearchPredicateSuffix */
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Helper function for compiling binary search functions.
|
|---|
| 16 | *
|
|---|
| 17 | * The generated code uses a while loop to repeatedly divide the search interval
|
|---|
| 18 | * in half until the desired element is found, or the search interval is empty.
|
|---|
| 19 | *
|
|---|
| 20 | * The following is an example of a generated function for calling `compileSearch("P", "c(x,y)<=0", true, ["y", "c"], false)`:
|
|---|
| 21 | *
|
|---|
| 22 | * ```js
|
|---|
| 23 | * function P(a,l,h,y,c){var i=l-1;while(l<=h){var m=(l+h)>>>1,x=a[m];if(c(x,y)<=0){i=m;l=m+1}else{h=m-1}}return i};
|
|---|
| 24 | * ```
|
|---|
| 25 | * @param {string} funcName The name of the function to be compiled.
|
|---|
| 26 | * @param {string} predicate The predicate / comparison operator to be used in the binary search.
|
|---|
| 27 | * @param {boolean} reversed Whether the search should be reversed.
|
|---|
| 28 | * @param {string[]} extraArgs Extra arguments to be passed to the function.
|
|---|
| 29 | * @param {boolean=} earlyOut Whether the search should return as soon as a match is found.
|
|---|
| 30 | * @returns {string} The compiled binary search function.
|
|---|
| 31 | */
|
|---|
| 32 | const compileSearch = (funcName, predicate, reversed, extraArgs, earlyOut) => {
|
|---|
| 33 | const code = [
|
|---|
| 34 | "function ",
|
|---|
| 35 | funcName,
|
|---|
| 36 | "(a,l,h,",
|
|---|
| 37 | extraArgs.join(","),
|
|---|
| 38 | "){",
|
|---|
| 39 | earlyOut ? "" : "var i=",
|
|---|
| 40 | reversed ? "l-1" : "h+1",
|
|---|
| 41 | ";while(l<=h){var m=(l+h)>>>1,x=a[m]"
|
|---|
| 42 | ];
|
|---|
| 43 |
|
|---|
| 44 | if (earlyOut) {
|
|---|
| 45 | if (!predicate.includes("c")) {
|
|---|
| 46 | code.push(";if(x===y){return m}else if(x<=y){");
|
|---|
| 47 | } else {
|
|---|
| 48 | code.push(";var p=c(x,y);if(p===0){return m}else if(p<=0){");
|
|---|
| 49 | }
|
|---|
| 50 | } else {
|
|---|
| 51 | code.push(";if(", predicate, "){i=m;");
|
|---|
| 52 | }
|
|---|
| 53 | if (reversed) {
|
|---|
| 54 | code.push("l=m+1}else{h=m-1}");
|
|---|
| 55 | } else {
|
|---|
| 56 | code.push("h=m-1}else{l=m+1}");
|
|---|
| 57 | }
|
|---|
| 58 | code.push("}");
|
|---|
| 59 | if (earlyOut) {
|
|---|
| 60 | code.push("return -1};");
|
|---|
| 61 | } else {
|
|---|
| 62 | code.push("return i};");
|
|---|
| 63 | }
|
|---|
| 64 | return code.join("");
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Defines the search type used by this module.
|
|---|
| 69 | * @template T
|
|---|
| 70 | * @typedef {(items: T[], start: number, compareFn?: number | ((item: T, needle: number) => number), l?: number, h?: number) => number} Search
|
|---|
| 71 | */
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * This helper functions generate code for two binary search functions:
|
|---|
| 75 | * A(): Performs a binary search on an array using the comparison operator specified.
|
|---|
| 76 | * P(): Performs a binary search on an array using a _custom comparison function_
|
|---|
| 77 | * `c(x,y)` **and** comparison operator specified by `predicate`.
|
|---|
| 78 | * @template T
|
|---|
| 79 | * @param {BinarySearchPredicate} predicate The predicate / comparison operator to be used in the binary search.
|
|---|
| 80 | * @param {boolean} reversed Whether the search should be reversed.
|
|---|
| 81 | * @param {SearchPredicateSuffix} suffix The suffix to be used in the function name.
|
|---|
| 82 | * @param {boolean=} earlyOut Whether the search should return as soon as a match is found.
|
|---|
| 83 | * @returns {Search<T>} The compiled binary search function.
|
|---|
| 84 | */
|
|---|
| 85 | const compileBoundsSearch = (predicate, reversed, suffix, earlyOut) => {
|
|---|
| 86 | const arg1 = compileSearch("A", `x${predicate}y`, reversed, ["y"], earlyOut);
|
|---|
| 87 |
|
|---|
| 88 | const arg2 = compileSearch(
|
|---|
| 89 | "P",
|
|---|
| 90 | `c(x,y)${predicate}0`,
|
|---|
| 91 | reversed,
|
|---|
| 92 | ["y", "c"],
|
|---|
| 93 | earlyOut
|
|---|
| 94 | );
|
|---|
| 95 |
|
|---|
| 96 | const fnHeader = "function dispatchBinarySearch";
|
|---|
| 97 |
|
|---|
| 98 | const fnBody =
|
|---|
| 99 | // eslint-disable-next-line no-multi-str
|
|---|
| 100 | "(a,y,c,l,h){\
|
|---|
| 101 | if(typeof(c)==='function'){\
|
|---|
| 102 | return P(a,(l===void 0)?0:l|0,(h===void 0)?a.length-1:h|0,y,c)\
|
|---|
| 103 | }else{\
|
|---|
| 104 | return A(a,(c===void 0)?0:c|0,(l===void 0)?a.length-1:l|0,y)\
|
|---|
| 105 | }}\
|
|---|
| 106 | return dispatchBinarySearch";
|
|---|
| 107 |
|
|---|
| 108 | const fnArgList = [arg1, arg2, fnHeader, suffix, fnBody, suffix];
|
|---|
| 109 | const fnSource = fnArgList.join("");
|
|---|
| 110 | // eslint-disable-next-line no-new-func
|
|---|
| 111 | const result = new Function(fnSource);
|
|---|
| 112 | return result();
|
|---|
| 113 | };
|
|---|
| 114 |
|
|---|
| 115 | const fns = {
|
|---|
| 116 | ge: compileBoundsSearch(">=", false, "GE"),
|
|---|
| 117 | gt: compileBoundsSearch(">", false, "GT"),
|
|---|
| 118 | lt: compileBoundsSearch("<", true, "LT"),
|
|---|
| 119 | le: compileBoundsSearch("<=", true, "LE"),
|
|---|
| 120 | eq: compileBoundsSearch("-", true, "EQ", true)
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * These functions are used to perform binary searches on arrays.
|
|---|
| 125 | * @example
|
|---|
| 126 | * ```js
|
|---|
| 127 | * const { gt, le} = require("./binarySearchBounds");
|
|---|
| 128 | * const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|---|
| 129 | *
|
|---|
| 130 | * // Find the index of the first element greater than 5
|
|---|
| 131 | * const index1 = gt(arr, 5); // index1 === 3
|
|---|
| 132 | *
|
|---|
| 133 | * // Find the index of the first element less than or equal to 5
|
|---|
| 134 | * const index2 = le(arr, 5); // index2 === 4
|
|---|
| 135 | * ```
|
|---|
| 136 | */
|
|---|
| 137 | module.exports = fns;
|
|---|