diff options
author | pkoppstein <pkoppstein@gmail.com> | 2015-01-03 01:05:04 -0500 |
---|---|---|
committer | David Tolnay <dtolnay@gmail.com> | 2015-07-24 12:29:20 -0700 |
commit | 5856a2d2c0f611c95231c8ef84384a27394ffc0c (patch) | |
tree | c337fd2aaaf48eee2b99984cf5f20cd4fcea9ef3 | |
parent | 227f62e013f7ac7166963618a52e066db925ef06 (diff) |
simplify implementation of bsearch to use "until" and avoid "last"; tested
-rw-r--r-- | builtin.c | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -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])" |