Harden and fully cover the C++ translation

Add an exhaustive readable-instance sweep and a coverage gate requiring
100% line coverage of the summarised implementation. Delete branches that
are unreachable under `Readable`, centralise and assert the 32-bit offset
contract at the public entry points, and stop materialising `n` zero bytes
when deciding `keyAfterN n k < upper`.

Also add the extreme-offset regression test, harden the corpus parsers,
remove unused model helpers, document the literal-total / fast-readable
split, and refresh the validation and benchmark numbers.
This commit is contained in:
andrew committed 2026-11-17 18:46:24 -05:00
1 parent e32e42d933
commit 3bb5d062ff
15 files changed
+620 -196

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
# conflict-range
A formalised **closed form** for the minimal conflict range of a read/result
pair, together with a C++20 translation and its test harness.
Given a database, a range read (two selectors plus offsets, either traversal
direction), and the keys a transaction observed, `Statement.closedForm` computes
a minimal sound conflict range: a set of keys that is sufficient to detect every
conflicting write, and none of whose proper subranges is sufficient.
## Layout
| Path | Purpose |
| --- | --- |
| `Statement.lean` | The statement: keys, selectors, reads, results, conflict ranges, soundness, minimality, and the closed form itself. It imports only Mathlib and states the definitions and headlines without using `theorem`, `axiom`, or `sorry`. |
| `FormalConflictRange/` | The reference development that proves the closed form minimal. |
| `Bridge.lean` | Definitional bridge from `Statement`'s types to the reference development, plus the transport lemmas. |
| `Proof.lean` | Proves `Statement.ClosedFormMinimal` and `Statement.MinimalUnique` by transporting the reference theorems across the bridge. |
| `Challenge.lean` | The same two headline statements with placeholder proofs (`sorry`). This is the trusted statement side of the comparator check. |
| `Solution.lean` | The solution side of the comparator check; it imports `Proof`. |
| `comparator.json` | Comparator configuration: challenge/solution modules, theorem names, and permitted axioms. |
| `cpp/` | A C++20 translation of `closedForm`, an equivalent summarised implementation, golden-vector and fuzz tests, and a benchmark. See `cpp/README.md`. |
## Build and check
```sh
lake build
lake comparator
```
This repository pins Lean `v4.35.0` and Mathlib `v4.35.0`
(`lean-toolchain`, `lakefile.toml`). `lake comparator` checks that the solution
proves the challenge's statements and that the proofs use only `propext`,
`Quot.sound`, and `Classical.choice`.
## Status and scope
* `Statement.ClosedFormMinimal` and `Statement.MinimalUnique` are proved, and no
proof in `Proof.lean`, `Bridge.lean`, or `FormalConflictRange/` uses `sorry`.
* `Challenge.lean` deliberately contains `sorry`: it is the statement side of
the comparator check, not part of the proof.
* Some docstrings in `FormalConflictRange/` are a research log and refer to
files that are **not** part of this repository:
`FormalConflictRange/Counterexample`, `FormalConflictRange/Open`, and
`notes/main.tex`. Those references appear only in comments and docstrings, so
they do not affect the build or the headline proofs, but their specific claims
cannot be checked here. `FormalConflictRange/Prototype.lean` is likewise an
experimental prototype, not part of the headline proof path.
* The C++ tree is validated against Lean-generated golden vectors, by
differential fuzzing, and by a deterministic exhaustive sweep of small
readable instances; `cpp/README.md` describes the model, the tests, the
coverage check, and the benchmark. The summarised implementation has 100%
line coverage via `cpp/run-coverage.sh`.
## License
Apache License 2.0; see `LICENSE`.
+1
View File
@@ -1,2 +1,3 @@
build/ build/
build-coverage/
oracle/golden.txt oracle/golden.txt
+13
View File
@@ -28,6 +28,19 @@ target_link_libraries(golden_test PRIVATE cr_literal)
add_executable(diff_corpus tests/diff_corpus.cpp) add_executable(diff_corpus tests/diff_corpus.cpp)
target_link_libraries(diff_corpus PRIVATE cr_literal cr_fast) target_link_libraries(diff_corpus PRIVATE cr_literal cr_fast)
# Regression test for offsets at the extremes of `Int`.
add_executable(extreme_test tests/extreme.cpp)
target_include_directories(extreme_test PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/tests)
target_link_libraries(extreme_test PRIVATE cr_literal cr_fast)
# Deterministic exhaustive sweep of small readable instances. This is the
# driver for the line-coverage check in `run-coverage.sh`.
add_executable(exhaustive_test tests/exhaustive.cpp)
target_include_directories(exhaustive_test PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/tests)
target_link_libraries(exhaustive_test PRIVATE cr_literal cr_fast)
if(CR_BUILD_BENCH) if(CR_BUILD_BENCH)
add_executable(cr_bench tests/bench.cpp) add_executable(cr_bench tests/bench.cpp)
target_include_directories(cr_bench PRIVATE target_include_directories(cr_bench PRIVATE
+118 -26
View File
@@ -15,10 +15,15 @@ The Lean build is untouched.
| `src/fast.cpp` | The same decision tree, but with a summary of the reported keys and lazily cached cardinalities. | | `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`. | | `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/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/diff_corpus.cpp` | Compares literal and fast on the sorted subset of the golden corpus. |
| `tests/readable_gen.h` | Readable-by-construction instance generator (owns the selector keys and reported keys, with borrowed views into them). |
| `tests/extreme.cpp` | Regression test for offsets at the ends of the 32-bit input range. |
| `tests/exhaustive.cpp` | Deterministic exhaustive sweep of small readable instances (also the coverage driver). |
| `tests/fuzz_readable.cpp` | libFuzzer differential test: literal vs. fast on readable instances. | | `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/fuzz_raw.cpp` | libFuzzer safety test on arbitrary instances under ASan/UBSan. |
| `tests/bench.cpp` | nanobench literal-vs-fast benchmark. | | `tests/bench.cpp` | nanobench literal-vs-fast benchmark. |
| `run-fuzz.sh` | Rebuilds and runs both fuzzers for a fixed time. |
| `run-coverage.sh` | Builds with Clang source-based coverage and requires 100% line coverage of `src/fast.cpp`. |
| `third_party/nanobench.h` | Vendored nanobench (commit `7252da9d96bd0d71f508e9b2813ab1f57fdfaec1`). | | `third_party/nanobench.h` | Vendored nanobench (commit `7252da9d96bd0d71f508e9b2813ab1f57fdfaec1`). |
## Data model ## Data model
@@ -30,6 +35,26 @@ them in place. `ConflictRange` returns owned `std::string`s because the decision
procedure can synthesise endpoints (`keyAfter`, clamp bounds) that are not procedure can synthesise endpoints (`keyAfter`, clamp bounds) that are not
present in the input. present in the input.
Caller-supplied selector offsets must fit in `std::int32_t`. Internally they are
stored as `Int = std::int64_t`, because the closed form also builds derived
selectors whose offsets (for example `end.offset - residualT`) need not fit in
32 bits. All intermediate `Int` arithmetic assumes 64-bit signed integers do not
overflow; with 32-bit input offsets and in-memory key counts that is far from
the limit. The corpus parsers reject offsets outside the 32-bit input range, and
both public `closedForm` entry points assert the same contract in debug builds.
The assertion lives at the API boundary rather than in `Selector`, because
derived selectors legitimately exceed the input range.
## Scope of the two implementations
`literal.cpp` is the total transliteration: it is defined on arbitrary inputs,
and `golden_test` compares it with Lean over the whole generated corpus, most of
which is not readable. `fast.cpp` is the readable-only implementation:
`diff_corpus` filters the corpus to readable cases, and the fuzzers and the
exhaustive sweep generate readable instances. The Lean anchor therefore covers
`literal` on all inputs, while `fast` is anchored to `literal` on the readable
subset.
## Build ## Build
```sh ```sh
@@ -57,13 +82,70 @@ cases that satisfy the readable sorting invariant:
./cpp/build/diff_corpus cpp/oracle/golden.txt ./cpp/build/diff_corpus cpp/oracle/golden.txt
``` ```
`extreme_test` checks both implementations on offsets from `INT32_MIN` to
`INT32_MAX`, which the fuzzers do not generate:
```sh
./cpp/build/extreme_test
# extreme: 7574 cases, 0 mismatches
```
`exhaustive_test` enumerates small readable instances instead of sampling them:
every sorted database of up to four keys over a fixed universe of boundary
keys, every selector key (including `⊥` and `⊤`) with offsets in `[-3, 4]`, and
every readable result (the complete traversal, each nonempty truncated prefix,
or the empty result when the traversal is empty) in both directions:
```sh
./cpp/build/exhaustive_test
# exhaustive: 10562068 cases, 0 mismatches
```
It runs in about a second and is also the coverage driver.
## Coverage
```sh
./cpp/run-coverage.sh
```
The script configures a Clang source-based-coverage build, runs
`exhaustive_test`, and fails unless `src/fast.cpp` has 100% line coverage.
`exhaustive_test` alone covers every line of the summarised implementation:
```text
src/fast.cpp 836 regions (12 missed) 131 functions (0 missed) 773 lines (0 missed) 670 branches (125 missed)
```
That is 100.00% line and function coverage, 98.56% region coverage, and 81.34%
branch coverage. The missed branches are short-circuit operands: line coverage
counts a condition as covered once any operand decides it, but branch coverage
requires every operand to be the deciding one. Reaching 100% branch coverage
would need inputs that force each redundant operand to disagree with the rest of
its condition, which is not practical here.
Because equivalence is only promised on readable inputs, `src/fast.cpp` deletes
*control-flow branches* that no readable input can take: the complete-closed
family, the offset-one total/any wrappers and their `end.offset <= 1` shortcut,
the `end.offset <= 1` shortcut in `completeForwardCrossedGeneral`, the
`crossedCompleteEndLeOne` guard, and one `lastK < end.key` test that readability
always satisfies.
The deletion criterion is deliberately limited to unreachable arms. An
individual predicate operand that a dispatch invariant makes always true at one
call site (for example `!c.r->reverse` inside a condition only reached after the
forward branch was selected) is kept, because it is part of the Lean condition
that the translation mirrors; re-proving the invariant at every site would make
the tree harder to audit. Those operands are why every line is covered while branch coverage stays
around 81%.
## Fuzzing ## Fuzzing
```sh ```sh
cd cpp cd cpp
mkdir -p /tmp/cr-corpus mkdir -p /tmp/cr-corpus/readable /tmp/cr-corpus/raw
./build/fuzz_readable -max_total_time=300 /tmp/cr-corpus ./build/fuzz_readable -max_total_time=300 /tmp/cr-corpus/readable
./build/fuzz_raw -max_total_time=300 /tmp/cr-corpus-raw ./build/fuzz_raw -max_total_time=300 /tmp/cr-corpus/raw
``` ```
or simply `./cpp/run-fuzz.sh` (optionally `./cpp/run-fuzz.sh build 600`). or simply `./cpp/run-fuzz.sh` (optionally `./cpp/run-fuzz.sh build 600`).
@@ -85,8 +167,16 @@ One representative validation run on this machine:
| --- | ---: | --- | | --- | ---: | --- |
| literal vs. Lean golden vectors | 40,000 | 0 mismatches (exact endpoints) | | literal vs. Lean golden vectors | 40,000 | 0 mismatches (exact endpoints) |
| fast vs. literal on sorted corpus | 18,971 | 0 mismatches | | fast vs. literal on sorted corpus | 18,971 | 0 mismatches |
| readable differential fuzzing | 2,310,641 | 0 mismatches | | extreme-offset regression (`extreme_test`) | 7,574 | 0 mismatches |
| raw safety fuzzing | 1,936,245 | no ASan/UBSan findings | | exhaustive readable sweep (`exhaustive_test`) | 10,562,068 | 0 mismatches |
| readable differential fuzzing (60 s) | 1,182,424 | 0 mismatches |
| raw safety fuzzing (60 s) | 1,905,270 | no ASan/UBSan findings |
`extreme_test` covers offsets the fuzzers deliberately avoid (they draw offsets
from `[-256, 255]`). Both implementations decide the closed form's
`keyAfterN n k < upper` test from the key lengths (`keyAfterNLt`) rather than
materialising `n` zero bytes, so an `end.offset` near `INT32_MAX` stays
constant-time instead of attempting a 2 GiB allocation.
## Benchmark ## Benchmark
@@ -94,37 +184,38 @@ One representative validation run on this machine:
./cpp/build/cr_bench ./cpp/build/cr_bench
``` ```
Representative results (Clang 21, `-O3 -DNDEBUG`, medians of nine runs pinned Representative results (Clang 21, default Release build: `-O3 -DNDEBUG`; medians
to one core; relative to the literal translation, lower is better for `fast`). 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: CPU frequency scaling makes the absolute numbers noisy, but the shape is stable:
| case | literal | fast | fast/literal | | case | literal | fast | fast/literal |
| --- | ---: | ---: | ---: | | --- | ---: | ---: | ---: |
| small complete (8 keys) | 41.1 ns | 16.6 ns | 0.40x | | small complete (8 keys) | 41.6 ns | 16.2 ns | 0.39x |
| small truncated forward | 16.9 ns | 23.1 ns | 1.37x | | small truncated forward | 16.9 ns | 22.9 ns | 1.35x |
| crossed truncated forward | 23.9 ns | 24.4 ns | 1.02x | | crossed truncated forward | 56.0 ns | 46.6 ns | 0.83x |
| crossed truncated reverse | 24.0 ns | 24.4 ns | 1.02x | | crossed truncated reverse | 130.1 ns | 82.7 ns | 0.64x |
| long complete forward (512 keys) | 2093 ns | 31.9 ns | 0.02x | | long complete forward (512 keys) | 2189 ns | 32.6 ns | 0.01x |
| long truncated forward | 14.8 ns | 19.8 ns | 1.34x | | long truncated forward | 15.0 ns | 19.6 ns | 1.31x |
| long truncated reverse | 10.4 ns | 14.2 ns | 1.36x | | long truncated reverse | 9.5 ns | 12.8 ns | 1.35x |
The large win is on complete reads: the literal translation rescans the reported The large win is on complete reads: the literal translation rescans the reported
keys many times (`residualT`, `reportedBelowBegin`, `allBelowEnd`, ...), while keys many times (`residualT`, `reportedBelowBegin`, `allBelowEnd`, ...), while
the summary answers those questions in `O(log n)` or from a cached count. 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 Plain truncated cases are dominated by the shared decision tree, so the two
two implementations are close; the literal path is slightly cheaper there implementations are close; the literal path is slightly cheaper there because it
because it performs no summary setup. performs no summary setup. Crossed cases report 16 of a 33-key range, so the
summary wins there too.
Against the same implementation with owned input keys (the previous model), the Against the same implementation with owned input keys (the previous model), the
borrowed-key model changes the costs as follows (nine interleaved runs each, borrowed-key model changes the costs as follows (nine interleaved runs each,
median): median). The crossed cases are absent from this table: their benchmark inputs
were corrected to nonempty ranges after the owned-key measurements.
| case | literal (borrow vs own) | fast (borrow vs own) | | case | literal (borrow vs own) | fast (borrow vs own) |
| --- | ---: | ---: | | --- | ---: | ---: |
| small complete | -64% | -17% | | small complete | -64% | -17% |
| small truncated forward | -33% | -19% | | small truncated forward | -33% | -19% |
| crossed truncated forward | +4% | +2% |
| crossed truncated reverse | +4% | +2% |
| long complete forward | -69% | -15% | | long complete forward | -69% | -15% |
| long truncated forward | -29% | -6% | | long truncated forward | -29% | -6% |
| long truncated reverse | -16% | +16% | | long truncated reverse | -16% | +16% |
@@ -133,15 +224,15 @@ 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 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 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 because it previously normalised complete results the same way. Truncated
reverse (`fast`) is the one small regression: it is two nanoseconds and a few reverse (`fast`) is the one small regression: it is a few nanoseconds and a few
dozen instructions, and the other paths move in the opposite direction, so it 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. reads as code-layout/code-generation noise rather than extra algorithmic work.
## Why the fast implementation is equivalent ## Why the fast implementation is equivalent
The fast implementation replays exactly the same decision tree. The only The fast implementation replays the same decision tree on readable inputs. The
changes are: only changes are:
* `reportedBelowBegin` and `residualT` are summarised once. On readable inputs the * `reportedBelowBegin` and `residualT` are summarised once. On readable inputs the
reported keys are sorted in traversal order, so both counts are binary reported keys are sorted in traversal order, so both counts are binary
@@ -153,6 +244,7 @@ changes are:
owned-key model copied and reversed the reported keys here.) owned-key model copied and reversed the reported keys here.)
* `allBelowEnd` is `residualT == 0` and `anyAtOrAboveEnd` is `residualT != 0`. * `allBelowEnd` is `residualT == 0` and `anyAtOrAboveEnd` is `residualT != 0`.
No branch condition or returned endpoint is otherwise changed. Equivalence is No branch condition or returned endpoint is otherwise changed on readable
therefore only claimed for readable inputs; `fuzz_raw` deliberately does not inputs; branches that `Readable` makes unreachable are omitted. Equivalence is
therefore only claimed for readable inputs, and `fuzz_raw` deliberately does not
compare outputs on arbitrary inputs. compare outputs on arbitrary inputs.
+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
# Build with Clang source-based coverage and require 100% line coverage of the
# summarised implementation (`src/fast.cpp`). `exhaustive_test` enumerates
# small readable instances deterministically and is sufficient on its own.
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
build="${1:-$here/build-coverage}"
cxx="${CXX:-clang++-21}"
profdata="${LLVM_PROFDATA:-$(command -v llvm-profdata-21 || command -v llvm-profdata || true)}"
cov="${LLVM_COV:-$(command -v llvm-cov-21 || command -v llvm-cov || true)}"
if [[ -z "$profdata" || -z "$cov" ]]; then
echo "llvm-profdata and llvm-cov are required" >&2
exit 2
fi
cmake -S "$here" -B "$build" -G Ninja \
-DCMAKE_CXX_COMPILER="$cxx" \
-DCMAKE_BUILD_TYPE=Debug \
-DCR_BUILD_FUZZERS=OFF \
-DCR_BUILD_BENCH=OFF \
-DCMAKE_CXX_FLAGS="-fprofile-instr-generate -fcoverage-mapping" \
-DCMAKE_EXE_LINKER_FLAGS="-fprofile-instr-generate"
cmake --build "$build" --target exhaustive_test
profdir="$build/prof"
rm -rf "$profdir"
mkdir -p "$profdir"
cd "$here"
LLVM_PROFILE_FILE="$profdir/exhaustive-%p.profraw" "$build/exhaustive_test"
"$profdata" merge -sparse "$profdir"/*.profraw -o "$profdir/cov.profdata"
report() {
"$cov" report "$build/exhaustive_test" \
-instr-profile="$profdir/cov.profdata" src/fast.cpp
}
output="$(report)"
printf '%s\n' "$output"
line_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $10 }')"
if [[ "$line_cover" != "100.00%" ]]; then
echo "fast.cpp line coverage is $line_cover, expected 100.00%" >&2
exit 1
fi
echo "fast.cpp: 100.00% line coverage"
+6 -4
View File
@@ -1,15 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Build (if needed) and run the differential and safety fuzzers. # Build and run the differential and safety fuzzers.
set -euo pipefail set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
build="${1:-$here/build}" build="${1:-$here/build}"
seconds="${2:-300}" seconds="${2:-300}"
if [[ ! -x "$build/fuzz_readable" ]]; then # Configure once, then rebuild every run: the build is incremental, so this
cmake -S "$here" -B "$build" -G Ninja -DCMAKE_CXX_COMPILER=clang++-21 # cannot fuzz stale binaries after a source edit.
cmake --build "$build" --target fuzz_readable fuzz_raw if [[ ! -f "$build/CMakeCache.txt" ]]; then
cmake -S "$here" -B "$build" -G Ninja -DCMAKE_CXX_COMPILER="${CXX:-clang++-21}"
fi fi
cmake --build "$build" --target fuzz_readable fuzz_raw
corpus="${TMPDIR:-/tmp}/cr-fuzz-corpus" corpus="${TMPDIR:-/tmp}/cr-fuzz-corpus"
mkdir -p "$corpus/readable" "$corpus/raw" mkdir -p "$corpus/readable" "$corpus/raw"
+34 -106
View File
@@ -5,11 +5,13 @@
// `res.keys` (and re-derives small arithmetic facts) on almost every branch. // `res.keys` (and re-derives small arithmetic facts) on almost every branch.
// This version summarises the reported keys once (two binary searches on the // This version summarises the reported keys once (two binary searches on the
// sorted traversal order) and caches the handful of interval cardinalities the // sorted traversal order) and caches the handful of interval cardinalities the
// decision tree needs, then replays the same tree. Reversed results are handled // decision tree needs, then replays the same tree for readable inputs. Branches
// through a `reversed` view instead of copying the key list. // that the `Readable` invariant makes unreachable are omitted. Reversed results
// are handled through a `reversed` view instead of copying the key list.
#include "fast.h" #include "fast.h"
#include <algorithm> #include <algorithm>
#include <cassert>
#include <utility> #include <utility>
namespace cr::fast { namespace cr::fast {
@@ -32,8 +34,14 @@ struct Obs {
bool ascending = true; bool ascending = true;
std::size_t n = 0; std::size_t n = 0;
const KeyView& firstK() const { return reversed ? keys.back() : keys.front(); } const KeyView& firstK() const {
const KeyView& lastK() const { return reversed ? keys.front() : keys.back(); } assert(!keys.empty());
return reversed ? keys.back() : keys.front();
}
const KeyView& lastK() const {
assert(!keys.empty());
return reversed ? keys.front() : keys.back();
}
}; };
// Facts that only depend on the read and on the *multiset* of reported keys. // Facts that only depend on the read and on the *multiset* of reported keys.
@@ -206,12 +214,11 @@ bool clampsToBottom(const Ctx& c, const Obs& o) {
} }
std::uint64_t cELo(Int oB, Int oE) { std::uint64_t cELo(Int oB, Int oE) {
return std::max(toNat(static_cast<__int128>(2) - oE), return std::max(toNat(2 - oE), toNat(oB - oE + 1));
toNat(static_cast<__int128>(oB) - oE + 1));
} }
std::uint64_t cEminP(Int oE) { std::uint64_t cEminP(Int oE) {
return toNat(static_cast<__int128>(2) - oE); return toNat(2 - oE);
} }
std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) { std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) {
@@ -229,10 +236,6 @@ std::uint64_t closedFormDeficit(const Ctx& c) {
return residualD(rB(c), cardBE(c)); return residualD(rB(c), cardBE(c));
} }
std::uint64_t gapOvershoot(const Ctx& c) {
return natSub(rE(c), static_cast<std::uint64_t>(c.t));
}
std::uint64_t crossedOvershootGeneral(const Ctx& c) { std::uint64_t crossedOvershootGeneral(const Ctx& c) {
return natSub(natSub(rE(c), static_cast<std::uint64_t>(c.t)), rB(c)); return natSub(natSub(rE(c), static_cast<std::uint64_t>(c.t)), rB(c));
} }
@@ -432,7 +435,7 @@ struct Impl {
NatInf lhs = NatInf::of(rE(c)); NatInf lhs = NatInf::of(rE(c));
NatInf rhs = niAdd(cardOK(c.r->end_.key, o.lastK()), NatInf rhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
NatInf::of(static_cast<std::uint64_t>(c.n))); NatInf::of(static_cast<std::uint64_t>(c.n)));
__int128 nPlusBegin = static_cast<__int128>(c.n) + c.r->begin.offset; Int nPlusBegin = static_cast<Int>(c.n) + c.r->begin.offset;
return niEq(lhs, rhs) && nPlusBegin == 1; return niEq(lhs, rhs) && nPlusBegin == 1;
} }
@@ -471,88 +474,21 @@ struct Impl {
if (isTop(c.r->end_.key)) return mk(std::move(L), topKey()); if (isTop(c.r->end_.key)) return mk(std::move(L), topKey());
KeyView E = *c.r->end_.key; KeyView E = *c.r->end_.key;
std::uint64_t m = natSub(natSub(rE(c), c.t), 1); std::uint64_t m = natSub(natSub(rE(c), c.t), 1);
if (keyLt(keyAfterN(m, E), o.lastK())) // Lean's `keyAfterN m E < o.lastK()`, decided without building the `m`
// zero bytes.
if (keyAfterNLt(m, E, o.lastK()))
return mk(asKey(E), topKey()); return mk(asKey(E), topKey());
return mk(std::move(L), topKey()); return mk(std::move(L), topKey());
} }
ConflictRange truncatedForwardClosed(const Ctx& c, const Obs& o) const {
NatInf cap = cardEB(c);
NatInf room = NatInf::of(natSub(rE(c), static_cast<std::uint64_t>(c.n)));
if (niLe(cap, room))
return mk(c.r->begin.key, after(o.lastK()));
return mk(c.r->end_.key, after(o.lastK()));
}
ConflictRange completeClosedWith(OptKey endKey, const Ctx& c, const Obs& o) const {
(void)o;
OptKeyView begin = c.r->begin.key;
if (optLt(c.r->end_.key, c.r->begin.key) &&
niLe(NatInf::of(gapOvershoot(c)), cardEB(c)))
begin = c.r->end_.key;
if (c.r->end_.offset <= 1)
return mk(c.r->begin.key, c.r->end_.key);
if (c.t == rE(c)) return mk(std::move(begin), std::move(endKey));
return mk(std::move(begin), topKey());
}
ConflictRange completeReversedClosed(const Ctx& c, const Obs& o) const {
return completeClosedWith(after(o.firstK()), c, o);
}
ConflictRange completeForwardClosed(const Ctx& c, const Obs& o) const {
return completeClosedWith(after(o.lastK()), c, o);
}
ConflictRange completeClosed(const Ctx& c, const Obs& o) const {
return c.r->reverse ? completeReversedClosed(c, o)
: completeForwardClosed(c, o);
}
ConflictRange closedFormOffsetOneTruncated(const Ctx& c, const Obs& o) const {
if (c.r->end_.offset <= 0)
return mk(c.r->begin.key, c.r->end_.key);
return mk(c.r->begin.key, after(o.lastK()));
}
ConflictRange topRange(const Ctx& c) const { ConflictRange topRange(const Ctx& c) const {
return mk(c.r->begin.key, topKey()); return mk(c.r->begin.key, topKey());
} }
ConflictRange closedFormOffsetOneWith(OptKey endKey, const Ctx& c,
const Obs& o) const {
(void)o;
if (c.r->end_.offset <= 1)
return mk(c.r->begin.key, c.r->end_.key);
if (c.t == rE(c))
return mk(c.r->begin.key, std::move(endKey));
return topRange(c);
}
ConflictRange closedFormOffsetOneRev(const Ctx& c, const Obs& o) const {
return closedFormOffsetOneWith(after(o.firstK()), c, o);
}
ConflictRange closedFormOffsetOne(const Ctx& c, const Obs& o) const { ConflictRange closedFormOffsetOne(const Ctx& c, const Obs& o) const {
return closedFormOffsetOneWith(after(o.lastK()), c, o); if (c.t == rE(c))
} return mk(c.r->begin.key, after(o.lastK()));
ConflictRange closedFormOffsetOneAny(const Ctx& c, const Obs& o) const { return topRange(c);
return c.r->reverse ? closedFormOffsetOneRev(c, o)
: closedFormOffsetOne(c, o);
}
ConflictRange closedFormOffsetOneUniversal(const Ctx& c, const Obs& o) const {
if (o.more)
return c.r->reverse ? closedFormReverseTruncatedZero(c, o)
: closedFormOffsetOneTruncated(c, o);
return closedFormOffsetOneAny(c, o);
}
ConflictRange closedFormOffsetOneTotal(const Ctx& c, const Obs& o) const {
if (optLt(c.r->end_.key, c.r->begin.key)) {
if (o.more)
return c.r->reverse ? closedFormReverseTruncatedZero(c, o)
: truncatedForwardClosed(c, o);
return completeClosed(c, o);
}
return closedFormOffsetOneUniversal(c, o);
} }
// ----- zero crossed ------------------------------------------------------ // ----- zero crossed ------------------------------------------------------
@@ -562,8 +498,6 @@ struct Impl {
} }
ConflictRange completeForwardCrossedGeneral(const Ctx& c, const Obs& o) const { ConflictRange completeForwardCrossedGeneral(const Ctx& c, const Obs& o) const {
if (c.r->end_.offset <= 1)
return mk(c.r->begin.key, c.r->end_.key);
if (c.t + rB(c) == rE(c)) if (c.t + rB(c) == rE(c))
return mk(c.r->end_.key, after(o.lastK())); return mk(c.r->end_.key, after(o.lastK()));
return mk(crossedBegin(c), topKey()); return mk(crossedBegin(c), topKey());
@@ -592,7 +526,7 @@ struct Impl {
NatInf g = cardOK(c.r->end_.key, H); NatInf g = cardOK(c.r->end_.key, H);
bool useEnd = oLtK(c.r->end_.key, H) && bool useEnd = oLtK(c.r->end_.key, H) &&
niLt(niSub(NatInf::of(e), NatInf::of(t)), g); niLt(niSub(NatInf::of(e), NatInf::of(t)), g);
return mk(useEnd ? c.r->end_.key : OptKey{H}, topKey()); return mk(useEnd ? c.r->end_.key : asKey(H), topKey());
} }
ConflictRange a = zeroCrossLongAnchor(c, o); ConflictRange a = zeroCrossLongAnchor(c, o);
return mk(std::move(a.begin), optMax(a.end_, c.r->begin.key)); return mk(std::move(a.begin), optMax(a.end_, c.r->begin.key));
@@ -601,7 +535,7 @@ struct Impl {
ConflictRange closedFormZeroCrossUnderEnd(const Ctx& c, const Obs& o) const { ConflictRange closedFormZeroCrossUnderEnd(const Ctx& c, const Obs& o) const {
RangeRead a = zeroBeginAnchor(c, o); RangeRead a = zeroBeginAnchor(c, o);
Ctx ca = makeCtx(a, o); Ctx ca = makeCtx(a, o);
return closedFormOffsetOneTotal(ca, o); return closedFormOffsetOne(ca, o);
} }
ConflictRange closedFormZeroCrossComplete(const Ctx& c, const Obs& o) const { ConflictRange closedFormZeroCrossComplete(const Ctx& c, const Obs& o) const {
@@ -671,8 +605,8 @@ struct Impl {
intervalCard(r.end_.key, r.begin.key)) && intervalCard(r.end_.key, r.begin.key)) &&
niLe(NatInf::of(natSub(e, b)), niLe(NatInf::of(natSub(e, b)),
intervalCard(r.begin.key, r.end_.key)); intervalCard(r.begin.key, r.end_.key));
__int128 ePlusEnd = static_cast<__int128>(e) + r.end_.offset - 1; Int ePlusEnd = static_cast<Int>(e) + r.end_.offset - 1;
__int128 bPlusBegin = static_cast<__int128>(b) + r.begin.offset - 1; Int bPlusBegin = static_cast<Int>(b) + r.begin.offset - 1;
inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd; inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd;
} }
return !inner; return !inner;
@@ -694,8 +628,7 @@ struct Impl {
if (r.begin.offset <= 0) { if (r.begin.offset <= 0) {
if (r.end_.offset >= 2) return mk(bottomKey(), topKey()); if (r.end_.offset >= 2) return mk(bottomKey(), topKey());
if (niLt(intervalCard(r.end_.key, r.begin.key), if (niLt(intervalCard(r.end_.key, r.begin.key),
NatInf::of(toNat(static_cast<__int128>(r.end_.offset) - NatInf::of(toNat(r.end_.offset - r.begin.offset))))
r.begin.offset))))
return mk(bottomKey(), r.end_.key); return mk(bottomKey(), r.end_.key);
return mk(bottomKey(), optMax(r.begin.key, r.end_.key)); return mk(bottomKey(), optMax(r.begin.key, r.end_.key));
} }
@@ -826,10 +759,7 @@ struct Impl {
return zeroCrossOvershootComplete(c, o) || negCrossOvershoot(c, o); return zeroCrossOvershootComplete(c, o) || negCrossOvershoot(c, o);
} }
bool crossedCompleteEndLeOne(const Ctx& c, const Obs& o) const { bool crossedCompleteEndLeOne(const Ctx& c) const {
if (!(!c.r->reverse && !o.more && c.r->begin.offset <= 0 &&
kLtO(o.firstK(), c.r->begin.key)))
return false;
if (c.r->end_.offset == 1 && bSlack(c) < c.below) if (c.r->end_.offset == 1 && bSlack(c) < c.below)
return true; return true;
return c.r->end_.offset <= 0 && return c.r->end_.offset <= 0 &&
@@ -842,7 +772,7 @@ struct Impl {
} }
bool zeroCrossLongFinite(const Ctx& c, const Obs& o) const { bool zeroCrossLongFinite(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && !c.r->reverse && return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
c.r->end_.offset >= 2 && !isTop(c.r->begin.key) && c.r->end_.offset >= 2 && !isTop(c.r->begin.key) &&
optLt(c.r->end_.key, c.r->begin.key) && optLt(c.r->end_.key, c.r->begin.key) &&
kLtO(leastReportedK(c, o), c.r->begin.key); kLtO(leastReportedK(c, o), c.r->begin.key);
@@ -862,7 +792,7 @@ struct Impl {
ConflictRange closedFormCrossedComplete(const Ctx& c, const Obs& o) const { ConflictRange closedFormCrossedComplete(const Ctx& c, const Obs& o) const {
if (crossedCompleteOvershoot(c, o)) if (crossedCompleteOvershoot(c, o))
return closedFormNegCrossTopUnified(c, o); return closedFormNegCrossTopUnified(c, o);
if (crossedCompleteEndLeOne(c, o)) if (crossedCompleteEndLeOne(c))
return closedFormNegCrossNonclampUnified(c, o); return closedFormNegCrossNonclampUnified(c, o);
if (crossedCompleteBeginZero(c, o)) if (crossedCompleteBeginZero(c, o))
return closedFormZeroCrossResidual(c, o); return closedFormZeroCrossResidual(c, o);
@@ -891,8 +821,7 @@ struct Impl {
c.r->end_.offset <= 1 && c.r->end_.offset <= 1 &&
niLe(niAdd(niAdd(cardEB(c), niLe(niAdd(niAdd(cardEB(c),
NatInf::of(static_cast<std::uint64_t>(c.n))), NatInf::of(static_cast<std::uint64_t>(c.n))),
NatInf::of(toNat(static_cast<__int128>(1) - NatInf::of(toNat(1 - c.r->end_.offset))),
c.r->end_.offset))),
NatInf::of(bSlack(c) + 1)); NatInf::of(bSlack(c) + 1));
} }
@@ -954,8 +883,7 @@ struct Impl {
bool negCrossTruncTopTopEq(const Ctx& c, const Obs& o) const { bool negCrossTruncTopTopEq(const Ctx& c, const Obs& o) const {
if (!negCrossTruncTopTop(c, o)) return false; if (!negCrossTruncTopTop(c, o)) return false;
std::uint64_t extra = std::uint64_t extra = toNat(1 - c.r->end_.offset);
toNat(static_cast<__int128>(1) - c.r->end_.offset);
return static_cast<std::uint64_t>(c.n) + extra == bSlack(c) + 1; return static_cast<std::uint64_t>(c.n) + extra == bSlack(c) + 1;
} }
@@ -1001,9 +929,8 @@ struct Impl {
if (c.r->end_.offset <= 0) { if (c.r->end_.offset <= 0) {
if (optLt(c.r->end_.key, c.r->begin.key)) { if (optLt(c.r->end_.key, c.r->begin.key)) {
if (clampNonposBottom(c)) { if (clampNonposBottom(c)) {
if (kLtO(o.lastK(), c.r->end_.key)) // Readability gives `lastK < resolve end <= end.key`.
return closedFormNegCrossTruncNonposGapA2aBottom(c, o); return closedFormNegCrossTruncNonposGapA2aBottom(c, o);
return mk(bottomKey(), clampLongUpper(c, o));
} }
return mk(OptKey{o.firstK()}, c.r->begin.key); return mk(OptKey{o.firstK()}, c.r->begin.key);
} }
@@ -1071,6 +998,7 @@ struct Impl {
} // namespace } // namespace
ConflictRange closedForm(const RangeRead& r, const Result& res) { ConflictRange closedForm(const RangeRead& r, const Result& res) {
assertInputOffsetsFitInt32(r);
return Impl{}.closedForm(r, res); return Impl{}.closedForm(r, res);
} }
+14 -12
View File
@@ -13,6 +13,7 @@ namespace {
using cr::afterFirst; using cr::afterFirst;
using cr::afterLast; using cr::afterLast;
using cr::asKey; using cr::asKey;
using cr::assertInputOffsetsFitInt32;
using cr::beginSlack; using cr::beginSlack;
using cr::bottomKey; using cr::bottomKey;
using cr::clampNonposD; using cr::clampNonposD;
@@ -22,7 +23,7 @@ using cr::intervalCard;
using cr::isBottom; using cr::isBottom;
using cr::isTop; using cr::isTop;
using cr::keyAfter; using cr::keyAfter;
using cr::keyAfterN; using cr::keyAfterNLt;
using cr::keyLt; using cr::keyLt;
using cr::last; using cr::last;
using cr::lastKey; using cr::lastKey;
@@ -113,12 +114,11 @@ bool clampsToBottom(const RangeRead& r, const Result& res) {
} }
std::uint64_t cELo(Int oB, Int oE) { std::uint64_t cELo(Int oB, Int oE) {
return std::max(toNat(static_cast<__int128>(2) - oE), return std::max(toNat(2 - oE), toNat(oB - oE + 1));
toNat(static_cast<__int128>(oB) - oE + 1));
} }
std::uint64_t cEminP(Int oE) { std::uint64_t cEminP(Int oE) {
return toNat(static_cast<__int128>(2) - oE); return toNat(2 - oE);
} }
std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) { std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) {
@@ -355,7 +355,7 @@ bool sentinelLargeTight(const RangeRead& r, const Result& res) {
NatInf lhs = NatInf::of(residualE(r)); NatInf lhs = NatInf::of(residualE(r));
NatInf rhs = niAdd(intervalCard(r.end_.key, last(res)), NatInf rhs = niAdd(intervalCard(r.end_.key, last(res)),
NatInf::of(res.keys.size())); NatInf::of(res.keys.size()));
__int128 nPlusBegin = static_cast<__int128>(res.keys.size()) + r.begin.offset; Int nPlusBegin = static_cast<Int>(res.keys.size()) + r.begin.offset;
return niEq(lhs, rhs) && nPlusBegin == 1; return niEq(lhs, rhs) && nPlusBegin == 1;
} }
@@ -400,7 +400,9 @@ ConflictRange closedFormReverseTruncatedZero(const RangeRead& r, const Result& r
if (isTop(r.end_.key)) return mkRange(std::move(L), topKey()); if (isTop(r.end_.key)) return mkRange(std::move(L), topKey());
KeyView E = *r.end_.key; KeyView E = *r.end_.key;
std::uint64_t n = natSub(natSub(residualE(r), residualT(r, res)), 1); std::uint64_t n = natSub(natSub(residualE(r), residualT(r, res)), 1);
if (keyLt(keyAfterN(n, E), lastKey(res))) // Lean's `keyAfterN n E < lastKey res`, decided without building the `n`
// zero bytes.
if (keyAfterNLt(n, E, lastKey(res)))
return mkRange(asKey(E), topKey()); return mkRange(asKey(E), topKey());
return mkRange(std::move(L), topKey()); return mkRange(std::move(L), topKey());
} }
@@ -592,8 +594,8 @@ bool AlwaysEmpty(const RangeRead& r) {
niLe(NatInf::of(e), underEnd) && niLe(NatInf::of(e), underEnd) &&
niLe(NatInf::of(natSub(b, e)), intervalCard(r.end_.key, r.begin.key)) && 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)); niLe(NatInf::of(natSub(e, b)), intervalCard(r.begin.key, r.end_.key));
__int128 ePlusEnd = static_cast<__int128>(e) + r.end_.offset - 1; Int ePlusEnd = static_cast<Int>(e) + r.end_.offset - 1;
__int128 bPlusBegin = static_cast<__int128>(b) + r.begin.offset - 1; Int bPlusBegin = static_cast<Int>(b) + r.begin.offset - 1;
inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd; inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd;
} }
return !inner; return !inner;
@@ -615,8 +617,7 @@ ConflictRange emptyValue(const RangeRead& r) {
if (r.begin.offset <= 0) { if (r.begin.offset <= 0) {
if (r.end_.offset >= 2) return mkRange(bottomKey(), topKey()); if (r.end_.offset >= 2) return mkRange(bottomKey(), topKey());
if (niLt(intervalCard(r.end_.key, r.begin.key), if (niLt(intervalCard(r.end_.key, r.begin.key),
NatInf::of(toNat(static_cast<__int128>(r.end_.offset) - NatInf::of(toNat(r.end_.offset - r.begin.offset))))
r.begin.offset))))
return mkRange(bottomKey(), r.end_.key); return mkRange(bottomKey(), r.end_.key);
return mkRange(bottomKey(), optMax(r.begin.key, r.end_.key)); return mkRange(bottomKey(), optMax(r.begin.key, r.end_.key));
} }
@@ -799,7 +800,7 @@ bool negCrossTruncRevCrossedLow(const RangeRead& r, const Result& res) {
r.end_.offset <= 1 && r.end_.offset <= 1 &&
niLe(niAdd(niAdd(intervalCard(r.end_.key, r.begin.key), niLe(niAdd(niAdd(intervalCard(r.end_.key, r.begin.key),
NatInf::of(res.keys.size())), NatInf::of(res.keys.size())),
NatInf::of(toNat(static_cast<__int128>(1) - r.end_.offset))), NatInf::of(toNat(1 - r.end_.offset))),
NatInf::of(beginSlack(r) + 1)); NatInf::of(beginSlack(r) + 1));
} }
@@ -861,7 +862,7 @@ bool negCrossTruncTopTop(const RangeRead& r, const Result& res) {
bool negCrossTruncTopTopEq(const RangeRead& r, const Result& res) { bool negCrossTruncTopTopEq(const RangeRead& r, const Result& res) {
if (!negCrossTruncTopTop(r, res)) return false; if (!negCrossTruncTopTop(r, res)) return false;
std::uint64_t extra = toNat(static_cast<__int128>(1) - r.end_.offset); std::uint64_t extra = toNat(1 - r.end_.offset);
return res.keys.size() + extra == beginSlack(r) + 1; return res.keys.size() + extra == beginSlack(r) + 1;
} }
@@ -951,6 +952,7 @@ ConflictRange closedFormCrossed(const RangeRead& r, const Result& res) {
} // namespace } // namespace
ConflictRange closedForm(const RangeRead& r, const Result& res) { ConflictRange closedForm(const RangeRead& r, const Result& res) {
assertInputOffsetsFitInt32(r);
if (res.keys.empty()) return emptyValue(r); if (res.keys.empty()) return emptyValue(r);
if (r.begin.offset >= 1 || clampedNonpos(r, res)) if (r.begin.offset >= 1 || clampedNonpos(r, res))
return closedFormPositive(r, res); return closedFormPositive(r, res);
+50 -23
View File
@@ -16,8 +16,10 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <cassert>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <limits>
#include <optional> #include <optional>
#include <span> #include <span>
#include <string> #include <string>
@@ -36,10 +38,16 @@ using Key = std::string;
using KeyView = std::string_view; using KeyView = std::string_view;
using OptKey = std::optional<Key>; using OptKey = std::optional<Key>;
using OptKeyView = std::optional<KeyView>; using OptKeyView = std::optional<KeyView>;
// Lean's unbounded `Int`. Caller-supplied offsets are required to fit in
// `std::int32_t` (see `Selector`), so all intermediate arithmetic in `Int`
// stays far from overflow; the code below relies on that.
using Int = std::int64_t; using Int = std::int64_t;
struct Selector { struct Selector {
OptKeyView key; OptKeyView key;
// Offsets supplied by callers must lie in `[INT32_MIN, INT32_MAX]`. They are
// stored as `Int` because the closed form also builds derived selectors whose
// offsets (for example `end.offset - residualT`) need not fit in 32 bits.
Int offset = 0; Int offset = 0;
}; };
@@ -49,6 +57,29 @@ struct RangeRead {
bool reverse = false; bool reverse = false;
}; };
// The public offset contract: caller-supplied offsets must fit in
// `std::int32_t`. This is checked at the public `closedForm` entry points
// (debug builds only) and by the corpus parsers. It is deliberately not
// checked in `Selector`, because the decision procedure derives selectors
// whose offsets may exceed this range.
inline constexpr Int kMinInputOffset =
std::numeric_limits<std::int32_t>::min();
inline constexpr Int kMaxInputOffset =
std::numeric_limits<std::int32_t>::max();
inline bool offsetsFitInt32(Int begin, Int end) {
return begin >= kMinInputOffset && begin <= kMaxInputOffset &&
end >= kMinInputOffset && end <= kMaxInputOffset;
}
inline bool inputOffsetsFitInt32(const RangeRead& r) {
return offsetsFitInt32(r.begin.offset, r.end_.offset);
}
inline void assertInputOffsetsFitInt32(const RangeRead& r) {
assert(inputOffsetsFitInt32(r));
}
struct Result { struct Result {
// A non-owning view of the reported keys. The caller keeps the underlying // A non-owning view of the reported keys. The caller keeps the underlying
// keys alive for the duration of the call. // keys alive for the duration of the call.
@@ -72,8 +103,6 @@ inline bool operator!=(const ConflictRange& a, const ConflictRange& b) {
// Borrowed/owned conversions // Borrowed/owned conversions
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
inline KeyView keyView(const Key& k) { return KeyView(k); }
inline OptKeyView optView(const OptKey& k) { inline OptKeyView optView(const OptKey& k) {
return k ? OptKeyView{KeyView(*k)} : std::nullopt; return k ? OptKeyView{KeyView(*k)} : std::nullopt;
} }
@@ -93,8 +122,6 @@ inline OptKey ownKey(OptKey&& k) { return std::move(k); }
inline bool keyLt(KeyView a, KeyView b) { return a.compare(b) < 0; } 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> template <class A, class B>
inline bool optLt(const std::optional<A>& a, const std::optional<B>& b) { inline bool optLt(const std::optional<A>& a, const std::optional<B>& b) {
if (!a) return false; // ⊤ < x is false if (!a) return false; // ⊤ < x is false
@@ -136,7 +163,6 @@ inline KeyView emptyKeyView() {
static const Key empty; static const Key empty;
return KeyView(empty); return KeyView(empty);
} }
inline OptKeyView topKeyView() { return std::nullopt; }
inline OptKeyView bottomKeyView() { return OptKeyView{emptyKeyView()}; } inline OptKeyView bottomKeyView() { return OptKeyView{emptyKeyView()}; }
inline bool isTop(OptKeyView k) { return !k.has_value(); } inline bool isTop(OptKeyView k) { return !k.has_value(); }
@@ -151,13 +177,6 @@ inline Key keyAfter(KeyView k) {
return out; 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) { inline bool isPrefix(KeyView prefix, KeyView whole) {
return prefix.size() <= whole.size() && return prefix.size() <= whole.size() &&
std::equal(prefix.begin(), prefix.end(), whole.begin()); std::equal(prefix.begin(), prefix.end(), whole.begin());
@@ -169,6 +188,20 @@ inline bool allZeroFrom(KeyView k, std::size_t from) {
return true; return true;
} }
// Decide Lean's `keyAfterN n k < upper`, i.e. `k ++ 0^n < upper`, without
// materialising the `n` zero bytes. `n` comes from an offset, so with 32-bit
// input offsets it can still be about `2^31`; the comparison only needs the
// lengths and the first `upper.size() - k.size()` bytes of `upper`.
inline bool keyAfterNLt(std::uint64_t n, KeyView k, KeyView upper) {
const std::size_t m = std::min(k.size(), upper.size());
const int c = KeyView(k).substr(0, m).compare(KeyView(upper).substr(0, m));
if (c != 0) return c < 0;
if (k.size() >= upper.size()) return false; // `upper` is a prefix of `k ++ 0^n`
const std::uint64_t rem = static_cast<std::uint64_t>(upper.size() - k.size());
if (n < rem) return true; // proper prefix, or a nonzero byte decides `0 < b`
return !allZeroFrom(upper, k.size()); // equal iff `n == rem` and all zeros
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// `ℕ∞` (ENat) // `ℕ∞` (ENat)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -209,10 +242,9 @@ inline NatInf niSub(NatInf a, NatInf b) {
} }
// `Int.toNat`. // `Int.toNat`.
inline std::uint64_t toNat(__int128 x) { inline std::uint64_t toNat(Int x) {
return x <= 0 ? 0 : static_cast<std::uint64_t>(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. // Natural-number truncated subtraction.
inline std::uint64_t natSub(std::uint64_t a, std::uint64_t b) { inline std::uint64_t natSub(std::uint64_t a, std::uint64_t b) {
@@ -293,21 +325,16 @@ inline std::uint64_t reportedBelowBegin(const RangeRead& r, const Result& res) {
} }
inline std::uint64_t residualB(const RangeRead& r) { inline std::uint64_t residualB(const RangeRead& r) {
return toNat(static_cast<__int128>(r.begin.offset) - 1); return toNat(r.begin.offset - 1);
} }
inline std::uint64_t residualE(const RangeRead& r) { inline std::uint64_t residualE(const RangeRead& r) {
return toNat(static_cast<__int128>(r.end_.offset) - 1); return toNat(r.end_.offset - 1);
} }
inline std::uint64_t beginSlack(const RangeRead& r) { inline std::uint64_t beginSlack(const RangeRead& r) {
return toNat(-static_cast<__int128>(r.begin.offset)); return toNat(-r.begin.offset);
} }
inline std::uint64_t clampNonposD(const RangeRead& r) { inline std::uint64_t clampNonposD(const RangeRead& r) {
return toNat(-static_cast<__int128>(r.end_.offset)); return toNat(-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 } // namespace cr
+12 -11
View File
@@ -23,7 +23,8 @@ struct Case {
Result res; Result res;
const char* name = ""; const char* name = "";
// Build the borrowed views once the case sits at its final address. // Build the borrowed views in place. `Case` is move-only (through
// `Instance`), and the views must be rebuilt after any move.
void prepare() { void prepare() {
inst.finish(); inst.finish();
r = inst.read(); r = inst.read();
@@ -38,9 +39,7 @@ std::vector<Key> makeDb(std::size_t n, std::size_t len) {
Key k; Key k;
k.reserve(len); k.reserve(len);
for (std::size_t j = 0; j < len; ++j) for (std::size_t j = 0; j < len; ++j)
k.push_back(static_cast<Byte>((i >> (8 * j)) & 0xff)); k.push_back(static_cast<char>(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)); db.push_back(std::move(k));
} }
std::sort(db.begin(), db.end()); std::sort(db.begin(), db.end());
@@ -117,10 +116,12 @@ int main() {
// A short truncated read (cross-free). // A short truncated read (cross-free).
cases.push_back(makeCase(smallDb, smallDb[1], 1, top, 1, false, true, cases.push_back(makeCase(smallDb, smallDb[1], 1, top, 1, false, true,
"small truncated fwd")); "small truncated fwd"));
// A crossed read: the end selector resolves before the begin selector. // A crossed read: the end *key* is below the begin key, but the offsets move
cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, false, // the resolved anchors back into order, so the range is nonempty and the
// dispatch takes the crossed branch.
cases.push_back(makeCase(mediumDb, mediumDb[40], -30, mediumDb[3], 40, false,
true, "crossed truncated fwd")); true, "crossed truncated fwd"));
cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, true, cases.push_back(makeCase(mediumDb, mediumDb[40], -30, mediumDb[3], 40, true,
true, "crossed truncated rev")); true, "crossed truncated rev"));
// Long complete / truncated reads. // Long complete / truncated reads.
cases.push_back( cases.push_back(
@@ -130,10 +131,10 @@ int main() {
cases.push_back(makeCase(longDb, bot, 1, top, 1, true, true, cases.push_back(makeCase(longDb, bot, 1, top, 1, true, true,
"long truncated rev")); "long truncated rev"));
// The case vector is final now, so the borrowed views can point at it. // Prepare each case in place (its views borrow from the instance) and check
for (Case& c : cases) c.prepare(); // that both implementations agree before timing them.
for (Case& c : cases) {
for (const Case& c : cases) { c.prepare();
check(c); check(c);
Bench bench; Bench bench;
bench.title(c.name).relative(true).warmup(20).minEpochIterations(200); bench.title(c.name).relative(true).warmup(20).minEpochIterations(200);
+31 -8
View File
@@ -1,5 +1,6 @@
// Compare the literal translation with the summarised implementation on the // Compare the literal translation with the summarised implementation on the
// Lean-generated corpus (readable or not). // Lean-generated corpus (readable or not).
#include <cstdint>
#include <cstdio> #include <cstdio>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
@@ -26,7 +27,7 @@ bool parseKey(const std::string& tok, OptKey& out) {
for (std::size_t i = 0; i < tok.size(); i += 2) { for (std::size_t i = 0; i < tok.size(); i += 2) {
int hi = hex(tok[i]), lo = hex(tok[i + 1]); int hi = hex(tok[i]), lo = hex(tok[i + 1]);
if (hi < 0 || lo < 0) return false; if (hi < 0 || lo < 0) return false;
k.push_back(static_cast<Byte>(hi * 16 + lo)); k.push_back(static_cast<char>(static_cast<Byte>(hi * 16 + lo)));
} }
out = std::move(k); out = std::move(k);
return true; return true;
@@ -37,7 +38,8 @@ std::string encKey(const OptKey& k) {
if (k->empty()) return "-"; if (k->empty()) return "-";
static const char* d = "0123456789abcdef"; static const char* d = "0123456789abcdef";
std::string s; std::string s;
for (Byte b : *k) { s.push_back(d[b >> 4]); s.push_back(d[b & 15]); } for (char ch : *k) {
const Byte b = static_cast<Byte>(ch); s.push_back(d[b >> 4]); s.push_back(d[b & 15]); }
return s; return s;
} }
} // namespace } // namespace
@@ -45,9 +47,15 @@ std::string encKey(const OptKey& k) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
if (argc < 2) return 2; if (argc < 2) return 2;
std::ifstream in(argv[1]); std::ifstream in(argv[1]);
if (!in) {
std::fprintf(stderr, "cannot open %s\n", argv[1]);
return 2;
}
std::string line; std::string line;
std::size_t cases = 0, bad = 0; std::size_t lineNo = 0;
std::size_t cases = 0, bad = 0, skipped = 0;
while (std::getline(in, line)) { while (std::getline(in, line)) {
++lineNo;
if (line.empty() || line[0] == '#') continue; if (line.empty() || line[0] == '#') continue;
std::istringstream ss(line); std::istringstream ss(line);
std::string bk, ek, arrow, ob, oe; std::string bk, ek, arrow, ob, oe;
@@ -55,8 +63,14 @@ int main(int argc, char** argv) {
int rev = 0, more = 0; int rev = 0, more = 0;
std::size_t n = 0; std::size_t n = 0;
OptKey beginKey, endKey; OptKey beginKey, endKey;
if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n)) continue; if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n) ||
if (!parseKey(bk, beginKey) || !parseKey(ek, endKey)) continue; !parseKey(bk, beginKey) || !parseKey(ek, endKey) || n > line.size() ||
!cr::offsetsFitInt32(static_cast<cr::Int>(bo),
static_cast<cr::Int>(eo))) {
std::fprintf(stderr, "line %zu: bad header: %s\n", lineNo, line.c_str());
++bad;
continue;
}
// Owned reported keys plus the views borrowed by both implementations. // Owned reported keys plus the views borrowed by both implementations.
std::vector<Key> keys; std::vector<Key> keys;
keys.reserve(n); keys.reserve(n);
@@ -68,7 +82,11 @@ int main(int argc, char** argv) {
if (!parseKey(kt, p) || !p) { ok = false; break; } if (!parseKey(kt, p) || !p) { ok = false; break; }
keys.push_back(std::move(*p)); keys.push_back(std::move(*p));
} }
if (!ok || !(ss >> arrow >> ob >> oe) || arrow != "=>") continue; if (!ok || !(ss >> arrow >> ob >> oe) || arrow != "=>") {
std::fprintf(stderr, "line %zu: bad body: %s\n", lineNo, line.c_str());
++bad;
continue;
}
// `fast` assumes the readable sorting invariant, so only compare cases that // `fast` assumes the readable sorting invariant, so only compare cases that
// satisfy it (this is a superset of the readable cases). // satisfy it (this is a superset of the readable cases).
const bool reverse = rev != 0; const bool reverse = rev != 0;
@@ -80,7 +98,7 @@ int main(int argc, char** argv) {
break; break;
} }
} }
if (!sorted) continue; if (!sorted) { ++skipped; continue; }
std::vector<KeyView> keyViews; std::vector<KeyView> keyViews;
keyViews.reserve(keys.size()); keyViews.reserve(keys.size());
for (const Key& k : keys) keyViews.push_back(k); for (const Key& k : keys) keyViews.push_back(k);
@@ -99,6 +117,11 @@ int main(int argc, char** argv) {
} }
} }
} }
std::printf("diff: %zu cases, %zu mismatches\n", cases, bad); if (cases == 0) {
std::fprintf(stderr, "diff: no comparable cases in %s\n", argv[1]);
return 1;
}
std::printf("diff: %zu cases, %zu skipped, %zu mismatches\n", cases, skipped,
bad);
return bad == 0 ? 0 : 1; return bad == 0 ? 0 : 1;
} }
+111
View File
@@ -0,0 +1,111 @@
// A deterministic exhaustive sweep of small readable instances, comparing the
// literal translation with the summarised implementation.
//
// The fuzzers sample readable instances; this test enumerates them: every
// sorted database (up to four keys) over a fixed universe of interesting keys,
// every selector key (including ⊥ and ⊤) paired with offsets in [-3, 4], and
// every readable result (the complete traversal, each nonempty truncated
// prefix, or the empty result when the traversal is empty) in both directions.
//
// Together with `golden_test`, `diff_corpus`, and `extreme_test`, this covers
// every line of `src/fast.cpp`.
#include <algorithm>
#include <bit>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include "../src/fast.h"
#include "../src/literal.h"
#include "readable_gen.h"
namespace {
using namespace cr;
const Int kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4};
void check(const RangeRead& r, const Result& res, long long& cases,
long long& bad, const char* kind, Int bo, Int eo, bool reverse,
std::size_t keep) {
++cases;
if (!gen::equivalent(literal::closedForm(r, res), fast::closedForm(r, res))) {
if (++bad <= 10)
std::fprintf(stderr,
"MISMATCH %s bo=%lld eo=%lld rev=%d keep=%zu\n", kind,
static_cast<long long>(bo), static_cast<long long>(eo),
reverse, keep);
}
}
void run(const std::vector<Key>& db, const std::vector<OptKey>& keys,
long long& cases, long long& bad) {
for (const OptKey& beginKey : keys) {
for (const OptKey& endKey : keys) {
for (Int beginOffset : kOffsets) {
for (Int endOffset : kOffsets) {
const Selector begin{optView(beginKey), beginOffset};
const Selector end{optView(endKey), endOffset};
const std::vector<Key> complete = gen::inRange(
db, gen::resolve(db, begin), gen::resolve(db, end));
for (bool reverse : {false, true}) {
const RangeRead r{begin, end, reverse};
std::vector<Key> ordered = complete;
if (reverse) std::reverse(ordered.begin(), ordered.end());
if (ordered.empty()) {
// The only readable result for an empty traversal.
const std::vector<KeyView> none;
check(r, Result{none, false}, cases, bad, "empty", beginOffset,
endOffset, reverse, 0);
continue;
}
// Every nonempty prefix, including the whole traversal with
// `more = true` (readability does not require a proper prefix).
for (std::size_t keep = 1; keep <= ordered.size(); ++keep) {
std::vector<KeyView> views;
views.reserve(keep);
for (std::size_t i = 0; i < keep; ++i) views.push_back(ordered[i]);
check(r, Result{views, true}, cases, bad, "truncated",
beginOffset, endOffset, reverse, keep);
}
// The complete result.
std::vector<KeyView> views;
views.reserve(ordered.size());
for (const Key& k : ordered) views.push_back(k);
check(r, Result{views, false}, cases, bad, "complete", beginOffset,
endOffset, reverse, ordered.size());
}
}
}
}
}
}
} // namespace
int main() {
// Prefix chains exercise the `intervalCard` and `keyAfter` cases; `⊤` and
// the empty key are handled separately as selector keys.
const std::vector<Key> universe{
Key(""), Key("\0", 1), Key("\0\0", 2), Key("\0\xff", 2),
Key("a"), Key("a\0", 2), Key("a\0\0", 3), Key("a\0\0\0", 4),
Key("b"), Key("\xff", 1)};
std::vector<OptKey> keys;
keys.emplace_back(std::nullopt);
for (const Key& k : universe) keys.emplace_back(k);
long long cases = 0, bad = 0;
const std::uint32_t limit = 1u << universe.size();
for (std::uint32_t mask = 0; mask < limit; ++mask) {
if (std::popcount(mask) > 4) continue;
std::vector<Key> db;
for (std::size_t i = 0; i < universe.size(); ++i)
if (mask & (1u << i)) db.push_back(universe[i]);
run(db, keys, cases, bad);
}
std::printf("exhaustive: %lld cases, %lld mismatches\n", cases, bad);
return bad == 0 ? 0 : 1;
}
+102
View File
@@ -0,0 +1,102 @@
// Regression test for offsets at the extremes of the input domain. Offsets
// supplied by callers are 32-bit; a positive `end.offset` near `INT32_MAX`
// would otherwise materialise about 2^31 zero bytes for Lean's
// `keyAfterN n k < upper` test, so both implementations decide it from key
// lengths (`keyAfterNLt`) instead of allocating.
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <utility>
#include <vector>
#include "../src/fast.h"
#include "../src/literal.h"
#include "readable_gen.h"
namespace {
using namespace cr;
} // namespace
int main() {
// Sorted, duplicate-free, with an empty key and a two-byte `0xff` key.
const std::vector<Key> db{Key{}, Key(1, '\0'), Key("a"), Key("b"),
Key("\xff\xff", 2)};
// Selector keys are owned here and borrowed by the selectors below.
std::vector<OptKey> owned;
owned.reserve(4);
owned.push_back(std::nullopt); // ⊤
owned.push_back(OptKey{Key{}}); // ⊥
owned.push_back(OptKey{Key("a")});
owned.push_back(OptKey{Key("\xff\xff", 2)});
// The public offset contract accepts exactly the 32-bit boundaries; the
// entry-point assertions and corpus parsers share this predicate.
const RangeRead boundary{Selector{std::nullopt, kMinInputOffset},
Selector{std::nullopt, kMaxInputOffset}, false};
const RangeRead outside{Selector{std::nullopt, kMinInputOffset - 1},
Selector{std::nullopt, 0}, false};
if (!inputOffsetsFitInt32(boundary) || inputOffsetsFitInt32(outside)) {
std::fprintf(stderr, "offset contract check failed\n");
return 1;
}
const Int offs[] = {std::numeric_limits<std::int32_t>::min(),
std::numeric_limits<std::int32_t>::min() + 1,
-1000000000,
-3,
-1,
0,
1,
2,
3,
1000000000,
std::numeric_limits<std::int32_t>::max() - 1,
std::numeric_limits<std::int32_t>::max()};
std::vector<Selector> sels;
sels.reserve(owned.size() * (sizeof(offs) / sizeof(offs[0])));
for (const OptKey& k : owned)
for (Int off : offs) sels.push_back(Selector{optView(k), off});
long long cases = 0, bad = 0;
for (const Selector& bs : sels)
for (const Selector& es : sels) {
std::vector<Key> complete =
gen::inRange(db, gen::resolve(db, bs), gen::resolve(db, es));
for (bool reverse : {false, true}) {
std::vector<Key> ordered = complete;
if (reverse) std::reverse(ordered.begin(), ordered.end());
std::vector<std::pair<std::size_t, bool>> keeps;
keeps.emplace_back(ordered.size(), false);
if (!ordered.empty()) {
keeps.emplace_back(1, true);
if (ordered.size() > 1)
keeps.emplace_back((ordered.size() + 1) / 2, true);
}
for (auto [keep, more] : keeps) {
std::vector<KeyView> views;
views.reserve(keep);
for (std::size_t i = 0; i < keep; ++i) views.push_back(ordered[i]);
RangeRead r{bs, es, reverse};
Result res{views, more};
ConflictRange a = literal::closedForm(r, res);
ConflictRange b = fast::closedForm(r, res);
++cases;
if (!gen::equivalent(a, b)) {
if (++bad <= 10)
std::fprintf(stderr,
"MISMATCH bo=%lld eo=%lld rev=%d more=%d keys=%zu\n",
static_cast<long long>(bs.offset),
static_cast<long long>(es.offset), reverse, more,
keep);
}
}
}
}
std::printf("extreme: %lld cases, %lld mismatches\n", cases, bad);
return bad == 0 ? 0 : 1;
}
+11 -3
View File
@@ -1,5 +1,6 @@
// Validates the C++ literal translation against golden vectors produced by // Validates the C++ literal translation against golden vectors produced by
// `cpp/oracle/gen.lean` (which calls the real `Statement.closedForm`). // `cpp/oracle/gen.lean` (which calls the real `Statement.closedForm`).
#include <cstdint>
#include <cstdio> #include <cstdio>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
@@ -34,7 +35,7 @@ bool parseKey(const std::string& tok, OptKey& out) {
int hi = hex(tok[i]); int hi = hex(tok[i]);
int lo = hex(tok[i + 1]); int lo = hex(tok[i + 1]);
if (hi < 0 || lo < 0) return false; if (hi < 0 || lo < 0) return false;
k.push_back(static_cast<Byte>(hi * 16 + lo)); k.push_back(static_cast<char>(static_cast<Byte>(hi * 16 + lo)));
} }
out = std::move(k); out = std::move(k);
return true; return true;
@@ -46,7 +47,8 @@ std::string encKey(const OptKey& k) {
static const char* digits = "0123456789abcdef"; static const char* digits = "0123456789abcdef";
std::string out; std::string out;
out.reserve(k->size() * 2); out.reserve(k->size() * 2);
for (Byte b : *k) { for (char ch : *k) {
const Byte b = static_cast<Byte>(ch);
out.push_back(digits[b >> 4]); out.push_back(digits[b >> 4]);
out.push_back(digits[b & 0xf]); out.push_back(digits[b & 0xf]);
} }
@@ -79,7 +81,9 @@ int main(int argc, char** argv) {
std::size_t n = 0; std::size_t n = 0;
OptKey beginKey, endKey; OptKey beginKey, endKey;
if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n) || if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n) ||
!parseKey(bk, beginKey) || !parseKey(ek, endKey)) { !parseKey(bk, beginKey) || !parseKey(ek, endKey) || n > line.size() ||
!cr::offsetsFitInt32(static_cast<cr::Int>(bo),
static_cast<cr::Int>(eo))) {
std::fprintf(stderr, "line %zu: bad header: %s\n", lineNo, line.c_str()); std::fprintf(stderr, "line %zu: bad header: %s\n", lineNo, line.c_str());
++bad; ++bad;
continue; continue;
@@ -125,6 +129,10 @@ int main(int argc, char** argv) {
} }
} }
} }
if (cases == 0) {
std::fprintf(stderr, "golden: no cases in %s\n", argv[1]);
return 1;
}
std::printf("golden: %zu cases, %zu mismatches\n", cases, bad); std::printf("golden: %zu cases, %zu mismatches\n", cases, bad);
return bad == 0 ? 0 : 1; return bad == 0 ? 0 : 1;
} }
+14 -3
View File
@@ -48,7 +48,8 @@ inline Key randomKey(Bytes& b, std::size_t maxLen) {
std::size_t len = b.u8() % (maxLen + 1); std::size_t len = b.u8() % (maxLen + 1);
Key k; Key k;
k.reserve(len); k.reserve(len);
for (std::size_t i = 0; i < len; ++i) k.push_back(weightedByte(b)); for (std::size_t i = 0; i < len; ++i)
k.push_back(static_cast<char>(weightedByte(b)));
return k; return k;
} }
@@ -82,7 +83,7 @@ inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
anchor = static_cast<Int>(below) + s.offset - 1; anchor = static_cast<Int>(below) + s.offset - 1;
} }
if (anchor < 0) return OptKey{Key{}}; if (anchor < 0) return OptKey{Key{}};
if (static_cast<std::size_t>(anchor) >= db.size()) return std::nullopt; if (anchor >= static_cast<Int>(db.size())) return std::nullopt;
return OptKey{db[static_cast<std::size_t>(anchor)]}; return OptKey{db[static_cast<std::size_t>(anchor)]};
} }
@@ -112,7 +113,17 @@ struct Instance {
bool reverse = false; bool reverse = false;
bool more = false; bool more = false;
// Must be called once `keys` is final; safe again after a move. Instance() = default;
// Move-only: `views` borrow from `keys`. Moving a `std::vector` keeps its
// elements at the same addresses, so the views remain valid; a copy would
// leave the new instance's views pointing into the source's storage.
Instance(const Instance&) = delete;
Instance& operator=(const Instance&) = delete;
Instance(Instance&&) = default;
Instance& operator=(Instance&&) = default;
// Must be called once `keys` is final; call again after mutating `keys` or
// after moving keys in by hand.
void finish() { void finish() {
views.clear(); views.clear();
views.reserve(keys.size()); views.reserve(keys.size());