Store public selector offsets as int32

This commit is contained in:
andrew committed 2026-11-18 11:28:47 -05:00
1 parent c8e49aead8
commit 24f28dd9c8
10 files changed
+247 -227

No files matched your search

+8 -9
View File
@@ -35,15 +35,14 @@ 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 Selector offsets are `std::int32_t`, matching the caller contract. The closed
stored as `Int = std::int64_t`, because the closed form also builds derived form converts the public `RangeRead` to `detail::RangeRead`, whose offsets are
selectors whose offsets (for example `end.offset - residualT`) need not fit in `Int = std::int64_t`, before deriving selectors (for example
32 bits. All intermediate `Int` arithmetic assumes 64-bit signed integers do not `end.offset - residualT`). All intermediate `Int` arithmetic assumes 64-bit
overflow; with 32-bit input offsets and in-memory key counts that is far from signed integers do not overflow; with 32-bit input offsets and in-memory key
the limit. The corpus parsers reject offsets outside the 32-bit input range, and counts that is far from the limit. The corpus parsers read offsets into a wider
both public `closedForm` entry points assert the same contract in debug builds. integer and reject values outside the 32-bit range before narrowing to
The assertion lives at the API boundary rather than in `Selector`, because `Selector`.
derived selectors legitimately exceed the input range.
## Scope of the two implementations ## Scope of the two implementations
+19 -18
View File
@@ -20,6 +20,8 @@
namespace cr::fast { namespace cr::fast {
namespace { namespace {
using WideRead = detail::RangeRead;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Observational summary // Observational summary
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -46,7 +48,7 @@ struct Obs {
// 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.
// They are therefore invariant under reversing the view. // They are therefore invariant under reversing the view.
struct Ctx { struct Ctx {
const RangeRead* r = nullptr; const WideRead* r = nullptr;
std::size_t n = 0; std::size_t n = 0;
std::size_t below = 0; // number of reported keys < begin.key std::size_t below = 0; // number of reported keys < begin.key
std::size_t t = 0; // number of reported keys >= end.key std::size_t t = 0; // number of reported keys >= end.key
@@ -105,7 +107,7 @@ std::size_t countLess(std::span<const KeyView> ks, OptKeyView x,
return n - lo; return n - lo;
} }
Ctx makeCtx(const RangeRead& r, const Obs& o) { Ctx makeCtx(const WideRead& r, const Obs& o) {
Ctx c; Ctx c;
c.r = &r; c.r = &r;
c.n = o.n; c.n = o.n;
@@ -322,7 +324,7 @@ struct Impl {
ConflictRange closedFormNegCrossCompleteResidual(const Ctx& c, const Obs& o) const { ConflictRange closedFormNegCrossCompleteResidual(const Ctx& c, const Obs& o) const {
// Keep the synthesised end key alive while `rp` borrows it. // Keep the synthesised end key alive while `rp` borrows it.
OptKey reducedEnd = after(o.lastK()); OptKey reducedEnd = after(o.lastK());
RangeRead rp = *c.r; WideRead rp = *c.r;
rp.end_.key = optView(reducedEnd); rp.end_.key = optView(reducedEnd);
rp.end_.offset = rp.end_.offset - static_cast<Int>(c.t); rp.end_.offset = rp.end_.offset - static_cast<Int>(c.t);
Ctx cp = makeCtx(rp, o); Ctx cp = makeCtx(rp, o);
@@ -463,15 +465,15 @@ struct Impl {
return mk(crossedBegin(c), topKey()); return mk(crossedBegin(c), topKey());
} }
RangeRead zeroBeginAnchor(const Ctx& c, const Obs& o) const { WideRead zeroBeginAnchor(const Ctx& c, const Obs& o) const {
RangeRead out = *c.r; WideRead out = *c.r;
out.begin.key = asKey(o.firstK()); out.begin.key = asKey(o.firstK());
out.begin.offset = 1; out.begin.offset = 1;
return out; return out;
} }
ConflictRange zeroCrossLongAnchor(const Ctx& c, const Obs& o) const { ConflictRange zeroCrossLongAnchor(const Ctx& c, const Obs& o) const {
RangeRead a = zeroBeginAnchor(c, o); WideRead a = zeroBeginAnchor(c, o);
Ctx ca = makeCtx(a, o); Ctx ca = makeCtx(a, o);
if (oLtK(c.r->end_.key, o.firstK())) if (oLtK(c.r->end_.key, o.firstK()))
return completeForwardCrossedGeneral(ca, o); return completeForwardCrossedGeneral(ca, o);
@@ -493,7 +495,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); WideRead a = zeroBeginAnchor(c, o);
Ctx ca = makeCtx(a, o); Ctx ca = makeCtx(a, o);
return closedFormOffsetOne(ca, o); return closedFormOffsetOne(ca, o);
} }
@@ -511,7 +513,7 @@ struct Impl {
} }
ConflictRange closedFormZeroCrossCompleteRev(const Ctx& c, const Obs& o) const { ConflictRange closedFormZeroCrossCompleteRev(const Ctx& c, const Obs& o) const {
RangeRead r2 = *c.r; WideRead r2 = *c.r;
r2.reverse = false; r2.reverse = false;
Obs o2 = o; Obs o2 = o;
o2.reversed = true; o2.reversed = true;
@@ -538,7 +540,7 @@ struct Impl {
// ----- empty result ------------------------------------------------------ // ----- empty result ------------------------------------------------------
bool AlwaysEmpty(const RangeRead& r) const { bool AlwaysEmpty(const WideRead& r) const {
NatInf gap = intervalCard(r.begin.key, r.end_.key); NatInf gap = intervalCard(r.begin.key, r.end_.key);
std::uint64_t b = noPinB(gap, r.begin.offset, r.end_.offset); 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 e = cELo(r.begin.offset, r.end_.offset);
@@ -568,18 +570,18 @@ struct Impl {
return !inner; return !inner;
} }
ConflictRange emptyRange(const RangeRead& r) const { ConflictRange emptyRange(const WideRead& r) const {
return mk(r.begin.key, r.begin.key); return mk(r.begin.key, r.begin.key);
} }
ConflictRange crossedRangeGeneral(const RangeRead& r) const { ConflictRange crossedRangeGeneral(const WideRead& r) const {
NatInf diff = niSub(NatInf::of(residualE(r)), NatInf::of(residualB(r))); NatInf diff = niSub(NatInf::of(residualE(r)), NatInf::of(residualB(r)));
if (niLe(diff, intervalCard(r.end_.key, r.begin.key))) if (niLe(diff, intervalCard(r.end_.key, r.begin.key)))
return mk(r.end_.key, topKey()); return mk(r.end_.key, topKey());
return mk(r.begin.key, topKey()); return mk(r.begin.key, topKey());
} }
ConflictRange emptyValue(const RangeRead& r) const { ConflictRange emptyValue(const WideRead& r) const {
if (AlwaysEmpty(r)) return emptyRange(r); if (AlwaysEmpty(r)) return emptyRange(r);
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());
@@ -593,7 +595,7 @@ struct Impl {
return crossedRangeGeneral(r); return crossedRangeGeneral(r);
} }
ConflictRange topRange(const RangeRead& r) const { ConflictRange topRange(const WideRead& r) const {
return mk(r.begin.key, topKey()); return mk(r.begin.key, topKey());
} }
@@ -624,7 +626,7 @@ struct Impl {
ConflictRange closedFormComplete(const Ctx& c, const Obs& o) const { ConflictRange closedFormComplete(const Ctx& c, const Obs& o) const {
if (!c.r->reverse) return closedFormCompleteForward(c, o); if (!c.r->reverse) return closedFormCompleteForward(c, o);
RangeRead r2 = *c.r; WideRead r2 = *c.r;
r2.reverse = false; r2.reverse = false;
Obs o2 = o; Obs o2 = o;
o2.reversed = true; o2.reversed = true;
@@ -886,7 +888,7 @@ struct Impl {
ConflictRange closedFormCrossed(const Ctx& c, const Obs& o) const { ConflictRange closedFormCrossed(const Ctx& c, const Obs& o) const {
if (!o.more) { if (!o.more) {
if (!c.r->reverse) return closedFormCrossedComplete(c, o); if (!c.r->reverse) return closedFormCrossedComplete(c, o);
RangeRead r2 = *c.r; WideRead r2 = *c.r;
r2.reverse = false; r2.reverse = false;
Obs o2 = o; Obs o2 = o;
o2.reversed = true; o2.reversed = true;
@@ -912,7 +914,7 @@ struct Impl {
// ----- entry point ------------------------------------------------------ // ----- entry point ------------------------------------------------------
ConflictRange closedForm(const RangeRead& r, const Result& res) const { ConflictRange closedForm(const WideRead& r, const Result& res) const {
if (res.keys.empty()) return emptyValue(r); if (res.keys.empty()) return emptyValue(r);
Obs o; Obs o;
o.keys = res.keys; o.keys = res.keys;
@@ -930,8 +932,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(detail::widen(r), res);
return Impl{}.closedForm(r, res);
} }
} // namespace cr::fast } // namespace cr::fast
+132 -131
View File
@@ -12,10 +12,11 @@
namespace cr::literal { namespace cr::literal {
namespace { namespace {
using WideRead = cr::detail::RangeRead;
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;
@@ -59,13 +60,13 @@ ConflictRange mkRange(B&& b, E&& e) {
// Arithmetic helpers // Arithmetic helpers
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
NatInf clampCap(const RangeRead& r) { return intervalCard(r.end_.key, r.begin.key); } NatInf clampCap(const WideRead& r) { return intervalCard(r.end_.key, r.begin.key); }
std::uint64_t clampSlack(const RangeRead& r, const Result& res) { std::uint64_t clampSlack(const WideRead& r, const Result& res) {
return natSub(beginSlack(r) + 1, reportedBelowBegin(r, res)); return natSub(beginSlack(r) + 1, reportedBelowBegin(r, res));
} }
OptKey clampEndOneUpper(const RangeRead& r, const Result& res) { OptKey clampEndOneUpper(const WideRead& r, const Result& res) {
NatInf slack = NatInf::of(clampSlack(r, res)); NatInf slack = NatInf::of(clampSlack(r, res));
NatInf cap = clampCap(r); NatInf cap = clampCap(r);
if (niLe(slack, cap) && (niLt(slack, cap) || first(res) != bottomKeyView())) if (niLe(slack, cap) && (niLt(slack, cap) || first(res) != bottomKeyView()))
@@ -73,25 +74,25 @@ OptKey clampEndOneUpper(const RangeRead& r, const Result& res) {
return ownKey(r.end_.key); return ownKey(r.end_.key);
} }
NatInf clampHighCap(const RangeRead& r, const Result& res) { NatInf clampHighCap(const WideRead& r, const Result& res) {
return intervalCard(afterLast(res), r.begin.key); return intervalCard(afterLast(res), r.begin.key);
} }
std::uint64_t clampHighThresh(const RangeRead& r, const Result& res) { std::uint64_t clampHighThresh(const WideRead& r, const Result& res) {
return clampSlack(r, res) + (isBottom(r.end_.key) ? 1 : 0); return clampSlack(r, res) + (isBottom(r.end_.key) ? 1 : 0);
} }
OptKey clampHighU(const RangeRead& r, const Result& res) { OptKey clampHighU(const WideRead& r, const Result& res) {
if (niLe(NatInf::of(clampHighThresh(r, res)), clampHighCap(r, res))) if (niLe(NatInf::of(clampHighThresh(r, res)), clampHighCap(r, res)))
return ownKey(r.begin.key); return ownKey(r.begin.key);
return afterLast(res); return afterLast(res);
} }
std::uint64_t clampLongC(const RangeRead& r, const Result& res) { std::uint64_t clampLongC(const WideRead& r, const Result& res) {
return natSub(natSub(beginSlack(r), reportedBelowBegin(r, res)), clampNonposD(r)); return natSub(natSub(beginSlack(r), reportedBelowBegin(r, res)), clampNonposD(r));
} }
OptKey clampLongUpper(const RangeRead& r, const Result& res) { OptKey clampLongUpper(const WideRead& r, const Result& res) {
NatInf cap = clampCap(r); NatInf cap = clampCap(r);
NatInf c = NatInf::of(clampLongC(r, res)); NatInf c = NatInf::of(clampLongC(r, res));
if (niLt(cap, c) || (niEq(cap, c) && first(res) == bottomKeyView())) if (niLt(cap, c) || (niEq(cap, c) && first(res) == bottomKeyView()))
@@ -99,19 +100,19 @@ OptKey clampLongUpper(const RangeRead& r, const Result& res) {
return ownKey(r.begin.key); return ownKey(r.begin.key);
} }
bool clampNonposBottom(const RangeRead& r, const Result& res) { bool clampNonposBottom(const WideRead& r, const Result& res) {
NatInf lhs = niAdd(NatInf::of(reportedBelowBegin(r, res)), NatInf lhs = niAdd(NatInf::of(reportedBelowBegin(r, res)),
niSub(NatInf::of(clampNonposD(r) + 1), niSub(NatInf::of(clampNonposD(r) + 1),
intervalCard(r.begin.key, r.end_.key))); intervalCard(r.begin.key, r.end_.key)));
return niLe(lhs, NatInf::of(beginSlack(r))); return niLe(lhs, NatInf::of(beginSlack(r)));
} }
bool clampedNonpos(const RangeRead& r, const Result& res) { bool clampedNonpos(const WideRead& r, const Result& res) {
return r.begin.offset <= 0 && return r.begin.offset <= 0 &&
optLe(r.begin.key, leastReportedOpt(r, res)); optLe(r.begin.key, leastReportedOpt(r, res));
} }
bool clampsToBottom(const RangeRead& r, const Result& res) { bool clampsToBottom(const WideRead& r, const Result& res) {
return r.begin.offset <= 0 && optLe(r.begin.key, first(res)); return r.begin.offset <= 0 && optLe(r.begin.key, first(res));
} }
@@ -129,36 +130,36 @@ std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) {
return lo > KBE.n ? lo - KBE.n : 0; return lo > KBE.n ? lo - KBE.n : 0;
} }
std::uint64_t residualD(const RangeRead& r, NatInf g) { std::uint64_t residualD(const WideRead& r, NatInf g) {
if (g.inf) return 0; if (g.inf) return 0;
return natSub(residualB(r), g.n); return natSub(residualB(r), g.n);
} }
std::uint64_t closedFormDeficit(const RangeRead& r) { std::uint64_t closedFormDeficit(const WideRead& r) {
return residualD(r, intervalCard(r.begin.key, r.end_.key)); return residualD(r, intervalCard(r.begin.key, r.end_.key));
} }
bool allBelowEnd(const RangeRead& r, const Result& res) { bool allBelowEnd(const WideRead& r, const Result& res) {
for (KeyView k : res.keys) for (KeyView k : res.keys)
if (!optLt(asKey(k), r.end_.key)) return false; if (!optLt(asKey(k), r.end_.key)) return false;
return true; return true;
} }
bool anyAtOrAboveEnd(const RangeRead& r, const Result& res) { bool anyAtOrAboveEnd(const WideRead& r, const Result& res) {
for (KeyView k : res.keys) for (KeyView k : res.keys)
if (optLe(r.end_.key, asKey(k))) return true; if (optLe(r.end_.key, asKey(k))) return true;
return false; return false;
} }
std::uint64_t gapOvershoot(const RangeRead& r, const Result& res) { std::uint64_t gapOvershoot(const WideRead& r, const Result& res) {
return natSub(residualE(r), residualT(r, res)); return natSub(residualE(r), residualT(r, res));
} }
std::uint64_t crossedOvershootGeneral(const RangeRead& r, const Result& res) { std::uint64_t crossedOvershootGeneral(const WideRead& r, const Result& res) {
return natSub(natSub(residualE(r), residualT(r, res)), residualB(r)); return natSub(natSub(residualE(r), residualT(r, res)), residualB(r));
} }
OptKeyView crossedBegin(const RangeRead& r, const Result& res) { OptKeyView crossedBegin(const WideRead& r, const Result& res) {
if (niLe(NatInf::of(crossedOvershootGeneral(r, res)), if (niLe(NatInf::of(crossedOvershootGeneral(r, res)),
intervalCard(r.end_.key, r.begin.key))) intervalCard(r.end_.key, r.begin.key)))
return r.end_.key; return r.end_.key;
@@ -169,39 +170,39 @@ OptKeyView crossedBegin(const RangeRead& r, const Result& res) {
// Crossed complete cases // Crossed complete cases
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
bool negCrossClampEndOne(const RangeRead& r, const Result& res) { bool negCrossClampEndOne(const WideRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more && return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset == 1 && optLt(first(res), r.begin.key) && r.end_.offset == 1 && optLt(first(res), r.begin.key) &&
reportedBelowBegin(r, res) <= beginSlack(r); reportedBelowBegin(r, res) <= beginSlack(r);
} }
bool negCrossHeadBottom(const RangeRead& r, const Result& res) { bool negCrossHeadBottom(const WideRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more && return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset <= 0 && optLe(r.begin.key, r.end_.key) && r.end_.offset <= 0 && optLe(r.begin.key, r.end_.key) &&
first(res) == bottomKeyView(); first(res) == bottomKeyView();
} }
bool negCrossClampNonposLastGe(const RangeRead& r, const Result& res) { bool negCrossClampNonposLastGe(const WideRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more && return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset <= 0 && optLt(first(res), r.begin.key) && r.end_.offset <= 0 && optLt(first(res), r.begin.key) &&
optLe(r.begin.key, last(res)) && optLe(r.begin.key, last(res)) &&
reportedBelowBegin(r, res) <= beginSlack(r); reportedBelowBegin(r, res) <= beginSlack(r);
} }
bool negCrossClampNonposBottom(const RangeRead& r, const Result& res) { bool negCrossClampNonposBottom(const WideRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more && return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset <= 0 && optLe(r.begin.key, r.end_.key) && r.end_.offset <= 0 && optLe(r.begin.key, r.end_.key) &&
optLt(first(res), r.begin.key) && clampNonposBottom(r, res); optLt(first(res), r.begin.key) && clampNonposBottom(r, res);
} }
bool negCrossClampLongNonpos(const RangeRead& r, const Result& res) { bool negCrossClampLongNonpos(const WideRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more && return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset <= 0 && optLt(first(res), r.begin.key) && r.end_.offset <= 0 && optLt(first(res), r.begin.key) &&
reportedBelowBegin(r, res) <= beginSlack(r) && reportedBelowBegin(r, res) <= beginSlack(r) &&
clampNonposBottom(r, res) && optLt(r.end_.key, r.begin.key); clampNonposBottom(r, res) && optLt(r.end_.key, r.begin.key);
} }
ConflictRange closedFormNegCrossClampUnified(const RangeRead& r, const Result& res) { ConflictRange closedFormNegCrossClampUnified(const WideRead& r, const Result& res) {
OptKey end; OptKey end;
if (negCrossClampEndOne(r, res)) if (negCrossClampEndOne(r, res))
end = clampEndOneUpper(r, res); end = clampEndOneUpper(r, res);
@@ -213,31 +214,31 @@ ConflictRange closedFormNegCrossClampUnified(const RangeRead& r, const Result& r
return mkRange(bottomKey(), std::move(end)); return mkRange(bottomKey(), std::move(end));
} }
OptKey negCrossCompleteHighM(const RangeRead& r, const Result& res) { OptKey negCrossCompleteHighM(const WideRead& r, const Result& res) {
return optMax(r.begin.key, afterLast(res)); return optMax(r.begin.key, afterLast(res));
} }
std::uint64_t negCrossHighD(const RangeRead& r, const Result& res) { std::uint64_t negCrossHighD(const WideRead& r, const Result& res) {
return natSub(residualE(r), res.keys.size()); return natSub(residualE(r), res.keys.size());
} }
NatInf negCrossHighGapCap(const RangeRead& r, const Result& res) { NatInf negCrossHighGapCap(const WideRead& r, const Result& res) {
return intervalCard(r.end_.key, first(res)); return intervalCard(r.end_.key, first(res));
} }
bool negCrossHighUsesEnd(const RangeRead& r, const Result& res) { bool negCrossHighUsesEnd(const WideRead& r, const Result& res) {
NatInf d = NatInf::of(negCrossHighD(r, res)); NatInf d = NatInf::of(negCrossHighD(r, res));
NatInf gap = negCrossHighGapCap(r, res); NatInf gap = negCrossHighGapCap(r, res);
return niLt(d, gap) || (niLe(d, gap) && optLt(r.begin.key, topKey())); return niLt(d, gap) || (niLe(d, gap) && optLt(r.begin.key, topKey()));
} }
bool negCrossCompleteHigh(const RangeRead& r, const Result& res) { bool negCrossCompleteHigh(const WideRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more && return !r.reverse && r.begin.offset < 0 && !res.more &&
r.end_.offset >= 2 && optLt(first(res), r.begin.key) && r.end_.offset >= 2 && optLt(first(res), r.begin.key) &&
optLt(r.end_.key, first(res)); optLt(r.end_.key, first(res));
} }
ConflictRange closedFormNegCrossCompleteHigh(const RangeRead& r, const Result& res) { ConflictRange closedFormNegCrossCompleteHigh(const WideRead& r, const Result& res) {
if (reportedBelowBegin(r, res) <= beginSlack(r)) { if (reportedBelowBegin(r, res) <= beginSlack(r)) {
if (res.keys.size() == residualE(r)) return mkRange(bottomKey(), clampHighU(r, res)); if (res.keys.size() == residualE(r)) return mkRange(bottomKey(), clampHighU(r, res));
return mkRange(bottomKey(), topKey()); return mkRange(bottomKey(), topKey());
@@ -250,23 +251,23 @@ ConflictRange closedFormNegCrossCompleteHigh(const RangeRead& r, const Result& r
ConflictRange topOfLast(KeyView k) { return mkRange(asKey(k), topKey()); } ConflictRange topOfLast(KeyView k) { return mkRange(asKey(k), topKey()); }
ConflictRange closedFormNegCrossOvershoot(const RangeRead& r, const Result& res) { ConflictRange closedFormNegCrossOvershoot(const WideRead& r, const Result& res) {
if (reportedBelowBegin(r, res) <= beginSlack(r)) if (reportedBelowBegin(r, res) <= beginSlack(r))
return mkRange(bottomKey(), topKey()); return mkRange(bottomKey(), topKey());
return topOfLast(firstKey(res)); return topOfLast(firstKey(res));
} }
ConflictRange closedFormNegCrossEndOne(const RangeRead& r, const Result& res) { ConflictRange closedFormNegCrossEndOne(const WideRead& r, const Result& res) {
return mkRange(first(res), r.end_.key); return mkRange(first(res), r.end_.key);
} }
ConflictRange closedFormNegCrossEndOneLong(const RangeRead& r, const Result& res) { ConflictRange closedFormNegCrossEndOneLong(const WideRead& r, const Result& res) {
return mkRange(first(res), r.begin.key); return mkRange(first(res), r.begin.key);
} }
ConflictRange closedFormNegCrossCompleteResidual(const RangeRead& r, const Result& res) { ConflictRange closedFormNegCrossCompleteResidual(const WideRead& r, const Result& res) {
OptKey reducedEnd = afterLast(res); OptKey reducedEnd = afterLast(res);
RangeRead rp = r; WideRead rp = r;
rp.end_.key = optView(reducedEnd); rp.end_.key = optView(reducedEnd);
rp.end_.offset = r.end_.offset - static_cast<Int>(residualT(r, res)); rp.end_.offset = r.end_.offset - static_cast<Int>(residualT(r, res));
if (rp.end_.offset >= 2) if (rp.end_.offset >= 2)
@@ -278,17 +279,17 @@ ConflictRange closedFormNegCrossCompleteResidual(const RangeRead& r, const Resul
return closedFormNegCrossEndOneLong(rp, res); return closedFormNegCrossEndOneLong(rp, res);
} }
ConflictRange closedFormNegCrossCompleteHighResidual(const RangeRead& r, const Result& res) { ConflictRange closedFormNegCrossCompleteHighResidual(const WideRead& r, const Result& res) {
if (negCrossCompleteHigh(r, res)) if (negCrossCompleteHigh(r, res))
return closedFormNegCrossCompleteHigh(r, res); return closedFormNegCrossCompleteHigh(r, res);
return closedFormNegCrossCompleteResidual(r, res); return closedFormNegCrossCompleteResidual(r, res);
} }
ConflictRange closedFormNegCrossNonclampUnified(const RangeRead& r, const Result& res) { ConflictRange closedFormNegCrossNonclampUnified(const WideRead& r, const Result& res) {
return mkRange(first(res), optMax(r.end_.key, r.begin.key)); return mkRange(first(res), optMax(r.end_.key, r.begin.key));
} }
ConflictRange closedFormNegCrossTopUnified(const RangeRead& r, const Result& res) { ConflictRange closedFormNegCrossTopUnified(const WideRead& r, const Result& res) {
OptKeyView begin = reportedBelowBegin(r, res) <= beginSlack(r) OptKeyView begin = reportedBelowBegin(r, res) <= beginSlack(r)
? bottomKeyView() ? bottomKeyView()
: leastReportedOpt(r, res); : leastReportedOpt(r, res);
@@ -299,7 +300,7 @@ ConflictRange closedFormNegCrossTopUnified(const RangeRead& r, const Result& res
// Crossed truncated cases // Crossed truncated cases
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
bool negCrossTruncNonposGap(const RangeRead& r, const Result& res) { bool negCrossTruncNonposGap(const WideRead& r, const Result& res) {
return res.more && !r.reverse && r.begin.offset < 0 && return res.more && !r.reverse && r.begin.offset < 0 &&
optLt(leastReportedOpt(r, res), r.begin.key) && optLt(leastReportedOpt(r, res), r.begin.key) &&
r.end_.offset <= 0 && r.end_.offset <= 0 &&
@@ -312,12 +313,12 @@ bool negCrossTruncNonposGap(const RangeRead& r, const Result& res) {
NatInf::of(clampNonposD(r) + 1))); NatInf::of(clampNonposD(r) + 1)));
} }
bool negCrossTruncNonposGapA2aBottom(const RangeRead& r, const Result& res) { bool negCrossTruncNonposGapA2aBottom(const WideRead& r, const Result& res) {
return negCrossTruncNonposGap(r, res) && optLt(r.end_.key, r.begin.key) && return negCrossTruncNonposGap(r, res) && optLt(r.end_.key, r.begin.key) &&
clampNonposBottom(r, res); clampNonposBottom(r, res);
} }
bool negCrossTruncNonposGapA2aBottomHigh(const RangeRead& r, const Result& res) { bool negCrossTruncNonposGapA2aBottomHigh(const WideRead& r, const Result& res) {
std::uint64_t extra = isBottom(first(res)) ? 1 : 0; std::uint64_t extra = isBottom(first(res)) ? 1 : 0;
NatInf lhs = NatInf::of(beginSlack(r) + 1 + extra); NatInf lhs = NatInf::of(beginSlack(r) + 1 + extra);
NatInf rhs = niAdd(NatInf::of(res.keys.size()), NatInf rhs = niAdd(NatInf::of(res.keys.size()),
@@ -325,26 +326,26 @@ bool negCrossTruncNonposGapA2aBottomHigh(const RangeRead& r, const Result& res)
return negCrossTruncNonposGapA2aBottom(r, res) && niLe(lhs, rhs); return negCrossTruncNonposGapA2aBottom(r, res) && niLe(lhs, rhs);
} }
ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const RangeRead& r, ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const WideRead& r,
const Result& res) { const Result& res) {
if (negCrossTruncNonposGapA2aBottomHigh(r, res)) if (negCrossTruncNonposGapA2aBottomHigh(r, res))
return mkRange(bottomKey(), r.begin.key); return mkRange(bottomKey(), r.begin.key);
return mkRange(bottomKey(), r.end_.key); return mkRange(bottomKey(), r.end_.key);
} }
bool finiteUnderCond(const RangeRead& r, const Result& res) { bool finiteUnderCond(const WideRead& r, const Result& res) {
NatInf lhs = niAdd(NatInf::of(natSub(reportedBelowBegin(r, res), 1)), NatInf lhs = niAdd(NatInf::of(natSub(reportedBelowBegin(r, res), 1)),
intervalCard(afterFirst(res), r.begin.key)); intervalCard(afterFirst(res), r.begin.key));
return niLe(lhs, NatInf::of(beginSlack(r))); return niLe(lhs, NatInf::of(beginSlack(r)));
} }
bool negCrossTruncRevResidualFiniteOver(const RangeRead& r, const Result& res) { bool negCrossTruncRevResidualFiniteOver(const WideRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 && r.end_.offset >= 2 && 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) && optLt(r.end_.key, last(res)) && optLt(last(res), r.begin.key) &&
residualT(r, res) == residualE(r); residualT(r, res) == residualE(r);
} }
ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const RangeRead& r, ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const WideRead& r,
const Result& res) { const Result& res) {
OptKeyView L = last(res); OptKeyView L = last(res);
OptKey U = finiteUnderCond(r, res) ? afterFirst(res) : ownKey(r.begin.key); OptKey U = finiteUnderCond(r, res) ? afterFirst(res) : ownKey(r.begin.key);
@@ -353,7 +354,7 @@ ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const RangeRead& r
return mkRange(std::move(L), std::move(U)); return mkRange(std::move(L), std::move(U));
} }
bool sentinelLargeTight(const RangeRead& r, const Result& res) { bool sentinelLargeTight(const WideRead& 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()));
@@ -361,7 +362,7 @@ bool sentinelLargeTight(const RangeRead& r, const Result& res) {
return niEq(lhs, rhs) && nPlusBegin == 1; return niEq(lhs, rhs) && nPlusBegin == 1;
} }
bool negCrossTruncRevResidualTopLarge(const RangeRead& r, const Result& res) { bool negCrossTruncRevResidualTopLarge(const WideRead& r, const Result& res) {
if (!(res.more && r.reverse && r.begin.offset < 0 && r.end_.offset >= 2 && if (!(res.more && r.reverse && r.begin.offset < 0 && r.end_.offset >= 2 &&
optLt(r.end_.key, last(res)) && residualT(r, res) < residualE(r))) optLt(r.end_.key, last(res)) && residualT(r, res) < residualE(r)))
return false; return false;
@@ -371,11 +372,11 @@ bool negCrossTruncRevResidualTopLarge(const RangeRead& r, const Result& res) {
return niLe(lhs, rhs); return niLe(lhs, rhs);
} }
bool negCrossTruncRevResidualTopLargeSentinel(const RangeRead& r, const Result& res) { bool negCrossTruncRevResidualTopLargeSentinel(const WideRead& r, const Result& res) {
return negCrossTruncRevResidualTopLarge(r, res) && isTop(r.begin.key); return negCrossTruncRevResidualTopLarge(r, res) && isTop(r.begin.key);
} }
ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const RangeRead& r, ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const WideRead& r,
const Result& res) { const Result& res) {
OptKeyView L = last(res); OptKeyView L = last(res);
if (negCrossTruncRevResidualTopLargeSentinel(r, res) && if (negCrossTruncRevResidualTopLargeSentinel(r, res) &&
@@ -384,7 +385,7 @@ ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const RangeRead&
return mkRange(r.end_.key, topKey()); return mkRange(r.end_.key, topKey());
} }
ConflictRange truncatedRangeRev(const RangeRead& r, KeyView lastReported) { ConflictRange truncatedRangeRev(const WideRead& r, KeyView lastReported) {
return mkRange(asKey(lastReported), r.end_.key); return mkRange(asKey(lastReported), r.end_.key);
} }
@@ -393,7 +394,7 @@ ConflictRange truncatedRangeRev(const RangeRead& r, KeyView lastReported) {
// Complete and offset-one cases // Complete and offset-one cases
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
ConflictRange closedFormReverseTruncatedZero(const RangeRead& r, const Result& res) { ConflictRange closedFormReverseTruncatedZero(const WideRead& r, const Result& res) {
if (r.end_.offset <= 1) return truncatedRangeRev(r, lastKey(res)); if (r.end_.offset <= 1) return truncatedRangeRev(r, lastKey(res));
OptKeyView L = last(res); OptKeyView L = last(res);
if (residualT(r, res) == residualE(r)) { if (residualT(r, res) == residualE(r)) {
@@ -409,18 +410,18 @@ ConflictRange closedFormReverseTruncatedZero(const RangeRead& r, const Result& r
return mkRange(std::move(L), topKey()); return mkRange(std::move(L), topKey());
} }
ConflictRange truncatedRange(const RangeRead& r, KeyView lastReported) { ConflictRange truncatedRange(const WideRead& r, KeyView lastReported) {
return mkRange(r.begin.key, asKey(keyAfter(lastReported))); return mkRange(r.begin.key, asKey(keyAfter(lastReported)));
} }
ConflictRange truncatedForwardClosed(const RangeRead& r, const Result& res) { ConflictRange truncatedForwardClosed(const WideRead& r, const Result& res) {
NatInf cap = intervalCard(r.end_.key, r.begin.key); NatInf cap = intervalCard(r.end_.key, r.begin.key);
NatInf room = NatInf::of(natSub(residualE(r), res.keys.size())); NatInf room = NatInf::of(natSub(residualE(r), res.keys.size()));
if (niLe(cap, room)) return truncatedRange(r, lastKey(res)); if (niLe(cap, room)) return truncatedRange(r, lastKey(res));
return mkRange(r.end_.key, afterLast(res)); return mkRange(r.end_.key, afterLast(res));
} }
ConflictRange completeClosedWith(const OptKey& endKey, const RangeRead& r, ConflictRange completeClosedWith(const OptKey& endKey, const WideRead& r,
const Result& res) { const Result& res) {
OptKeyView begin = r.begin.key; OptKeyView begin = r.begin.key;
if (optLt(r.end_.key, r.begin.key) && if (optLt(r.end_.key, r.begin.key) &&
@@ -434,48 +435,48 @@ ConflictRange completeClosedWith(const OptKey& endKey, const RangeRead& r,
return mkRange(std::move(begin), topKey()); return mkRange(std::move(begin), topKey());
} }
ConflictRange completeReversedClosed(const RangeRead& r, const Result& res) { ConflictRange completeReversedClosed(const WideRead& r, const Result& res) {
return completeClosedWith(afterFirst(res), r, res); return completeClosedWith(afterFirst(res), r, res);
} }
ConflictRange completeForwardClosed(const RangeRead& r, const Result& res) { ConflictRange completeForwardClosed(const WideRead& r, const Result& res) {
return completeClosedWith(afterLast(res), r, res); return completeClosedWith(afterLast(res), r, res);
} }
ConflictRange completeClosed(const RangeRead& r, const Result& res) { ConflictRange completeClosed(const WideRead& r, const Result& res) {
return r.reverse ? completeReversedClosed(r, res) : completeForwardClosed(r, res); return r.reverse ? completeReversedClosed(r, res) : completeForwardClosed(r, res);
} }
ConflictRange closedFormOffsetOneTruncated(const RangeRead& r, const Result& res) { ConflictRange closedFormOffsetOneTruncated(const WideRead& r, const Result& res) {
if (r.end_.offset <= 0) return mkRange(r.begin.key, r.end_.key); if (r.end_.offset <= 0) return mkRange(r.begin.key, r.end_.key);
return truncatedRange(r, lastKey(res)); return truncatedRange(r, lastKey(res));
} }
ConflictRange topRange(const RangeRead& r) { return mkRange(r.begin.key, topKey()); } ConflictRange topRange(const WideRead& r) { return mkRange(r.begin.key, topKey()); }
ConflictRange closedFormOffsetOneWith(KeyView endKey, const RangeRead& r, ConflictRange closedFormOffsetOneWith(KeyView endKey, const WideRead& r,
const Result& res) { const Result& res) {
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key); if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
if (residualT(r, res) == residualE(r)) return truncatedRange(r, endKey); if (residualT(r, res) == residualE(r)) return truncatedRange(r, endKey);
return topRange(r); return topRange(r);
} }
ConflictRange closedFormOffsetOneRev(const RangeRead& r, const Result& res) { ConflictRange closedFormOffsetOneRev(const WideRead& r, const Result& res) {
return closedFormOffsetOneWith(firstKey(res), r, res); return closedFormOffsetOneWith(firstKey(res), r, res);
} }
ConflictRange closedFormOffsetOne(const RangeRead& r, const Result& res) { ConflictRange closedFormOffsetOne(const WideRead& r, const Result& res) {
return closedFormOffsetOneWith(lastKey(res), r, res); return closedFormOffsetOneWith(lastKey(res), r, res);
} }
ConflictRange closedFormOffsetOneAny(const RangeRead& r, const Result& res) { ConflictRange closedFormOffsetOneAny(const WideRead& r, const Result& res) {
return r.reverse ? closedFormOffsetOneRev(r, res) : closedFormOffsetOne(r, res); return r.reverse ? closedFormOffsetOneRev(r, res) : closedFormOffsetOne(r, res);
} }
ConflictRange closedFormOffsetOneUniversal(const RangeRead& r, const Result& res) { ConflictRange closedFormOffsetOneUniversal(const WideRead& r, const Result& res) {
if (res.more) if (res.more)
return r.reverse ? closedFormReverseTruncatedZero(r, res) return r.reverse ? closedFormReverseTruncatedZero(r, res)
: closedFormOffsetOneTruncated(r, res); : closedFormOffsetOneTruncated(r, res);
return closedFormOffsetOneAny(r, res); return closedFormOffsetOneAny(r, res);
} }
ConflictRange closedFormOffsetOneTotal(const RangeRead& r, const Result& res) { ConflictRange closedFormOffsetOneTotal(const WideRead& r, const Result& res) {
if (optLt(r.end_.key, r.begin.key)) { if (optLt(r.end_.key, r.begin.key)) {
if (res.more) if (res.more)
return r.reverse ? closedFormReverseTruncatedZero(r, res) return r.reverse ? closedFormReverseTruncatedZero(r, res)
@@ -485,7 +486,7 @@ ConflictRange closedFormOffsetOneTotal(const RangeRead& r, const Result& res) {
return closedFormOffsetOneUniversal(r, res); return closedFormOffsetOneUniversal(r, res);
} }
ConflictRange closedFormZeroCrossEndOneLong(const RangeRead& r, const Result& res) { ConflictRange closedFormZeroCrossEndOneLong(const WideRead& r, const Result& res) {
return mkRange(leastReportedOpt(r, res), r.begin.key); return mkRange(leastReportedOpt(r, res), r.begin.key);
} }
@@ -493,31 +494,31 @@ ConflictRange closedFormZeroCrossEndOneLong(const RangeRead& r, const Result& re
// Zero-crossed cases // Zero-crossed cases
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
ConflictRange closedFormZeroCrossEnd(const RangeRead& r, const Result& res) { ConflictRange closedFormZeroCrossEnd(const WideRead& r, const Result& res) {
return mkRange(leastReportedOpt(r, res), r.end_.key); return mkRange(leastReportedOpt(r, res), r.end_.key);
} }
ConflictRange completeForwardCrossedGeneral(const RangeRead& r, const Result& res) { ConflictRange completeForwardCrossedGeneral(const WideRead& r, const Result& res) {
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key); if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
if (residualT(r, res) + residualB(r) == residualE(r)) if (residualT(r, res) + residualB(r) == residualE(r))
return mkRange(r.end_.key, afterLast(res)); return mkRange(r.end_.key, afterLast(res));
return mkRange(crossedBegin(r, res), topKey()); return mkRange(crossedBegin(r, res), topKey());
} }
RangeRead zeroBeginAnchor(const RangeRead& r, const Result& res) { WideRead zeroBeginAnchor(const WideRead& r, const Result& res) {
RangeRead out = r; WideRead out = r;
out.begin.key = first(res); out.begin.key = first(res);
out.begin.offset = 1; out.begin.offset = 1;
return out; return out;
} }
ConflictRange zeroCrossLongAnchor(const RangeRead& r, const Result& res) { ConflictRange zeroCrossLongAnchor(const WideRead& r, const Result& res) {
if (optLt(r.end_.key, first(res))) if (optLt(r.end_.key, first(res)))
return completeForwardCrossedGeneral(zeroBeginAnchor(r, res), res); return completeForwardCrossedGeneral(zeroBeginAnchor(r, res), res);
return closedFormOffsetOne(zeroBeginAnchor(r, res), res); return closedFormOffsetOne(zeroBeginAnchor(r, res), res);
} }
ConflictRange closedFormZeroCrossLong(const RangeRead& r, const Result& res) { ConflictRange closedFormZeroCrossLong(const WideRead& r, const Result& res) {
if (isTop(r.begin.key)) { if (isTop(r.begin.key)) {
OptKeyView H = first(res); OptKeyView H = first(res);
std::uint64_t e = residualE(r); std::uint64_t e = residualE(r);
@@ -531,11 +532,11 @@ ConflictRange closedFormZeroCrossLong(const RangeRead& r, const Result& res) {
return mkRange(a.begin, optMax(a.end_, r.begin.key)); return mkRange(a.begin, optMax(a.end_, r.begin.key));
} }
ConflictRange closedFormZeroCrossUnderEnd(const RangeRead& r, const Result& res) { ConflictRange closedFormZeroCrossUnderEnd(const WideRead& r, const Result& res) {
return closedFormOffsetOneTotal(zeroBeginAnchor(r, res), res); return closedFormOffsetOneTotal(zeroBeginAnchor(r, res), res);
} }
ConflictRange closedFormZeroCrossComplete(const RangeRead& r, const Result& res) { ConflictRange closedFormZeroCrossComplete(const WideRead& r, const Result& res) {
if (r.end_.offset <= 0) return closedFormZeroCrossEnd(r, res); if (r.end_.offset <= 0) return closedFormZeroCrossEnd(r, res);
if (r.end_.offset == 1) { if (r.end_.offset == 1) {
if (optLt(r.end_.key, r.begin.key)) if (optLt(r.end_.key, r.begin.key))
@@ -547,23 +548,23 @@ ConflictRange closedFormZeroCrossComplete(const RangeRead& r, const Result& res)
return closedFormZeroCrossUnderEnd(r, res); return closedFormZeroCrossUnderEnd(r, res);
} }
ConflictRange closedFormZeroCrossCompleteRev(const RangeRead& r, const Result& res) { ConflictRange closedFormZeroCrossCompleteRev(const WideRead& r, const Result& res) {
std::vector<KeyView> rev = reversedKeys(res); std::vector<KeyView> rev = reversedKeys(res);
return closedFormZeroCrossComplete(forwardRead(r), Result{rev, false}); return closedFormZeroCrossComplete(forwardRead(r), Result{rev, false});
} }
bool zeroCrossUnderEnd(const RangeRead& r, const Result& res) { bool zeroCrossUnderEnd(const WideRead& r, const Result& res) {
return r.begin.offset == 0 && !res.more && !r.reverse && return r.begin.offset == 0 && !res.more && !r.reverse &&
r.end_.offset >= 2 && optLt(leastReportedOpt(r, res), r.begin.key) && r.end_.offset >= 2 && optLt(leastReportedOpt(r, res), r.begin.key) &&
optLe(r.begin.key, r.end_.key); optLe(r.begin.key, r.end_.key);
} }
ConflictRange closedFormZeroCrossResidual(const RangeRead& r, const Result& res) { ConflictRange closedFormZeroCrossResidual(const WideRead& r, const Result& res) {
if (zeroCrossUnderEnd(r, res)) return closedFormZeroCrossUnderEnd(r, res); if (zeroCrossUnderEnd(r, res)) return closedFormZeroCrossUnderEnd(r, res);
return closedFormZeroCrossLong(r, res); return closedFormZeroCrossLong(r, res);
} }
ConflictRange closedFormZeroCrossTruncRevCrossed(const RangeRead& r, const Result& res) { ConflictRange closedFormZeroCrossTruncRevCrossed(const WideRead& r, const Result& res) {
Result complete = res; Result complete = res;
complete.more = false; complete.more = false;
return closedFormZeroCrossCompleteRev(r, complete); return closedFormZeroCrossCompleteRev(r, complete);
@@ -573,7 +574,7 @@ ConflictRange closedFormZeroCrossTruncRevCrossed(const RangeRead& r, const Resul
// Empty result // Empty result
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
bool AlwaysEmpty(const RangeRead& r) { bool AlwaysEmpty(const WideRead& r) {
NatInf gap = intervalCard(r.begin.key, r.end_.key); NatInf gap = intervalCard(r.begin.key, r.end_.key);
std::uint64_t b = noPinB(gap, r.begin.offset, r.end_.offset); 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 e = cELo(r.begin.offset, r.end_.offset);
@@ -603,18 +604,18 @@ bool AlwaysEmpty(const RangeRead& r) {
return !inner; return !inner;
} }
ConflictRange emptyRange(const RangeRead& r) { ConflictRange emptyRange(const WideRead& r) {
return mkRange(r.begin.key, r.begin.key); return mkRange(r.begin.key, r.begin.key);
} }
ConflictRange crossedRangeGeneral(const RangeRead& r) { ConflictRange crossedRangeGeneral(const WideRead& r) {
NatInf diff = niSub(NatInf::of(residualE(r)), NatInf::of(residualB(r))); NatInf diff = niSub(NatInf::of(residualE(r)), NatInf::of(residualB(r)));
if (niLe(diff, intervalCard(r.end_.key, r.begin.key))) if (niLe(diff, intervalCard(r.end_.key, r.begin.key)))
return mkRange(r.end_.key, topKey()); return mkRange(r.end_.key, topKey());
return mkRange(r.begin.key, topKey()); return mkRange(r.begin.key, topKey());
} }
ConflictRange emptyValue(const RangeRead& r) { ConflictRange emptyValue(const WideRead& r) {
if (AlwaysEmpty(r)) return emptyRange(r); if (AlwaysEmpty(r)) return emptyRange(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());
@@ -632,15 +633,15 @@ ConflictRange emptyValue(const RangeRead& r) {
// Complete and forward-truncated results // Complete and forward-truncated results
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
OptKeyView closedFormBegin(const RangeRead& r, const Result& res) { OptKeyView closedFormBegin(const WideRead& r, const Result& res) {
return clampsToBottom(r, res) ? bottomKeyView() : r.begin.key; return clampsToBottom(r, res) ? bottomKeyView() : r.begin.key;
} }
bool closedFormFinite(const RangeRead& r, const Result& res) { bool closedFormFinite(const WideRead& r, const Result& res) {
return closedFormDeficit(r) + residualT(r, res) == residualE(r); return closedFormDeficit(r) + residualT(r, res) == residualE(r);
} }
ConflictRange closedFormCompleteForward(const RangeRead& r, const Result& res) { ConflictRange closedFormCompleteForward(const WideRead& r, const Result& res) {
OptKeyView begin = closedFormBegin(r, res); OptKeyView begin = closedFormBegin(r, res);
if (r.end_.offset <= 1) return mkRange(begin, r.end_.key); if (r.end_.offset <= 1) return mkRange(begin, r.end_.key);
if (clampsToBottom(r, res) || optLe(r.begin.key, r.end_.key)) { if (clampsToBottom(r, res) || optLe(r.begin.key, r.end_.key)) {
@@ -654,18 +655,18 @@ ConflictRange closedFormCompleteForward(const RangeRead& r, const Result& res) {
return mkRange(crossedBegin(r, res), topKey()); return mkRange(crossedBegin(r, res), topKey());
} }
ConflictRange closedFormComplete(const RangeRead& r, const Result& res) { ConflictRange closedFormComplete(const WideRead& r, const Result& res) {
if (!r.reverse) return closedFormCompleteForward(forwardRead(r), res); if (!r.reverse) return closedFormCompleteForward(forwardRead(r), res);
std::vector<KeyView> rev = reversedKeys(res); std::vector<KeyView> rev = reversedKeys(res);
return closedFormCompleteForward(forwardRead(r), Result{rev, false}); return closedFormCompleteForward(forwardRead(r), Result{rev, false});
} }
NatInf closedFormForwardCapacity(const RangeRead& r, const Result& res) { NatInf closedFormForwardCapacity(const WideRead& r, const Result& res) {
if (clampsToBottom(r, res)) return NatInf::of(0); if (clampsToBottom(r, res)) return NatInf::of(0);
return intervalCard(r.end_.key, r.begin.key); return intervalCard(r.end_.key, r.begin.key);
} }
ConflictRange closedFormForwardTruncated(const RangeRead& r, const Result& res) { ConflictRange closedFormForwardTruncated(const WideRead& r, const Result& res) {
OptKeyView begin = closedFormBegin(r, res); OptKeyView begin = closedFormBegin(r, res);
if (r.end_.offset <= 0) return mkRange(begin, r.end_.key); if (r.end_.offset <= 0) return mkRange(begin, r.end_.key);
std::uint64_t room = natSub(natSub(residualE(r), res.keys.size()), residualB(r)); std::uint64_t room = natSub(natSub(residualE(r), res.keys.size()), residualB(r));
@@ -678,22 +679,22 @@ ConflictRange closedFormForwardTruncated(const RangeRead& r, const Result& res)
// Reversed truncation // Reversed truncation
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
std::uint64_t closedFormReverseCount(const RangeRead& r, const Result& res) { std::uint64_t closedFormReverseCount(const WideRead& r, const Result& res) {
if (optLt(r.end_.key, r.begin.key)) if (optLt(r.end_.key, r.begin.key))
return static_cast<std::uint64_t>(res.keys.size()); return static_cast<std::uint64_t>(res.keys.size());
return residualT(r, res); return residualT(r, res);
} }
bool closedFormReverseFinite(const RangeRead& r, const Result& res) { bool closedFormReverseFinite(const WideRead& r, const Result& res) {
return closedFormDeficit(r) + closedFormReverseCount(r, res) == residualE(r); return closedFormDeficit(r) + closedFormReverseCount(r, res) == residualE(r);
} }
bool closedFormCrossedRoom(const RangeRead& r, const Result& res) { bool closedFormCrossedRoom(const WideRead& r, const Result& res) {
NatInf lhs = NatInf::of(natSub(residualE(r), res.keys.size())); NatInf lhs = NatInf::of(natSub(residualE(r), res.keys.size()));
return niLe(lhs, intervalCard(r.end_.key, last(res))); return niLe(lhs, intervalCard(r.end_.key, last(res)));
} }
ConflictRange closedFormReverseTruncatedSkip(const RangeRead& r, const Result& res) { ConflictRange closedFormReverseTruncatedSkip(const WideRead& r, const Result& res) {
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key); if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
if (allBelowEnd(r, res)) return topRange(r); if (allBelowEnd(r, res)) return topRange(r);
if (closedFormReverseFinite(r, res)) { if (closedFormReverseFinite(r, res)) {
@@ -706,12 +707,12 @@ ConflictRange closedFormReverseTruncatedSkip(const RangeRead& r, const Result& r
return mkRange(begin, topKey()); return mkRange(begin, topKey());
} }
ConflictRange closedFormReverseTruncated(const RangeRead& r, const Result& res) { ConflictRange closedFormReverseTruncated(const WideRead& r, const Result& res) {
if (residualB(r) == 0) return closedFormReverseTruncatedZero(r, res); if (residualB(r) == 0) return closedFormReverseTruncatedZero(r, res);
return closedFormReverseTruncatedSkip(r, res); return closedFormReverseTruncatedSkip(r, res);
} }
ConflictRange closedFormPositive(const RangeRead& r, const Result& res) { ConflictRange closedFormPositive(const WideRead& r, const Result& res) {
if (!res.more) return closedFormComplete(r, res); if (!res.more) return closedFormComplete(r, res);
if (!r.reverse) return closedFormForwardTruncated(r, res); if (!r.reverse) return closedFormForwardTruncated(r, res);
return closedFormReverseTruncated(r, res); return closedFormReverseTruncated(r, res);
@@ -721,25 +722,25 @@ ConflictRange closedFormPositive(const RangeRead& r, const Result& res) {
// Crossed table // Crossed table
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
bool overshoot(const RangeRead& r, const Result& res) { bool overshoot(const WideRead& r, const Result& res) {
return r.end_.offset >= 2 && allBelowEnd(r, res); return r.end_.offset >= 2 && allBelowEnd(r, res);
} }
bool zeroCrossOvershootComplete(const RangeRead& r, const Result& res) { bool zeroCrossOvershootComplete(const WideRead& r, const Result& res) {
return r.begin.offset == 0 && !res.more && !r.reverse && overshoot(r, res) && return r.begin.offset == 0 && !res.more && !r.reverse && overshoot(r, res) &&
optLt(leastReportedOpt(r, res), r.begin.key); optLt(leastReportedOpt(r, res), r.begin.key);
} }
bool negCrossOvershoot(const RangeRead& r, const Result& res) { bool negCrossOvershoot(const WideRead& r, const Result& res) {
return !r.reverse && r.begin.offset < 0 && !res.more && overshoot(r, res) && return !r.reverse && r.begin.offset < 0 && !res.more && overshoot(r, res) &&
optLt(first(res), r.begin.key); optLt(first(res), r.begin.key);
} }
bool crossedCompleteOvershoot(const RangeRead& r, const Result& res) { bool crossedCompleteOvershoot(const WideRead& r, const Result& res) {
return zeroCrossOvershootComplete(r, res) || negCrossOvershoot(r, res); return zeroCrossOvershootComplete(r, res) || negCrossOvershoot(r, res);
} }
bool crossedCompleteEndLeOne(const RangeRead& r, const Result& res) { bool crossedCompleteEndLeOne(const WideRead& r, const Result& res) {
if (!(!r.reverse && !res.more && r.begin.offset <= 0 && if (!(!r.reverse && !res.more && r.begin.offset <= 0 &&
optLt(first(res), r.begin.key))) optLt(first(res), r.begin.key)))
return false; return false;
@@ -750,30 +751,30 @@ bool crossedCompleteEndLeOne(const RangeRead& r, const Result& res) {
clampNonposBottom(r, res)); clampNonposBottom(r, res));
} }
bool zeroCrossLongTop(const RangeRead& r, const Result& res) { bool zeroCrossLongTop(const WideRead& r, const Result& res) {
return r.begin.offset == 0 && !res.more && !r.reverse && return r.begin.offset == 0 && !res.more && !r.reverse &&
r.end_.offset >= 2 && isTop(r.begin.key); r.end_.offset >= 2 && isTop(r.begin.key);
} }
bool zeroCrossLongFinite(const RangeRead& r, const Result& res) { bool zeroCrossLongFinite(const WideRead& r, const Result& res) {
return r.begin.offset == 0 && !res.more && !r.reverse && return r.begin.offset == 0 && !res.more && !r.reverse &&
r.end_.offset >= 2 && !isTop(r.begin.key) && r.end_.offset >= 2 && !isTop(r.begin.key) &&
optLt(r.end_.key, r.begin.key) && optLt(r.end_.key, r.begin.key) &&
optLt(leastReportedOpt(r, res), r.begin.key); optLt(leastReportedOpt(r, res), r.begin.key);
} }
bool crossedCompleteBeginZero(const RangeRead& r, const Result& res) { bool crossedCompleteBeginZero(const WideRead& r, const Result& res) {
return zeroCrossUnderEnd(r, res) || zeroCrossLongTop(r, res) || return zeroCrossUnderEnd(r, res) || zeroCrossLongTop(r, res) ||
zeroCrossLongFinite(r, res); zeroCrossLongFinite(r, res);
} }
bool crossedCompleteClamp(const RangeRead& r, const Result& res) { bool crossedCompleteClamp(const WideRead& r, const Result& res) {
return negCrossClampEndOne(r, res) || negCrossHeadBottom(r, res) || return negCrossClampEndOne(r, res) || negCrossHeadBottom(r, res) ||
negCrossClampNonposBottom(r, res) || negCrossClampNonposBottom(r, res) ||
negCrossClampLongNonpos(r, res); negCrossClampLongNonpos(r, res);
} }
ConflictRange closedFormCrossedComplete(const RangeRead& r, const Result& res) { ConflictRange closedFormCrossedComplete(const WideRead& r, const Result& res) {
if (crossedCompleteOvershoot(r, res)) if (crossedCompleteOvershoot(r, res))
return closedFormNegCrossTopUnified(r, res); return closedFormNegCrossTopUnified(r, res);
if (crossedCompleteEndLeOne(r, res)) if (crossedCompleteEndLeOne(r, res))
@@ -785,18 +786,18 @@ ConflictRange closedFormCrossedComplete(const RangeRead& r, const Result& res) {
return closedFormNegCrossCompleteHighResidual(r, res); return closedFormNegCrossCompleteHighResidual(r, res);
} }
bool zeroCrossTruncRevCrossed(const RangeRead& r, const Result& res) { bool zeroCrossTruncRevCrossed(const WideRead& r, const Result& res) {
return r.begin.offset == 0 && r.reverse && res.more && return r.begin.offset == 0 && r.reverse && res.more &&
optLt(leastReportedOpt(r, res), r.begin.key); optLt(leastReportedOpt(r, res), r.begin.key);
} }
bool negCrossTruncRevUnder(const RangeRead& r, const Result& res) { bool negCrossTruncRevUnder(const WideRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 && return res.more && r.reverse && r.begin.offset < 0 &&
optLt(last(res), r.begin.key) && optLe(r.begin.key, r.end_.key) && optLt(last(res), r.begin.key) && optLe(r.begin.key, r.end_.key) &&
r.end_.offset <= 1; r.end_.offset <= 1;
} }
bool negCrossTruncRevCrossedLow(const RangeRead& r, const Result& res) { bool negCrossTruncRevCrossedLow(const WideRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 && return res.more && r.reverse && r.begin.offset < 0 &&
optLt(last(res), r.begin.key) && optLt(r.end_.key, r.begin.key) && optLt(last(res), r.begin.key) && optLt(r.end_.key, r.begin.key) &&
r.end_.offset <= 1 && r.end_.offset <= 1 &&
@@ -806,24 +807,24 @@ bool negCrossTruncRevCrossedLow(const RangeRead& r, const Result& res) {
NatInf::of(beginSlack(r) + 1)); NatInf::of(beginSlack(r) + 1));
} }
bool revTruncLeOneLow(const RangeRead& r, const Result& res) { bool revTruncLeOneLow(const WideRead& r, const Result& res) {
return negCrossTruncRevUnder(r, res) || return negCrossTruncRevUnder(r, res) ||
negCrossTruncRevCrossedLow(r, res); negCrossTruncRevCrossedLow(r, res);
} }
bool negCrossTruncRevBelowEnd(const RangeRead& r, const Result& res) { bool negCrossTruncRevBelowEnd(const WideRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 && return res.more && r.reverse && r.begin.offset < 0 &&
optLt(last(res), r.begin.key) && overshoot(r, res); optLt(last(res), r.begin.key) && overshoot(r, res);
} }
bool negCrossTruncRevResidualRoom(const RangeRead& r, const Result& res) { bool negCrossTruncRevResidualRoom(const WideRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 && return res.more && r.reverse && r.begin.offset < 0 &&
r.end_.offset >= 2 && anyAtOrAboveEnd(r, res) && r.end_.offset >= 2 && anyAtOrAboveEnd(r, res) &&
niLe(niAdd(intervalCard(r.end_.key, first(res)), NatInf::of(2)), niLe(niAdd(intervalCard(r.end_.key, first(res)), NatInf::of(2)),
NatInf::of(residualE(r))); NatInf::of(residualE(r)));
} }
bool negCrossTruncRevResidualTopCrossedSmallGap(const RangeRead& r, bool negCrossTruncRevResidualTopCrossedSmallGap(const WideRead& r,
const Result& res) { const Result& res) {
if (!(res.more && r.reverse && r.begin.offset < 0 && if (!(res.more && r.reverse && r.begin.offset < 0 &&
r.end_.offset >= 2 && optLt(r.end_.key, last(res)))) r.end_.offset >= 2 && optLt(r.end_.key, last(res))))
@@ -833,52 +834,52 @@ bool negCrossTruncRevResidualTopCrossedSmallGap(const RangeRead& r,
return niLt(lhs, NatInf::of(residualE(r))); return niLt(lhs, NatInf::of(residualE(r)));
} }
bool negCrossTruncRevResidualTopCrossed(const RangeRead& r, const Result& res) { bool negCrossTruncRevResidualTopCrossed(const WideRead& r, const Result& res) {
return res.more && r.reverse && r.begin.offset < 0 && return res.more && r.reverse && r.begin.offset < 0 &&
r.end_.offset >= 2 && optLe(last(res), r.end_.key) && r.end_.offset >= 2 && optLe(last(res), r.end_.key) &&
residualT(r, res) < residualE(r); residualT(r, res) < residualE(r);
} }
bool revTruncGeTwoTop(const RangeRead& r, const Result& res) { bool revTruncGeTwoTop(const WideRead& r, const Result& res) {
return negCrossTruncRevBelowEnd(r, res) || return negCrossTruncRevBelowEnd(r, res) ||
negCrossTruncRevResidualRoom(r, res) || negCrossTruncRevResidualRoom(r, res) ||
negCrossTruncRevResidualTopCrossedSmallGap(r, res) || negCrossTruncRevResidualTopCrossedSmallGap(r, res) ||
negCrossTruncRevResidualTopCrossed(r, res); negCrossTruncRevResidualTopCrossed(r, res);
} }
bool negCrossTruncRevResidualTopLargeCrossed(const RangeRead& r, bool negCrossTruncRevResidualTopLargeCrossed(const WideRead& r,
const Result& res) { const Result& res) {
return negCrossTruncRevResidualTopLarge(r, res) && return negCrossTruncRevResidualTopLarge(r, res) &&
optLt(last(res), r.begin.key) && !isTop(r.begin.key); optLt(last(res), r.begin.key) && !isTop(r.begin.key);
} }
bool revTruncGeTwoLarge(const RangeRead& r, const Result& res) { bool revTruncGeTwoLarge(const WideRead& r, const Result& res) {
return negCrossTruncRevResidualTopLargeCrossed(r, res) || return negCrossTruncRevResidualTopLargeCrossed(r, res) ||
negCrossTruncRevResidualTopLargeSentinel(r, res); negCrossTruncRevResidualTopLargeSentinel(r, res);
} }
bool negCrossTruncTopTop(const RangeRead& r, const Result& res) { bool negCrossTruncTopTop(const WideRead& r, const Result& res) {
return res.more && !r.reverse && r.begin.offset < 0 && return res.more && !r.reverse && r.begin.offset < 0 &&
isTop(r.begin.key) && isTop(r.end_.key) && r.end_.offset <= 0; isTop(r.begin.key) && isTop(r.end_.key) && r.end_.offset <= 0;
} }
bool negCrossTruncTopTopEq(const RangeRead& r, const Result& res) { bool negCrossTruncTopTopEq(const WideRead& r, const Result& res) {
if (!negCrossTruncTopTop(r, res)) return false; if (!negCrossTruncTopTop(r, res)) return false;
std::uint64_t extra = toNat(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;
} }
std::uint64_t closedFormClampSlack(const RangeRead& r, const Result& res) { std::uint64_t closedFormClampSlack(const WideRead& r, const Result& res) {
return natSub(beginSlack(r) + 1, res.keys.size()) + return natSub(beginSlack(r) + 1, res.keys.size()) +
(isBottom(first(res)) ? 1 : 0); (isBottom(first(res)) ? 1 : 0);
} }
bool closedFormClampGapSmall(const RangeRead& r, const Result& res) { bool closedFormClampGapSmall(const WideRead& r, const Result& res) {
return niLe(intervalCard(asKey(lastKey(res)), r.begin.key), return niLe(intervalCard(asKey(lastKey(res)), r.begin.key),
NatInf::of(closedFormClampSlack(r, res))); NatInf::of(closedFormClampSlack(r, res)));
} }
bool closedFormClampGapExactSaturated(const RangeRead& r, const Result& res) { bool closedFormClampGapExactSaturated(const WideRead& r, const Result& res) {
if (!(first(res) != bottomKeyView() && isBottom(r.end_.key) && if (!(first(res) != bottomKeyView() && isBottom(r.end_.key) &&
residualE(r) == res.keys.size())) residualE(r) == res.keys.size()))
return false; return false;
@@ -886,24 +887,24 @@ bool closedFormClampGapExactSaturated(const RangeRead& r, const Result& res) {
NatInf::of(natSub(beginSlack(r) + 1, res.keys.size()))); NatInf::of(natSub(beginSlack(r) + 1, res.keys.size())));
} }
bool closedFormClampGapLow(const RangeRead& r, const Result& res) { bool closedFormClampGapLow(const WideRead& r, const Result& res) {
return closedFormClampGapSmall(r, res) || return closedFormClampGapSmall(r, res) ||
closedFormClampGapExactSaturated(r, res); closedFormClampGapExactSaturated(r, res);
} }
OptKey closedFormClampUpper(const RangeRead& r, const Result& res) { OptKey closedFormClampUpper(const WideRead& r, const Result& res) {
if (closedFormClampGapLow(r, res)) return afterLast(res); if (closedFormClampGapLow(r, res)) return afterLast(res);
return ownKey(r.begin.key); return ownKey(r.begin.key);
} }
bool closedFormForwardSaturated(const RangeRead& r, const Result& res) { bool closedFormForwardSaturated(const WideRead& r, const Result& res) {
if (!(residualE(r) > 0 && optLt(r.end_.key, first(res)))) return false; if (!(residualE(r) > 0 && optLt(r.end_.key, first(res)))) return false;
NatInf rhs = niAdd(intervalCard(r.end_.key, first(res)), NatInf rhs = niAdd(intervalCard(r.end_.key, first(res)),
NatInf::of(res.keys.size())); NatInf::of(res.keys.size()));
return niLt(NatInf::of(residualE(r)), rhs); return niLt(NatInf::of(residualE(r)), rhs);
} }
ConflictRange closedFormForwardTruncatedNonpos(const RangeRead& r, ConflictRange closedFormForwardTruncatedNonpos(const WideRead& r,
const Result& res) { const Result& res) {
if (reportedBelowBegin(r, res) <= beginSlack(r)) { if (reportedBelowBegin(r, res) <= beginSlack(r)) {
if (r.end_.offset <= 0) { if (r.end_.offset <= 0) {
@@ -929,7 +930,7 @@ ConflictRange closedFormForwardTruncatedNonpos(const RangeRead& r,
return mkRange(begin, optMax(r.begin.key, afterLast(res))); return mkRange(begin, optMax(r.begin.key, afterLast(res)));
} }
ConflictRange closedFormCrossed(const RangeRead& r, const Result& res) { ConflictRange closedFormCrossed(const WideRead& r, const Result& res) {
if (!res.more) { if (!res.more) {
if (!r.reverse) return closedFormCrossedComplete(r, res); if (!r.reverse) return closedFormCrossedComplete(r, res);
std::vector<KeyView> rev = reversedKeys(res); std::vector<KeyView> rev = reversedKeys(res);
@@ -954,11 +955,11 @@ 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); const WideRead wr = cr::detail::widen(r);
if (res.keys.empty()) return emptyValue(r); if (res.keys.empty()) return emptyValue(wr);
if (r.begin.offset >= 1 || clampedNonpos(r, res)) if (wr.begin.offset >= 1 || clampedNonpos(wr, res))
return closedFormPositive(r, res); return closedFormPositive(wr, res);
return closedFormCrossed(r, res); return closedFormCrossed(wr, res);
} }
} // namespace cr::literal } // namespace cr::literal
+52 -34
View File
@@ -18,7 +18,6 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <cassert>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
@@ -40,16 +39,28 @@ 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 // Lean's unbounded `Int`. Public selector offsets are 32-bit; the internal
// `std::int32_t` (see `Selector`), so all intermediate arithmetic in `Int` // wide-offset read used by the decision procedure stores derived offsets here.
// 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 // Caller-supplied offsets are `std::int32_t`, matching FoundationDB.
// stored as `Int` because the closed form also builds derived selectors whose std::int32_t offset = 0;
// offsets (for example `end.offset - residualT`) need not fit in 32 bits. };
struct RangeRead {
Selector begin;
Selector end_;
bool reverse = false;
};
namespace detail {
// Internal mirror of `RangeRead` whose offsets are wide enough for derived
// selectors such as `end.offset - residualT`.
struct Selector {
OptKeyView key;
Int offset = 0; Int offset = 0;
}; };
@@ -59,11 +70,17 @@ struct RangeRead {
bool reverse = false; bool reverse = false;
}; };
// The public offset contract: caller-supplied offsets must fit in inline RangeRead widen(const ::cr::RangeRead& r) {
// `std::int32_t`. This is checked at the public `closedForm` entry points return {{r.begin.key, r.begin.offset},
// (debug builds only) and by the corpus parsers. It is deliberately not {r.end_.key, r.end_.offset},
// checked in `Selector`, because the decision procedure derives selectors r.reverse};
// whose offsets may exceed this range. }
} // namespace detail
// The offset contract shared with the corpus parsers. `Selector::offset` is
// already `std::int32_t`, so parsers validate wider parsed values with these
// bounds before narrowing.
inline constexpr Int kMinInputOffset = inline constexpr Int kMinInputOffset =
std::numeric_limits<std::int32_t>::min(); std::numeric_limits<std::int32_t>::min();
inline constexpr Int kMaxInputOffset = inline constexpr Int kMaxInputOffset =
@@ -74,14 +91,6 @@ inline bool offsetsFitInt32(Int begin, Int end) {
end >= kMinInputOffset && end <= 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.
@@ -272,15 +281,18 @@ inline OptKey afterLast(const Result& res) {
return asKey(keyAfter(lastKey(res))); return asKey(keyAfter(lastKey(res)));
} }
inline KeyView leastReported(const RangeRead& r, const Result& res) { template <class R>
inline KeyView leastReported(const R& r, const Result& res) {
return r.reverse ? lastKey(res) : firstKey(res); return r.reverse ? lastKey(res) : firstKey(res);
} }
inline OptKeyView leastReportedOpt(const RangeRead& r, const Result& res) { template <class R>
inline OptKeyView leastReportedOpt(const R& r, const Result& res) {
return asKey(leastReported(r, res)); return asKey(leastReported(r, res));
} }
inline RangeRead forwardRead(const RangeRead& r) { template <class R>
RangeRead out = r; inline R forwardRead(const R& r) {
R out = r;
out.reverse = false; out.reverse = false;
return out; return out;
} }
@@ -310,7 +322,8 @@ inline NatInf intervalCard(OptKeyView b, OptKeyView e) {
// Result-based counters, in the exact form used by the statement // Result-based counters, in the exact form used by the statement
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
inline std::uint64_t residualT(const RangeRead& r, const Result& res) { template <class R>
inline std::uint64_t residualT(const R& r, const Result& res) {
if (isTop(r.end_.key)) return 0; if (isTop(r.end_.key)) return 0;
std::uint64_t count = 0; std::uint64_t count = 0;
for (KeyView k : res.keys) for (KeyView k : res.keys)
@@ -318,7 +331,8 @@ inline std::uint64_t residualT(const RangeRead& r, const Result& res) {
return count; return count;
} }
inline std::uint64_t reportedBelowBegin(const RangeRead& r, const Result& res) { template <class R>
inline std::uint64_t reportedBelowBegin(const R& r, const Result& res) {
if (isTop(r.begin.key)) return static_cast<std::uint64_t>(res.keys.size()); if (isTop(r.begin.key)) return static_cast<std::uint64_t>(res.keys.size());
std::uint64_t count = 0; std::uint64_t count = 0;
for (KeyView k : res.keys) for (KeyView k : res.keys)
@@ -326,17 +340,21 @@ inline std::uint64_t reportedBelowBegin(const RangeRead& r, const Result& res) {
return count; return count;
} }
inline std::uint64_t residualB(const RangeRead& r) { template <class R>
return toNat(r.begin.offset - 1); inline std::uint64_t residualB(const R& r) {
return toNat(static_cast<Int>(r.begin.offset) - 1);
} }
inline std::uint64_t residualE(const RangeRead& r) { template <class R>
return toNat(r.end_.offset - 1); inline std::uint64_t residualE(const R& r) {
return toNat(static_cast<Int>(r.end_.offset) - 1);
} }
inline std::uint64_t beginSlack(const RangeRead& r) { template <class R>
return toNat(-r.begin.offset); inline std::uint64_t beginSlack(const R& r) {
return toNat(-static_cast<Int>(r.begin.offset));
} }
inline std::uint64_t clampNonposD(const RangeRead& r) { template <class R>
return toNat(-r.end_.offset); inline std::uint64_t clampNonposD(const R& r) {
return toNat(-static_cast<Int>(r.end_.offset));
} }
} // namespace cr } // namespace cr
+2 -2
View File
@@ -49,8 +49,8 @@ std::vector<Key> makeDb(std::size_t n, std::size_t len) {
return db; return db;
} }
Case makeCase(const std::vector<Key>& db, OptKey beginKey, Int beginOff, Case makeCase(const std::vector<Key>& db, OptKey beginKey, std::int32_t beginOff,
OptKey endKey, Int endOff, bool reverse, bool truncate, OptKey endKey, std::int32_t endOff, bool reverse, bool truncate,
const char* name) { const char* name) {
gen::Instance inst; gen::Instance inst;
inst.beginKey = std::move(beginKey); inst.beginKey = std::move(beginKey);
+2 -2
View File
@@ -104,8 +104,8 @@ int main(int argc, char** argv) {
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);
RangeRead r{Selector{optView(beginKey), static_cast<Int>(bo)}, RangeRead r{Selector{optView(beginKey), static_cast<std::int32_t>(bo)},
Selector{optView(endKey), static_cast<Int>(eo)}, reverse}; Selector{optView(endKey), static_cast<std::int32_t>(eo)}, reverse};
Result res{keyViews, more != 0}; Result res{keyViews, more != 0};
++cases; ++cases;
ConflictRange a = literal::closedForm(r, res); ConflictRange a = literal::closedForm(r, res);
+3 -3
View File
@@ -28,7 +28,7 @@ using namespace cr;
// Offset 5 is what lets the default sweep exercise the last three // Offset 5 is what lets the default sweep exercise the last three
// short-circuit branch sides in `fast.cpp` (all in crossed subtrees). // short-circuit branch sides in `fast.cpp` (all in crossed subtrees).
const Int kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5}; const std::int32_t kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
void check(const RangeRead& r, const Result& res, long long& cases, void check(const RangeRead& r, const Result& res, long long& cases,
long long& bad, const char* kind, Int bo, Int eo, bool reverse, long long& bad, const char* kind, Int bo, Int eo, bool reverse,
@@ -47,8 +47,8 @@ void run(const std::vector<Key>& db, const std::vector<OptKey>& keys,
long long& cases, long long& bad) { long long& cases, long long& bad) {
for (const OptKey& beginKey : keys) { for (const OptKey& beginKey : keys) {
for (const OptKey& endKey : keys) { for (const OptKey& endKey : keys) {
for (Int beginOffset : kOffsets) { for (std::int32_t beginOffset : kOffsets) {
for (Int endOffset : kOffsets) { for (std::int32_t endOffset : kOffsets) {
const Selector begin{optView(beginKey), beginOffset}; const Selector begin{optView(beginKey), beginOffset};
const Selector end{optView(endKey), endOffset}; const Selector end{optView(endKey), endOffset};
const std::vector<Key> complete = gen::inRange( const std::vector<Key> complete = gen::inRange(
+21 -20
View File
@@ -9,6 +9,7 @@
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <limits> #include <limits>
#include <type_traits>
#include <utility> #include <utility>
#include <vector> #include <vector>
@@ -33,33 +34,33 @@ int main() {
owned.push_back(OptKey{Key("a")}); owned.push_back(OptKey{Key("a")});
owned.push_back(OptKey{Key("\xff\xff", 2)}); owned.push_back(OptKey{Key("\xff\xff", 2)});
// The public offset contract accepts exactly the 32-bit boundaries; the // `Selector` stores offsets as `std::int32_t`, so `RangeRead` itself cannot
// entry-point assertions and corpus parsers share this predicate. // violate the input contract. The corpus parsers still validate wider
const RangeRead boundary{Selector{std::nullopt, kMinInputOffset}, // parsed values with `offsetsFitInt32` before narrowing.
Selector{std::nullopt, kMaxInputOffset}, false}; static_assert(std::is_same_v<decltype(Selector::offset), std::int32_t>,
const RangeRead outside{Selector{std::nullopt, kMinInputOffset - 1}, "Selector::offset must match the 32-bit input contract");
Selector{std::nullopt, 0}, false}; if (!offsetsFitInt32(kMinInputOffset, kMaxInputOffset) ||
if (!inputOffsetsFitInt32(boundary) || inputOffsetsFitInt32(outside)) { offsetsFitInt32(kMinInputOffset - 1, kMaxInputOffset + 1)) {
std::fprintf(stderr, "offset contract check failed\n"); std::fprintf(stderr, "offset contract check failed\n");
return 1; return 1;
} }
const Int offs[] = {std::numeric_limits<std::int32_t>::min(), const std::int32_t offs[] = {std::numeric_limits<std::int32_t>::min(),
std::numeric_limits<std::int32_t>::min() + 1, std::numeric_limits<std::int32_t>::min() + 1,
-1000000000, -1000000000,
-3, -3,
-1, -1,
0, 0,
1, 1,
2, 2,
3, 3,
1000000000, 1000000000,
std::numeric_limits<std::int32_t>::max() - 1, std::numeric_limits<std::int32_t>::max() - 1,
std::numeric_limits<std::int32_t>::max()}; std::numeric_limits<std::int32_t>::max()};
std::vector<Selector> sels; std::vector<Selector> sels;
sels.reserve(owned.size() * (sizeof(offs) / sizeof(offs[0]))); sels.reserve(owned.size() * (sizeof(offs) / sizeof(offs[0])));
for (const OptKey& k : owned) for (const OptKey& k : owned)
for (Int off : offs) sels.push_back(Selector{optView(k), off}); for (std::int32_t off : offs) sels.push_back(Selector{optView(k), off});
long long cases = 0, bad = 0; long long cases = 0, bad = 0;
for (const Selector& bs : sels) for (const Selector& bs : sels)
+2 -2
View File
@@ -109,8 +109,8 @@ int main(int argc, char** argv) {
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);
RangeRead r{Selector{optView(beginKey), static_cast<Int>(bo)}, RangeRead r{Selector{optView(beginKey), static_cast<std::int32_t>(bo)},
Selector{optView(endKey), static_cast<Int>(eo)}, rev != 0}; Selector{optView(endKey), static_cast<std::int32_t>(eo)}, rev != 0};
Result res{keyViews, more != 0}; Result res{keyViews, more != 0};
OptKey wantBegin, wantEnd; OptKey wantBegin, wantEnd;
if (!parseKey(ob, wantBegin) || !parseKey(oe, wantEnd)) { if (!parseKey(ob, wantBegin) || !parseKey(oe, wantEnd)) {
+6 -6
View File
@@ -61,15 +61,15 @@ inline OptKey randomOptKey(Bytes& b, std::size_t maxLen) {
return OptKey{randomKey(b, maxLen)}; return OptKey{randomKey(b, maxLen)};
} }
inline Int randomOffset(Bytes& b) { inline std::int32_t randomOffset(Bytes& b) {
// Bias towards the offsets that decide the interesting boundary cases, but // Bias towards the offsets that decide the interesting boundary cases, but
// still allow the full [-256, 255] window. // still allow the full [-256, 255] window.
std::uint8_t pick = b.u8(); std::uint8_t pick = b.u8();
if (pick < 192) { if (pick < 192) {
static const Int small[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5}; static const std::int32_t small[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
return small[pick % 9]; return small[pick % 9];
} }
return static_cast<Int>(b.u16() % 512) - 256; return static_cast<std::int32_t>(b.u16() % 512) - 256;
} }
inline OptKey resolve(const std::vector<Key>& db, const Selector& s) { inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
@@ -77,7 +77,7 @@ inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
if (!s.key) { if (!s.key) {
anchor = static_cast<Int>(db.size()) + s.offset - 1; anchor = static_cast<Int>(db.size()) + s.offset - 1;
} else if (s.key->empty()) { } else if (s.key->empty()) {
anchor = s.offset - 1; anchor = static_cast<Int>(s.offset) - 1;
} else { } else {
std::size_t below = 0; std::size_t below = 0;
for (const Key& k : db) for (const Key& k : db)
@@ -110,8 +110,8 @@ struct Instance {
// Owned selector keys; `read()` borrows them. // Owned selector keys; `read()` borrows them.
OptKey beginKey; OptKey beginKey;
OptKey endKey; OptKey endKey;
Int beginOffset = 0; std::int32_t beginOffset = 0;
Int endOffset = 0; std::int32_t endOffset = 0;
bool reverse = false; bool reverse = false;
bool more = false; bool more = false;