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
///|
/// Write the function `name`, which looks a key up in the sorted
/// `ReadOnlyArray[Int]` named `values` by binary search and returns its
/// index, or -1 when the array does not hold it.
///
/// The array is named in the function body rather than passed in: on
/// wasm-gc, passing it measurably slowed down the lookup.
pub fn Emitter::write_sorted_search(
self : Emitter,
name~ : String,
values~ : String,
) -> Unit {
self.write(
(
$|///|
$|/// Index of `key` in the sorted array `\{values}`, or -1 if it is absent.
$|fn \{name}(key : Int) -> Int {
$| let mut left = 0
$| let mut right = \{values}.length() - 1
$| while left <= right {
$| let mid = (left + right) / 2
$| let value = \{values}[mid]
$| if key < value {
$| right = mid - 1
$| } else if key > value {
$| left = mid + 1
$| } else {
$| return mid
$| }
$| }
$| -1
$|}
$|
),
)
}