Tree maps #
This file develops the type Std.TreeMap of tree maps.
Lemmas about the operations on Std.TreeMap will be available in the
module Std.Data.TreeMap.Lemmas.
See the module Std.Data.TreeMap.Raw.Basic for a variant of this type which is safe to use in
nested inductive types and Std.Data.ExtTreeMap.Basic for a variant with extensionality.
Tree maps.
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
ais less than (or equal) tob, thenbis greater than (or equal) toaand vice versa (see theOrientedCmptypeclass). - If
ais less than or equal tobandbis, in turn, less than or equal toc, thenais less than or equal toc(see theTransCmptypeclass).
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.
For use in proofs, the type Std.ExtTreeMap of extensional tree maps should be preferred. This
type comes with several extensionality lemmas and provides the same functions but requires a
TransCmp instance to work with.
These tree maps contain a bundled well-formedness invariant, which means that they cannot
be used in nested inductive types. For these use cases, Std.TreeMap.Raw and
Std.TreeMap.Raw.WF unbundle the invariant from the tree map. When in doubt, prefer
TreeMap over TreeMap.Raw.
- inner : DTreeMap α (fun (x : α) => β) cmp
Internal implementation detail of the tree map.
Instances For
Equations
Two tree maps are equivalent in the sense of Equiv iff all the keys and values are equal.
Equations
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
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
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
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.
Equations
Instances For
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
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.
Equations
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
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
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
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
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
Instances For
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.
Equations
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.
Equations
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.
Equations
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.
Equations
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
Given a proof that the tree map is not empty, retrieves the key-value pair with the smallest key.
Equations
Instances For
Tries to retrieve the key-value pair with the smallest key in the tree map, panicking if the map is empty.
Equations
Instances For
Tries to retrieve the key-value pair with the smallest key in the tree map, returning fallback if
the tree map is empty.
Equations
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
Given a proof that the tree map is not empty, retrieves the key-value pair with the largest key.
Equations
Instances For
Tries to retrieve the key-value pair with the largest key in the tree map, panicking if the map is empty.
Equations
Instances For
Tries to retrieve the key-value pair with the largest key in the tree map, returning fallback if
the tree map is empty.
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 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
getEntryGE, getEntryGT, getEntryLE, getEntryLT can be found in
Std.Data.TreeMap.AdditionalOperations.
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
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
Instances For
getKeyGE, getKeyGT, getKeyLE, getKeyLT can be found in
Std.Data.TreeMap.AdditionalOperations.
Folds the given monadic function over the mappings in the map in ascending order.
Equations
Instances For
Folds the given function over the mappings in the map in descending order.
Equations
Instances For
Support for the for loop construct in do blocks. Iteration happens in ascending order.
Equations
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
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
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
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
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.