Insertion, erasure, and rank in sorted lists #
In a list l sorted by (· ≤ ·) over a linear order, the rank l.countP (· < a) of a gives
the exact split position at which List.orderedInsert (· ≤ ·) a l inserts a and at which
l.erase a removes a when a ∈ l, and characterizes the indices i whose entry satisfies
l[i] < a.
List.insertSortedUnique inserts into a strictly increasing list, skipping an equal entry.
Main results #
List.Pairwise.insertSortedUnique: insertion without repetition preserves strict ordering.List.Pairwise.orderedInsert_eq_take_append_drop: in a sorted list,orderedInsert (· ≤ ·) a lsplitslatl.countP (· < a)intol.take k ++ a :: l.drop k.List.Pairwise.erase_eq_take_append_drop: in a sorted list containinga,l.erase asplitslatk = l.countP (· < a)intol.take k ++ l.drop (k + 1).List.Pairwise.getElem_lt_iff_lt_countP: in a sorted list,l[i] < a ↔ i < l.countP (· < a).
Insert x before the first larger entry, or keep the list if the first entry at least
x equals x. On a strictly increasing list, this inserts without repetition.
The result contains the inserted element and every original entry.
This insertion adds at most one list entry.
Inserting without repetition into a strictly increasing list preserves strict order.
In a list sorted by (· ≤ ·), orderedInsert (· ≤ ·) a l inserts a immediately after the
prefix of length l.countP (· < a) consisting of elements strictly smaller than a.
In a list sorted by (· ≤ ·), erasing a present element a ∈ l removes the entry at index
k = l.countP (· < a) (the first occurrence of a), splitting l into
l.take k ++ l.drop (k + 1).
In a list sorted by (· ≤ ·), the i-th entry is strictly below a if and only if i lies
below the rank l.countP (· < a) of a.