Documentation

CslibFrontier.Foundations.Data.Nat.WeightedIndex

Selecting a column by its natural weight #

The columns of a row receive consecutive intervals of natural labels, of lengths given by their weights. Subtracting each weight from the remaining label finds the column containing it. A label outside every interval returns the row width. This also handles zero weights and empty rows.

Main definitions #

Main results #

def Cslib.scanWeightedRow (n : ℕ) :
(ℕ → ℕ) → ℕ → ℕ

The column containing label in the consecutive intervals of lengths weight 0 through weight (n - 1), or n when all intervals have been exhausted.

theorem Cslib.scanWeightedRow_spec (n : ℕ) (weight : ℕ → ℕ) (label : ℕ) :
have j := scanWeightedRow n weight label; j ≤ n ∧ ∑ i ∈ Finset.range j, weight i ≤ label ∧ (j < n → label < ∑ i ∈ Finset.range (j + 1), weight i)

The returned column is at most the width. Its prefix weight fits the label, and if this is a real column its interval reaches strictly beyond the label.

theorem Cslib.scanWeightedRow_eq_iff {n j : ℕ} (hj : j < n) (weight : ℕ → ℕ) (label : ℕ) :
scanWeightedRow n weight label = j ↔ ∑ i ∈ Finset.range j, weight i ≤ label ∧ label < ∑ i ∈ Finset.range j, weight i + weight j

A label selects column j < n exactly on the half-open interval starting at the prefix weight of the first j columns.