Documentation

CslibFrontier.Foundations.Data.List.SortedCount

Ranks and blocks in sorted lists #

In a list sorted by a key, the entries with a given key form one contiguous block, which starts right after the entries with smaller keys. These facts underlie counting-sort and rank arguments, such as the LF-mapping of the Burrows–Wheeler transform.

Main results #

theorem List.Pairwise.filter_eq_takeWhile {α : Type u_1} {p : α → Bool} {l : List α} (h : Pairwise (fun (a b : α) => p b = true → p a = true) l) :

Filtering keeps a prefix if every entry satisfying p forces earlier entries to satisfy it.

theorem List.getElem?_filter_countP_take {α : Type u_1} (p : α → Bool) {l : List α} {r : ℕ} (hr : r < l.length) (hp : p l[r] = true) :
(filter p l)[countP p (take r l)]? = some l[r]

Among the entries satisfying p, entry r is preceded by exactly the matching entries of l.take r.

theorem List.Pairwise.filter_key_eq {α : Type u_1} {β : Type u_2} [LinearOrder β] (f : α → β) (g : β) {l : List α} :
Pairwise (fun (x y : α) => f x ≤ f y) l → List.filter (fun (x : α) => decide (f x = g)) l = List.take (countP (fun (x : α) => decide (f x = g)) l) (List.drop (countP (fun (x : α) => decide (f x < g)) l) l)

In a list sorted by a key f, the entries with key g form one block, which starts after the entries with smaller key.

theorem List.Pairwise.count_take_countP_lt_add {α : Type u_1} [LinearOrder α] (c : α) (k : ℕ) {l : List α} :
Pairwise (fun (x1 x2 : α) => x1 ≤ x2) l → count c (List.take (countP (fun (x : α) => decide (x < c)) l + k) l) = min k (count c l)

In a sorted list, the prefix that ends k places into the block of c contains min k (l.count c) copies of c.