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

+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