(funcName, predicate, reversed, extraArgs, earlyOut)
| 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. |
no test coverage detected