package cascade
Module Cascade.Order_maintenanceSource
Order maintenance: a total order over opaque elements that answers precedence queries in O(1) while supporting insertion and deletion.
This is the order-maintenance problem (Dietz & Sleator 1987; simplified by Bender, Cole, Demaine, Farach-Colton & Zito, ESA 2002): keep a sequence under insert_after / remove so that compare tells, in constant time, which of two live elements comes first.
The incremental rule merger uses it for cascade precedence: rules keep a stable position handle even as merges delete some and factoring inserts new shared rules, so "does rule A come before rule B?" stays O(1) instead of re-deriving list indices after every edit.
Elements are identified by a node handle returned at insertion. A handle stays valid until it is removed; using a removed handle is an error.
A mutable ordered collection of 'a elements.
A stable handle to one element, returned by add_last / insert_after.
add_last t x appends x after every current element and returns its handle.
insert_after t n x inserts x immediately after n and returns its handle. n must be live.
insert_before t n x inserts x immediately before n and returns its handle. n must be live.
remove t n deletes n from the order. n must be live; it must not be used afterwards.
compare a b is negative when a precedes b, positive when b precedes a, and 0 only when a and b are the same handle. O(1). Both handles must be live.
next n is the handle immediately after n, or None at the end.
prev n is the handle immediately before n, or None at the start.