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 | * This helper functions generate code for two binary search functions:
|
---|
69 | * A(): Performs a binary search on an array using the comparison operator specified.
|
---|
70 | * P(): Performs a binary search on an array using a _custom comparison function_
|
---|
71 | * `c(x,y)` **and** comparison operator specified by `predicate`.
|
---|
72 | * @param {BinarySearchPredicate} predicate The predicate / comparison operator to be used in the binary search.
|
---|
73 | * @param {boolean} reversed Whether the search should be reversed.
|
---|
74 | * @param {SearchPredicateSuffix} suffix The suffix to be used in the function name.
|
---|
75 | * @param {boolean=} earlyOut Whether the search should return as soon as a match is found.
|
---|
76 | * @returns {Function} The compiled binary search function.
|
---|
77 | */
|
---|
78 | const compileBoundsSearch = (predicate, reversed, suffix, earlyOut) => {
|
---|
79 | const arg1 = compileSearch("A", `x${predicate}y`, reversed, ["y"], earlyOut);
|
---|
80 |
|
---|
81 | const arg2 = compileSearch(
|
---|
82 | "P",
|
---|
83 | `c(x,y)${predicate}0`,
|
---|
84 | reversed,
|
---|
85 | ["y", "c"],
|
---|
86 | earlyOut
|
---|
87 | );
|
---|
88 |
|
---|
89 | const fnHeader = "function dispatchBinarySearch";
|
---|
90 |
|
---|
91 | const fnBody =
|
---|
92 | // eslint-disable-next-line no-multi-str
|
---|
93 | "(a,y,c,l,h){\
|
---|
94 | if(typeof(c)==='function'){\
|
---|
95 | return P(a,(l===void 0)?0:l|0,(h===void 0)?a.length-1:h|0,y,c)\
|
---|
96 | }else{\
|
---|
97 | return A(a,(c===void 0)?0:c|0,(l===void 0)?a.length-1:l|0,y)\
|
---|
98 | }}\
|
---|
99 | return dispatchBinarySearch";
|
---|
100 |
|
---|
101 | const fnArgList = [arg1, arg2, fnHeader, suffix, fnBody, suffix];
|
---|
102 | const fnSource = fnArgList.join("");
|
---|
103 | // eslint-disable-next-line no-new-func
|
---|
104 | const result = new Function(fnSource);
|
---|
105 | return result();
|
---|
106 | };
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * These functions are used to perform binary searches on arrays.
|
---|
110 | * @example
|
---|
111 | * ```js
|
---|
112 | * const { gt, le} = require("./binarySearchBounds");
|
---|
113 | * const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
---|
114 | *
|
---|
115 | * // Find the index of the first element greater than 5
|
---|
116 | * const index1 = gt(arr, 5); // index1 === 3
|
---|
117 | *
|
---|
118 | * // Find the index of the first element less than or equal to 5
|
---|
119 | * const index2 = le(arr, 5); // index2 === 4
|
---|
120 | * ```
|
---|
121 | */
|
---|
122 | module.exports = {
|
---|
123 | ge: compileBoundsSearch(">=", false, "GE"),
|
---|
124 | gt: compileBoundsSearch(">", false, "GT"),
|
---|
125 | lt: compileBoundsSearch("<", true, "LT"),
|
---|
126 | le: compileBoundsSearch("<=", true, "LE"),
|
---|
127 | eq: compileBoundsSearch("-", true, "EQ", true)
|
---|
128 | };
|
---|