refactor: move bsearch function to C-code (#2945)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This commit fixes issue #527 and move the bsearch function to a native C-code. This change improves the performance of this function.
authored by Eloy Coto · committed by GitHub · parent 18bd837 · Browse files · 3 files·+46 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+46 added−32 removed
- tests:+11 added−1 removed
- other:+35 added−31 removed
Provenance
- Commit
562ffec- Tree
4379df7- Author
Eloy Coto <eloy.coto@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
18bd837
3 files changed+46 added−32 removed
modified:src/builtin.c+35 added
833 unmodified lines834 return type_error2(input, keys, "cannot be sorted, as they are not both arrays");835 }836 }837 838+/*839+ * Assuming the input array is sorted, bsearch/1 returns840+ * the index of the target if the target is in the input array; and otherwise841+ * (-1 - ix), where ix is the insertion point that would leave the array sorted.842+ * If the input is not sorted, bsearch will terminate but with irrelevant results.843+ */844+static jv f_bsearch(jq_state *jq, jv input, jv target) {845+ if (jv_get_kind(input) != JV_KIND_ARRAY) {846+ jv_free(target);847+ return type_error(input, "cannot be searched from");848+ }849+ int start = 0;850+ int end = jv_array_length(jv_copy(input));851+ jv answer = jv_invalid();852+ while (start < end) {853+ int mid = start + (end - start) / 2;854+ int result = jv_cmp(jv_copy(target), jv_array_get(jv_copy(input), mid));855+ if (result == 0) {856+ answer = jv_number(mid);857+ break;858+ } else if (result < 0) {859+ end = mid;860+ } else {861+ start = mid + 1;862+ }863+ }864+ if (!jv_is_valid(answer)) {865+ answer = jv_number(-1 - start);866+ }867+ jv_free(input);868+ jv_free(target);869+ return answer;870+}871+872 static jv f_group_by_impl(jq_state *jq, jv input, jv keys) {873 if (jv_get_kind(input) == JV_KIND_ARRAY &&874 jv_get_kind(keys) == JV_KIND_ARRAY &&875 jv_array_length(jv_copy(input)) == jv_array_length(jv_copy(keys))) {1001 unmodified lines1877 CFUNC(f_nan, "nan", 1),1878 CFUNC(f_sort, "sort", 1),1879 CFUNC(f_sort_by_impl, "_sort_by_impl", 2),1880 CFUNC(f_group_by_impl, "_group_by_impl", 2),1881+ CFUNC(f_bsearch, "bsearch", 2),1882 CFUNC(f_min, "min", 1),1883 CFUNC(f_max, "max", 1),1884 CFUNC(f_min_by_impl, "_min_by_impl", 2),1885 CFUNC(f_max_by_impl, "_max_by_impl", 2),133 unmodified linesmodified:src/builtin.jq-31 removed
217 unmodified lines218 path(def r: (.[]?|r), .; r) as $p |219 getpath($p) |220 reduce path(.[]?) as $q ([$p, .]; [$p+$q]);221 222-# Assuming the input array is sorted, bsearch/1 returns223-# the index of the target if the target is in the input array; and otherwise224-# (-1 - ix), where ix is the insertion point that would leave the array sorted.225-# If the input is not sorted, bsearch will terminate but with irrelevant results.226-def bsearch($target):227- if length == 0 then -1228- elif length == 1 then229- if $target == .[0] then 0 elif $target < .[0] then -1 else -2 end230- else . as $in231- # state variable: [start, end, answer]232- # where start and end are the upper and lower offsets to use.233- | [0, length-1, null]234- | until( .[0] > .[1] ;235- if .[2] != null then (.[1] = -1) # i.e. break236- else237- ( ( (.[1] + .[0]) / 2 ) | floor ) as $mid238- | $in[$mid] as $monkey239- | if $monkey == $target then (.[2] = $mid) # success240- elif .[0] == .[1] then (.[1] = -1) # failure241- elif $monkey < $target then (.[0] = ($mid + 1))242- else (.[1] = ($mid - 1))243- end244- end )245- | if .[2] == null then # compute the insertion point246- if $in[ .[0] ] < $target then (-2 -.[0])247- else (-1 -.[0])248- end249- else .[2]250- end251- end;252-222 # Apply f to composite entities recursively, and to atoms223 def walk(f):224 def w:225 if type == "object"28 unmodified linesmodified:tests/jq.test+11 added-1 removed
1640 unmodified lines1641 ascii_upcase1642 "useful but not for é"1643 "USEFUL BUT NOT FOR é"1644 1645-bsearch(0,2,4)1645+bsearch(0,1,2,3,4)1646 [1,2,3]1647 -11648+01649 11650+21651 -41652 1653+bsearch({x:1})1654+[{ "x": 0 },{ "x": 1 },{ "x": 2 }]1655+11656+1657+try ["OK", bsearch(0)] catch ["KO",.]1658+"aa"1659+["KO","string (\"aa\") cannot be searched from"]1660+1661 # strptime tests are in optional.test1662 1663 strftime("%Y-%m-%dT%H:%M:%SZ")1664 [2015,2,5,23,51,47,4,63]601 unmodified lines