1 |
9533
|
aaronmk
|
#!/bin/bash -e
|
2 |
|
|
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
|
3 |
|
|
|
4 |
|
|
if self_not_included; then
|
5 |
|
|
|
6 |
|
|
function binsearch() # usage: min=# max=# binsearch >_cmd args... (uses $i)
|
7 |
|
|
# because integer division trucates, $max is *exclusive*
|
8 |
|
|
{
|
9 |
|
|
echo_func; kw_params min max; : "${min?}" "${max?}"
|
10 |
|
|
local min="$min" max="$max" i
|
11 |
|
|
while true; do
|
12 |
9548
|
aaronmk
|
i=$(( (min + max)/2 )); log+ -2 echo_vars i
|
13 |
9533
|
aaronmk
|
if ! (( i > min )); then break; fi
|
14 |
9546
|
aaronmk
|
if echo_run "$@"; then min="$i"; echo_vars min
|
15 |
9533
|
aaronmk
|
else max="$i"; echo_vars max
|
16 |
|
|
fi
|
17 |
|
|
done
|
18 |
|
|
echo "$i"
|
19 |
|
|
}
|
20 |
|
|
alias binsearch='"binsearch" ' # last space alias-expands next word
|
21 |
|
|
|
22 |
|
|
fi
|