MCPcopy
hub / github.com/webpack/webpack / compileSearch

Function compileSearch

lib/util/binarySearchBounds.js:32–65  ·  view source on GitHub ↗
(funcName, predicate, reversed, extraArgs, earlyOut)

Source from the content-addressed store, hash-verified

30 * @returns {string} The compiled binary search function.
31 */
32const 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.

Callers 1

compileBoundsSearchFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected