From 7f310a4da7e8d96e2091181b48f3c41b9dc3803d Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 01:22:31 +0000 Subject: [PATCH] ocaml: phase 5.1 bs_bounds.ml baseline (lower/upper bound, fingerprint 3211) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C++-style lower_bound / upper_bound on a sorted array: lower_bound — first index >= x (loop uses arr.(mid) < x) upper_bound — first index > x (loop uses arr.(mid) <= x) a = [|1; 2; 2; 3; 3; 3; 5; 7; 9|] upper(x) - lower(x) gives the count of x in a: cnt3 = 3 cnt2 = 2 cnt5 = 1 cnt9 = 1 cnt4 = 0 fingerprint = 3*1000 + 2*100 + 1*10 + 1 + 0 = 3211 Tests parallel while loops with bisection on ref, mixed strict and non-strict comparison branches, count-via-subtraction idiom. 176 baseline programs total. --- lib/ocaml/baseline/bs_bounds.ml | 27 +++++++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 9 +++++++++ 3 files changed, 37 insertions(+) create mode 100644 lib/ocaml/baseline/bs_bounds.ml diff --git a/lib/ocaml/baseline/bs_bounds.ml b/lib/ocaml/baseline/bs_bounds.ml new file mode 100644 index 00000000..a1885344 --- /dev/null +++ b/lib/ocaml/baseline/bs_bounds.ml @@ -0,0 +1,27 @@ +let lower_bound arr x = + let lo = ref 0 and hi = ref (Array.length arr) in + while !lo < !hi do + let mid = (!lo + !hi) / 2 in + if arr.(mid) < x then lo := mid + 1 + else hi := mid + done; + !lo + +let upper_bound arr x = + let lo = ref 0 and hi = ref (Array.length arr) in + while !lo < !hi do + let mid = (!lo + !hi) / 2 in + if arr.(mid) <= x then lo := mid + 1 + else hi := mid + done; + !lo + +;; + +let a = [| 1; 2; 2; 3; 3; 3; 5; 7; 9 |] in +let cnt3 = upper_bound a 3 - lower_bound a 3 in +let cnt2 = upper_bound a 2 - lower_bound a 2 in +let cnt5 = upper_bound a 5 - lower_bound a 5 in +let cnt9 = upper_bound a 9 - lower_bound a 9 in +let cnt4 = upper_bound a 4 - lower_bound a 4 in +cnt3 * 1000 + cnt2 * 100 + cnt5 * 10 + cnt9 + cnt4 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index e5b67380..a1dc3edf 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -21,6 +21,7 @@ "bfs_grid.ml": 8, "btree.ml": 39, "brainfuck.ml": 75, + "bs_bounds.ml": 3211, "bsearch.ml": 7, "caesar.ml": 215, "calc.ml": 13, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index eaeecb98..83d4d614 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,15 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-11 Phase 5.1 — bs_bounds.ml baseline (lower_bound / + upper_bound binary search; counts of {3,2,5,9,4} in + [1;2;2;3;3;3;5;7;9] encoded as 3211). Standard half-open + variants: lower_bound returns the first index ≥ x (using `<`), + upper_bound returns the first index > x (using `≤`). Counts: + 3→3, 2→2, 5→1, 9→1, 4→0 → 3*1000+2*100+1*10+1+0 = 3211. Tests + parallel while loops with bisection on ref, mixed strict and + non-strict comparisons, computing counts via subtraction. + 176 baseline programs total. - 2026-05-11 Phase 5.1 — distinct_subseq.ml baseline (count distinct subsequences of "rabbbit" that equal "rabbit" = 3). Classic 2D DP: dp[i][j] = dp[i-1][j] + (s[i-1]=t[j-1] ? dp[i-1][j-1]