Project

General

Profile

1
#!/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
		i=$(( (min + max)/2 )); echo_vars i
13
		if ! (( i > min )); then break; fi
14
		if "$@"; then min="$i"; echo_vars min
15
		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
(2-2/6)