Documentation

CslibFrontier.DataStructure.PrefixSums.WordRAM.Tree

Balanced aggregation trees for dynamic prefix sums #

An array of 2 ^ h natural numbers is represented by a complete binary aggregation tree. The logical operations are addition at one position and summation of a prefix. Nodes are laid out in preorder in a contiguous region of 2 * 2 ^ h - 1 words. These definitions do not assign unit cost to a Lean operation: the concrete Word-RAM client supplies the execution and cost proofs.

Main definitions #

Main results #

References #

A complete binary tree with 2 ^ h leaves.

  • leaf (value : ℕ) : Tree 0

    A single array entry.

  • node {h : ℕ} (left right : Tree h) : Tree (h + 1)

    Two equally sized consecutive subarrays.

Instances For
    @[instance_reducible]
    @[reducible, inline]

    Point increment on the natural-number array of the word-RAM interface: the shared PrefixSums.addAt. The name remains only for Dynamic.spec, which will use addAt directly once it moves to PrefixSums.spec.

    The represented array, in its original order.

    The aggregate stored at the root.

    The sum of the first k entries. Calls beyond the end are clamped.

    Add δ at zero-based position i. The machine interface requires i < 2 ^ h.

    @[simp]

    A complete binary tree of height h has 2 ^ h leaves.

    @[simp]

    The sum of the leaf array equals the root aggregate t.total.

    @[simp]

    The prefix sum of length zero is zero.

    @[simp]

    The prefix sum over all 2 ^ h leaves equals t.total.

    The tree query has the ordinary array-prefix meaning.

    @[simp]
    theorem Cslib.DataStructure.PrefixSums.WordRAM.Tree.total_add {h : ℕ} (t : Tree h) (i δ : ℕ) :
    (t.add i δ).total = t.total + δ

    Point increments raise the total by precisely the increment.

    theorem Cslib.DataStructure.PrefixSums.WordRAM.Tree.leaves_add {h : ℕ} (t : Tree h) (i δ : ℕ) (hi : i < 2 ^ h) :
    (t.add i δ).leaves = addAt t.leaves i δ

    The tree update is the ordinary point increment on the represented array.

    Number of words occupied by a complete aggregation tree.

    @[simp]

    A leaf tree of height zero occupies one word.

    The memory stores each subtree's aggregate at its root, followed by its left and right subtrees. No assertion is made about memory outside the occupied interval.

    theorem Cslib.DataStructure.PrefixSums.WordRAM.Tree.Rep.root {h b : ℕ} {mem : ℕ → ℕ} {t : Tree h} (ht : t.Rep b mem) :
    mem b = t.total

    The root word at base address b stores t.total.

    theorem Cslib.DataStructure.PrefixSums.WordRAM.Tree.Rep.congr {h b : ℕ} {mem mem' : ℕ → ℕ} {t : Tree h} (ht : t.Rep b mem) (he : ∀ (a : ℕ), b ≤ a → a < b + words h → mem' a = mem a) :
    t.Rep b mem'

    A frame rule for the tree representation: only its contiguous footprint matters.

    theorem Cslib.DataStructure.PrefixSums.WordRAM.Tree.Rep.update_of_outside {h a b : ℕ} {mem : ℕ → ℕ} {t : Tree h} (ht : t.Rep b mem) (ha : a < b ∨ b + words h ≤ a) (v : ℕ) :
    t.Rep b (Function.update mem a v)

    Writing outside the representation's region preserves it.

    The all-zero array has a concrete, nonempty representation in zero memory.

    @[simp]

    The root aggregate of the all-zero tree is zero.

    All-zero memory represents the all-zero tree at any base address b.