// 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), then replays the same tree for readable inputs. // Branches and predicate operands that the `Readable` invariant or a dispatch // invariant makes unreachable/constant are omitted; `literal.cpp` keeps the // full Lean conditions for audit. Reversed results are handled through a // `reversed` view instead of copying the key list. #include "fast.h" #include #include 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 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; // `closedForm` handles an empty result before building the tree, so `keys` is // non-empty everywhere these are called. 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 }; NatInf cardBE(const Ctx& c) { return intervalCard(c.r->begin.key, c.r->end_.key); } // Each path consults this at most once, so it needs no cache. NatInf cardEB(const Ctx& c) { return intervalCard(c.r->end_.key, c.r->begin.key); } 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 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 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` // --------------------------------------------------------------------------- // 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; } const KeyView& leastReportedK(const Ctx& c, const Obs& o) { return c.r->reverse ? o.lastK() : o.firstK(); } // --------------------------------------------------------------------------- // Arithmetic helpers, on cached facts // --------------------------------------------------------------------------- template ConflictRange mk(B&& b, E&& e) { return ConflictRange{ownKey(std::forward(b)), ownKey(std::forward(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))); } // The caller reaches this only after `begin.offset >= 1` failed, so the offset // half of the condition is already known. bool clampedNonpos(const Ctx& c, const Obs& o) { return oLeK(c.r->begin.key, leastReportedK(c, o)); } // Called from the positive branch: if `begin.offset <= 0` then the entry // dispatch already established `clampedNonpos`, i.e. the selector key is at or // before the first reported key, so only the offset decides here. bool clampsToBottom(const Ctx& c) { return c.r->begin.offset <= 0; } std::uint64_t cELo(Int oB, Int oE) { return std::max(toNat(2 - oE), toNat(oB - oE + 1)); } std::uint64_t cEminP(Int oE) { return toNat(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 crossedOvershootGeneral(const Ctx& c) { return natSub(natSub(rE(c), static_cast(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 -------------------------------------------------- // The complete-crossed subtree is entered only with `!reverse` and `!more` // (`closedFormCrossed` normalises a reversed complete read first). Within it, // `crossedCompleteBeginZero` has already claimed every `begin.offset == 0` // case before these clamp nodes are tried, so `begin.offset < 0` holds here. bool negCrossClampEndOne(const Ctx& c, const Obs& o) const { // `crossedCompleteEndLeOne` already failed, and for `end.offset == 1` it // fails exactly when `bSlack >= below`. return c.r->end_.offset == 1; } bool negCrossHeadBottom(const Ctx& c, const Obs& o) const { return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) && o.firstK().empty(); } bool negCrossClampNonposBottom(const Ctx& c, const Obs& o) const { return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key); } bool negCrossClampLongNonpos(const Ctx& c, const Obs& o) const { return c.r->end_.offset <= 0 && 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)) 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(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 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(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 ------------------------------------------------- // `closedFormForwardTruncatedNonpos` calls this only inside // `below <= bSlack`, `end.offset <= 0`, `end < begin` and // `clampNonposBottom`; the remaining Lean conditions of // `negCrossTruncNonposGap`/`...A2aBottom` are invariants of the readable // forward-crossed regime. 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(c.n)), cardKO(keyAfter(o.lastK()), c.r->begin.key)); return 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))); } // Reached only in the reversed truncation branch (`more` set, `reverse` set, // `begin.offset < 0`, `end.offset >= 2`). bool negCrossTruncRevResidualFiniteOver(const Ctx& c, const Obs& o) const { return oLtK(c.r->end_.key, o.lastK()); } 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(c.n))); Int nPlusBegin = static_cast(c.n) + c.r->begin.offset; return niEq(lhs, rhs) && nPlusBegin == 1; } bool negCrossTruncRevResidualTopLarge(const Ctx& c, const Obs& o) const { if (!(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(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); // Lean's `keyAfterN m E < o.lastK()`, decided without building the `m` // zero bytes. if (keyAfterNLt(m, E, o.lastK())) return mk(asKey(E), topKey()); return mk(std::move(L), topKey()); } ConflictRange topRange(const Ctx& c) const { return mk(c.r->begin.key, topKey()); } ConflictRange closedFormOffsetOne(const Ctx& c, const Obs& o) const { if (c.t == rE(c)) return mk(c.r->begin.key, after(o.lastK())); return topRange(c); } // ----- 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.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 : asKey(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 closedFormOffsetOne(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 { // The only caller tests `crossedCompleteBeginZero`, i.e. `begin.offset == 0`. return 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 { // Readability makes the two cardinality bounds and `ePlusEnd > 0` // automatic once the first two bounds hold, so only the endpoint // ordering remains to decide. bool firstTwo = niLe(NatInf::of(b), underBegin) && niLe(NatInf::of(e), underEnd); Int ePlusEnd = static_cast(e) + r.end_.offset - 1; Int bPlusBegin = static_cast(b) + r.begin.offset - 1; inner = firstTwo && 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(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) ? bottomKeyView() : c.r->begin.key; } bool closedFormFinite(const Ctx& c) const { return closedFormDeficit(c) + static_cast(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) || 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)) 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(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(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(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 && overshoot(c); } bool negCrossOvershoot(const Ctx& c, const Obs& o) const { return c.r->begin.offset < 0 && overshoot(c); } bool crossedCompleteOvershoot(const Ctx& c, const Obs& o) const { return zeroCrossOvershootComplete(c, o) || negCrossOvershoot(c, o); } bool crossedCompleteEndLeOne(const Ctx& c) const { if (c.r->end_.offset == 1 && bSlack(c) < c.below) return true; return c.r->end_.offset <= 0 && !(c.below <= bSlack(c) && clampNonposBottom(c)); } // The three `zeroCross*` predicates above cover every `begin.offset == 0` // case of the complete-crossed subtree, so the mode alone decides here. bool crossedCompleteBeginZero(const Ctx& c, const Obs& o) const { return c.r->begin.offset == 0; } 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)) 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 --------------------------------------- // Every node below is reached only from the reversed truncation branch, where // `more` is set, `reverse` is set, `begin.offset < 0` and `end.offset >= 2` // (the `end.offset <= 1` case is handled by the dispatch). bool zeroCrossTruncRevCrossed(const Ctx& c, const Obs& o) const { return c.r->begin.offset == 0; } bool negCrossTruncRevUnder(const Ctx& c, const Obs& o) const { return optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1; } bool negCrossTruncRevCrossedLow(const Ctx& c, const Obs& o) const { return optLt(c.r->end_.key, c.r->begin.key) && c.r->end_.offset <= 1 && niLe(niAdd(niAdd(cardEB(c), NatInf::of(static_cast(c.n))), NatInf::of(toNat(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 overshoot(c); } bool negCrossTruncRevResidualRoom(const Ctx& c, const Obs& o) const { return 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 (!oLtK(c.r->end_.key, o.lastK())) return false; NatInf lhs = niAdd(cardOK(c.r->end_.key, o.lastK()), NatInf::of(static_cast(c.n))); return niLt(lhs, NatInf::of(rE(c))); } bool negCrossTruncRevResidualTopCrossed(const Ctx& c, const Obs& o) const { return !oLtK(c.r->end_.key, o.lastK()) && 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) && !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 isTop(c.r->begin.key); } bool negCrossTruncTopTopEq(const Ctx& c, const Obs& o) const { if (!negCrossTruncTopTop(c, o)) return false; std::uint64_t extra = toNat(1 - c.r->end_.offset); return static_cast(c.n) + extra == bSlack(c) + 1; } std::uint64_t closedFormClampSlack(const Ctx& c, const Obs& o) const { return natSub(bSlack(c) + 1, static_cast(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(c.n))) return false; return niEq(cardKO(keyAfter(o.lastK()), c.r->begin.key), NatInf::of(natSub(bSlack(c) + 1, static_cast(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(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)) { // Readability gives `lastK < resolve end <= end.key`. return closedFormNegCrossTruncNonposGapA2aBottom(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) { assertInputOffsetsFitInt32(r); return Impl{}.closedForm(r, res); } } // namespace cr::fast