Documentation

CslibFrontier.DataStructure.PrefixSums.Spec

Dynamic prefix sums #

The abstract state is a list over an additive commutative monoid. add pos δ increments one entry; out-of-range positions leave the list unchanged. sum len returns the sum of the first len entries; a length beyond the end sums the whole list. Every operation is admissible, and the length of the list never changes. The module chooses no representation and charges no cost; an implementation with machine bounds, such as a word-fit condition, restricts spec.valid.

Main definitions #

Main results #

References #

def Cslib.DataStructure.PrefixSums.addAt {α : Type u_1} [AddZeroClass α] (s : List α) (pos : ℕ) (δ : α) :
List α

Point increment on a list at 0-based index pos (no-op when s.length ≤ pos).

@[simp]
theorem Cslib.DataStructure.PrefixSums.length_addAt {α : Type u_1} [AddZeroClass α] (s : List α) (pos : ℕ) (δ : α) :
(addAt s pos δ).length = s.length
theorem Cslib.DataStructure.PrefixSums.addAt_of_length_le {α : Type u_1} [AddZeroClass α] {s : List α} {pos : ℕ} (δ : α) (h : s.length ≤ pos) :
addAt s pos δ = s
theorem Cslib.DataStructure.PrefixSums.getD_addAt {α : Type u_1} [AddZeroClass α] (s : List α) {pos : ℕ} (δ : α) (hpos : pos < s.length) (i : ℕ) :
(addAt s pos δ).getD i 0 = if i = pos then s.getD i 0 + δ else s.getD i 0
theorem Cslib.DataStructure.PrefixSums.addAt_append {α : Type u_1} [AddZeroClass α] (s t : List α) (pos : ℕ) (δ : α) :
addAt (s ++ t) pos δ = if pos < s.length then addAt s pos δ ++ t else s ++ addAt t (pos - s.length) δ

A point increment of a concatenation updates the part containing the position.

Dynamic prefix-sum operations over α, independent of storage representation.

  • add {α : Type} (pos : ℕ) (delta : α) : Op α

    Increment the entry at 0-based index pos by delta (no-op when out of bounds).

  • sum {α : Type} (len : ℕ) : Op α

    Return the sum (s.take len).sum of the first len entries.

Instances For
    @[instance_reducible]
    def Cslib.DataStructure.PrefixSums.instDecidableEqOp.decEq {α✝ : Type} [DecidableEq α✝] (x✝ x✝¹ : Op α✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    instance Cslib.DataStructure.PrefixSums.instReprOp {α✝ : Type} [Repr α✝] :
    Repr (Op α✝)
    @[reducible, inline]

    Return types for Op α.

    def Cslib.DataStructure.PrefixSums.step {α : Type} [AddCommMonoid α] (s : List α) (o : Op α) :
    List α × Op.Result α o

    Pure transition function of dynamic prefix sums on List α.

    @[reducible, inline]

    Mathematical specification of dynamic prefix sums on List α. All operations are admissible: out-of-bounds add is a no-op and sum len sums s.take len.

    @[simp]

    Every operation preserves the length of the list.