unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

tools/emitter/sorted_search.mbt
1///|2/// Write the function `name`, which looks a key up in the sorted3/// `ReadOnlyArray[Int]` named `values` by binary search and returns its4/// index, or -1 when the array does not hold it.5///6/// The array is named in the function body rather than passed in: on7/// wasm-gc, passing it measurably slowed down the lookup.8pub fn Emitter::write_sorted_search(9  self : Emitter,10  name~ : String,11  values~ : String,12) -> Unit {13  self.write(14    (15      $|///|16      $|/// Index of `key` in the sorted array `\{values}`, or -1 if it is absent.17      $|fn \{name}(key : Int) -> Int {18      $|  let mut left = 019      $|  let mut right = \{values}.length() - 120      $|  while left <= right {21      $|    let mid = (left + right) / 222      $|    let value = \{values}[mid]23      $|    if key < value {24      $|      right = mid - 125      $|    } else if key > value {26      $|      left = mid + 127      $|    } else {28      $|      return mid29      $|    }30      $|  }31      $|  -132      $|}33      $|34    ),35  )36}37