Tree sets #
This file develops the type Std.TreeSet
of tree sets.
Lemmas about the operations on Std.Data.TreeSet
will be available in the
module Std.Data.TreeSet.Lemmas
.
See the module Std.Data.TreeSet.Raw.Basic
for a variant of this type which is safe to use in
nested inductive types.
Tree sets.
A tree set stores elements of a certain type in a certain order. It depends on a comparator function that defines an ordering on the keys and provides efficient order-dependent queries, such as retrieval of the minimum or maximum.
To ensure that the operations behave as expected, the comparator function cmp
should satisfy
certain laws that ensure a consistent ordering:
- If
a
is less than (or equal) tob
, thenb
is greater than (or equal) toa
and vice versa (see theOrientedCmp
typeclass). - If
a
is less than or equal tob
andb
is, in turn, less than or equal toc
, thena
is less than or equal toc
(see theTransCmp
typeclass).
Keys for which cmp a b = Ordering.eq
are considered the same, i.e., there can be only one of them
be contained in a single tree set at the same time.
To avoid expensive copies, users should make sure that the tree set is used linearly.
Internally, the tree sets are represented as size-bounded trees, a type of self-balancing binary search tree with efficient order statistic lookups.
These tree sets contain a bundled well-formedness invariant, which means that they cannot
be used in nested inductive types. For these use cases, Std.TreeSet.Raw
and
Std.TreeSet.Raw.WF
unbundle the invariant from the tree set. When in doubt, prefer
TreeSet
over TreeSet.Raw
.
Internal implementation detail of the tree map.
Instances For
Creates a new empty tree set. It is also possible and recommended to
use the empty collection notations ∅
and {}
to create an empty tree set. simp
replaces
empty
with ∅
.
Equations
- Std.TreeSet.empty = { inner := Std.TreeMap.empty }
Instances For
Equations
- Std.TreeSet.instEmptyCollection = { emptyCollection := Std.TreeSet.empty }
Equations
- Std.TreeSet.instInhabited = { default := ∅ }
Inserts the given element into the set. If the tree set already contains an element that is
equal (with regard to cmp
) to the given element, then the tree set is returned unchanged.
Note: this non-replacement behavior is true for TreeSet
and TreeSet.Raw
.
The insert
function on TreeMap
, DTreeMap
, TreeMap.Raw
and DTreeMap.Raw
behaves
differently: it will overwrite an existing mapping.
Instances For
Equations
- Std.TreeSet.instSingleton = { singleton := fun (e : α) => ∅.insert e }
Equations
- Std.TreeSet.instInsert = { insert := fun (e : α) (s : Std.TreeSet α cmp) => s.insert e }
Checks whether an element is present in a set and inserts the element if it was not found.
If the tree set already contains an element that is equal (with regard to cmp
to the given
element, then the tree set is returned unchanged.
Equivalent to (but potentially faster than) calling contains
followed by insert
.
Equations
- t.containsThenInsert a = ((t.inner.containsThenInsertIfNew a ()).fst, { inner := (t.inner.containsThenInsertIfNew a ()).snd })
Instances For
Returns true
if a
, or an element equal to a
according to the comparator cmp
, is contained
in the set. There is also a Prop
-valued version of this: a ∈ t
is equivalent to
t.contains a = true
.
Observe that this is different behavior than for lists: for lists, ∈
uses =
and contains
uses
==
for equality checks, while for tree sets, both use the given comparator cmp
.
Instances For
Equations
- Std.TreeSet.instMembership = { mem := fun (m : Std.TreeSet α cmp) (a : α) => m.contains a = true }
Equations
Checks if given key is contained and returns the key if it is, otherwise none
.
The result in the some
case is guaranteed to be pointer equal to the key in the map.
Instances For
Retrieves the key from the set that matches a
. Ensures that such a key exists by requiring a proof
of a ∈ m
. The result is guaranteed to be pointer equal to the key in the set.
Instances For
Checks if given key is contained and returns the key if it is, otherwise panics. If no panic occurs the result is guaranteed to be pointer equal to the key in the set.
Instances For
Checks if given key is contained and returns the key if it is, otherwise fallback
.
If they key is contained the result is guaranteed to be pointer equal to the key in the set.
Instances For
Tries to retrieve the smallest element that is greater than or equal to the
given element, returning none
if no such element exists.
Instances For
getGE
, getGT
, getLE
, getLT
can be found in Std.Data.TreeSet.AdditionalOperations
.
Tries to retrieve the smallest element that is greater than or equal to the given element, panicking if no such element exists.
Instances For
Tries to retrieve the smallest element that is greater than or equal to the
given element, returning fallback
if no such element exists.
Instances For
Tries to retrieve the smallest element that is greater than the given element,
returning fallback
if no such element exists.
Instances For
Tries to retrieve the largest element that is less than or equal to the
given element, returning fallback
if no such element exists.
Instances For
Tries to retrieve the smallest element that is less than the given element,
returning fallback
if no such element exists.
Instances For
Removes all elements from the tree set for which the given function returns false
.
Equations
- Std.TreeSet.filter f m = { inner := Std.TreeMap.filter (fun (a : α) (x : Unit) => f a) m.inner }
Instances For
Monadically computes a value by folding the given function over the elements in the tree set in ascending order.
Equations
- Std.TreeSet.foldlM f init t = Std.TreeMap.foldlM (fun (c : δ) (a : α) (x : Unit) => f c a) init t.inner
Instances For
Monadically computes a value by folding the given function over the elements in the tree set in ascending order.
Equations
- Std.TreeSet.foldM f init t = Std.TreeSet.foldlM f init t
Instances For
Folds the given function over the elements of the tree set in ascending order.
Equations
- Std.TreeSet.foldl f init t = Std.TreeMap.foldl (fun (c : δ) (a : α) (x : Unit) => f c a) init t.inner
Instances For
Folds the given function over the elements of the tree set in ascending order.
Equations
- Std.TreeSet.fold f init t = Std.TreeSet.foldl f init t
Instances For
Monadically computes a value by folding the given function over the elements in the tree set in descending order.
Equations
- Std.TreeSet.foldrM f init t = Std.TreeMap.foldrM (fun (a : α) (x : Unit) (acc : δ) => f a acc) init t.inner
Instances For
Folds the given function over the elements of the tree set in descending order.
Equations
- Std.TreeSet.foldr f init t = Std.TreeMap.foldr (fun (a : α) (x : Unit) (acc : δ) => f a acc) init t.inner
Instances For
Folds the given function over the elements of the tree set in descending order.
Equations
- Std.TreeSet.revFold f init t = Std.TreeSet.foldr (fun (a : α) (acc : δ) => f acc a) init t
Instances For
Partitions a tree set into two tree sets based on a predicate.
Equations
- Std.TreeSet.partition f t = ({ inner := (Std.TreeMap.partition (fun (a : α) (x : Unit) => f a) t.inner).fst }, { inner := (Std.TreeMap.partition (fun (a : α) (x : Unit) => f a) t.inner).snd })
Instances For
Carries out a monadic action on each element in the tree set in ascending order.
Equations
- Std.TreeSet.forM f t = Std.TreeMap.forM (fun (a : α) (x : Unit) => f a) t.inner
Instances For
Support for the for
loop construct in do
blocks. The iteration happens in ascending
order.
Equations
- Std.TreeSet.forIn f init t = Std.TreeMap.forIn (fun (a : α) (x : Unit) (c : δ) => f a c) init t.inner
Instances For
Equations
- Std.TreeSet.instForM = { forM := fun [Monad m] (t : Std.TreeSet α cmp) (f : α → m PUnit) => Std.TreeSet.forM f t }
Transforms the tree set into a list of elements in ascending order.
Equations
Instances For
Transforms a list into a tree set.
Equations
- Std.TreeSet.ofList l cmp = { inner := Std.TreeMap.unitOfList l cmp }
Instances For
Transforms a list into a tree set.
Equations
- Std.TreeSet.fromList l cmp = Std.TreeSet.ofList l cmp
Instances For
Transforms an array into a tree set.
Equations
- Std.TreeSet.ofArray a cmp = { inner := Std.TreeMap.unitOfArray a cmp }
Instances For
Transforms an array into a tree set.
Equations
- Std.TreeSet.fromArray a cmp = Std.TreeSet.ofArray a cmp
Instances For
Returns a set that contains all mappings of t₁
and `t₂.
This function ensures that t₁
is used linearly.
Hence, as long as t₁
is unshared, the performance characteristics follow the following imperative
description: Iterate over all mappings in t₂
, inserting them into t₁
.
Hence, the runtime of this method scales logarithmically in the size of t₁
and linearly in the
size of t₂
as long as t₁
is unshared.
Equations
Instances For
Inserts multiple elements into the tree set by iterating over the given collection and calling
insert
. If the same element (with respect to cmp
) appears multiple times, the first occurrence
takes precedence.
Note: this precedence behavior is true for TreeSet
and TreeSet.Raw
. The insertMany
function on
TreeMap
, DTreeMap
, TreeMap.Raw
and DTreeMap.Raw
behaves differently: it will prefer the last
appearance.
Equations
- t.insertMany l = { inner := t.inner.insertManyIfNewUnit l }
Instances For
Equations
- Std.TreeSet.instRepr = { reprPrec := fun (m : Std.TreeSet α cmp) (prec : Nat) => Repr.addAppParen (Std.Format.text "TreeSet.ofList " ++ repr m.toList) prec }