Add a C++ translation of the closed form

- Literal transliteration of `Statement.closedForm` plus an equivalent
  summarised implementation
- Borrowed input keys (`std::string_view` / `std::span`) and owned result
  keys (`std::string`)
- Golden-vector test, differential/safety fuzzers, and a nanobench
  comparison

The literal and summarised implementations agree on 40,000 Lean golden
vectors, 18,971 sorted corpus cases, and the readable fuzzer; the raw fuzzer
runs both implementations under ASan/UBSan.  `build/` and the generated
`oracle/golden.txt` are ignored.
This commit is contained in:
andrew committed 2026-11-17 16:08:07 -05:00
1 parent 6559cc582b
commit e32e42d933
17 files changed
+8871

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
build/
oracle/golden.txt
+59
View File
@@ -0,0 +1,59 @@
cmake_minimum_required(VERSION 3.20)
project(conflict_range_cpp LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
endif()
option(CR_BUILD_FUZZERS "Build libFuzzer differential/safety harnesses" ON)
option(CR_BUILD_BENCH "Build the nanobench comparison" ON)
add_library(cr_literal STATIC src/literal.cpp)
target_include_directories(cr_literal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_library(cr_fast STATIC src/fast.cpp)
target_include_directories(cr_fast PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
# Golden-vector check: the literal translation against vectors produced by
# `lake env lean --run cpp/oracle/gen.lean`.
add_executable(golden_test tests/golden.cpp)
target_link_libraries(golden_test PRIVATE cr_literal)
# Literal vs. fast on Lean-generated cases that satisfy the readable sorting
# invariant.
add_executable(diff_corpus tests/diff_corpus.cpp)
target_link_libraries(diff_corpus PRIVATE cr_literal cr_fast)
if(CR_BUILD_BENCH)
add_executable(cr_bench tests/bench.cpp)
target_include_directories(cr_bench PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/third_party
${CMAKE_CURRENT_SOURCE_DIR}/tests)
target_link_libraries(cr_bench PRIVATE cr_literal cr_fast)
target_compile_definitions(cr_bench PRIVATE NDEBUG)
endif()
if(CR_BUILD_FUZZERS AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CR_FUZZ_FLAGS -fsanitize=fuzzer,address,undefined
-fno-sanitize-recover=all -g -O1)
# Compile the implementations into each fuzz target directly, so that the
# library code itself is instrumented (linking a plain static library would
# only instrument the harness).
add_executable(fuzz_readable tests/fuzz_readable.cpp src/literal.cpp
src/fast.cpp)
target_include_directories(fuzz_readable PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/tests)
target_compile_options(fuzz_readable PRIVATE ${CR_FUZZ_FLAGS})
target_link_options(fuzz_readable PRIVATE -fsanitize=fuzzer,address,undefined)
add_executable(fuzz_raw tests/fuzz_raw.cpp src/literal.cpp src/fast.cpp)
target_include_directories(fuzz_raw PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/tests)
target_compile_options(fuzz_raw PRIVATE ${CR_FUZZ_FLAGS})
target_link_options(fuzz_raw PRIVATE -fsanitize=fuzzer,address,undefined)
endif()
+158
View File
@@ -0,0 +1,158 @@
# C++ translation of `Statement.closedForm`
This directory contains a C++20 translation of `Statement.closedForm` from
`Statement.lean`, an equivalent summarised implementation, a libFuzzer
differential harness, and a nanobench comparison.
The Lean build is untouched.
## Layout
| Path | Purpose |
| --- | --- |
| `src/model.h` | Data model: borrowed input keys (`std::string_view`), owned result keys (`std::string`), `WithTop Key` = `std::optional`, `ℕ∞` = `NatInf`. |
| `src/literal.cpp` | Line-by-line transliteration of `Statement.closedForm`, including its repeated scans. |
| `src/fast.cpp` | The same decision tree, but with a summary of the reported keys and lazily cached cardinalities. |
| `oracle/gen.lean` | Deterministic generator of golden vectors by evaluating the real Lean `closedForm`. |
| `tests/golden.cpp` | Checks the literal translation against the Lean golden vectors. |
| `tests/readable_gen.h` | Readable-by-construction instance generator (owned database and selectors, with borrowed views into them). |
| `tests/fuzz_readable.cpp` | libFuzzer differential test: literal vs. fast on readable instances. |
| `tests/fuzz_raw.cpp` | libFuzzer safety test on arbitrary instances under ASan/UBSan. |
| `tests/bench.cpp` | nanobench literal-vs-fast benchmark. |
| `third_party/nanobench.h` | Vendored nanobench (commit `7252da9d96bd0d71f508e9b2813ab1f57fdfaec1`). |
## Data model
Inputs borrow and results own. `RangeRead` selectors and the reported keys in
`Result` are `std::string_view`s (the reported keys via a `std::span`), so a
caller can point them straight at existing storage and the implementation reads
them in place. `ConflictRange` returns owned `std::string`s because the decision
procedure can synthesise endpoints (`keyAfter`, clamp bounds) that are not
present in the input.
## Build
```sh
cd cpp
cmake -S . -B build -G Ninja -DCMAKE_CXX_COMPILER=clang++-21
cmake --build build
```
## Golden vectors from Lean
```sh
cd ..
lake env lean --run cpp/oracle/gen.lean > cpp/oracle/golden.txt
./cpp/build/golden_test cpp/oracle/golden.txt
# golden: 40000 cases, 0 mismatches
```
The golden test compares **exact endpoints**, because the literal translation
reproduces Lean's interval encoding exactly (including empty intervals).
`diff_corpus` compares the literal and fast implementations on the generated
cases that satisfy the readable sorting invariant:
```sh
./cpp/build/diff_corpus cpp/oracle/golden.txt
```
## Fuzzing
```sh
cd cpp
mkdir -p /tmp/cr-corpus
./build/fuzz_readable -max_total_time=300 /tmp/cr-corpus
./build/fuzz_raw -max_total_time=300 /tmp/cr-corpus-raw
```
or simply `./cpp/run-fuzz.sh` (optionally `./cpp/run-fuzz.sh build 600`).
The CMake fuzz targets compile the implementation sources directly so that the
decoder code, not just the harness, is covered by ASan/UBSan.
`fuzz_readable` builds a sorted, duplicate-free database, resolves both
selectors against it, and reports either the complete traversal or a nonempty
prefix. This makes every generated pair genuinely `Readable`. The differential
check treats all encodings of the empty interval as equivalent and otherwise
requires exact endpoints.
`fuzz_raw` only checks memory safety and undefined behaviour for arbitrary
(possibly unreadable) inputs.
One representative validation run on this machine:
| check | cases / runs | result |
| --- | ---: | --- |
| literal vs. Lean golden vectors | 40,000 | 0 mismatches (exact endpoints) |
| fast vs. literal on sorted corpus | 18,971 | 0 mismatches |
| readable differential fuzzing | 2,310,641 | 0 mismatches |
| raw safety fuzzing | 1,936,245 | no ASan/UBSan findings |
## Benchmark
```sh
./cpp/build/cr_bench
```
Representative results (Clang 21, `-O3 -DNDEBUG`, medians of nine runs pinned
to one core; relative to the literal translation, lower is better for `fast`).
CPU frequency scaling makes the absolute numbers noisy, but the shape is stable:
| case | literal | fast | fast/literal |
| --- | ---: | ---: | ---: |
| small complete (8 keys) | 41.1 ns | 16.6 ns | 0.40x |
| small truncated forward | 16.9 ns | 23.1 ns | 1.37x |
| crossed truncated forward | 23.9 ns | 24.4 ns | 1.02x |
| crossed truncated reverse | 24.0 ns | 24.4 ns | 1.02x |
| long complete forward (512 keys) | 2093 ns | 31.9 ns | 0.02x |
| long truncated forward | 14.8 ns | 19.8 ns | 1.34x |
| long truncated reverse | 10.4 ns | 14.2 ns | 1.36x |
The large win is on complete reads: the literal translation rescans the reported
keys many times (`residualT`, `reportedBelowBegin`, `allBelowEnd`, ...), while
the summary answers those questions in `O(log n)` or from a cached count.
Truncated and crossed cases are dominated by the shared decision tree, so the
two implementations are close; the literal path is slightly cheaper there
because it performs no summary setup.
Against the same implementation with owned input keys (the previous model), the
borrowed-key model changes the costs as follows (nine interleaved runs each,
median):
| case | literal (borrow vs own) | fast (borrow vs own) |
| --- | ---: | ---: |
| small complete | -64% | -17% |
| small truncated forward | -33% | -19% |
| crossed truncated forward | +4% | +2% |
| crossed truncated reverse | +4% | +2% |
| long complete forward | -69% | -15% |
| long truncated forward | -29% | -6% |
| long truncated reverse | -16% | +16% |
The complete-read gains come from not copying the reported keys just to
normalise the result: a forward result is now the caller's span, and a reverse
result is a reversed view rather than an owned copy. The fast path also benefits
because it previously normalised complete results the same way. Truncated
reverse (`fast`) is the one small regression: it is two nanoseconds and a few
dozen instructions, and the other paths move in the opposite direction, so it
reads as code-layout/code-generation noise rather than extra algorithmic work.
## Why the fast implementation is equivalent
The fast implementation replays exactly the same decision tree. The only
changes are:
* `reportedBelowBegin` and `residualT` are summarised once. On readable inputs the
reported keys are sorted in traversal order, so both counts are binary
searches. The summaries depend only on the read and on the multiset of
reported keys, so they survive the reversed-view normalisation.
* Interval cardinalities (`intervalCard`) are cached lazily.
* Complete observations are not copied: a forward result borrows the caller's
span, and a reversed result is viewed backwards in place. (The previous
owned-key model copied and reversed the reported keys here.)
* `allBelowEnd` is `residualT == 0` and `anyAtOrAboveEnd` is `residualT != 0`.
No branch condition or returned endpoint is otherwise changed. Equivalence is
therefore only claimed for readable inputs; `fuzz_raw` deliberately does not
compare outputs on arbitrary inputs.
+122
View File
@@ -0,0 +1,122 @@
import Statement
/-!
Deterministic golden-vector generator for the C++ translation of
`Statement.closedForm`.
Run with:
lake env lean --run cpp/oracle/gen.lean > cpp/oracle/golden.txt
Each output line is
<beginKey> <beginOffset> <endKey> <endOffset> <reverse> <more> <n> <key>... => <outBegin> <outEnd>
where a key is lowercase hex, `-` is the empty key, and `T` is `⊤`.
-/
open Statement
def hexDigit (n : Nat) : Char :=
if n < 10 then Char.ofNat (48 + n) else Char.ofNat (87 + n)
def hexByte (b : Byte) : String :=
let n := b.val
String.ofList [hexDigit (n / 16), hexDigit (n % 16)]
def encKey : WithTop Key → String
| none => "T"
| some k => if k.isEmpty then "-" else String.join (k.map hexByte)
def encRes (res : Result) : String :=
let more := if res.more then "1" else "0"
let ks := res.keys.map (fun k => if k.isEmpty then "-" else String.join (k.map hexByte))
more ++ " " ++ toString res.keys.length ++ " " ++ String.intercalate " " ks
def encCase (r : RangeRead) (res : Result) : String :=
let out := closedForm r res
encKey r.begin.key ++ " " ++ toString r.begin.offset ++ " " ++
encKey r.end_.key ++ " " ++ toString r.end_.offset ++ " " ++
(if r.reverse then "1" else "0") ++ " " ++ encRes res ++ " => " ++
encKey out.begin ++ " " ++ encKey out.end_
def key (xs : List Nat) : Key := xs.map (fun n => Fin.ofNat 256 n)
/-- Keys chosen to exercise prefixes, zero bytes, ordering, and the empty key. -/
def keyUniverse : List Key :=
[ key [], key [0], key [0, 0], key [0, 1], key [0, 2], key [1], key [1, 0],
key [1, 1], key [2], key [0, 0, 0], key [255], key [0, 1, 0], key [3, 0] ]
def keyChoices : List (WithTop Key) :=
(⊤ : WithTop Key) :: keyUniverse.map fun k => (k : WithTop Key)
def offsets : List Int :=
[-256, -10, -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 10, 100, 255, 256]
def nth {α : Type} (l : List α) (i : Nat) (d : α) : α :=
l.getD (i % l.length) d
def step (s : Nat) : Nat := (s * 1103515245 + 12345) % 2147483648
/-- A weighted byte: the boundary values are common, the rest is uniform. -/
def weightedByte (s : Nat) : Byte :=
let r := step s % 256
if r < 24 then 0
else if r < 40 then 1
else if r < 56 then 2
else if r < 72 then 255
else Fin.ofNat 256 r
/-- A random key of length `0..8` over all 256 byte values. -/
def randomKey (s : Nat) : Key :=
let len := s % 9
(List.range len).map fun i => weightedByte (s + i)
structure Gen where
r : RangeRead
res : Result
def mkCase (s : Nat) : Gen :=
let s1 := step s
let s2 := step s1
let s3 := step s2
let s4 := step s3
let s5 := step s4
let s6 := step s5
let s7 := step s6
let r : RangeRead :=
{ begin :=
{ key := nth keyChoices s1 (⊤ : WithTop Key)
offset := nth offsets s2 0 }
end_ :=
{ key := nth keyChoices s3 (⊤ : WithTop Key)
offset := nth offsets s4 0 }
reverse := s5 % 2 = 1 }
let mode := s6 % 6
let resKeys : List Key :=
match mode with
| 0 => []
| 1 => [randomKey s7]
| 2 =>
let start := s7 % keyUniverse.length
(keyUniverse.drop start).take (1 + (step s7) % 4)
| 3 =>
let start := s7 % keyUniverse.length
((keyUniverse.drop start).take (1 + (step s7) % 4)).reverse
| 4 =>
[randomKey s7, randomKey (step s7), randomKey (step (step s7))]
| _ =>
let a := randomKey s7
[a, a, randomKey (step s7)]
let more := s6 % 3 = 0
{ r := r, res := { keys := resKeys, more := more } }
def main : IO Unit := do
let n := 40000
let mut s := 1234567
let mut out : Array String := #[]
for _ in List.range n do
s := step s
let g := mkCase s
out := out.push (encCase g.r g.res)
IO.println (String.intercalate "\n" out.toList)
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
# Build (if needed) and run the differential and safety fuzzers.
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
build="${1:-$here/build}"
seconds="${2:-300}"
if [[ ! -x "$build/fuzz_readable" ]]; then
cmake -S "$here" -B "$build" -G Ninja -DCMAKE_CXX_COMPILER=clang++-21
cmake --build "$build" --target fuzz_readable fuzz_raw
fi
corpus="${TMPDIR:-/tmp}/cr-fuzz-corpus"
mkdir -p "$corpus/readable" "$corpus/raw"
echo "== differential fuzzing (readable instances) =="
"$build/fuzz_readable" -max_total_time="$seconds" -max_len=1024 "$corpus/readable"
echo "== safety fuzzing (arbitrary instances) =="
"$build/fuzz_raw" -max_total_time="$seconds" -max_len=1024 "$corpus/raw"
+1077
View File
File diff suppressed because it is too large. Load diff
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include "model.h"
namespace cr::fast {
// A summarised implementation of the same decision procedure as
// `Statement.closedForm`. It is equivalent on readable inputs (and in fact
// reproduces the same endpoints), but computes the result statistics once and
// avoids the literal translation's repeated scans and allocations.
ConflictRange closedForm(const RangeRead& r, const Result& res);
} // namespace cr::fast
+960
View File
@@ -0,0 +1,960 @@
// A deliberately literal transliteration of `Statement.closedForm` from
// Statement.lean. Every helper mirrors the Lean definition of the same name,
// including repeated scans of `res.keys`; correctness of the translation, not
// speed, is the goal of this file.
#include "literal.h"
#include <algorithm>
#include <utility>
namespace cr::literal {
namespace {
using cr::afterFirst;
using cr::afterLast;
using cr::asKey;
using cr::beginSlack;
using cr::bottomKey;
using cr::clampNonposD;
using cr::first;
using cr::firstKey;
using cr::intervalCard;
using cr::isBottom;
using cr::isTop;
using cr::keyAfter;
using cr::keyAfterN;
using cr::keyLt;
using cr::last;
using cr::lastKey;
using cr::leastReported;
using cr::leastReportedOpt;
using cr::natSub;
using cr::niAdd;
using cr::niEq;
using cr::niLe;
using cr::niLt;
using cr::niSub;
using cr::optGe;
using cr::optGt;
using cr::optLe;
using cr::optLt;
using cr::optMax;
using cr::optMin;
using cr::reportedBelowBegin;
using cr::residualB;
using cr::residualE;
using cr::residualT;
using cr::toNat;
using cr::topKey;
template <class B, class E>
ConflictRange mkRange(B&& b, E&& e) {
return ConflictRange{ownKey(std::forward<B>(b)), ownKey(std::forward<E>(e))};
}
// ---------------------------------------------------------------------------
// Arithmetic helpers
// ---------------------------------------------------------------------------
NatInf clampCap(const RangeRead& r) { return intervalCard(r.end_.key, r.begin.key); }
std::uint64_t clampSlack(const RangeRead& r, const Result& res) {
return natSub(beginSlack(r) + 1, reportedBelowBegin(r, res));
}
OptKey clampEndOneUpper(const RangeRead& r, const Result& res) {
NatInf slack = NatInf::of(clampSlack(r, res));
NatInf cap = clampCap(r);
if (niLe(slack, cap) && (niLt(slack, cap) || first(res) != bottomKeyView()))
return ownKey(r.begin.key);
return ownKey(r.end_.key);
}
NatInf clampHighCap(const RangeRead& r, const Result& res) {
return intervalCard(afterLast(res), r.begin.key);
}
std::uint64_t clampHighThresh(const RangeRead& r, const Result& res) {
return clampSlack(r, res) + (isBottom(r.end_.key) ? 1 : 0);
}
OptKey clampHighU(const RangeRead& r, const Result& res) {
if (niLe(NatInf::of(clampHighThresh(r, res)), clampHighCap(r, res)))
return ownKey(r.begin.key);
return afterLast(res);
}
std::uint64_t clampLongC(const RangeRead& r, const Result& res) {
return natSub(natSub(beginSlack(r), reportedBelowBegin(r, res)), clampNonposD(r));
}
OptKey clampLongUpper(const RangeRead& r, const Result& res) {
NatInf cap = clampCap(r);
NatInf c = NatInf::of(clampLongC(r, res));
if (niLt(cap, c) || (niEq(cap, c) && first(res) == bottomKeyView()))
return ownKey(r.end_.key);
return ownKey(r.begin.key);
}
bool clampNonposBottom(const RangeRead& r, const Result& res) {
NatInf lhs = niAdd(NatInf::of(reportedBelowBegin(r, res)),
niSub(NatInf::of(clampNonposD(r) + 1),
intervalCard(r.begin.key, r.end_.key)));
return niLe(lhs, NatInf::of(beginSlack(r)));
}
bool clampedNonpos(const RangeRead& r, const Result& res) {
return r.begin.offset <= 0 &&
optLe(r.begin.key, leastReportedOpt(r, res));
}
bool clampsToBottom(const RangeRead& r, const Result& res) {
return r.begin.offset <= 0 && optLe(r.begin.key, first(res));
}
std::uint64_t cELo(Int oB, Int oE) {
return std::max(toNat(static_cast<__int128>(2) - oE),
toNat(static_cast<__int128>(oB) - oE + 1));
}
std::uint64_t cEminP(Int oE) {
return toNat(static_cast<__int128>(2) - oE);
}
std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) {
if (KBE.inf) return 0;
std::uint64_t lo = cELo(oB, oE);
return lo > KBE.n ? lo - KBE.n : 0;
}
std::uint64_t residualD(const RangeRead& r, NatInf g) {
if (g.inf) return 0;
return natSub(residualB(r), g.n);
}
std::uint64_t closedFormDeficit(const RangeRead& r) {
return residualD(r, intervalCard(r.begin.key, r.end_.key));
}
bool allBelowEnd(const RangeRead& r, const Result& res) {
for (KeyView k : res.keys)
if (!optLt(asKey(k), r.end_.key)) return false;
return true;
}
bool anyAtOrAboveEnd(const RangeRead& r, const Result& res) {
for (KeyView k : res.keys)
if (optLe(r.end_.key, asKey(k))) return true;
return false;
}
std::uint64_t gapOvershoot(const RangeRead& r, const Result& res) {
return natSub(residualE(r), residualT(r, res));
}
std::uint64_t crossedOvershootGeneral(const RangeRead& r, const Result& res) {
return natSub(natSub(residualE(r), residualT(r, res)), residualB(r));
}
OptKeyView crossedBegin(const RangeRead& r, const Result& res) {
if (niLe(NatInf::of(crossedOvershootGeneral(r, res)),
intervalCard(r.end_.key, r.begin.key)))
return r.end_.key;
return r.begin.key;
}
// ---------------------------------------------------------------------------
// Crossed complete cases
// ---------------------------------------------------------------------------
bool negCrossClampEndOne(const RangeRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset == 1 && optLt(first(res), r.begin.key) &&
reportedBelowBegin(r, res) <= beginSlack(r);
}
bool negCrossHeadBottom(const RangeRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset <= 0 && optLe(r.begin.key, r.end_.key) &&
first(res) == bottomKeyView();
}
bool negCrossClampNonposLastGe(const RangeRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset <= 0 && optLt(first(res), r.begin.key) &&
optLe(r.begin.key, last(res)) &&
reportedBelowBegin(r, res) <= beginSlack(r);
}
bool negCrossClampNonposBottom(const RangeRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset <= 0 && optLe(r.begin.key, r.end_.key) &&
optLt(first(res), r.begin.key) && clampNonposBottom(r, res);
}
bool negCrossClampLongNonpos(const RangeRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset <= 0 && optLt(first(res), r.begin.key) &&
reportedBelowBegin(r, res) <= beginSlack(r) &&
clampNonposBottom(r, res) && optLt(r.end_.key, r.begin.key);
}
ConflictRange closedFormNegCrossClampUnified(const RangeRead& r, const Result& res) {
OptKey end;
if (negCrossClampEndOne(r, res))
end = clampEndOneUpper(r, res);
else if (negCrossClampLongNonpos(r, res) &&
!negCrossClampNonposLastGe(r, res))
end = clampLongUpper(r, res);
else
end = ownKey(r.end_.key);
return mkRange(bottomKey(), std::move(end));
}
OptKey negCrossCompleteHighM(const RangeRead& r, const Result& res) {
return optMax(r.begin.key, afterLast(res));
}
std::uint64_t negCrossHighD(const RangeRead& r, const Result& res) {
return natSub(residualE(r), res.keys.size());
}
NatInf negCrossHighGapCap(const RangeRead& r, const Result& res) {
return intervalCard(r.end_.key, first(res));
}
bool negCrossHighUsesEnd(const RangeRead& r, const Result& res) {
NatInf d = NatInf::of(negCrossHighD(r, res));
NatInf gap = negCrossHighGapCap(r, res);
return niLt(d, gap) || (niLe(d, gap) && optLt(r.begin.key, topKey()));
}
bool negCrossCompleteHigh(const RangeRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset >= 2 && optLt(first(res), r.begin.key) &&
optLt(r.end_.key, first(res));
}
ConflictRange closedFormNegCrossCompleteHigh(const RangeRead& r, const Result& res) {
if (reportedBelowBegin(r, res) <= beginSlack(r)) {
if (res.keys.size() == residualE(r)) return mkRange(bottomKey(), clampHighU(r, res));
return mkRange(bottomKey(), topKey());
}
if (res.keys.size() == residualE(r))
return mkRange(r.end_.key, negCrossCompleteHighM(r, res));
if (negCrossHighUsesEnd(r, res)) return mkRange(r.end_.key, topKey());
return mkRange(first(res), topKey());
}
ConflictRange topOfLast(KeyView k) { return mkRange(asKey(k), topKey()); }
ConflictRange closedFormNegCrossOvershoot(const RangeRead& r, const Result& res) {
if (reportedBelowBegin(r, res) <= beginSlack(r))
return mkRange(bottomKey(), topKey());
return topOfLast(firstKey(res));
}
ConflictRange closedFormNegCrossEndOne(const RangeRead& r, const Result& res) {
return mkRange(first(res), r.end_.key);
}
ConflictRange closedFormNegCrossEndOneLong(const RangeRead& r, const Result& res) {
return mkRange(first(res), r.begin.key);
}
ConflictRange closedFormNegCrossCompleteResidual(const RangeRead& r, const Result& res) {
OptKey reducedEnd = afterLast(res);
RangeRead rp = r;
rp.end_.key = optView(reducedEnd);
rp.end_.offset = r.end_.offset - static_cast<Int>(residualT(r, res));
if (rp.end_.offset >= 2)
return closedFormNegCrossOvershoot(rp, res);
if (reportedBelowBegin(rp, res) <= beginSlack(rp))
return mkRange(bottomKey(), clampEndOneUpper(rp, res));
if (optLe(r.begin.key, rp.end_.key))
return closedFormNegCrossEndOne(rp, res);
return closedFormNegCrossEndOneLong(rp, res);
}
ConflictRange closedFormNegCrossCompleteHighResidual(const RangeRead& r, const Result& res) {
if (negCrossCompleteHigh(r, res))
return closedFormNegCrossCompleteHigh(r, res);
return closedFormNegCrossCompleteResidual(r, res);
}
ConflictRange closedFormNegCrossNonclampUnified(const RangeRead& r, const Result& res) {
return mkRange(first(res), optMax(r.end_.key, r.begin.key));
}
ConflictRange closedFormNegCrossTopUnified(const RangeRead& r, const Result& res) {
OptKeyView begin = reportedBelowBegin(r, res) <= beginSlack(r)
? bottomKeyView()
: leastReportedOpt(r, res);
return mkRange(std::move(begin), topKey());
}
// ---------------------------------------------------------------------------
// Crossed truncated cases
// ---------------------------------------------------------------------------
bool negCrossTruncNonposGap(const RangeRead& r, const Result& res) {
return res.more && !r.reverse && r.begin.offset < 0 &&
optLt(leastReportedOpt(r, res), r.begin.key) &&
r.end_.offset <= 0 &&
reportedBelowBegin(r, res) <= beginSlack(r) &&
optLt(last(res), r.begin.key) &&
optLt(last(res), r.end_.key) &&
!isTop(r.end_.key) &&
(optLt(r.end_.key, r.begin.key) ||
niLt(intervalCard(r.begin.key, r.end_.key),
NatInf::of(clampNonposD(r) + 1)));
}
bool negCrossTruncNonposGapA2aBottom(const RangeRead& r, const Result& res) {
return negCrossTruncNonposGap(r, res) && optLt(r.end_.key, r.begin.key) &&
clampNonposBottom(r, res);
}
bool negCrossTruncNonposGapA2aBottomHigh(const RangeRead& r, const Result& res) {
std::uint64_t extra = isBottom(first(res)) ? 1 : 0;
NatInf lhs = NatInf::of(beginSlack(r) + 1 + extra);
NatInf rhs = niAdd(NatInf::of(res.keys.size()),
intervalCard(afterLast(res), r.begin.key));
return negCrossTruncNonposGapA2aBottom(r, res) && niLe(lhs, rhs);
}
ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const RangeRead& r,
const Result& res) {
if (negCrossTruncNonposGapA2aBottomHigh(r, res))
return mkRange(bottomKey(), r.begin.key);
return mkRange(bottomKey(), r.end_.key);
}
bool finiteUnderCond(const RangeRead& r, const Result& res) {
NatInf lhs = niAdd(NatInf::of(natSub(reportedBelowBegin(r, res), 1)),
intervalCard(afterFirst(res), r.begin.key));
return niLe(lhs, NatInf::of(beginSlack(r)));
}
bool negCrossTruncRevResidualFiniteOver(const RangeRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 && r.end_.offset >= 2 &&
optLt(r.end_.key, last(res)) && optLt(last(res), r.begin.key) &&
residualT(r, res) == residualE(r);
}
ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const RangeRead& r,
const Result& res) {
OptKeyView L = last(res);
OptKey U = finiteUnderCond(r, res) ? afterFirst(res) : ownKey(r.begin.key);
if (negCrossTruncRevResidualFiniteOver(r, res))
return mkRange(r.end_.key, U);
return mkRange(std::move(L), std::move(U));
}
bool sentinelLargeTight(const RangeRead& r, const Result& res) {
NatInf lhs = NatInf::of(residualE(r));
NatInf rhs = niAdd(intervalCard(r.end_.key, last(res)),
NatInf::of(res.keys.size()));
__int128 nPlusBegin = static_cast<__int128>(res.keys.size()) + r.begin.offset;
return niEq(lhs, rhs) && nPlusBegin == 1;
}
bool negCrossTruncRevResidualTopLarge(const RangeRead& r, const Result& res) {
if (!(res.more && r.reverse && r.begin.offset < 0 && r.end_.offset >= 2 &&
optLt(r.end_.key, last(res)) && residualT(r, res) < residualE(r)))
return false;
NatInf lhs = NatInf::of(residualE(r));
NatInf rhs = niAdd(intervalCard(r.end_.key, last(res)),
NatInf::of(res.keys.size()));
return niLe(lhs, rhs);
}
bool negCrossTruncRevResidualTopLargeSentinel(const RangeRead& r, const Result& res) {
return negCrossTruncRevResidualTopLarge(r, res) && isTop(r.begin.key);
}
ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const RangeRead& r,
const Result& res) {
OptKeyView L = last(res);
if (negCrossTruncRevResidualTopLargeSentinel(r, res) &&
sentinelLargeTight(r, res))
return mkRange(std::move(L), topKey());
return mkRange(r.end_.key, topKey());
}
ConflictRange truncatedRangeRev(const RangeRead& r, KeyView lastReported) {
return mkRange(asKey(lastReported), r.end_.key);
}
// ---------------------------------------------------------------------------
// Complete and offset-one cases
// ---------------------------------------------------------------------------
ConflictRange closedFormReverseTruncatedZero(const RangeRead& r, const Result& res) {
if (r.end_.offset <= 1) return truncatedRangeRev(r, lastKey(res));
OptKeyView L = last(res);
if (residualT(r, res) == residualE(r)) {
return mkRange(optMin(L, r.end_.key), afterFirst(res));
}
if (isTop(r.end_.key)) return mkRange(std::move(L), topKey());
KeyView E = *r.end_.key;
std::uint64_t n = natSub(natSub(residualE(r), residualT(r, res)), 1);
if (keyLt(keyAfterN(n, E), lastKey(res)))
return mkRange(asKey(E), topKey());
return mkRange(std::move(L), topKey());
}
ConflictRange truncatedRange(const RangeRead& r, KeyView lastReported) {
return mkRange(r.begin.key, asKey(keyAfter(lastReported)));
}
ConflictRange truncatedForwardClosed(const RangeRead& r, const Result& res) {
NatInf cap = intervalCard(r.end_.key, r.begin.key);
NatInf room = NatInf::of(natSub(residualE(r), res.keys.size()));
if (niLe(cap, room)) return truncatedRange(r, lastKey(res));
return mkRange(r.end_.key, afterLast(res));
}
ConflictRange completeClosedWith(const OptKey& endKey, const RangeRead& r,
const Result& res) {
OptKeyView begin = r.begin.key;
if (optLt(r.end_.key, r.begin.key) &&
niLe(NatInf::of(gapOvershoot(r, res)),
intervalCard(r.end_.key, r.begin.key)))
begin = r.end_.key;
if (r.end_.offset <= 1)
return mkRange(r.begin.key, r.end_.key);
if (residualT(r, res) == residualE(r))
return mkRange(std::move(begin), endKey);
return mkRange(std::move(begin), topKey());
}
ConflictRange completeReversedClosed(const RangeRead& r, const Result& res) {
return completeClosedWith(afterFirst(res), r, res);
}
ConflictRange completeForwardClosed(const RangeRead& r, const Result& res) {
return completeClosedWith(afterLast(res), r, res);
}
ConflictRange completeClosed(const RangeRead& r, const Result& res) {
return r.reverse ? completeReversedClosed(r, res) : completeForwardClosed(r, res);
}
ConflictRange closedFormOffsetOneTruncated(const RangeRead& r, const Result& res) {
if (r.end_.offset <= 0) return mkRange(r.begin.key, r.end_.key);
return truncatedRange(r, lastKey(res));
}
ConflictRange topRange(const RangeRead& r) { return mkRange(r.begin.key, topKey()); }
ConflictRange closedFormOffsetOneWith(KeyView endKey, const RangeRead& r,
const Result& res) {
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
if (residualT(r, res) == residualE(r)) return truncatedRange(r, endKey);
return topRange(r);
}
ConflictRange closedFormOffsetOneRev(const RangeRead& r, const Result& res) {
return closedFormOffsetOneWith(firstKey(res), r, res);
}
ConflictRange closedFormOffsetOne(const RangeRead& r, const Result& res) {
return closedFormOffsetOneWith(lastKey(res), r, res);
}
ConflictRange closedFormOffsetOneAny(const RangeRead& r, const Result& res) {
return r.reverse ? closedFormOffsetOneRev(r, res) : closedFormOffsetOne(r, res);
}
ConflictRange closedFormOffsetOneUniversal(const RangeRead& r, const Result& res) {
if (res.more)
return r.reverse ? closedFormReverseTruncatedZero(r, res)
: closedFormOffsetOneTruncated(r, res);
return closedFormOffsetOneAny(r, res);
}
ConflictRange closedFormOffsetOneTotal(const RangeRead& r, const Result& res) {
if (optLt(r.end_.key, r.begin.key)) {
if (res.more)
return r.reverse ? closedFormReverseTruncatedZero(r, res)
: truncatedForwardClosed(r, res);
return completeClosed(r, res);
}
return closedFormOffsetOneUniversal(r, res);
}
ConflictRange closedFormZeroCrossEndOneLong(const RangeRead& r, const Result& res) {
return mkRange(leastReportedOpt(r, res), r.begin.key);
}
// ---------------------------------------------------------------------------
// Zero-crossed cases
// ---------------------------------------------------------------------------
ConflictRange closedFormZeroCrossEnd(const RangeRead& r, const Result& res) {
return mkRange(leastReportedOpt(r, res), r.end_.key);
}
ConflictRange completeForwardCrossedGeneral(const RangeRead& r, const Result& res) {
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
if (residualT(r, res) + residualB(r) == residualE(r))
return mkRange(r.end_.key, afterLast(res));
return mkRange(crossedBegin(r, res), topKey());
}
RangeRead zeroBeginAnchor(const RangeRead& r, const Result& res) {
RangeRead out = r;
out.begin.key = first(res);
out.begin.offset = 1;
return out;
}
ConflictRange zeroCrossLongAnchor(const RangeRead& r, const Result& res) {
if (optLt(r.end_.key, first(res)))
return completeForwardCrossedGeneral(zeroBeginAnchor(r, res), res);
return closedFormOffsetOne(zeroBeginAnchor(r, res), res);
}
ConflictRange closedFormZeroCrossLong(const RangeRead& r, const Result& res) {
if (isTop(r.begin.key)) {
OptKeyView H = first(res);
std::uint64_t e = residualE(r);
std::uint64_t t = residualT(r, res);
NatInf g = intervalCard(r.end_.key, H);
bool useEnd = optLt(r.end_.key, H) &&
niLt(niSub(NatInf::of(e), NatInf::of(t)), g);
return mkRange(useEnd ? r.end_.key : H, topKey());
}
ConflictRange a = zeroCrossLongAnchor(r, res);
return mkRange(a.begin, optMax(a.end_, r.begin.key));
}
ConflictRange closedFormZeroCrossUnderEnd(const RangeRead& r, const Result& res) {
return closedFormOffsetOneTotal(zeroBeginAnchor(r, res), res);
}
ConflictRange closedFormZeroCrossComplete(const RangeRead& r, const Result& res) {
if (r.end_.offset <= 0) return closedFormZeroCrossEnd(r, res);
if (r.end_.offset == 1) {
if (optLt(r.end_.key, r.begin.key))
return closedFormZeroCrossEndOneLong(r, res);
return closedFormZeroCrossEnd(r, res);
}
if (isTop(r.begin.key) || optLt(r.end_.key, r.begin.key))
return closedFormZeroCrossLong(r, res);
return closedFormZeroCrossUnderEnd(r, res);
}
ConflictRange closedFormZeroCrossCompleteRev(const RangeRead& r, const Result& res) {
std::vector<KeyView> rev = reversedKeys(res);
return closedFormZeroCrossComplete(forwardRead(r), Result{rev, false});
}
bool zeroCrossUnderEnd(const RangeRead& r, const Result& res) {
return r.begin.offset == 0 && !res.more && !r.reverse &&
r.end_.offset >= 2 && optLt(leastReportedOpt(r, res), r.begin.key) &&
optLe(r.begin.key, r.end_.key);
}
ConflictRange closedFormZeroCrossResidual(const RangeRead& r, const Result& res) {
if (zeroCrossUnderEnd(r, res)) return closedFormZeroCrossUnderEnd(r, res);
return closedFormZeroCrossLong(r, res);
}
ConflictRange closedFormZeroCrossTruncRevCrossed(const RangeRead& r, const Result& res) {
Result complete = res;
complete.more = false;
return closedFormZeroCrossCompleteRev(r, complete);
}
// ---------------------------------------------------------------------------
// Empty result
// ---------------------------------------------------------------------------
bool AlwaysEmpty(const RangeRead& r) {
NatInf gap = intervalCard(r.begin.key, r.end_.key);
std::uint64_t b = noPinB(gap, r.begin.offset, r.end_.offset);
std::uint64_t e = cELo(r.begin.offset, r.end_.offset);
std::uint64_t cE = cEminP(r.end_.offset);
NatInf underBegin = intervalCard(bottomKey(), r.begin.key);
NatInf underEnd = intervalCard(bottomKey(), r.end_.key);
bool inner;
if (isTop(r.begin.key)) {
if (isTop(r.end_.key))
inner = r.begin.offset <= 0 && r.begin.offset < r.end_.offset;
else
inner = r.begin.offset <= 0 && r.begin.offset < r.end_.offset &&
niLe(NatInf::of(cE), underEnd);
} else if (isTop(r.end_.key)) {
inner = true;
} else {
bool firstFour =
niLe(NatInf::of(b), underBegin) &&
niLe(NatInf::of(e), underEnd) &&
niLe(NatInf::of(natSub(b, e)), intervalCard(r.end_.key, r.begin.key)) &&
niLe(NatInf::of(natSub(e, b)), intervalCard(r.begin.key, r.end_.key));
__int128 ePlusEnd = static_cast<__int128>(e) + r.end_.offset - 1;
__int128 bPlusBegin = static_cast<__int128>(b) + r.begin.offset - 1;
inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd;
}
return !inner;
}
ConflictRange emptyRange(const RangeRead& r) {
return mkRange(r.begin.key, r.begin.key);
}
ConflictRange crossedRangeGeneral(const RangeRead& r) {
NatInf diff = niSub(NatInf::of(residualE(r)), NatInf::of(residualB(r)));
if (niLe(diff, intervalCard(r.end_.key, r.begin.key)))
return mkRange(r.end_.key, topKey());
return mkRange(r.begin.key, topKey());
}
ConflictRange emptyValue(const RangeRead& r) {
if (AlwaysEmpty(r)) return emptyRange(r);
if (r.begin.offset <= 0) {
if (r.end_.offset >= 2) return mkRange(bottomKey(), topKey());
if (niLt(intervalCard(r.end_.key, r.begin.key),
NatInf::of(toNat(static_cast<__int128>(r.end_.offset) -
r.begin.offset))))
return mkRange(bottomKey(), r.end_.key);
return mkRange(bottomKey(), optMax(r.begin.key, r.end_.key));
}
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
if (optLe(r.begin.key, r.end_.key)) return topRange(r);
return crossedRangeGeneral(r);
}
// ---------------------------------------------------------------------------
// Complete and forward-truncated results
// ---------------------------------------------------------------------------
OptKeyView closedFormBegin(const RangeRead& r, const Result& res) {
return clampsToBottom(r, res) ? bottomKeyView() : r.begin.key;
}
bool closedFormFinite(const RangeRead& r, const Result& res) {
return closedFormDeficit(r) + residualT(r, res) == residualE(r);
}
ConflictRange closedFormCompleteForward(const RangeRead& r, const Result& res) {
OptKeyView begin = closedFormBegin(r, res);
if (r.end_.offset <= 1) return mkRange(begin, r.end_.key);
if (clampsToBottom(r, res) || optLe(r.begin.key, r.end_.key)) {
if (residualT(r, res) == residualE(r))
return mkRange(begin, afterLast(res));
if (allBelowEnd(r, res)) return mkRange(begin, topKey());
if (closedFormFinite(r, res)) return mkRange(begin, afterLast(res));
return mkRange(begin, topKey());
}
if (closedFormFinite(r, res)) return mkRange(r.end_.key, afterLast(res));
return mkRange(crossedBegin(r, res), topKey());
}
ConflictRange closedFormComplete(const RangeRead& r, const Result& res) {
if (!r.reverse) return closedFormCompleteForward(forwardRead(r), res);
std::vector<KeyView> rev = reversedKeys(res);
return closedFormCompleteForward(forwardRead(r), Result{rev, false});
}
NatInf closedFormForwardCapacity(const RangeRead& r, const Result& res) {
if (clampsToBottom(r, res)) return NatInf::of(0);
return intervalCard(r.end_.key, r.begin.key);
}
ConflictRange closedFormForwardTruncated(const RangeRead& r, const Result& res) {
OptKeyView begin = closedFormBegin(r, res);
if (r.end_.offset <= 0) return mkRange(begin, r.end_.key);
std::uint64_t room = natSub(natSub(residualE(r), res.keys.size()), residualB(r));
if (niLe(closedFormForwardCapacity(r, res), NatInf::of(room)))
return mkRange(begin, afterLast(res));
return mkRange(r.end_.key, afterLast(res));
}
// ---------------------------------------------------------------------------
// Reversed truncation
// ---------------------------------------------------------------------------
std::uint64_t closedFormReverseCount(const RangeRead& r, const Result& res) {
if (optLt(r.end_.key, r.begin.key))
return static_cast<std::uint64_t>(res.keys.size());
return residualT(r, res);
}
bool closedFormReverseFinite(const RangeRead& r, const Result& res) {
return closedFormDeficit(r) + closedFormReverseCount(r, res) == residualE(r);
}
bool closedFormCrossedRoom(const RangeRead& r, const Result& res) {
NatInf lhs = NatInf::of(natSub(residualE(r), res.keys.size()));
return niLe(lhs, intervalCard(r.end_.key, last(res)));
}
ConflictRange closedFormReverseTruncatedSkip(const RangeRead& r, const Result& res) {
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
if (allBelowEnd(r, res)) return topRange(r);
if (closedFormReverseFinite(r, res)) {
OptKeyView begin = optLt(r.end_.key, r.begin.key) ? r.end_.key : r.begin.key;
return mkRange(begin, afterFirst(res));
}
OptKeyView begin = (optLt(r.end_.key, r.begin.key) && closedFormCrossedRoom(r, res))
? r.end_.key
: r.begin.key;
return mkRange(begin, topKey());
}
ConflictRange closedFormReverseTruncated(const RangeRead& r, const Result& res) {
if (residualB(r) == 0) return closedFormReverseTruncatedZero(r, res);
return closedFormReverseTruncatedSkip(r, res);
}
ConflictRange closedFormPositive(const RangeRead& r, const Result& res) {
if (!res.more) return closedFormComplete(r, res);
if (!r.reverse) return closedFormForwardTruncated(r, res);
return closedFormReverseTruncated(r, res);
}
// ---------------------------------------------------------------------------
// Crossed table
// ---------------------------------------------------------------------------
bool overshoot(const RangeRead& r, const Result& res) {
return r.end_.offset >= 2 && allBelowEnd(r, res);
}
bool zeroCrossOvershootComplete(const RangeRead& r, const Result& res) {
return r.begin.offset == 0 && !res.more && !r.reverse && overshoot(r, res) &&
optLt(leastReportedOpt(r, res), r.begin.key);
}
bool negCrossOvershoot(const RangeRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more && overshoot(r, res) &&
optLt(first(res), r.begin.key);
}
bool crossedCompleteOvershoot(const RangeRead& r, const Result& res) {
return zeroCrossOvershootComplete(r, res) || negCrossOvershoot(r, res);
}
bool crossedCompleteEndLeOne(const RangeRead& r, const Result& res) {
if (!(!r.reverse && !res.more && r.begin.offset <= 0 &&
optLt(first(res), r.begin.key)))
return false;
if (r.end_.offset == 1 && beginSlack(r) < reportedBelowBegin(r, res))
return true;
return r.end_.offset <= 0 &&
!(reportedBelowBegin(r, res) <= beginSlack(r) &&
clampNonposBottom(r, res));
}
bool zeroCrossLongTop(const RangeRead& r, const Result& res) {
return r.begin.offset == 0 && !res.more && !r.reverse &&
r.end_.offset >= 2 && isTop(r.begin.key);
}
bool zeroCrossLongFinite(const RangeRead& r, const Result& res) {
return r.begin.offset == 0 && !res.more && !r.reverse &&
r.end_.offset >= 2 && !isTop(r.begin.key) &&
optLt(r.end_.key, r.begin.key) &&
optLt(leastReportedOpt(r, res), r.begin.key);
}
bool crossedCompleteBeginZero(const RangeRead& r, const Result& res) {
return zeroCrossUnderEnd(r, res) || zeroCrossLongTop(r, res) ||
zeroCrossLongFinite(r, res);
}
bool crossedCompleteClamp(const RangeRead& r, const Result& res) {
return negCrossClampEndOne(r, res) || negCrossHeadBottom(r, res) ||
negCrossClampNonposBottom(r, res) ||
negCrossClampLongNonpos(r, res);
}
ConflictRange closedFormCrossedComplete(const RangeRead& r, const Result& res) {
if (crossedCompleteOvershoot(r, res))
return closedFormNegCrossTopUnified(r, res);
if (crossedCompleteEndLeOne(r, res))
return closedFormNegCrossNonclampUnified(r, res);
if (crossedCompleteBeginZero(r, res))
return closedFormZeroCrossResidual(r, res);
if (crossedCompleteClamp(r, res))
return closedFormNegCrossClampUnified(r, res);
return closedFormNegCrossCompleteHighResidual(r, res);
}
bool zeroCrossTruncRevCrossed(const RangeRead& r, const Result& res) {
return r.begin.offset == 0 && r.reverse && res.more &&
optLt(leastReportedOpt(r, res), r.begin.key);
}
bool negCrossTruncRevUnder(const RangeRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 &&
optLt(last(res), r.begin.key) && optLe(r.begin.key, r.end_.key) &&
r.end_.offset <= 1;
}
bool negCrossTruncRevCrossedLow(const RangeRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 &&
optLt(last(res), r.begin.key) && optLt(r.end_.key, r.begin.key) &&
r.end_.offset <= 1 &&
niLe(niAdd(niAdd(intervalCard(r.end_.key, r.begin.key),
NatInf::of(res.keys.size())),
NatInf::of(toNat(static_cast<__int128>(1) - r.end_.offset))),
NatInf::of(beginSlack(r) + 1));
}
bool revTruncLeOneLow(const RangeRead& r, const Result& res) {
return negCrossTruncRevUnder(r, res) ||
negCrossTruncRevCrossedLow(r, res);
}
bool negCrossTruncRevBelowEnd(const RangeRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 &&
optLt(last(res), r.begin.key) && overshoot(r, res);
}
bool negCrossTruncRevResidualRoom(const RangeRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 &&
r.end_.offset >= 2 && anyAtOrAboveEnd(r, res) &&
niLe(niAdd(intervalCard(r.end_.key, first(res)), NatInf::of(2)),
NatInf::of(residualE(r)));
}
bool negCrossTruncRevResidualTopCrossedSmallGap(const RangeRead& r,
const Result& res) {
if (!(res.more && r.reverse && r.begin.offset < 0 &&
r.end_.offset >= 2 && optLt(r.end_.key, last(res))))
return false;
NatInf lhs = niAdd(intervalCard(r.end_.key, last(res)),
NatInf::of(res.keys.size()));
return niLt(lhs, NatInf::of(residualE(r)));
}
bool negCrossTruncRevResidualTopCrossed(const RangeRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 &&
r.end_.offset >= 2 && optLe(last(res), r.end_.key) &&
residualT(r, res) < residualE(r);
}
bool revTruncGeTwoTop(const RangeRead& r, const Result& res) {
return negCrossTruncRevBelowEnd(r, res) ||
negCrossTruncRevResidualRoom(r, res) ||
negCrossTruncRevResidualTopCrossedSmallGap(r, res) ||
negCrossTruncRevResidualTopCrossed(r, res);
}
bool negCrossTruncRevResidualTopLargeCrossed(const RangeRead& r,
const Result& res) {
return negCrossTruncRevResidualTopLarge(r, res) &&
optLt(last(res), r.begin.key) && !isTop(r.begin.key);
}
bool revTruncGeTwoLarge(const RangeRead& r, const Result& res) {
return negCrossTruncRevResidualTopLargeCrossed(r, res) ||
negCrossTruncRevResidualTopLargeSentinel(r, res);
}
bool negCrossTruncTopTop(const RangeRead& r, const Result& res) {
return res.more && !r.reverse && r.begin.offset < 0 &&
isTop(r.begin.key) && isTop(r.end_.key) && r.end_.offset <= 0;
}
bool negCrossTruncTopTopEq(const RangeRead& r, const Result& res) {
if (!negCrossTruncTopTop(r, res)) return false;
std::uint64_t extra = toNat(static_cast<__int128>(1) - r.end_.offset);
return res.keys.size() + extra == beginSlack(r) + 1;
}
std::uint64_t closedFormClampSlack(const RangeRead& r, const Result& res) {
return natSub(beginSlack(r) + 1, res.keys.size()) +
(isBottom(first(res)) ? 1 : 0);
}
bool closedFormClampGapSmall(const RangeRead& r, const Result& res) {
return niLe(intervalCard(asKey(lastKey(res)), r.begin.key),
NatInf::of(closedFormClampSlack(r, res)));
}
bool closedFormClampGapExactSaturated(const RangeRead& r, const Result& res) {
if (!(first(res) != bottomKeyView() && isBottom(r.end_.key) &&
residualE(r) == res.keys.size()))
return false;
return niEq(intervalCard(afterLast(res), r.begin.key),
NatInf::of(natSub(beginSlack(r) + 1, res.keys.size())));
}
bool closedFormClampGapLow(const RangeRead& r, const Result& res) {
return closedFormClampGapSmall(r, res) ||
closedFormClampGapExactSaturated(r, res);
}
OptKey closedFormClampUpper(const RangeRead& r, const Result& res) {
if (closedFormClampGapLow(r, res)) return afterLast(res);
return ownKey(r.begin.key);
}
bool closedFormForwardSaturated(const RangeRead& r, const Result& res) {
if (!(residualE(r) > 0 && optLt(r.end_.key, first(res)))) return false;
NatInf rhs = niAdd(intervalCard(r.end_.key, first(res)),
NatInf::of(res.keys.size()));
return niLt(NatInf::of(residualE(r)), rhs);
}
ConflictRange closedFormForwardTruncatedNonpos(const RangeRead& r,
const Result& res) {
if (reportedBelowBegin(r, res) <= beginSlack(r)) {
if (r.end_.offset <= 0) {
if (optLt(r.end_.key, r.begin.key)) {
if (clampNonposBottom(r, res)) {
if (optLt(last(res), r.end_.key))
return closedFormNegCrossTruncNonposGapA2aBottom(r, res);
return mkRange(bottomKey(), clampLongUpper(r, res));
}
return mkRange(first(res), r.begin.key);
}
if (negCrossTruncTopTopEq(r, res))
return mkRange(first(res), topKey());
if (clampNonposBottom(r, res))
return mkRange(bottomKey(), r.end_.key);
return mkRange(first(res), r.end_.key);
}
return mkRange(bottomKey(), closedFormClampUpper(r, res));
}
if (r.end_.offset <= 0)
return mkRange(first(res), optMax(r.begin.key, r.end_.key));
OptKeyView begin = closedFormForwardSaturated(r, res) ? r.end_.key : first(res);
return mkRange(begin, optMax(r.begin.key, afterLast(res)));
}
ConflictRange closedFormCrossed(const RangeRead& r, const Result& res) {
if (!res.more) {
if (!r.reverse) return closedFormCrossedComplete(r, res);
std::vector<KeyView> rev = reversedKeys(res);
return closedFormCrossedComplete(forwardRead(r), Result{rev, false});
}
if (r.reverse) {
if (zeroCrossTruncRevCrossed(r, res))
return closedFormZeroCrossTruncRevCrossed(r, res);
if (revTruncLeOneLow(r, res))
return truncatedRangeRev(r, lastKey(res));
if (r.end_.offset <= 1)
return mkRange(last(res), r.begin.key);
if (revTruncGeTwoTop(r, res))
return topOfLast(lastKey(res));
if (revTruncGeTwoLarge(r, res))
return closedFormNegCrossTruncRevResidualTopLargeUnified(r, res);
return closedFormNegCrossTruncRevResidualFiniteUnified(r, res);
}
return closedFormForwardTruncatedNonpos(r, res);
}
} // namespace
ConflictRange closedForm(const RangeRead& r, const Result& res) {
if (res.keys.empty()) return emptyValue(r);
if (r.begin.offset >= 1 || clampedNonpos(r, res))
return closedFormPositive(r, res);
return closedFormCrossed(r, res);
}
} // namespace cr::literal
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include "model.h"
namespace cr::literal {
// A literal transliteration of `Statement.closedForm` from Statement.lean.
ConflictRange closedForm(const RangeRead& r, const Result& res);
} // namespace cr::literal
+313
View File
@@ -0,0 +1,313 @@
// Shared data model for the C++ translation of `Statement.closedForm`.
//
// Keys are byte strings ordered lexicographically. The model separates
// ownership on the two sides of the API:
//
// * input keys (the selectors in `RangeRead` and the reported keys in
// `Result`) are `std::string_view`s, so callers can point them straight
// at their own storage without copying;
// * result keys (`ConflictRange`) are owned `std::string`s, because the
// decision procedure can synthesise new keys (`keyAfter`, `afterFirst`,
// clamp bounds) that outlive the inputs.
//
// `OptKey`/`OptKeyView` represent Lean's `WithTop Key`: `std::nullopt` is `⊤`,
// and an empty key is the bottom key. `NatInf` represents Lean's `ℕ∞`
// (`ENat`): `inf` is `⊤`, otherwise `n` is the finite value.
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace cr {
using Byte = std::uint8_t;
// Owned byte string. `std::string`'s small-string optimisation keeps the
// short keys used by this model allocation-free, and its comparisons go
// through `char_traits<char>`, which orders bytes as `unsigned char`.
using Key = std::string;
// Borrowed byte string, used for every input key.
using KeyView = std::string_view;
using OptKey = std::optional<Key>;
using OptKeyView = std::optional<KeyView>;
using Int = std::int64_t;
struct Selector {
OptKeyView key;
Int offset = 0;
};
struct RangeRead {
Selector begin;
Selector end_;
bool reverse = false;
};
struct Result {
// A non-owning view of the reported keys. The caller keeps the underlying
// keys alive for the duration of the call.
std::span<const KeyView> keys;
bool more = false;
};
struct ConflictRange {
OptKey begin;
OptKey end_;
};
inline bool operator==(const ConflictRange& a, const ConflictRange& b) {
return a.begin == b.begin && a.end_ == b.end_;
}
inline bool operator!=(const ConflictRange& a, const ConflictRange& b) {
return !(a == b);
}
// ---------------------------------------------------------------------------
// Borrowed/owned conversions
// ---------------------------------------------------------------------------
inline KeyView keyView(const Key& k) { return KeyView(k); }
inline OptKeyView optView(const OptKey& k) {
return k ? OptKeyView{KeyView(*k)} : std::nullopt;
}
inline OptKey ownKey(OptKeyView k) {
if (!k) return std::nullopt;
// The bottom key is the empty string; avoid the string_view constructor.
if (k->empty()) return OptKey{Key{}};
return OptKey{Key(*k)};
}
inline OptKey ownKey(const OptKey& k) { return k; }
inline OptKey ownKey(OptKey&& k) { return std::move(k); }
// ---------------------------------------------------------------------------
// Keys and `WithTop Key`
// ---------------------------------------------------------------------------
inline bool keyLt(KeyView a, KeyView b) { return a.compare(b) < 0; }
inline bool keyEq(KeyView a, KeyView b) { return a == b; }
template <class A, class B>
inline bool optLt(const std::optional<A>& a, const std::optional<B>& b) {
if (!a) return false; // ⊤ < x is false
if (!b) return true; // ↑k < ⊤
return keyLt(*a, *b);
}
template <class A, class B>
inline bool optLe(const std::optional<A>& a, const std::optional<B>& b) {
return !optLt(b, a);
}
template <class A, class B>
inline bool optGt(const std::optional<A>& a, const std::optional<B>& b) {
return optLt(b, a);
}
template <class A, class B>
inline bool optGe(const std::optional<A>& a, const std::optional<B>& b) {
return !optLt(a, b);
}
template <class T>
inline OptKey ownAny(const std::optional<T>& k) {
return k ? OptKey{Key(*k)} : std::nullopt;
}
template <class A, class B>
inline OptKey optMin(const std::optional<A>& a, const std::optional<B>& b) {
return optLt(a, b) ? ownAny(a) : ownAny(b);
}
template <class A, class B>
inline OptKey optMax(const std::optional<A>& a, const std::optional<B>& b) {
return optLt(a, b) ? ownAny(b) : ownAny(a);
}
inline OptKey topKey() { return std::nullopt; }
inline OptKey bottomKey() { return OptKey{Key{}}; }
// A view of a static empty string, so empty views always have a valid pointer.
inline KeyView emptyKeyView() {
static const Key empty;
return KeyView(empty);
}
inline OptKeyView topKeyView() { return std::nullopt; }
inline OptKeyView bottomKeyView() { return OptKeyView{emptyKeyView()}; }
inline bool isTop(OptKeyView k) { return !k.has_value(); }
inline bool isBottom(OptKeyView k) { return k.has_value() && k->empty(); }
inline OptKey asKey(Key k) { return OptKey{std::move(k)}; }
inline OptKeyView asKey(KeyView k) { return OptKeyView{k}; }
inline Key keyAfter(KeyView k) {
Key out(k);
out.push_back(0);
return out;
}
inline Key keyAfterN(std::uint64_t n, KeyView k) {
Key out(k);
out.reserve(out.size() + static_cast<std::size_t>(n));
out.insert(out.end(), static_cast<std::size_t>(n), 0);
return out;
}
inline bool isPrefix(KeyView prefix, KeyView whole) {
return prefix.size() <= whole.size() &&
std::equal(prefix.begin(), prefix.end(), whole.begin());
}
inline bool allZeroFrom(KeyView k, std::size_t from) {
for (std::size_t i = from; i < k.size(); ++i)
if (k[i] != 0) return false;
return true;
}
// ---------------------------------------------------------------------------
// `ℕ∞` (ENat)
// ---------------------------------------------------------------------------
struct NatInf {
bool inf = false;
std::uint64_t n = 0;
static NatInf top() { return NatInf{true, 0}; }
static NatInf of(std::uint64_t v) { return NatInf{false, v}; }
};
inline bool niLe(NatInf a, NatInf b) {
if (a.inf) return b.inf;
if (b.inf) return true;
return a.n <= b.n;
}
inline bool niLt(NatInf a, NatInf b) {
if (a.inf) return false;
if (b.inf) return true;
return a.n < b.n;
}
inline bool niEq(NatInf a, NatInf b) {
if (a.inf != b.inf) return false;
return a.inf || a.n == b.n;
}
inline NatInf niAdd(NatInf a, NatInf b) {
if (a.inf || b.inf) return NatInf::top();
return NatInf::of(a.n + b.n);
}
// Lean's `ENat` subtraction: `a - ⊤ = 0`, `⊤ - b = ⊤` (b finite), and
// finite values subtract with truncation at zero.
inline NatInf niSub(NatInf a, NatInf b) {
if (b.inf) return NatInf::of(0);
if (a.inf) return NatInf::top();
return NatInf::of(a.n >= b.n ? a.n - b.n : 0);
}
// `Int.toNat`.
inline std::uint64_t toNat(__int128 x) {
return x <= 0 ? 0 : static_cast<std::uint64_t>(x);
}
inline std::uint64_t toNat(Int x) { return toNat(static_cast<__int128>(x)); }
// Natural-number truncated subtraction.
inline std::uint64_t natSub(std::uint64_t a, std::uint64_t b) {
return a >= b ? a - b : 0;
}
// ---------------------------------------------------------------------------
// Result projections
// ---------------------------------------------------------------------------
inline KeyView firstKey(const Result& res) {
return res.keys.empty() ? emptyKeyView() : res.keys.front();
}
inline KeyView lastKey(const Result& res) {
return res.keys.empty() ? emptyKeyView() : res.keys.back();
}
inline OptKeyView first(const Result& res) { return asKey(firstKey(res)); }
inline OptKeyView last(const Result& res) { return asKey(lastKey(res)); }
inline OptKey afterFirst(const Result& res) {
return asKey(keyAfter(firstKey(res)));
}
inline OptKey afterLast(const Result& res) {
return asKey(keyAfter(lastKey(res)));
}
inline KeyView leastReported(const RangeRead& r, const Result& res) {
return r.reverse ? lastKey(res) : firstKey(res);
}
inline OptKeyView leastReportedOpt(const RangeRead& r, const Result& res) {
return asKey(leastReported(r, res));
}
inline RangeRead forwardRead(const RangeRead& r) {
RangeRead out = r;
out.reverse = false;
return out;
}
// A reversed view of the reported keys. The caller owns the returned vector,
// whose elements borrow from `res.keys`.
inline std::vector<KeyView> reversedKeys(const Result& res) {
return {res.keys.rbegin(), res.keys.rend()};
}
// ---------------------------------------------------------------------------
// Cardinality of `[b, e)`
// ---------------------------------------------------------------------------
inline NatInf intervalCard(OptKeyView b, OptKeyView e) {
if (!b) return NatInf::of(0); // b = ⊤
if (!e) return NatInf::top(); // e = ⊤
const KeyView bb = *b;
const KeyView ee = *e;
if (!keyLt(bb, ee)) return NatInf::of(0); // e <= b
if (isPrefix(bb, ee) && allZeroFrom(ee, bb.size()))
return NatInf::of(static_cast<std::uint64_t>(ee.size() - bb.size()));
return NatInf::top();
}
// ---------------------------------------------------------------------------
// Result-based counters, in the exact form used by the statement
// ---------------------------------------------------------------------------
inline std::uint64_t residualT(const RangeRead& r, const Result& res) {
if (isTop(r.end_.key)) return 0;
std::uint64_t count = 0;
for (KeyView k : res.keys)
if (!keyLt(k, *r.end_.key)) ++count; // r.end_.key <= k
return count;
}
inline std::uint64_t reportedBelowBegin(const RangeRead& r, const Result& res) {
if (isTop(r.begin.key)) return static_cast<std::uint64_t>(res.keys.size());
std::uint64_t count = 0;
for (KeyView k : res.keys)
if (keyLt(k, *r.begin.key)) ++count;
return count;
}
inline std::uint64_t residualB(const RangeRead& r) {
return toNat(static_cast<__int128>(r.begin.offset) - 1);
}
inline std::uint64_t residualE(const RangeRead& r) {
return toNat(static_cast<__int128>(r.end_.offset) - 1);
}
inline std::uint64_t beginSlack(const RangeRead& r) {
return toNat(-static_cast<__int128>(r.begin.offset));
}
inline std::uint64_t clampNonposD(const RangeRead& r) {
return toNat(-static_cast<__int128>(r.end_.offset));
}
inline std::uint64_t clampCap_(const RangeRead& r) {
NatInf c = intervalCard(r.end_.key, r.begin.key);
return niEq(c, NatInf::top()) ? 0 : c.n; // caller must check finiteness
}
} // namespace cr
+149
View File
@@ -0,0 +1,149 @@
// nanobench comparison of the literal translation and the summarised
// implementation on representative readable instances.
#define ANKERL_NANOBENCH_IMPLEMENT
#include "nanobench.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <vector>
#include "../src/fast.h"
#include "../src/literal.h"
#include "readable_gen.h"
namespace {
using namespace cr;
struct Case {
gen::Instance inst;
RangeRead r;
Result res;
const char* name = "";
// Build the borrowed views once the case sits at its final address.
void prepare() {
inst.finish();
r = inst.read();
res = inst.result();
}
};
std::vector<Key> makeDb(std::size_t n, std::size_t len) {
std::vector<Key> db;
db.reserve(n);
for (std::size_t i = 0; i < n; ++i) {
Key k;
k.reserve(len);
for (std::size_t j = 0; j < len; ++j)
k.push_back(static_cast<Byte>((i >> (8 * j)) & 0xff));
// Ensure strict ordering is preserved for the multi-byte encoding.
if (!db.empty() && !keyLt(db.back(), k)) k.push_back(static_cast<Byte>(i));
db.push_back(std::move(k));
}
std::sort(db.begin(), db.end());
db.erase(std::unique(db.begin(), db.end()), db.end());
return db;
}
Case makeCase(const std::vector<Key>& db, OptKey beginKey, Int beginOff,
OptKey endKey, Int endOff, bool reverse, bool truncate,
const char* name) {
gen::Instance inst;
inst.beginKey = std::move(beginKey);
inst.beginOffset = beginOff;
inst.endKey = std::move(endKey);
inst.endOffset = endOff;
inst.reverse = reverse;
OptKey lo = gen::resolve(db, Selector{optView(inst.beginKey), beginOff});
OptKey hi = gen::resolve(db, Selector{optView(inst.endKey), endOff});
std::vector<Key> complete = gen::inRange(db, lo, hi);
if (reverse) std::reverse(complete.begin(), complete.end());
if (truncate && complete.size() > 1) {
inst.keys.assign(
complete.begin(),
complete.begin() + static_cast<std::ptrdiff_t>(complete.size() / 2));
inst.more = true;
} else {
inst.keys = std::move(complete);
inst.more = false;
}
Case c;
c.inst = std::move(inst);
c.name = name;
return c;
}
void check(const Case& c) {
ConflictRange a = literal::closedForm(c.r, c.res);
ConflictRange b = fast::closedForm(c.r, c.res);
if (!gen::equivalent(a, b)) {
std::fprintf(stderr, "benchmark case %s disagrees between implementations\n",
c.name);
std::exit(1);
}
}
} // namespace
int main() {
using ankerl::nanobench::Bench;
const std::vector<Key> smallDb = makeDb(8, 1);
const std::vector<Key> mediumDb = makeDb(48, 2);
const std::vector<Key> longDb = makeDb(512, 3);
const OptKey bot = bottomKey();
const OptKey top = std::nullopt;
// A finite key above every key in `smallDb` and `longDb`, so the complete
// reads below take the scanning branch (`residualT`, `allBelowEnd`, ...)
// instead of short-circuiting on `⊤`.
Key aboveKey;
aboveKey.push_back(static_cast<char>(0xff));
aboveKey.push_back(static_cast<char>(0xff));
const OptKey above = OptKey{aboveKey};
std::vector<Case> cases;
// A short complete read. An end offset of 2 (rather than 1) skips the
// `end.offset <= 1` shortcut, so the literal translation actually rescans the
// reported keys and the summary has something to summarise.
cases.push_back(makeCase(smallDb, bot, 1, above, 2, false, false,
"small complete"));
// A short truncated read (cross-free).
cases.push_back(makeCase(smallDb, smallDb[1], 1, top, 1, false, true,
"small truncated fwd"));
// A crossed read: the end selector resolves before the begin selector.
cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, false,
true, "crossed truncated fwd"));
cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, true,
true, "crossed truncated rev"));
// Long complete / truncated reads.
cases.push_back(
makeCase(longDb, bot, 1, above, 2, false, false, "long complete fwd"));
cases.push_back(makeCase(longDb, bot, 1, top, 1, false, true,
"long truncated fwd"));
cases.push_back(makeCase(longDb, bot, 1, top, 1, true, true,
"long truncated rev"));
// The case vector is final now, so the borrowed views can point at it.
for (Case& c : cases) c.prepare();
for (const Case& c : cases) {
check(c);
Bench bench;
bench.title(c.name).relative(true).warmup(20).minEpochIterations(200);
bench.run("literal", [&] {
ankerl::nanobench::doNotOptimizeAway(
literal::closedForm(c.r, c.res));
});
bench.run("fast", [&] {
ankerl::nanobench::doNotOptimizeAway(fast::closedForm(c.r, c.res));
});
}
return 0;
}
+104
View File
@@ -0,0 +1,104 @@
// Compare the literal translation with the summarised implementation on the
// Lean-generated corpus (readable or not).
#include <cstdio>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "../src/fast.h"
#include "../src/literal.h"
namespace {
using namespace cr;
bool parseKey(const std::string& tok, OptKey& out) {
if (tok == "T") { out = std::nullopt; return true; }
if (tok == "-") { out = Key{}; return true; }
if (tok.size() % 2 != 0) return false;
auto hex = [](char c) -> int {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
};
Key k;
for (std::size_t i = 0; i < tok.size(); i += 2) {
int hi = hex(tok[i]), lo = hex(tok[i + 1]);
if (hi < 0 || lo < 0) return false;
k.push_back(static_cast<Byte>(hi * 16 + lo));
}
out = std::move(k);
return true;
}
std::string encKey(const OptKey& k) {
if (!k) return "T";
if (k->empty()) return "-";
static const char* d = "0123456789abcdef";
std::string s;
for (Byte b : *k) { s.push_back(d[b >> 4]); s.push_back(d[b & 15]); }
return s;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) return 2;
std::ifstream in(argv[1]);
std::string line;
std::size_t cases = 0, bad = 0;
while (std::getline(in, line)) {
if (line.empty() || line[0] == '#') continue;
std::istringstream ss(line);
std::string bk, ek, arrow, ob, oe;
long long bo = 0, eo = 0;
int rev = 0, more = 0;
std::size_t n = 0;
OptKey beginKey, endKey;
if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n)) continue;
if (!parseKey(bk, beginKey) || !parseKey(ek, endKey)) continue;
// Owned reported keys plus the views borrowed by both implementations.
std::vector<Key> keys;
keys.reserve(n);
bool ok = true;
for (std::size_t i = 0; i < n; ++i) {
std::string kt;
if (!(ss >> kt)) { ok = false; break; }
OptKey p;
if (!parseKey(kt, p) || !p) { ok = false; break; }
keys.push_back(std::move(*p));
}
if (!ok || !(ss >> arrow >> ob >> oe) || arrow != "=>") continue;
// `fast` assumes the readable sorting invariant, so only compare cases that
// satisfy it (this is a superset of the readable cases).
const bool reverse = rev != 0;
bool sorted = true;
for (std::size_t i = 1; i < keys.size(); ++i) {
if (reverse ? !keyLt(keys[i], keys[i - 1])
: !keyLt(keys[i - 1], keys[i])) {
sorted = false;
break;
}
}
if (!sorted) continue;
std::vector<KeyView> keyViews;
keyViews.reserve(keys.size());
for (const Key& k : keys) keyViews.push_back(k);
RangeRead r{Selector{optView(beginKey), static_cast<Int>(bo)},
Selector{optView(endKey), static_cast<Int>(eo)}, reverse};
Result res{keyViews, more != 0};
++cases;
ConflictRange a = literal::closedForm(r, res);
ConflictRange b = fast::closedForm(r, res);
if (a.begin != b.begin || a.end_ != b.end_) {
if (++bad <= 20) {
std::fprintf(stderr, "MISMATCH: lit [%s,%s) fast [%s,%s)\n %s\n",
encKey(a.begin).c_str(), encKey(a.end_).c_str(),
encKey(b.begin).c_str(), encKey(b.end_).c_str(),
line.c_str());
}
}
}
std::printf("diff: %zu cases, %zu mismatches\n", cases, bad);
return bad == 0 ? 0 : 1;
}
+41
View File
@@ -0,0 +1,41 @@
// libFuzzer safety harness: arbitrary (not necessarily readable) instances.
// Equivalence is only promised on readable inputs, so this harness merely runs
// both implementations under ASan/UBSan to catch memory-safety or UB issues in
// the defensive branches.
#include <cstddef>
#include <cstdint>
#include <vector>
#include "../src/fast.h"
#include "../src/literal.h"
#include "readable_gen.h"
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data,
std::size_t size) {
cr::gen::Bytes bytes{data, size, 0};
for (int i = 0; i < 8; ++i) {
const std::size_t maxLen = 1 + bytes.u8() % 16;
// Owned selector keys and reported keys; the borrows below point here.
cr::OptKey beginKey = cr::gen::randomOptKey(bytes, maxLen);
cr::OptKey endKey = cr::gen::randomOptKey(bytes, maxLen);
cr::RangeRead r;
r.begin = cr::Selector{cr::optView(beginKey),
cr::gen::randomOffset(bytes)};
r.end_ = cr::Selector{cr::optView(endKey), cr::gen::randomOffset(bytes)};
r.reverse = (bytes.u8() & 1) != 0;
std::vector<cr::Key> keys;
const std::size_t n = bytes.u8() % 16;
keys.reserve(n);
for (std::size_t j = 0; j < n; ++j)
keys.push_back(cr::gen::randomKey(bytes, maxLen));
std::vector<cr::KeyView> views;
views.reserve(keys.size());
for (const cr::Key& k : keys) views.push_back(k);
cr::Result res{views, (bytes.u8() & 1) != 0};
(void)cr::literal::closedForm(r, res);
(void)cr::fast::closedForm(r, res);
}
return 0;
}
+59
View File
@@ -0,0 +1,59 @@
// libFuzzer differential harness: the literal translation versus the
// summarised implementation, on readable-by-construction instances.
#include <cstddef>
#include <cstdint>
#include <string>
#include <cstdio>
#include <cstdlib>
#include "../src/fast.h"
#include "../src/literal.h"
#include "readable_gen.h"
namespace {
std::string enc(cr::OptKeyView k) {
if (!k) return "T";
if (k->empty()) return "-";
static const char* d = "0123456789abcdef";
std::string s;
for (char c : *k) {
auto b = static_cast<cr::Byte>(c);
s.push_back(d[b >> 4]);
s.push_back(d[b & 15]);
}
return s;
}
std::string enc(const cr::OptKey& k) { return enc(cr::optView(k)); }
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data,
std::size_t size) {
cr::gen::Bytes bytes{data, size, 0};
for (int i = 0; i < 8; ++i) {
cr::gen::Instance inst = cr::gen::readable(bytes);
cr::RangeRead r = inst.read();
cr::Result res = inst.result();
cr::ConflictRange lit = cr::literal::closedForm(r, res);
cr::ConflictRange fast = cr::fast::closedForm(r, res);
if (!cr::gen::equivalent(lit, fast)) {
std::fprintf(stderr,
"MISMATCH literal [%s,%s) fast [%s,%s)\n"
" beginKey=%s beginOffset=%lld endKey=%s endOffset=%lld "
"reverse=%d more=%d n=%zu\n",
enc(lit.begin).c_str(), enc(lit.end_).c_str(),
enc(fast.begin).c_str(), enc(fast.end_).c_str(),
enc(r.begin.key).c_str(),
static_cast<long long>(r.begin.offset),
enc(r.end_.key).c_str(),
static_cast<long long>(r.end_.offset), (int)r.reverse,
(int)res.more, res.keys.size());
for (cr::KeyView k : res.keys)
std::fprintf(stderr, " key %s\n", enc(k).c_str());
std::abort();
}
}
return 0;
}
+130
View File
@@ -0,0 +1,130 @@
// Validates the C++ literal translation against golden vectors produced by
// `cpp/oracle/gen.lean` (which calls the real `Statement.closedForm`).
#include <cstdio>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "../src/literal.h"
namespace {
using namespace cr;
bool parseKey(const std::string& tok, OptKey& out) {
if (tok == "T") {
out = std::nullopt;
return true;
}
if (tok == "-") {
out = Key{};
return true;
}
if (tok.size() % 2 != 0) return false;
Key k;
k.reserve(tok.size() / 2);
auto hex = [](char c) -> int {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
};
for (std::size_t i = 0; i < tok.size(); i += 2) {
int hi = hex(tok[i]);
int lo = hex(tok[i + 1]);
if (hi < 0 || lo < 0) return false;
k.push_back(static_cast<Byte>(hi * 16 + lo));
}
out = std::move(k);
return true;
}
std::string encKey(const OptKey& k) {
if (!k) return "T";
if (k->empty()) return "-";
static const char* digits = "0123456789abcdef";
std::string out;
out.reserve(k->size() * 2);
for (Byte b : *k) {
out.push_back(digits[b >> 4]);
out.push_back(digits[b & 0xf]);
}
return out;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: %s golden.txt\n", argv[0]);
return 2;
}
std::ifstream in(argv[1]);
if (!in) {
std::fprintf(stderr, "cannot open %s\n", argv[1]);
return 2;
}
std::string line;
std::size_t lineNo = 0;
std::size_t cases = 0;
std::size_t bad = 0;
while (std::getline(in, line)) {
++lineNo;
if (line.empty() || line[0] == '#') continue;
std::istringstream ss(line);
std::string bk, ek, arrow, ob, oe;
long long bo = 0, eo = 0;
int rev = 0, more = 0;
std::size_t n = 0;
OptKey beginKey, endKey;
if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n) ||
!parseKey(bk, beginKey) || !parseKey(ek, endKey)) {
std::fprintf(stderr, "line %zu: bad header: %s\n", lineNo, line.c_str());
++bad;
continue;
}
// Owned reported keys plus the views the literal translation borrows.
std::vector<Key> keys;
keys.reserve(n);
bool parseOk = true;
for (std::size_t i = 0; i < n; ++i) {
std::string kt;
if (!(ss >> kt)) { parseOk = false; break; }
OptKey parsed;
if (!parseKey(kt, parsed) || !parsed.has_value()) { parseOk = false; break; }
keys.push_back(std::move(*parsed));
}
if (!parseOk || !(ss >> arrow >> ob >> oe) || arrow != "=>") {
std::fprintf(stderr, "line %zu: bad body: %s\n", lineNo, line.c_str());
++bad;
continue;
}
std::vector<KeyView> keyViews;
keyViews.reserve(keys.size());
for (const Key& k : keys) keyViews.push_back(k);
RangeRead r{Selector{optView(beginKey), static_cast<Int>(bo)},
Selector{optView(endKey), static_cast<Int>(eo)}, rev != 0};
Result res{keyViews, more != 0};
OptKey wantBegin, wantEnd;
if (!parseKey(ob, wantBegin) || !parseKey(oe, wantEnd)) {
std::fprintf(stderr, "line %zu: bad expected output\n", lineNo);
++bad;
continue;
}
ConflictRange got = literal::closedForm(r, res);
++cases;
if (got.begin != wantBegin || got.end_ != wantEnd) {
++bad;
if (bad <= 20) {
std::fprintf(stderr,
"MISMATCH line %zu: got [%s,%s) want [%s,%s)\n %s\n",
lineNo, encKey(got.begin).c_str(),
encKey(got.end_).c_str(), encKey(wantBegin).c_str(),
encKey(wantEnd).c_str(), line.c_str());
}
}
}
std::printf("golden: %zu cases, %zu mismatches\n", cases, bad);
return bad == 0 ? 0 : 1;
}
+180
View File
@@ -0,0 +1,180 @@
// Deterministic, readable-by-construction instance generation shared by the
// differential fuzzer and the benchmarks.
//
// A generated pair `(r, res)` always satisfies `Readable r res`: we build a
// sorted, duplicate-free database, resolve the two selectors against it, and
// report either the complete traversal or a nonempty prefix of it.
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <utility>
#include <vector>
#include "../src/model.h"
namespace cr::gen {
struct Bytes {
const std::uint8_t* data = nullptr;
std::size_t size = 0;
std::size_t pos = 0;
std::uint8_t u8() {
if (size == 0) return 0;
std::uint8_t v = data[pos % size];
++pos;
return v;
}
std::uint16_t u16() {
std::uint16_t lo = u8();
std::uint16_t hi = u8();
return static_cast<std::uint16_t>(lo | (hi << 8));
}
};
inline Byte weightedByte(Bytes& b) {
// Weight the common boundary bytes; otherwise take the raw byte.
std::uint8_t r = b.u8();
if (r < 24) return 0;
if (r < 40) return 1;
if (r < 56) return 2;
if (r < 72) return 255;
return b.u8();
}
inline Key randomKey(Bytes& b, std::size_t maxLen) {
std::size_t len = b.u8() % (maxLen + 1);
Key k;
k.reserve(len);
for (std::size_t i = 0; i < len; ++i) k.push_back(weightedByte(b));
return k;
}
inline OptKey randomOptKey(Bytes& b, std::size_t maxLen) {
// One in eight selectors is anchored at `⊤`.
if ((b.u8() & 7) == 0) return std::nullopt;
return OptKey{randomKey(b, maxLen)};
}
inline Int randomOffset(Bytes& b) {
// Bias towards the offsets that decide the interesting boundary cases, but
// still allow the full [-256, 255] window.
std::uint8_t pick = b.u8();
if (pick < 192) {
static const Int small[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
return small[pick % 9];
}
return static_cast<Int>(b.u16() % 512) - 256;
}
inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
Int anchor;
if (!s.key) {
anchor = static_cast<Int>(db.size()) + s.offset - 1;
} else if (s.key->empty()) {
anchor = s.offset - 1;
} else {
std::size_t below = 0;
for (const Key& k : db)
if (keyLt(k, *s.key)) ++below;
anchor = static_cast<Int>(below) + s.offset - 1;
}
if (anchor < 0) return OptKey{Key{}};
if (static_cast<std::size_t>(anchor) >= db.size()) return std::nullopt;
return OptKey{db[static_cast<std::size_t>(anchor)]};
}
inline std::vector<Key> inRange(const std::vector<Key>& db, const OptKey& lo,
const OptKey& hi) {
std::vector<Key> out;
for (const Key& k : db) {
bool geLo = lo.has_value() && !keyLt(k, *lo);
bool ltHi = !hi || keyLt(k, *hi);
if (geLo && ltHi) out.push_back(k);
}
return out;
}
// Owning storage for a generated instance. `RangeRead` and `Result` borrow
// keys, so the instance keeps the keys alive and rebuilds views on demand.
struct Instance {
// Reported keys in traversal order; backing storage for `views`.
std::vector<Key> keys;
// Views into `keys`, built by `finish()`.
std::vector<KeyView> views;
// Owned selector keys; `read()` borrows them.
OptKey beginKey;
OptKey endKey;
Int beginOffset = 0;
Int endOffset = 0;
bool reverse = false;
bool more = false;
// Must be called once `keys` is final; safe again after a move.
void finish() {
views.clear();
views.reserve(keys.size());
for (const Key& k : keys) views.push_back(k);
}
RangeRead read() const {
RangeRead r;
r.begin = Selector{optView(beginKey), beginOffset};
r.end_ = Selector{optView(endKey), endOffset};
r.reverse = reverse;
return r;
}
Result result() const { return Result{views, more}; }
};
inline Instance readable(Bytes& b) {
const std::size_t maxLen = 1 + b.u8() % 16; // key length 0..16
std::vector<Key> db;
const std::size_t count = b.u8() % 20; // 0..19 stored keys
db.reserve(count);
for (std::size_t i = 0; i < count; ++i) db.push_back(randomKey(b, maxLen));
std::sort(db.begin(), db.end());
db.erase(std::unique(db.begin(), db.end()), db.end());
Instance inst;
inst.beginKey = randomOptKey(b, maxLen);
inst.beginOffset = randomOffset(b);
inst.endKey = randomOptKey(b, maxLen);
inst.endOffset = randomOffset(b);
inst.reverse = (b.u8() & 1) != 0;
OptKey lo = resolve(db, Selector{optView(inst.beginKey), inst.beginOffset});
OptKey hi = resolve(db, Selector{optView(inst.endKey), inst.endOffset});
std::vector<Key> complete = inRange(db, lo, hi);
if (inst.reverse) std::reverse(complete.begin(), complete.end());
if (!complete.empty() && (b.u8() & 1) != 0) {
std::size_t m = 1 + b.u8() % complete.size();
inst.keys.assign(complete.begin(),
complete.begin() + static_cast<std::ptrdiff_t>(m));
inst.more = true;
} else {
inst.keys = std::move(complete);
inst.more = false;
}
inst.finish();
return inst;
}
// Range equivalence used by the differential check: any encoding of the empty
// interval is equivalent to any other; nonempty intervals must match exactly.
inline bool isEmptyRange(const ConflictRange& c) {
return !optLt(c.begin, c.end_);
}
inline bool equivalent(const ConflictRange& a, const ConflictRange& b) {
const bool ea = isEmptyRange(a);
const bool eb = isEmptyRange(b);
if (ea || eb) return ea && eb;
return a.begin == b.begin && a.end_ == b.end_;
}
} // namespace cr::gen
+5473
View File
File diff suppressed because it is too large. Load diff