Harden and fully cover the C++ translation
Add an exhaustive readable-instance sweep and a coverage gate requiring 100% line coverage of the summarised implementation. Delete branches that are unreachable under `Readable`, centralise and assert the 32-bit offset contract at the public entry points, and stop materialising `n` zero bytes when deciding `keyAfterN n k < upper`. Also add the extreme-offset regression test, harden the corpus parsers, remove unused model helpers, document the literal-total / fast-readable split, and refresh the validation and benchmark numbers.
This commit is contained in:
1 parent
e32e42d933
commit
3bb5d062ff
15 files changed
+620
-196
No files matched your search
+34
-106
@@ -5,11 +5,13 @@
|
||||
// `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.
|
||||
// decision tree needs, then replays the same tree for readable inputs. Branches
|
||||
// that the `Readable` invariant makes unreachable are omitted. Reversed results
|
||||
// are handled through a `reversed` view instead of copying the key list.
|
||||
#include "fast.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
|
||||
namespace cr::fast {
|
||||
@@ -32,8 +34,14 @@ struct Obs {
|
||||
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(); }
|
||||
const KeyView& firstK() const {
|
||||
assert(!keys.empty());
|
||||
return reversed ? keys.back() : keys.front();
|
||||
}
|
||||
const KeyView& lastK() const {
|
||||
assert(!keys.empty());
|
||||
return reversed ? keys.front() : keys.back();
|
||||
}
|
||||
};
|
||||
|
||||
// Facts that only depend on the read and on the *multiset* of reported keys.
|
||||
@@ -206,12 +214,11 @@ bool clampsToBottom(const Ctx& c, const Obs& o) {
|
||||
}
|
||||
|
||||
std::uint64_t cELo(Int oB, Int oE) {
|
||||
return std::max(toNat(static_cast<__int128>(2) - oE),
|
||||
toNat(static_cast<__int128>(oB) - oE + 1));
|
||||
return std::max(toNat(2 - oE), toNat(oB - oE + 1));
|
||||
}
|
||||
|
||||
std::uint64_t cEminP(Int oE) {
|
||||
return toNat(static_cast<__int128>(2) - oE);
|
||||
return toNat(2 - oE);
|
||||
}
|
||||
|
||||
std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) {
|
||||
@@ -229,10 +236,6 @@ 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));
|
||||
}
|
||||
@@ -432,7 +435,7 @@ struct Impl {
|
||||
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;
|
||||
Int nPlusBegin = static_cast<Int>(c.n) + c.r->begin.offset;
|
||||
return niEq(lhs, rhs) && nPlusBegin == 1;
|
||||
}
|
||||
|
||||
@@ -471,88 +474,21 @@ struct Impl {
|
||||
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()))
|
||||
// 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 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);
|
||||
if (c.t == rE(c))
|
||||
return mk(c.r->begin.key, after(o.lastK()));
|
||||
return topRange(c);
|
||||
}
|
||||
|
||||
// ----- zero crossed ------------------------------------------------------
|
||||
@@ -562,8 +498,6 @@ struct Impl {
|
||||
}
|
||||
|
||||
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());
|
||||
@@ -592,7 +526,7 @@ struct Impl {
|
||||
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());
|
||||
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));
|
||||
@@ -601,7 +535,7 @@ struct Impl {
|
||||
ConflictRange closedFormZeroCrossUnderEnd(const Ctx& c, const Obs& o) const {
|
||||
RangeRead a = zeroBeginAnchor(c, o);
|
||||
Ctx ca = makeCtx(a, o);
|
||||
return closedFormOffsetOneTotal(ca, o);
|
||||
return closedFormOffsetOne(ca, o);
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossComplete(const Ctx& c, const Obs& o) const {
|
||||
@@ -671,8 +605,8 @@ struct Impl {
|
||||
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;
|
||||
Int ePlusEnd = static_cast<Int>(e) + r.end_.offset - 1;
|
||||
Int bPlusBegin = static_cast<Int>(b) + r.begin.offset - 1;
|
||||
inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd;
|
||||
}
|
||||
return !inner;
|
||||
@@ -694,8 +628,7 @@ struct Impl {
|
||||
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))))
|
||||
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));
|
||||
}
|
||||
@@ -826,10 +759,7 @@ struct Impl {
|
||||
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;
|
||||
bool crossedCompleteEndLeOne(const Ctx& c) const {
|
||||
if (c.r->end_.offset == 1 && bSlack(c) < c.below)
|
||||
return true;
|
||||
return c.r->end_.offset <= 0 &&
|
||||
@@ -842,7 +772,7 @@ struct Impl {
|
||||
}
|
||||
|
||||
bool zeroCrossLongFinite(const Ctx& c, const Obs& o) const {
|
||||
return c.r->begin.offset == 0 && !c.r->reverse &&
|
||||
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
|
||||
c.r->end_.offset >= 2 && !isTop(c.r->begin.key) &&
|
||||
optLt(c.r->end_.key, c.r->begin.key) &&
|
||||
kLtO(leastReportedK(c, o), c.r->begin.key);
|
||||
@@ -862,7 +792,7 @@ struct Impl {
|
||||
ConflictRange closedFormCrossedComplete(const Ctx& c, const Obs& o) const {
|
||||
if (crossedCompleteOvershoot(c, o))
|
||||
return closedFormNegCrossTopUnified(c, o);
|
||||
if (crossedCompleteEndLeOne(c, o))
|
||||
if (crossedCompleteEndLeOne(c))
|
||||
return closedFormNegCrossNonclampUnified(c, o);
|
||||
if (crossedCompleteBeginZero(c, o))
|
||||
return closedFormZeroCrossResidual(c, o);
|
||||
@@ -891,8 +821,7 @@ struct Impl {
|
||||
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(toNat(1 - c.r->end_.offset))),
|
||||
NatInf::of(bSlack(c) + 1));
|
||||
}
|
||||
|
||||
@@ -954,8 +883,7 @@ struct Impl {
|
||||
|
||||
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);
|
||||
std::uint64_t extra = toNat(1 - c.r->end_.offset);
|
||||
return static_cast<std::uint64_t>(c.n) + extra == bSlack(c) + 1;
|
||||
}
|
||||
|
||||
@@ -1001,9 +929,8 @@ struct Impl {
|
||||
if (c.r->end_.offset <= 0) {
|
||||
if (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));
|
||||
// Readability gives `lastK < resolve end <= end.key`.
|
||||
return closedFormNegCrossTruncNonposGapA2aBottom(c, o);
|
||||
}
|
||||
return mk(OptKey{o.firstK()}, c.r->begin.key);
|
||||
}
|
||||
@@ -1071,6 +998,7 @@ struct Impl {
|
||||
} // namespace
|
||||
|
||||
ConflictRange closedForm(const RangeRead& r, const Result& res) {
|
||||
assertInputOffsetsFitInt32(r);
|
||||
return Impl{}.closedForm(r, res);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user