- 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.
1078 lines
39 KiB
C++
1078 lines
39 KiB
C++
// A summarised implementation of the same decision procedure as
|
||
// `Statement.closedForm`.
|
||
//
|
||
// `literal.cpp` mirrors the Lean definition line by line and therefore rescans
|
||
// `res.keys` (and re-derives small arithmetic facts) on almost every branch.
|
||
// This version summarises the reported keys once (two binary searches on the
|
||
// sorted traversal order) and caches the handful of interval cardinalities the
|
||
// decision tree needs, then replays the same tree. Reversed results are handled
|
||
// through a `reversed` view instead of copying the key list.
|
||
#include "fast.h"
|
||
|
||
#include <algorithm>
|
||
#include <utility>
|
||
|
||
namespace cr::fast {
|
||
namespace {
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Observational summary
|
||
// ---------------------------------------------------------------------------
|
||
|
||
// A view of the reported keys. `reversed` means the logically forward view is
|
||
// the reverse of the underlying span; this is how the complete/truncated
|
||
// normalisation of a reversed read is represented without copying.
|
||
struct Obs {
|
||
std::span<const KeyView> keys;
|
||
bool reversed = false;
|
||
bool more = false;
|
||
// Whether the *underlying* span is ascending. Readable results are sorted
|
||
// in traversal order, so `!r.reverse` records this and lets the summary use
|
||
// binary searches. A reversed view does not change the span itself.
|
||
bool ascending = true;
|
||
std::size_t n = 0;
|
||
|
||
const KeyView& firstK() const { return reversed ? keys.back() : keys.front(); }
|
||
const KeyView& lastK() const { return reversed ? keys.front() : keys.back(); }
|
||
};
|
||
|
||
// Facts that only depend on the read and on the *multiset* of reported keys.
|
||
// They are therefore invariant under reversing the view.
|
||
struct Ctx {
|
||
const RangeRead* r = nullptr;
|
||
std::size_t n = 0;
|
||
std::size_t below = 0; // number of reported keys < begin.key
|
||
std::size_t t = 0; // number of reported keys >= end.key
|
||
// Lazily computed interval cardinalities.
|
||
mutable bool haveBE = false;
|
||
mutable bool haveEB = false;
|
||
mutable NatInf be;
|
||
mutable NatInf eb;
|
||
};
|
||
|
||
NatInf cardBE(const Ctx& c) {
|
||
if (!c.haveBE) {
|
||
c.be = intervalCard(c.r->begin.key, c.r->end_.key);
|
||
c.haveBE = true;
|
||
}
|
||
return c.be;
|
||
}
|
||
|
||
NatInf cardEB(const Ctx& c) {
|
||
if (!c.haveEB) {
|
||
c.eb = intervalCard(c.r->end_.key, c.r->begin.key);
|
||
c.haveEB = true;
|
||
}
|
||
return c.eb;
|
||
}
|
||
|
||
NatInf cardKO(KeyView b, OptKeyView e) {
|
||
if (!e) return NatInf::top();
|
||
if (!keyLt(b, *e)) return NatInf::of(0);
|
||
if (isPrefix(b, *e) && allZeroFrom(*e, b.size()))
|
||
return NatInf::of(e->size() - b.size());
|
||
return NatInf::top();
|
||
}
|
||
|
||
NatInf cardOK(OptKeyView b, KeyView e) {
|
||
if (!b) return NatInf::of(0);
|
||
if (!keyLt(*b, e)) return NatInf::of(0);
|
||
if (isPrefix(*b, e) && allZeroFrom(e, b->size()))
|
||
return NatInf::of(e.size() - b->size());
|
||
return NatInf::top();
|
||
}
|
||
|
||
// Number of reported keys strictly below `x`. `ascending` says whether the
|
||
// underlying span is ascending (readable forward) or descending (readable
|
||
// reverse). This is a binary search, so the summary is cheap even for long
|
||
// results.
|
||
std::size_t countLess(std::span<const KeyView> ks, OptKeyView x,
|
||
bool ascending) {
|
||
if (!x) return ks.size(); // every key is below ⊤
|
||
if (x->empty()) return 0; // no key is below the empty key
|
||
const std::size_t n = ks.size();
|
||
if (ascending) {
|
||
std::size_t lo = 0, hi = n;
|
||
while (lo < hi) {
|
||
std::size_t mid = lo + (hi - lo) / 2;
|
||
if (keyLt(ks[mid], *x)) lo = mid + 1;
|
||
else hi = mid;
|
||
}
|
||
return lo;
|
||
}
|
||
// Descending: the keys below `x` form a suffix.
|
||
std::size_t lo = 0, hi = n;
|
||
while (lo < hi) {
|
||
std::size_t mid = lo + (hi - lo) / 2;
|
||
if (keyLt(ks[mid], *x)) hi = mid;
|
||
else lo = mid + 1;
|
||
}
|
||
return n - lo;
|
||
}
|
||
|
||
Ctx makeCtx(const RangeRead& r, const Obs& o) {
|
||
Ctx c;
|
||
c.r = &r;
|
||
c.n = o.n;
|
||
const std::span<const KeyView> ks = o.keys;
|
||
c.below = countLess(ks, r.begin.key, o.ascending);
|
||
c.t = o.n - countLess(ks, r.end_.key, o.ascending);
|
||
return c;
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Comparisons that avoid allocating a one-element `OptKey`
|
||
// ---------------------------------------------------------------------------
|
||
|
||
// ↑k < b
|
||
bool kLtO(KeyView k, OptKeyView b) { return !b || keyLt(k, *b); }
|
||
// ↑k <= b
|
||
bool kLeO(KeyView k, OptKeyView b) { return !b || !keyLt(*b, k); }
|
||
// a < ↑k
|
||
bool oLtK(OptKeyView a, KeyView k) { return a.has_value() && keyLt(*a, k); }
|
||
// a <= ↑k
|
||
bool oLeK(OptKeyView a, KeyView k) { return a.has_value() && !keyLt(k, *a); }
|
||
|
||
OptKey after(KeyView k) { return OptKey{keyAfter(k)}; }
|
||
|
||
std::uint64_t bSlack(const Ctx& c) { return beginSlack(*c.r); }
|
||
std::uint64_t rE(const Ctx& c) { return residualE(*c.r); }
|
||
std::uint64_t rB(const Ctx& c) { return residualB(*c.r); }
|
||
std::uint64_t npd(const Ctx& c) { return clampNonposD(*c.r); }
|
||
|
||
bool allBelowEnd(const Ctx& c) { return c.t == 0; }
|
||
bool anyAtOrAboveEnd(const Ctx& c) { return c.t != 0; }
|
||
|
||
const KeyView& leastReportedK(const Ctx& c, const Obs& o) {
|
||
return c.r->reverse ? o.lastK() : o.firstK();
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Arithmetic helpers, on cached facts
|
||
// ---------------------------------------------------------------------------
|
||
|
||
template <class B, class E>
|
||
ConflictRange mk(B&& b, E&& e) {
|
||
return ConflictRange{ownKey(std::forward<B>(b)), ownKey(std::forward<E>(e))};
|
||
}
|
||
|
||
std::uint64_t clampSlack(const Ctx& c) {
|
||
return natSub(bSlack(c) + 1, c.below);
|
||
}
|
||
|
||
OptKey clampEndOneUpper(const Ctx& c, const Obs& o) {
|
||
NatInf slack = NatInf::of(clampSlack(c));
|
||
NatInf cap = cardEB(c);
|
||
if (niLe(slack, cap) && (niLt(slack, cap) || !o.firstK().empty()))
|
||
return ownKey(c.r->begin.key);
|
||
return ownKey(c.r->end_.key);
|
||
}
|
||
|
||
std::uint64_t clampHighThresh(const Ctx& c) {
|
||
return clampSlack(c) + (isBottom(c.r->end_.key) ? 1 : 0);
|
||
}
|
||
|
||
OptKey clampHighU(const Ctx& c, const Obs& o) {
|
||
NatInf cap = cardKO(keyAfter(o.lastK()), c.r->begin.key);
|
||
if (niLe(NatInf::of(clampHighThresh(c)), cap))
|
||
return ownKey(c.r->begin.key);
|
||
return after(o.lastK());
|
||
}
|
||
|
||
std::uint64_t clampLongC(const Ctx& c) {
|
||
return natSub(natSub(bSlack(c), c.below), npd(c));
|
||
}
|
||
|
||
OptKey clampLongUpper(const Ctx& c, const Obs& o) {
|
||
NatInf cap = cardEB(c);
|
||
NatInf val = NatInf::of(clampLongC(c));
|
||
if (niLt(cap, val) || (niEq(cap, val) && o.firstK().empty()))
|
||
return ownKey(c.r->end_.key);
|
||
return ownKey(c.r->begin.key);
|
||
}
|
||
|
||
bool clampNonposBottom(const Ctx& c) {
|
||
NatInf lhs = niAdd(NatInf::of(c.below),
|
||
niSub(NatInf::of(npd(c) + 1), cardBE(c)));
|
||
return niLe(lhs, NatInf::of(bSlack(c)));
|
||
}
|
||
|
||
bool clampedNonpos(const Ctx& c, const Obs& o) {
|
||
return c.r->begin.offset <= 0 && oLeK(c.r->begin.key, leastReportedK(c, o));
|
||
}
|
||
|
||
bool clampsToBottom(const Ctx& c, const Obs& o) {
|
||
return c.r->begin.offset <= 0 && oLeK(c.r->begin.key, o.firstK());
|
||
}
|
||
|
||
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(std::uint64_t rb, NatInf g) {
|
||
if (g.inf) return 0;
|
||
return natSub(rb, g.n);
|
||
}
|
||
|
||
std::uint64_t closedFormDeficit(const Ctx& c) {
|
||
return residualD(rB(c), cardBE(c));
|
||
}
|
||
|
||
std::uint64_t gapOvershoot(const Ctx& c) {
|
||
return natSub(rE(c), static_cast<std::uint64_t>(c.t));
|
||
}
|
||
|
||
std::uint64_t crossedOvershootGeneral(const Ctx& c) {
|
||
return natSub(natSub(rE(c), static_cast<std::uint64_t>(c.t)), rB(c));
|
||
}
|
||
|
||
OptKeyView crossedBegin(const Ctx& c) {
|
||
if (niLe(NatInf::of(crossedOvershootGeneral(c)), cardEB(c)))
|
||
return c.r->end_.key;
|
||
return c.r->begin.key;
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// The decision tree
|
||
// ---------------------------------------------------------------------------
|
||
|
||
struct Impl {
|
||
// ----- crossed complete --------------------------------------------------
|
||
|
||
bool negCrossClampEndOne(const Ctx& c, const Obs& o) const {
|
||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||
c.r->end_.offset == 1 && kLtO(o.firstK(), c.r->begin.key) &&
|
||
c.below <= bSlack(c);
|
||
}
|
||
|
||
bool negCrossHeadBottom(const Ctx& c, const Obs& o) const {
|
||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||
c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
|
||
o.firstK().empty();
|
||
}
|
||
|
||
bool negCrossClampNonposLastGe(const Ctx& c, const Obs& o) const {
|
||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||
c.r->end_.offset <= 0 && kLtO(o.firstK(), c.r->begin.key) &&
|
||
oLeK(c.r->begin.key, o.lastK()) && c.below <= bSlack(c);
|
||
}
|
||
|
||
bool negCrossClampNonposBottom(const Ctx& c, const Obs& o) const {
|
||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||
c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
|
||
kLtO(o.firstK(), c.r->begin.key) && clampNonposBottom(c);
|
||
}
|
||
|
||
bool negCrossClampLongNonpos(const Ctx& c, const Obs& o) const {
|
||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||
c.r->end_.offset <= 0 && kLtO(o.firstK(), c.r->begin.key) &&
|
||
c.below <= bSlack(c) && clampNonposBottom(c) &&
|
||
optLt(c.r->end_.key, c.r->begin.key);
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossClampUnified(const Ctx& c, const Obs& o) const {
|
||
OptKey end;
|
||
if (negCrossClampEndOne(c, o))
|
||
end = clampEndOneUpper(c, o);
|
||
else if (negCrossClampLongNonpos(c, o) &&
|
||
!negCrossClampNonposLastGe(c, o))
|
||
end = clampLongUpper(c, o);
|
||
else
|
||
end = c.r->end_.key;
|
||
return mk(bottomKey(), std::move(end));
|
||
}
|
||
|
||
OptKey negCrossCompleteHighM(const Ctx& c, const Obs& o) const {
|
||
return optMax(c.r->begin.key, after(o.lastK()));
|
||
}
|
||
|
||
std::uint64_t negCrossHighD(const Ctx& c) const {
|
||
return natSub(rE(c), static_cast<std::uint64_t>(c.n));
|
||
}
|
||
|
||
NatInf negCrossHighGapCap(const Ctx& c, const Obs& o) const {
|
||
return cardOK(c.r->end_.key, o.firstK());
|
||
}
|
||
|
||
bool negCrossHighUsesEnd(const Ctx& c, const Obs& o) const {
|
||
NatInf d = NatInf::of(negCrossHighD(c));
|
||
NatInf gap = negCrossHighGapCap(c, o);
|
||
return niLt(d, gap) ||
|
||
(niLe(d, gap) && c.r->begin.key.has_value());
|
||
}
|
||
|
||
bool negCrossCompleteHigh(const Ctx& c, const Obs& o) const {
|
||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||
c.r->end_.offset >= 2 && kLtO(o.firstK(), c.r->begin.key) &&
|
||
oLtK(c.r->end_.key, o.firstK());
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossCompleteHigh(const Ctx& c, const Obs& o) const {
|
||
if (c.below <= bSlack(c)) {
|
||
if (c.n == rE(c)) return mk(bottomKey(), clampHighU(c, o));
|
||
return mk(bottomKey(), topKey());
|
||
}
|
||
if (c.n == rE(c))
|
||
return mk(c.r->end_.key, negCrossCompleteHighM(c, o));
|
||
if (negCrossHighUsesEnd(c, o)) return mk(c.r->end_.key, topKey());
|
||
return mk(OptKey{o.firstK()}, topKey());
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossOvershoot(const Ctx& c, const Obs& o) const {
|
||
if (c.below <= bSlack(c)) return mk(bottomKey(), topKey());
|
||
return mk(OptKey{o.firstK()}, topKey());
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossEndOne(const Ctx& c, const Obs& o) const {
|
||
return mk(OptKey{o.firstK()}, c.r->end_.key);
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossEndOneLong(const Ctx& c, const Obs& o) const {
|
||
return mk(OptKey{o.firstK()}, c.r->begin.key);
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossCompleteResidual(const Ctx& c, const Obs& o) const {
|
||
// Keep the synthesised end key alive while `rp` borrows it.
|
||
OptKey reducedEnd = after(o.lastK());
|
||
RangeRead rp = *c.r;
|
||
rp.end_.key = optView(reducedEnd);
|
||
rp.end_.offset = rp.end_.offset - static_cast<Int>(c.t);
|
||
Ctx cp = makeCtx(rp, o);
|
||
if (rp.end_.offset >= 2)
|
||
return closedFormNegCrossOvershoot(cp, o);
|
||
if (cp.below <= bSlack(cp))
|
||
return mk(bottomKey(), clampEndOneUpper(cp, o));
|
||
if (optLe(c.r->begin.key, rp.end_.key))
|
||
return closedFormNegCrossEndOne(cp, o);
|
||
return closedFormNegCrossEndOneLong(cp, o);
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossCompleteHighResidual(const Ctx& c, const Obs& o) const {
|
||
if (negCrossCompleteHigh(c, o))
|
||
return closedFormNegCrossCompleteHigh(c, o);
|
||
return closedFormNegCrossCompleteResidual(c, o);
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossNonclampUnified(const Ctx& c, const Obs& o) const {
|
||
return mk(OptKey{o.firstK()}, optMax(c.r->end_.key, c.r->begin.key));
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossTopUnified(const Ctx& c, const Obs& o) const {
|
||
OptKey begin = c.below <= bSlack(c) ? bottomKey()
|
||
: OptKey{leastReportedK(c, o)};
|
||
return mk(std::move(begin), topKey());
|
||
}
|
||
|
||
// ----- crossed truncated -------------------------------------------------
|
||
|
||
bool negCrossTruncNonposGap(const Ctx& c, const Obs& o) const {
|
||
return o.more && !c.r->reverse && c.r->begin.offset < 0 &&
|
||
kLtO(leastReportedK(c, o), c.r->begin.key) &&
|
||
c.r->end_.offset <= 0 && c.below <= bSlack(c) &&
|
||
kLtO(o.lastK(), c.r->begin.key) &&
|
||
kLtO(o.lastK(), c.r->end_.key) &&
|
||
!isTop(c.r->end_.key) &&
|
||
(optLt(c.r->end_.key, c.r->begin.key) ||
|
||
niLt(cardBE(c), NatInf::of(npd(c) + 1)));
|
||
}
|
||
|
||
bool negCrossTruncNonposGapA2aBottom(const Ctx& c, const Obs& o) const {
|
||
return negCrossTruncNonposGap(c, o) &&
|
||
optLt(c.r->end_.key, c.r->begin.key) && clampNonposBottom(c);
|
||
}
|
||
|
||
bool negCrossTruncNonposGapA2aBottomHigh(const Ctx& c, const Obs& o) const {
|
||
std::uint64_t extra = o.firstK().empty() ? 1 : 0;
|
||
NatInf lhs = NatInf::of(bSlack(c) + 1 + extra);
|
||
NatInf rhs = niAdd(NatInf::of(static_cast<std::uint64_t>(c.n)),
|
||
cardKO(keyAfter(o.lastK()), c.r->begin.key));
|
||
return negCrossTruncNonposGapA2aBottom(c, o) && niLe(lhs, rhs);
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const Ctx& c,
|
||
const Obs& o) const {
|
||
if (negCrossTruncNonposGapA2aBottomHigh(c, o))
|
||
return mk(bottomKey(), c.r->begin.key);
|
||
return mk(bottomKey(), c.r->end_.key);
|
||
}
|
||
|
||
bool finiteUnderCond(const Ctx& c, const Obs& o) const {
|
||
NatInf lhs = niAdd(NatInf::of(natSub(c.below, 1)),
|
||
cardKO(keyAfter(o.firstK()), c.r->begin.key));
|
||
return niLe(lhs, NatInf::of(bSlack(c)));
|
||
}
|
||
|
||
bool negCrossTruncRevResidualFiniteOver(const Ctx& c, const Obs& o) const {
|
||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK()) &&
|
||
kLtO(o.lastK(), c.r->begin.key) && c.t == rE(c);
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const Ctx& c,
|
||
const Obs& o) const {
|
||
OptKeyView L = asKey(o.lastK());
|
||
OptKey U = finiteUnderCond(c, o) ? after(o.firstK()) : ownKey(c.r->begin.key);
|
||
if (negCrossTruncRevResidualFiniteOver(c, o))
|
||
return mk(c.r->end_.key, U);
|
||
return mk(std::move(L), std::move(U));
|
||
}
|
||
|
||
bool sentinelLargeTight(const Ctx& c, const Obs& o) const {
|
||
NatInf lhs = NatInf::of(rE(c));
|
||
NatInf rhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
|
||
NatInf::of(static_cast<std::uint64_t>(c.n)));
|
||
__int128 nPlusBegin = static_cast<__int128>(c.n) + c.r->begin.offset;
|
||
return niEq(lhs, rhs) && nPlusBegin == 1;
|
||
}
|
||
|
||
bool negCrossTruncRevResidualTopLarge(const Ctx& c, const Obs& o) const {
|
||
if (!(o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK()) &&
|
||
c.t < rE(c)))
|
||
return false;
|
||
NatInf lhs = NatInf::of(rE(c));
|
||
NatInf rhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
|
||
NatInf::of(static_cast<std::uint64_t>(c.n)));
|
||
return niLe(lhs, rhs);
|
||
}
|
||
|
||
bool negCrossTruncRevResidualTopLargeSentinel(const Ctx& c, const Obs& o) const {
|
||
return negCrossTruncRevResidualTopLarge(c, o) && isTop(c.r->begin.key);
|
||
}
|
||
|
||
ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const Ctx& c,
|
||
const Obs& o) const {
|
||
OptKey L = OptKey{o.lastK()};
|
||
if (negCrossTruncRevResidualTopLargeSentinel(c, o) &&
|
||
sentinelLargeTight(c, o))
|
||
return mk(std::move(L), topKey());
|
||
return mk(c.r->end_.key, topKey());
|
||
}
|
||
|
||
// ----- complete / offset one ---------------------------------------------
|
||
|
||
ConflictRange closedFormReverseTruncatedZero(const Ctx& c, const Obs& o) const {
|
||
if (c.r->end_.offset <= 1)
|
||
return mk(asKey(o.lastK()), c.r->end_.key);
|
||
OptKeyView L = asKey(o.lastK());
|
||
if (c.t == rE(c))
|
||
return mk(optMin(L, c.r->end_.key), after(o.firstK()));
|
||
if (isTop(c.r->end_.key)) return mk(std::move(L), topKey());
|
||
KeyView E = *c.r->end_.key;
|
||
std::uint64_t m = natSub(natSub(rE(c), c.t), 1);
|
||
if (keyLt(keyAfterN(m, E), o.lastK()))
|
||
return mk(asKey(E), topKey());
|
||
return mk(std::move(L), topKey());
|
||
}
|
||
|
||
ConflictRange truncatedForwardClosed(const Ctx& c, const Obs& o) const {
|
||
NatInf cap = cardEB(c);
|
||
NatInf room = NatInf::of(natSub(rE(c), static_cast<std::uint64_t>(c.n)));
|
||
if (niLe(cap, room))
|
||
return mk(c.r->begin.key, after(o.lastK()));
|
||
return mk(c.r->end_.key, after(o.lastK()));
|
||
}
|
||
|
||
ConflictRange completeClosedWith(OptKey endKey, const Ctx& c, const Obs& o) const {
|
||
(void)o;
|
||
OptKeyView begin = c.r->begin.key;
|
||
if (optLt(c.r->end_.key, c.r->begin.key) &&
|
||
niLe(NatInf::of(gapOvershoot(c)), cardEB(c)))
|
||
begin = c.r->end_.key;
|
||
if (c.r->end_.offset <= 1)
|
||
return mk(c.r->begin.key, c.r->end_.key);
|
||
if (c.t == rE(c)) return mk(std::move(begin), std::move(endKey));
|
||
return mk(std::move(begin), topKey());
|
||
}
|
||
|
||
ConflictRange completeReversedClosed(const Ctx& c, const Obs& o) const {
|
||
return completeClosedWith(after(o.firstK()), c, o);
|
||
}
|
||
ConflictRange completeForwardClosed(const Ctx& c, const Obs& o) const {
|
||
return completeClosedWith(after(o.lastK()), c, o);
|
||
}
|
||
ConflictRange completeClosed(const Ctx& c, const Obs& o) const {
|
||
return c.r->reverse ? completeReversedClosed(c, o)
|
||
: completeForwardClosed(c, o);
|
||
}
|
||
|
||
ConflictRange closedFormOffsetOneTruncated(const Ctx& c, const Obs& o) const {
|
||
if (c.r->end_.offset <= 0)
|
||
return mk(c.r->begin.key, c.r->end_.key);
|
||
return mk(c.r->begin.key, after(o.lastK()));
|
||
}
|
||
|
||
ConflictRange topRange(const Ctx& c) const {
|
||
return mk(c.r->begin.key, topKey());
|
||
}
|
||
|
||
ConflictRange closedFormOffsetOneWith(OptKey endKey, const Ctx& c,
|
||
const Obs& o) const {
|
||
(void)o;
|
||
if (c.r->end_.offset <= 1)
|
||
return mk(c.r->begin.key, c.r->end_.key);
|
||
if (c.t == rE(c))
|
||
return mk(c.r->begin.key, std::move(endKey));
|
||
return topRange(c);
|
||
}
|
||
|
||
ConflictRange closedFormOffsetOneRev(const Ctx& c, const Obs& o) const {
|
||
return closedFormOffsetOneWith(after(o.firstK()), c, o);
|
||
}
|
||
ConflictRange closedFormOffsetOne(const Ctx& c, const Obs& o) const {
|
||
return closedFormOffsetOneWith(after(o.lastK()), c, o);
|
||
}
|
||
ConflictRange closedFormOffsetOneAny(const Ctx& c, const Obs& o) const {
|
||
return c.r->reverse ? closedFormOffsetOneRev(c, o)
|
||
: closedFormOffsetOne(c, o);
|
||
}
|
||
|
||
ConflictRange closedFormOffsetOneUniversal(const Ctx& c, const Obs& o) const {
|
||
if (o.more)
|
||
return c.r->reverse ? closedFormReverseTruncatedZero(c, o)
|
||
: closedFormOffsetOneTruncated(c, o);
|
||
return closedFormOffsetOneAny(c, o);
|
||
}
|
||
|
||
ConflictRange closedFormOffsetOneTotal(const Ctx& c, const Obs& o) const {
|
||
if (optLt(c.r->end_.key, c.r->begin.key)) {
|
||
if (o.more)
|
||
return c.r->reverse ? closedFormReverseTruncatedZero(c, o)
|
||
: truncatedForwardClosed(c, o);
|
||
return completeClosed(c, o);
|
||
}
|
||
return closedFormOffsetOneUniversal(c, o);
|
||
}
|
||
|
||
// ----- zero crossed ------------------------------------------------------
|
||
|
||
ConflictRange closedFormZeroCrossEnd(const Ctx& c, const Obs& o) const {
|
||
return mk(OptKey{leastReportedK(c, o)}, c.r->end_.key);
|
||
}
|
||
|
||
ConflictRange completeForwardCrossedGeneral(const Ctx& c, const Obs& o) const {
|
||
if (c.r->end_.offset <= 1)
|
||
return mk(c.r->begin.key, c.r->end_.key);
|
||
if (c.t + rB(c) == rE(c))
|
||
return mk(c.r->end_.key, after(o.lastK()));
|
||
return mk(crossedBegin(c), topKey());
|
||
}
|
||
|
||
RangeRead zeroBeginAnchor(const Ctx& c, const Obs& o) const {
|
||
RangeRead out = *c.r;
|
||
out.begin.key = asKey(o.firstK());
|
||
out.begin.offset = 1;
|
||
return out;
|
||
}
|
||
|
||
ConflictRange zeroCrossLongAnchor(const Ctx& c, const Obs& o) const {
|
||
RangeRead a = zeroBeginAnchor(c, o);
|
||
Ctx ca = makeCtx(a, o);
|
||
if (oLtK(c.r->end_.key, o.firstK()))
|
||
return completeForwardCrossedGeneral(ca, o);
|
||
return closedFormOffsetOne(ca, o);
|
||
}
|
||
|
||
ConflictRange closedFormZeroCrossLong(const Ctx& c, const Obs& o) const {
|
||
if (isTop(c.r->begin.key)) {
|
||
KeyView H = o.firstK();
|
||
std::uint64_t e = rE(c);
|
||
std::uint64_t t = c.t;
|
||
NatInf g = cardOK(c.r->end_.key, H);
|
||
bool useEnd = oLtK(c.r->end_.key, H) &&
|
||
niLt(niSub(NatInf::of(e), NatInf::of(t)), g);
|
||
return mk(useEnd ? c.r->end_.key : OptKey{H}, topKey());
|
||
}
|
||
ConflictRange a = zeroCrossLongAnchor(c, o);
|
||
return mk(std::move(a.begin), optMax(a.end_, c.r->begin.key));
|
||
}
|
||
|
||
ConflictRange closedFormZeroCrossUnderEnd(const Ctx& c, const Obs& o) const {
|
||
RangeRead a = zeroBeginAnchor(c, o);
|
||
Ctx ca = makeCtx(a, o);
|
||
return closedFormOffsetOneTotal(ca, o);
|
||
}
|
||
|
||
ConflictRange closedFormZeroCrossComplete(const Ctx& c, const Obs& o) const {
|
||
if (c.r->end_.offset <= 0) return closedFormZeroCrossEnd(c, o);
|
||
if (c.r->end_.offset == 1) {
|
||
if (optLt(c.r->end_.key, c.r->begin.key))
|
||
return mk(OptKey{leastReportedK(c, o)}, c.r->begin.key);
|
||
return closedFormZeroCrossEnd(c, o);
|
||
}
|
||
if (isTop(c.r->begin.key) || optLt(c.r->end_.key, c.r->begin.key))
|
||
return closedFormZeroCrossLong(c, o);
|
||
return closedFormZeroCrossUnderEnd(c, o);
|
||
}
|
||
|
||
ConflictRange closedFormZeroCrossCompleteRev(const Ctx& c, const Obs& o) const {
|
||
RangeRead r2 = *c.r;
|
||
r2.reverse = false;
|
||
Obs o2 = o;
|
||
o2.reversed = true;
|
||
o2.more = false;
|
||
Ctx c2 = makeCtx(r2, o2);
|
||
return closedFormZeroCrossComplete(c2, o2);
|
||
}
|
||
|
||
bool zeroCrossUnderEnd(const Ctx& c, const Obs& o) const {
|
||
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
|
||
c.r->end_.offset >= 2 &&
|
||
kLtO(leastReportedK(c, o), c.r->begin.key) &&
|
||
optLe(c.r->begin.key, c.r->end_.key);
|
||
}
|
||
|
||
ConflictRange closedFormZeroCrossResidual(const Ctx& c, const Obs& o) const {
|
||
if (zeroCrossUnderEnd(c, o)) return closedFormZeroCrossUnderEnd(c, o);
|
||
return closedFormZeroCrossLong(c, o);
|
||
}
|
||
|
||
ConflictRange closedFormZeroCrossTruncRevCrossed(const Ctx& c, const Obs& o) const {
|
||
Obs complete = o;
|
||
complete.more = false;
|
||
return closedFormZeroCrossCompleteRev(c, complete);
|
||
}
|
||
|
||
// ----- empty result ------------------------------------------------------
|
||
|
||
bool AlwaysEmpty(const RangeRead& r) const {
|
||
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) const {
|
||
return mk(r.begin.key, r.begin.key);
|
||
}
|
||
|
||
ConflictRange crossedRangeGeneral(const RangeRead& r) const {
|
||
NatInf diff = niSub(NatInf::of(residualE(r)), NatInf::of(residualB(r)));
|
||
if (niLe(diff, intervalCard(r.end_.key, r.begin.key)))
|
||
return mk(r.end_.key, topKey());
|
||
return mk(r.begin.key, topKey());
|
||
}
|
||
|
||
ConflictRange emptyValue(const RangeRead& r) const {
|
||
if (AlwaysEmpty(r)) return emptyRange(r);
|
||
if (r.begin.offset <= 0) {
|
||
if (r.end_.offset >= 2) return mk(bottomKey(), topKey());
|
||
if (niLt(intervalCard(r.end_.key, r.begin.key),
|
||
NatInf::of(toNat(static_cast<__int128>(r.end_.offset) -
|
||
r.begin.offset))))
|
||
return mk(bottomKey(), r.end_.key);
|
||
return mk(bottomKey(), optMax(r.begin.key, r.end_.key));
|
||
}
|
||
if (r.end_.offset <= 1) return mk(r.begin.key, r.end_.key);
|
||
if (optLe(r.begin.key, r.end_.key)) return topRange(r);
|
||
return crossedRangeGeneral(r);
|
||
}
|
||
|
||
ConflictRange topRange(const RangeRead& r) const {
|
||
return mk(r.begin.key, topKey());
|
||
}
|
||
|
||
// ----- complete and forward truncated results ----------------------------
|
||
|
||
OptKeyView closedFormBegin(const Ctx& c, const Obs& o) const {
|
||
return clampsToBottom(c, o) ? bottomKeyView() : c.r->begin.key;
|
||
}
|
||
|
||
bool closedFormFinite(const Ctx& c) const {
|
||
return closedFormDeficit(c) + static_cast<std::uint64_t>(c.t) == rE(c);
|
||
}
|
||
|
||
ConflictRange closedFormCompleteForward(const Ctx& c, const Obs& o) const {
|
||
OptKeyView begin = closedFormBegin(c, o);
|
||
if (c.r->end_.offset <= 1)
|
||
return mk(std::move(begin), c.r->end_.key);
|
||
if (clampsToBottom(c, o) || optLe(c.r->begin.key, c.r->end_.key)) {
|
||
if (c.t == rE(c)) return mk(std::move(begin), after(o.lastK()));
|
||
if (allBelowEnd(c)) return mk(std::move(begin), topKey());
|
||
if (closedFormFinite(c)) return mk(std::move(begin), after(o.lastK()));
|
||
return mk(std::move(begin), topKey());
|
||
}
|
||
if (closedFormFinite(c))
|
||
return mk(c.r->end_.key, after(o.lastK()));
|
||
return mk(crossedBegin(c), topKey());
|
||
}
|
||
|
||
ConflictRange closedFormComplete(const Ctx& c, const Obs& o) const {
|
||
if (!c.r->reverse) return closedFormCompleteForward(c, o);
|
||
RangeRead r2 = *c.r;
|
||
r2.reverse = false;
|
||
Obs o2 = o;
|
||
o2.reversed = true;
|
||
o2.more = false;
|
||
Ctx c2 = makeCtx(r2, o2);
|
||
return closedFormCompleteForward(c2, o2);
|
||
}
|
||
|
||
NatInf closedFormForwardCapacity(const Ctx& c, const Obs& o) const {
|
||
if (clampsToBottom(c, o)) return NatInf::of(0);
|
||
return cardEB(c);
|
||
}
|
||
|
||
ConflictRange closedFormForwardTruncated(const Ctx& c, const Obs& o) const {
|
||
OptKeyView begin = closedFormBegin(c, o);
|
||
if (c.r->end_.offset <= 0)
|
||
return mk(std::move(begin), c.r->end_.key);
|
||
std::uint64_t room =
|
||
natSub(natSub(rE(c), static_cast<std::uint64_t>(c.n)), rB(c));
|
||
if (niLe(closedFormForwardCapacity(c, o), NatInf::of(room)))
|
||
return mk(std::move(begin), after(o.lastK()));
|
||
return mk(c.r->end_.key, after(o.lastK()));
|
||
}
|
||
|
||
// ----- reversed truncation ----------------------------------------------
|
||
|
||
std::uint64_t closedFormReverseCount(const Ctx& c) const {
|
||
if (optLt(c.r->end_.key, c.r->begin.key))
|
||
return static_cast<std::uint64_t>(c.n);
|
||
return c.t;
|
||
}
|
||
|
||
bool closedFormReverseFinite(const Ctx& c) const {
|
||
return closedFormDeficit(c) + closedFormReverseCount(c) == rE(c);
|
||
}
|
||
|
||
bool closedFormCrossedRoom(const Ctx& c, const Obs& o) const {
|
||
NatInf lhs =
|
||
NatInf::of(natSub(rE(c), static_cast<std::uint64_t>(c.n)));
|
||
return niLe(lhs, cardOK(c.r->end_.key, o.lastK()));
|
||
}
|
||
|
||
ConflictRange closedFormReverseTruncatedSkip(const Ctx& c, const Obs& o) const {
|
||
if (c.r->end_.offset <= 1)
|
||
return mk(c.r->begin.key, c.r->end_.key);
|
||
if (allBelowEnd(c)) return topRange(c);
|
||
if (closedFormReverseFinite(c)) {
|
||
OptKeyView begin = optLt(c.r->end_.key, c.r->begin.key)
|
||
? c.r->end_.key
|
||
: c.r->begin.key;
|
||
return mk(std::move(begin), after(o.firstK()));
|
||
}
|
||
OptKeyView begin =
|
||
(optLt(c.r->end_.key, c.r->begin.key) && closedFormCrossedRoom(c, o))
|
||
? c.r->end_.key
|
||
: c.r->begin.key;
|
||
return mk(std::move(begin), topKey());
|
||
}
|
||
|
||
ConflictRange closedFormReverseTruncated(const Ctx& c, const Obs& o) const {
|
||
if (rB(c) == 0) return closedFormReverseTruncatedZero(c, o);
|
||
return closedFormReverseTruncatedSkip(c, o);
|
||
}
|
||
|
||
ConflictRange closedFormPositive(const Ctx& c, const Obs& o) const {
|
||
if (!o.more) return closedFormComplete(c, o);
|
||
if (!c.r->reverse) return closedFormForwardTruncated(c, o);
|
||
return closedFormReverseTruncated(c, o);
|
||
}
|
||
|
||
// ----- crossed dispatch --------------------------------------------------
|
||
|
||
bool overshoot(const Ctx& c) const {
|
||
return c.r->end_.offset >= 2 && allBelowEnd(c);
|
||
}
|
||
|
||
bool zeroCrossOvershootComplete(const Ctx& c, const Obs& o) const {
|
||
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
|
||
overshoot(c) && kLtO(leastReportedK(c, o), c.r->begin.key);
|
||
}
|
||
|
||
bool negCrossOvershoot(const Ctx& c, const Obs& o) const {
|
||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more && overshoot(c) &&
|
||
kLtO(o.firstK(), c.r->begin.key);
|
||
}
|
||
|
||
bool crossedCompleteOvershoot(const Ctx& c, const Obs& o) const {
|
||
return zeroCrossOvershootComplete(c, o) || negCrossOvershoot(c, o);
|
||
}
|
||
|
||
bool crossedCompleteEndLeOne(const Ctx& c, const Obs& o) const {
|
||
if (!(!c.r->reverse && !o.more && c.r->begin.offset <= 0 &&
|
||
kLtO(o.firstK(), c.r->begin.key)))
|
||
return false;
|
||
if (c.r->end_.offset == 1 && bSlack(c) < c.below)
|
||
return true;
|
||
return c.r->end_.offset <= 0 &&
|
||
!(c.below <= bSlack(c) && clampNonposBottom(c));
|
||
}
|
||
|
||
bool zeroCrossLongTop(const Ctx& c, const Obs& o) const {
|
||
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
|
||
c.r->end_.offset >= 2 && isTop(c.r->begin.key);
|
||
}
|
||
|
||
bool zeroCrossLongFinite(const Ctx& c, const Obs& o) const {
|
||
return c.r->begin.offset == 0 && !c.r->reverse &&
|
||
c.r->end_.offset >= 2 && !isTop(c.r->begin.key) &&
|
||
optLt(c.r->end_.key, c.r->begin.key) &&
|
||
kLtO(leastReportedK(c, o), c.r->begin.key);
|
||
}
|
||
|
||
bool crossedCompleteBeginZero(const Ctx& c, const Obs& o) const {
|
||
return zeroCrossUnderEnd(c, o) || zeroCrossLongTop(c, o) ||
|
||
zeroCrossLongFinite(c, o);
|
||
}
|
||
|
||
bool crossedCompleteClamp(const Ctx& c, const Obs& o) const {
|
||
return negCrossClampEndOne(c, o) || negCrossHeadBottom(c, o) ||
|
||
negCrossClampNonposBottom(c, o) ||
|
||
negCrossClampLongNonpos(c, o);
|
||
}
|
||
|
||
ConflictRange closedFormCrossedComplete(const Ctx& c, const Obs& o) const {
|
||
if (crossedCompleteOvershoot(c, o))
|
||
return closedFormNegCrossTopUnified(c, o);
|
||
if (crossedCompleteEndLeOne(c, o))
|
||
return closedFormNegCrossNonclampUnified(c, o);
|
||
if (crossedCompleteBeginZero(c, o))
|
||
return closedFormZeroCrossResidual(c, o);
|
||
if (crossedCompleteClamp(c, o))
|
||
return closedFormNegCrossClampUnified(c, o);
|
||
return closedFormNegCrossCompleteHighResidual(c, o);
|
||
}
|
||
|
||
// ----- crossed truncated dispatch ---------------------------------------
|
||
|
||
bool zeroCrossTruncRevCrossed(const Ctx& c, const Obs& o) const {
|
||
return c.r->begin.offset == 0 && c.r->reverse && o.more &&
|
||
kLtO(leastReportedK(c, o), c.r->begin.key);
|
||
}
|
||
|
||
bool negCrossTruncRevUnder(const Ctx& c, const Obs& o) const {
|
||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||
kLtO(o.lastK(), c.r->begin.key) &&
|
||
optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
|
||
}
|
||
|
||
bool negCrossTruncRevCrossedLow(const Ctx& c, const Obs& o) const {
|
||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||
kLtO(o.lastK(), c.r->begin.key) &&
|
||
optLt(c.r->end_.key, c.r->begin.key) &&
|
||
c.r->end_.offset <= 1 &&
|
||
niLe(niAdd(niAdd(cardEB(c),
|
||
NatInf::of(static_cast<std::uint64_t>(c.n))),
|
||
NatInf::of(toNat(static_cast<__int128>(1) -
|
||
c.r->end_.offset))),
|
||
NatInf::of(bSlack(c) + 1));
|
||
}
|
||
|
||
bool revTruncLeOneLow(const Ctx& c, const Obs& o) const {
|
||
return negCrossTruncRevUnder(c, o) ||
|
||
negCrossTruncRevCrossedLow(c, o);
|
||
}
|
||
|
||
bool negCrossTruncRevBelowEnd(const Ctx& c, const Obs& o) const {
|
||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||
kLtO(o.lastK(), c.r->begin.key) && overshoot(c);
|
||
}
|
||
|
||
bool negCrossTruncRevResidualRoom(const Ctx& c, const Obs& o) const {
|
||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||
c.r->end_.offset >= 2 && anyAtOrAboveEnd(c) &&
|
||
niLe(niAdd(cardOK(c.r->end_.key, o.firstK()), NatInf::of(2)),
|
||
NatInf::of(rE(c)));
|
||
}
|
||
|
||
bool negCrossTruncRevResidualTopCrossedSmallGap(const Ctx& c,
|
||
const Obs& o) const {
|
||
if (!(o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK())))
|
||
return false;
|
||
NatInf lhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
|
||
NatInf::of(static_cast<std::uint64_t>(c.n)));
|
||
return niLt(lhs, NatInf::of(rE(c)));
|
||
}
|
||
|
||
bool negCrossTruncRevResidualTopCrossed(const Ctx& c, const Obs& o) const {
|
||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||
c.r->end_.offset >= 2 && kLeO(o.lastK(), c.r->end_.key) &&
|
||
c.t < rE(c);
|
||
}
|
||
|
||
bool revTruncGeTwoTop(const Ctx& c, const Obs& o) const {
|
||
return negCrossTruncRevBelowEnd(c, o) ||
|
||
negCrossTruncRevResidualRoom(c, o) ||
|
||
negCrossTruncRevResidualTopCrossedSmallGap(c, o) ||
|
||
negCrossTruncRevResidualTopCrossed(c, o);
|
||
}
|
||
|
||
bool negCrossTruncRevResidualTopLargeCrossed(const Ctx& c, const Obs& o) const {
|
||
return negCrossTruncRevResidualTopLarge(c, o) &&
|
||
kLtO(o.lastK(), c.r->begin.key) && !isTop(c.r->begin.key);
|
||
}
|
||
|
||
bool revTruncGeTwoLarge(const Ctx& c, const Obs& o) const {
|
||
return negCrossTruncRevResidualTopLargeCrossed(c, o) ||
|
||
negCrossTruncRevResidualTopLargeSentinel(c, o);
|
||
}
|
||
|
||
bool negCrossTruncTopTop(const Ctx& c, const Obs& o) const {
|
||
return o.more && !c.r->reverse && c.r->begin.offset < 0 &&
|
||
isTop(c.r->begin.key) && isTop(c.r->end_.key) &&
|
||
c.r->end_.offset <= 0;
|
||
}
|
||
|
||
bool negCrossTruncTopTopEq(const Ctx& c, const Obs& o) const {
|
||
if (!negCrossTruncTopTop(c, o)) return false;
|
||
std::uint64_t extra =
|
||
toNat(static_cast<__int128>(1) - c.r->end_.offset);
|
||
return static_cast<std::uint64_t>(c.n) + extra == bSlack(c) + 1;
|
||
}
|
||
|
||
std::uint64_t closedFormClampSlack(const Ctx& c, const Obs& o) const {
|
||
return natSub(bSlack(c) + 1, static_cast<std::uint64_t>(c.n)) +
|
||
(o.firstK().empty() ? 1 : 0);
|
||
}
|
||
|
||
bool closedFormClampGapSmall(const Ctx& c, const Obs& o) const {
|
||
return niLe(cardKO(o.lastK(), c.r->begin.key),
|
||
NatInf::of(closedFormClampSlack(c, o)));
|
||
}
|
||
|
||
bool closedFormClampGapExactSaturated(const Ctx& c, const Obs& o) const {
|
||
if (!(!o.firstK().empty() && isBottom(c.r->end_.key) &&
|
||
rE(c) == static_cast<std::uint64_t>(c.n)))
|
||
return false;
|
||
return niEq(cardKO(keyAfter(o.lastK()), c.r->begin.key),
|
||
NatInf::of(natSub(bSlack(c) + 1,
|
||
static_cast<std::uint64_t>(c.n))));
|
||
}
|
||
|
||
bool closedFormClampGapLow(const Ctx& c, const Obs& o) const {
|
||
return closedFormClampGapSmall(c, o) ||
|
||
closedFormClampGapExactSaturated(c, o);
|
||
}
|
||
|
||
OptKey closedFormClampUpper(const Ctx& c, const Obs& o) const {
|
||
if (closedFormClampGapLow(c, o)) return after(o.lastK());
|
||
return ownKey(c.r->begin.key);
|
||
}
|
||
|
||
bool closedFormForwardSaturated(const Ctx& c, const Obs& o) const {
|
||
if (!(rE(c) > 0 && oLtK(c.r->end_.key, o.firstK()))) return false;
|
||
NatInf rhs = niAdd(cardOK(c.r->end_.key, o.firstK()),
|
||
NatInf::of(static_cast<std::uint64_t>(c.n)));
|
||
return niLt(NatInf::of(rE(c)), rhs);
|
||
}
|
||
|
||
ConflictRange closedFormForwardTruncatedNonpos(const Ctx& c,
|
||
const Obs& o) const {
|
||
if (c.below <= bSlack(c)) {
|
||
if (c.r->end_.offset <= 0) {
|
||
if (optLt(c.r->end_.key, c.r->begin.key)) {
|
||
if (clampNonposBottom(c)) {
|
||
if (kLtO(o.lastK(), c.r->end_.key))
|
||
return closedFormNegCrossTruncNonposGapA2aBottom(c, o);
|
||
return mk(bottomKey(), clampLongUpper(c, o));
|
||
}
|
||
return mk(OptKey{o.firstK()}, c.r->begin.key);
|
||
}
|
||
if (negCrossTruncTopTopEq(c, o))
|
||
return mk(OptKey{o.firstK()}, topKey());
|
||
if (clampNonposBottom(c))
|
||
return mk(bottomKey(), c.r->end_.key);
|
||
return mk(OptKey{o.firstK()}, c.r->end_.key);
|
||
}
|
||
return mk(bottomKey(), closedFormClampUpper(c, o));
|
||
}
|
||
if (c.r->end_.offset <= 0)
|
||
return mk(asKey(o.firstK()),
|
||
optMax(c.r->begin.key, c.r->end_.key));
|
||
OptKeyView begin = closedFormForwardSaturated(c, o)
|
||
? c.r->end_.key
|
||
: asKey(o.firstK());
|
||
return mk(std::move(begin), optMax(c.r->begin.key, after(o.lastK())));
|
||
}
|
||
|
||
ConflictRange closedFormCrossed(const Ctx& c, const Obs& o) const {
|
||
if (!o.more) {
|
||
if (!c.r->reverse) return closedFormCrossedComplete(c, o);
|
||
RangeRead r2 = *c.r;
|
||
r2.reverse = false;
|
||
Obs o2 = o;
|
||
o2.reversed = true;
|
||
o2.more = false;
|
||
Ctx c2 = makeCtx(r2, o2);
|
||
return closedFormCrossedComplete(c2, o2);
|
||
}
|
||
if (c.r->reverse) {
|
||
if (zeroCrossTruncRevCrossed(c, o))
|
||
return closedFormZeroCrossTruncRevCrossed(c, o);
|
||
if (revTruncLeOneLow(c, o))
|
||
return mk(OptKey{o.lastK()}, c.r->end_.key);
|
||
if (c.r->end_.offset <= 1)
|
||
return mk(OptKey{o.lastK()}, c.r->begin.key);
|
||
if (revTruncGeTwoTop(c, o))
|
||
return mk(OptKey{o.lastK()}, topKey());
|
||
if (revTruncGeTwoLarge(c, o))
|
||
return closedFormNegCrossTruncRevResidualTopLargeUnified(c, o);
|
||
return closedFormNegCrossTruncRevResidualFiniteUnified(c, o);
|
||
}
|
||
return closedFormForwardTruncatedNonpos(c, o);
|
||
}
|
||
|
||
// ----- entry point ------------------------------------------------------
|
||
|
||
ConflictRange closedForm(const RangeRead& r, const Result& res) const {
|
||
if (res.keys.empty()) return emptyValue(r);
|
||
Obs o;
|
||
o.keys = res.keys;
|
||
o.reversed = false;
|
||
o.more = res.more;
|
||
o.ascending = !r.reverse;
|
||
o.n = res.keys.size();
|
||
Ctx c = makeCtx(r, o);
|
||
if (r.begin.offset >= 1 || clampedNonpos(c, o))
|
||
return closedFormPositive(c, o);
|
||
return closedFormCrossed(c, o);
|
||
}
|
||
};
|
||
|
||
} // namespace
|
||
|
||
ConflictRange closedForm(const RangeRead& r, const Result& res) {
|
||
return Impl{}.closedForm(r, res);
|
||
}
|
||
|
||
} // namespace cr::fast
|