Tree maps without a bundled well-formedness invariant, suitable for use in nested
inductive types. The well-formedness invariant is called Raw.WF
. When in doubt, prefer TreeMap
over TreeMap.Raw
. Lemmas about the operations on Std.TreeMap.Raw
are available in the
module Std.Data.TreeMap.Raw.Lemmas
.
A tree map stores an assignment of keys to values. 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 entry
with key either a
or b
in a tree map. Looking up either a
or b
always yields the same entry,
if any is present.
To avoid expensive copies, users should make sure that the tree map is used linearly.
Internally, the tree maps are represented as size-bounded trees, a type of self-balancing binary search tree with efficient order statistic lookups.
- inner : DTreeMap.Raw α (fun (x : α) => β) cmp
Internal implementation detail of the tree map.
Instances For
Well-formedness predicate for tree maps. Users of TreeMap
will not need to interact with
this. Users of TreeMap.Raw
will need to provide proofs of WF
to lemmas and should use lemmas
like WF.empty
and WF.insert
(which are always named exactly like the operations they are about)
to show that map operations preserve well-formedness. The constructors of this type are internal
implementation details and should not be accessed by users.
Internal implementation detail of the tree map.
Instances For
Creates a new empty tree map. It is also possible and recommended to
use the empty collection notations ∅
and {}
to create an empty tree map. simp
replaces
empty
with ∅
.
Equations
- Std.TreeMap.Raw.empty = { inner := Std.DTreeMap.Raw.empty }
Instances For
Equations
- Std.TreeMap.Raw.instEmptyCollection = { emptyCollection := Std.TreeMap.Raw.empty }
Equations
- Std.TreeMap.Raw.instInhabited = { default := ∅ }
Equations
- Std.TreeMap.Raw.instInsertProd = { insert := fun (e : α × β) (s : Std.TreeMap.Raw α β cmp) => s.insert e.fst e.snd }
If there is no mapping for the given key, inserts the given mapping into the map. Otherwise, returns the map unaltered.
Equations
- t.insertIfNew a b = { inner := t.inner.insertIfNew a b }
Instances For
Checks whether a key is present in a map and unconditionally inserts a value for the key.
Equivalent to (but potentially faster than) calling contains
followed by insert
.
Equations
- t.containsThenInsert a b = ((t.inner.containsThenInsert a b).fst, { inner := (t.inner.containsThenInsert a b).snd })
Instances For
Checks whether a key is present in a map and inserts a value for the key if it was not found.
If the returned Bool
is true
, then the returned map is unaltered. If the Bool
is false
,
then the returned map has a new value inserted.
Equivalent to (but potentially faster than) calling contains
followed by insertIfNew
.
Equations
- t.containsThenInsertIfNew a b = ((t.inner.containsThenInsertIfNew a b).fst, { inner := (t.inner.containsThenInsertIfNew a b).snd })
Instances For
Checks whether a key is present in a map, returning the associated value, and inserts a value for the key if it was not found.
If the returned value is some v
, then the returned map is unaltered. If it is none
, then the
returned map has a new value inserted.
Equivalent to (but potentially faster than) calling get?
followed by insertIfNew
.
Uses the LawfulEqCmp
instance to cast the retrieved value to the correct type.
Equations
- t.getThenInsertIfNew? a b = ((Std.DTreeMap.Raw.Const.getThenInsertIfNew? t.inner a b).fst, { inner := (Std.DTreeMap.Raw.Const.getThenInsertIfNew? t.inner a b).snd })
Instances For
Returns true
if there is a mapping for the given key a
or a key that is equal to a
according
to the comparator cmp
. 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 maps, both use the given comparator cmp
.
Instances For
Equations
- Std.TreeMap.Raw.instMembership = { mem := fun (t : Std.TreeMap.Raw α β cmp) (a : α) => t.contains a = true }
Equations
Tries to retrieve the mapping for the given key, returning none
if no such mapping is present.
Uses the LawfulEqCmp
instance to cast the retrieved value to the correct type.
Equations
- t.get? a = Std.DTreeMap.Raw.Const.get? t.inner a
Instances For
Tries to retrieve the mapping for the given key, returning none
if no such mapping is present.
Uses the LawfulEqCmp
instance to cast the retrieved value to the correct type.
Instances For
Given a proof that a mapping for the given key is present, retrieves the mapping for the given key.
Uses the LawfulEqCmp
instance to cast the retrieved value to the correct type.
Equations
- t.get a h = Std.DTreeMap.Raw.Const.get t.inner a h
Instances For
Tries to retrieve the mapping for the given key, panicking if no such mapping is present.
Uses the LawfulEqCmp
instance to cast the retrieved value to the correct type.
Equations
- t.get! a = Std.DTreeMap.Raw.Const.get! t.inner a
Instances For
Tries to retrieve the mapping for the given key, panicking if no such mapping is present.
Uses the LawfulEqCmp
instance to cast the retrieved value to the correct type.
Instances For
Tries to retrieve the mapping for the given key, returning fallback
if no such mapping is present.
Uses the LawfulEqCmp
instance to cast the retrieved value to the correct type.
Equations
- t.getD a fallback = Std.DTreeMap.Raw.Const.getD t.inner a fallback
Instances For
Tries to retrieve the mapping for the given key, returning fallback
if no such mapping is present.
Uses the LawfulEqCmp
instance to cast the retrieved value to the correct type.
Instances For
Equations
- Std.TreeMap.Raw.instGetElem?Mem = GetElem?.mk (fun (m : Std.TreeMap.Raw α β cmp) (a : α) => m.get? a) fun [Inhabited β] (m : Std.TreeMap.Raw α β cmp) (a : α) => m.get! a
Checks if a mapping for the given key exists and returns the key if it does, 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 mapping that matches a
. Ensures that such a mapping exists by
requiring a proof of a ∈ m
. The result is guaranteed to be pointer equal to the key in the map.
Instances For
Checks if a mapping for the given key exists and returns the key if it does, otherwise panics. If no panic occurs the result is guaranteed to be pointer equal to the key in the map.
Instances For
Checks if a mapping for the given key exists and returns the key if it does, otherwise fallback
.
If a mapping exists the result is guaranteed to be pointer equal to the key in the map.
Instances For
Tries to retrieve the key-value pair with the smallest key in the tree map, returning none
if the
map is empty.
Equations
Instances For
We do not provide min
for the raw trees.
Tries to retrieve the key-value pair with the smallest key in the tree map, returning fallback
if
the tree map is empty.
Equations
- t.minD fallback = Std.DTreeMap.Raw.Const.minD t.inner fallback
Instances For
Tries to retrieve the key-value pair with the largest key in the tree map, returning none
if the
map is empty.
Equations
Instances For
We do not provide max
for the raw trees.
Tries to retrieve the key-value pair with the largest key in the tree map, returning fallback
if
the tree map is empty.
Equations
- t.maxD fallback = Std.DTreeMap.Raw.Const.maxD t.inner fallback
Instances For
We do not provide minKey
for the raw trees.
We do not provide maxKey
for the raw trees.
We do not provide entryAtIdx
for the raw trees.
Returns the key-value pair with the n
-th smallest key, or fallback
if n
is at least t.size
.
Equations
- t.entryAtIdxD n fallback = Std.DTreeMap.Raw.Const.entryAtIdxD t.inner n fallback
Instances For
Returns the n
-th smallest key, or none
if n
is at least t.size
.
Equations
- t.keyAtIndex? n = t.inner.keyAtIndex? n
Instances For
We do not provide keyAtIndex
for the raw trees.
Returns the n
-th smallest key, or panics if n
is at least t.size
.
Equations
- t.keyAtIndex! n = t.inner.keyAtIndex! n
Instances For
Returns the n
-th smallest key, or fallback
if n
is at least t.size
.
Equations
- t.keyAtIndexD n fallback = t.inner.keyAtIndexD n fallback
Instances For
Tries to retrieve the key-value pair with the smallest key that is greater than or equal to the
given key, returning none
if no such pair exists.
Equations
Instances For
Tries to retrieve the key-value pair with the smallest key that is greater than the given key,
returning none
if no such pair exists.
Equations
Instances For
Tries to retrieve the key-value pair with the largest key that is less than or equal to the
given key, returning none
if no such pair exists.
Equations
Instances For
Tries to retrieve the key-value pair with the smallest key that is less than the given key,
returning none
if no such pair exists.
Equations
Instances For
Tries to retrieve the key-value pair with the smallest key that is greater than or equal to the given key, panicking if no such pair exists.
Equations
Instances For
Tries to retrieve the key-value pair with the smallest key that is greater than or equal to the
given key, returning fallback
if no such pair exists.
Equations
- t.getEntryGED k fallback = Std.DTreeMap.Raw.Const.getEntryGED t.inner k fallback
Instances For
Tries to retrieve the key-value pair with the smallest key that is greater than the given key,
returning fallback
if no such pair exists.
Equations
- t.getEntryGTD k fallback = Std.DTreeMap.Raw.Const.getEntryGTD t.inner k fallback
Instances For
Tries to retrieve the key-value pair with the largest key that is less than or equal to the
given key, returning fallback
if no such pair exists.
Equations
- t.getEntryLED k fallback = Std.DTreeMap.Raw.Const.getEntryLED t.inner k fallback
Instances For
Tries to retrieve the key-value pair with the smallest key that is less than the given key,
returning fallback
if no such pair exists.
Equations
- t.getEntryLTD k fallback = Std.DTreeMap.Raw.Const.getEntryLTD t.inner k fallback
Instances For
Tries to retrieve the smallest key that is greater than or equal to the
given key, returning fallback
if no such key exists.
Instances For
Tries to retrieve the smallest key that is greater than the given key,
returning fallback
if no such key exists.
Instances For
Tries to retrieve the largest key that is less than or equal to the
given key, returning fallback
if no such key exists.
Instances For
Tries to retrieve the smallest key that is less than the given key,
returning fallback
if no such key exists.
Instances For
Removes all mappings of the map for which the given function returns false
.
Equations
- Std.TreeMap.Raw.filter f t = { inner := Std.DTreeMap.Raw.filter f t.inner }
Instances For
Folds the given monadic function over the mappings in the map in ascending order.
Equations
- Std.TreeMap.Raw.foldlM f init t = Std.DTreeMap.Raw.foldlM f init t.inner
Instances For
Folds the given monadic function over the mappings in the map in ascending order.
Equations
- Std.TreeMap.Raw.foldM f init t = Std.TreeMap.Raw.foldlM f init t
Instances For
Folds the given function over the mappings in the map in ascending order.
Equations
- Std.TreeMap.Raw.foldl f init t = Std.DTreeMap.Raw.foldl f init t.inner
Instances For
Folds the given function over the mappings in the map in ascending order.
Equations
- Std.TreeMap.Raw.fold f init t = Std.TreeMap.Raw.foldl f init t
Instances For
Folds the given monadic function over the mappings in the map in descending order.
Equations
- Std.TreeMap.Raw.foldrM f init t = Std.DTreeMap.Raw.foldrM f init t.inner
Instances For
Folds the given function over the mappings in the map in descending order.
Equations
- Std.TreeMap.Raw.foldr f init t = Std.DTreeMap.Raw.foldr f init t.inner
Instances For
Folds the given function over the mappings in the map in descending order.
Equations
- Std.TreeMap.Raw.revFold f init t = Std.TreeMap.Raw.foldr (fun (k : α) (v : β) (acc : δ) => f acc k v) init t
Instances For
Partitions a tree map into two tree maps based on a predicate.
Equations
- Std.TreeMap.Raw.partition f t = ({ inner := (Std.DTreeMap.Raw.partition f t.inner).fst }, { inner := (Std.DTreeMap.Raw.partition f t.inner).snd })
Instances For
Support for the for
loop construct in do
blocks. Iteration happens in ascending order.
Equations
- Std.TreeMap.Raw.forIn f init t = Std.DTreeMap.Raw.forIn (fun (a : α) (b : β) (c : δ) => f a b c) init t.inner
Instances For
Equations
- Std.TreeMap.Raw.instForMProd = { forM := fun [Monad m] (t : Std.TreeMap.Raw α β cmp) (f : α × β → m PUnit) => Std.TreeMap.Raw.forM (fun (a : α) (b : β) => f (a, b)) t }
Returns an array of all values present in the tree map in ascending order.
Equations
- t.valuesArray = t.inner.valuesArray
Instances For
Transforms a list of mappings into a tree map.
Equations
- Std.TreeMap.Raw.ofList l cmp = { inner := Std.DTreeMap.Raw.Const.ofList l cmp }
Instances For
Transforms a list of mappings into a tree map.
Equations
- Std.TreeMap.Raw.fromList l cmp = Std.TreeMap.Raw.ofList l cmp
Instances For
Transforms a list of keys into a tree map.
Equations
- Std.TreeMap.Raw.unitOfList l cmp = { inner := Std.DTreeMap.Raw.Const.unitOfList l cmp }
Instances For
Transforms a list of mappings into a tree map.
Equations
- Std.TreeMap.Raw.ofArray a cmp = { inner := Std.DTreeMap.Raw.Const.ofArray a cmp }
Instances For
Transforms a list of mappings into a tree map.
Equations
- Std.TreeMap.Raw.fromArray a cmp = Std.TreeMap.Raw.ofArray a cmp
Instances For
Transforms an array of keys into a tree map.
Equations
- Std.TreeMap.Raw.unitOfArray a cmp = { inner := Std.DTreeMap.Raw.Const.unitOfArray a cmp }
Instances For
Modifies in place the value associated with a given key.
This function ensures that the value is used linearly.
Equations
- t.modify a f = { inner := Std.DTreeMap.Raw.Const.modify t.inner a f }
Instances For
Modifies in place the value associated with a given key,
allowing creating new values and deleting values via an Option
valued replacement function.
This function ensures that the value is used linearly.
Equations
- t.alter a f = { inner := Std.DTreeMap.Raw.Const.alter t.inner a f }
Instances For
Returns a map that contains all mappings of t₁
and t₂
. In case that both maps contain the
same key k
with respect to cmp
, the provided function is used to determine the new value from
the respective values in t₁
and t₂
.
This function ensures that t₁
is used linearly. It also uses the individual values in t₁
linearly if the merge function uses the second argument (i.e. the first of type β a
) 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₁
if t₁
does not contain a
conflicting mapping yet. If t₁
does contain a conflicting mapping, use the given merge function to
merge the mapping in t₂
into the mapping in t₁
. Then return 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
- Std.TreeMap.Raw.mergeWith mergeFn t₁ t₂ = { inner := Std.DTreeMap.Raw.Const.mergeWith mergeFn t₁.inner t₂.inner }
Instances For
Returns a map that contains all mappings of t₁
and t₂
. In case that both maps contain the
same key k
with respect to cmp
, the provided function is used to determine the new value from
the respective values in t₁
and t₂
.
This function ensures that t₁
is used linearly. It also uses the individual values in t₁
linearly if the merge function uses the second argument (i.e. the first of type β a
) 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₁
if t₁
does not contain a
conflicting mapping yet. If t₁
does contain a conflicting mapping, use the given merge function to
merge the mapping in t₂
into the mapping in t₁
. Then return 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
- Std.TreeMap.Raw.mergeBy mergeFn t₁ t₂ = Std.TreeMap.Raw.mergeWith mergeFn t₁ t₂
Instances For
Inserts multiple mappings into the tree map by iterating over the given collection and calling
insert
. If the same key appears multiple times, the last occurrence takes precedence.
Note: this precedence behavior is true for TreeMap
, DTreeMap
, TreeMap.Raw
and DTreeMap.Raw
.
The insertMany
function on TreeSet
and TreeSet.Raw
behaves differently: it will prefer the first
appearance.
Equations
- t.insertMany l = { inner := Std.DTreeMap.Raw.Const.insertMany t.inner l }
Instances For
Inserts multiple elements into the tree map by iterating over the given collection and calling
insertIfNew
. If the same key appears multiple times, the first occurrence takes precedence.
Equations
- t.insertManyIfNewUnit l = { inner := Std.DTreeMap.Raw.Const.insertManyIfNewUnit t.inner l }
Instances For
Equations
- Std.TreeMap.Raw.instRepr = { reprPrec := fun (m : Std.TreeMap.Raw α β cmp) (prec : Nat) => Repr.addAppParen (Std.Format.text "TreeMap.Raw.ofList " ++ repr m.toList) prec }