simplify implementation of bsearch to use "until" and avoid "last"; tested
This commit is contained in:
1 parent
227f62e013
commit
5856a2d2c0
1 file changed
+12
-12
@@ -1614,18 +1614,18 @@ static const char* const jq_builtins[] = {
|
||||
" else . as $in"
|
||||
"" // # state variable: [start, end, answer]
|
||||
"" // # where start and end are the upper and lower offsets to use.
|
||||
" | last( [0, length-1, null]"
|
||||
" | while( .[0] <= .[1] ;"
|
||||
" (if .[2] != null then (.[1] = -1)" // # i.e. break
|
||||
" else"
|
||||
" ( ( (.[1] + .[0]) / 2 ) | floor ) as $mid"
|
||||
" | $in[$mid] as $monkey"
|
||||
" | if $monkey == target then (.[2] = $mid)" // # success
|
||||
" elif .[0] == .[1] then (.[1] = -1)" // # failure
|
||||
" elif $monkey < target then (.[0] = ($mid + 1))"
|
||||
" else (.[1] = ($mid - 1))"
|
||||
" end"
|
||||
" end )))"
|
||||
" | [0, length-1, null]"
|
||||
" | until( .[0] > .[1] ;"
|
||||
" if .[2] != null then (.[1] = -1)" // # i.e. break
|
||||
" else"
|
||||
" ( ( (.[1] + .[0]) / 2 ) | floor ) as $mid"
|
||||
" | $in[$mid] as $monkey"
|
||||
" | if $monkey == target then (.[2] = $mid)" // # success
|
||||
" elif .[0] == .[1] then (.[1] = -1)" // # failure
|
||||
" elif $monkey < target then (.[0] = ($mid + 1))"
|
||||
" else (.[1] = ($mid - 1))"
|
||||
" end"
|
||||
" end )"
|
||||
" | if .[2] == null then" // # compute the insertion point
|
||||
" if $in[ .[0] ] < target then (-2 -.[0])"
|
||||
" else (-1 -.[0])"
|
||||
|
||||
Reference in new issue
Block a user