Dependent hash maps #
This file develops the type Std.Data.DHashMap
of dependent hash maps.
The operations map
and filterMap
on Std.Data.DHashMap
are defined in the module
Std.Data.DHashMap.AdditionalOperations
.
Lemmas about the operations on Std.Data.DHashMap
are available in the
module Std.Data.DHashMap.Lemmas
.
See the module Std.Data.DHashMap.Raw
for a variant of this type which is safe to use in
nested inductive types.
For implementation notes, see the docstring of the module Std.Data.DHashMap.Internal.Defs
.
Dependent hash maps.
This is a simple separate-chaining hash table. The data of the hash map consists of a cached size and an array of buckets, where each bucket is a linked list of key-value pais. The number of buckets is always a power of two. The hash map doubles its size upon inserting an element such that the number of elements is more than 75% of the number of buckets.
The hash table is backed by an Array
. Users should make sure that the hash map is used linearly to
avoid expensive copies.
The hash map uses ==
(provided by the BEq
typeclass) to compare keys and hash
(provided by
the Hashable
typeclass) to hash them. To ensure that the operations behave as expected, ==
should be an equivalence relation and a == b
should imply hash a = hash b
(see also the
EquivBEq
and LawfulHashable
typeclasses). Both of these conditions are automatic if the BEq
instance is lawful, i.e., if a == b
implies a = b
.
These hash maps contain a bundled well-formedness invariant, which means that they cannot
be used in nested inductive types. For these use cases, Std.Data.DHashMap.Raw
and
Std.Data.DHashMap.Raw.WF
unbundle the invariant from the hash map. When in doubt, prefer
DHashMap
over DHashMap.Raw
.
Equations
- Std.DHashMap α β = { m : Std.DHashMap.Raw α β // m.WF }
Instances For
Creates a new empty hash map. The optional parameter capacity
can be supplied to presize the
map so that it can hold the given number of mappings without reallocating. It is also possible to
use the empty collection notations ∅
and {}
to create an empty hash map with the default
capacity.
Equations
- Std.DHashMap.empty capacity = ⟨Std.DHashMap.Raw.empty capacity, ⋯⟩
Instances For
Equations
- Std.DHashMap.instEmptyCollection = { emptyCollection := Std.DHashMap.empty }
Inserts the given mapping into the map, replacing an existing mapping for the key if there is one.
Equations
- m.insert a b = ⟨(Std.DHashMap.Internal.Raw₀.insert ⟨m.val, ⋯⟩ a b).val, ⋯⟩
Instances For
If there is no mapping for the given key, inserts the given mapping into the map. Otherwise, returns the map unaltered.
Equations
- m.insertIfNew a b = ⟨(Std.DHashMap.Internal.Raw₀.insertIfNew ⟨m.val, ⋯⟩ a b).val, ⋯⟩
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
- m.containsThenInsert a b = ((Std.DHashMap.Internal.Raw₀.containsThenInsert ⟨m.val, ⋯⟩ a b).fst, ⟨(Std.DHashMap.Internal.Raw₀.containsThenInsert ⟨m.val, ⋯⟩ a b).snd.val, ⋯⟩)
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
- m.containsThenInsertIfNew a b = ((Std.DHashMap.Internal.Raw₀.containsThenInsertIfNew ⟨m.val, ⋯⟩ a b).fst, ⟨(Std.DHashMap.Internal.Raw₀.containsThenInsertIfNew ⟨m.val, ⋯⟩ a b).snd.val, ⋯⟩)
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 LawfulBEq
instance to cast the retrieved value to the correct type.
Equations
- m.getThenInsertIfNew? a b = ((Std.DHashMap.Internal.Raw₀.getThenInsertIfNew? ⟨m.val, ⋯⟩ a b).fst, ⟨(Std.DHashMap.Internal.Raw₀.getThenInsertIfNew? ⟨m.val, ⋯⟩ a b).snd.val, ⋯⟩)
Instances For
Tries to retrieve the mapping for the given key, returning none
if no such mapping is present.
Uses the LawfulBEq
instance to cast the retrieved value to the correct type.
Equations
- m.get? a = Std.DHashMap.Internal.Raw₀.get? ⟨m.val, ⋯⟩ a
Instances For
Returns true
if there is a mapping for the given key. There is also a Prop
-valued version
of this: a ∈ m
is equivalent to m.contains a = true
.
Observe that this is different behavior than for lists: for lists, ∈
uses =
and contains
uses
==
for comparisons, while for hash maps, both use ==
.
Equations
- m.contains a = Std.DHashMap.Internal.Raw₀.contains ⟨m.val, ⋯⟩ a
Instances For
Equations
- Std.DHashMap.instMembership = { mem := fun (m : Std.DHashMap α β) (a : α) => m.contains a = true }
Equations
- Std.DHashMap.instDecidableMem = inferInstance
Retrieves the mapping for the given key. Ensures that such a mapping exists by requiring a proof
of a ∈ m
.
Uses the LawfulBEq
instance to cast the retrieved value to the correct type.
Equations
- m.get a h = Std.DHashMap.Internal.Raw₀.get ⟨m.val, ⋯⟩ a h
Instances For
Tries to retrieve the mapping for the given key, panicking if no such mapping is present.
Uses the LawfulBEq
instance to cast the retrieved value to the correct type.
Equations
- m.get! a = Std.DHashMap.Internal.Raw₀.get! ⟨m.val, ⋯⟩ a
Instances For
Tries to retrieve the mapping for the given key, returning fallback
if no such mapping is present.
Uses the LawfulBEq
instance to cast the retrieved value to the correct type.
Equations
- m.getD a fallback = Std.DHashMap.Internal.Raw₀.getD ⟨m.val, ⋯⟩ a fallback
Instances For
Removes the mapping for the given key if it exists.
Equations
- m.erase a = ⟨(Std.DHashMap.Internal.Raw₀.erase ⟨m.val, ⋯⟩ a).val, ⋯⟩
Instances For
Tries to retrieve the mapping for the given key, returning none
if no such mapping is present.
Equations
- Std.DHashMap.Const.get? m a = Std.DHashMap.Internal.Raw₀.Const.get? ⟨m.val, ⋯⟩ a
Instances For
Retrieves the mapping for the given key. Ensures that such a mapping exists by requiring a proof of
a ∈ m
.
Equations
- Std.DHashMap.Const.get m a h = Std.DHashMap.Internal.Raw₀.Const.get ⟨m.val, ⋯⟩ a h
Instances For
Tries to retrieve the mapping for the given key, returning fallback
if no such mapping is present.
Equations
- Std.DHashMap.Const.getD m a fallback = Std.DHashMap.Internal.Raw₀.Const.getD ⟨m.val, ⋯⟩ a fallback
Instances For
Tries to retrieve the mapping for the given key, panicking if no such mapping is present.
Equations
- Std.DHashMap.Const.get! m a = Std.DHashMap.Internal.Raw₀.Const.get! ⟨m.val, ⋯⟩ a
Instances For
Equivalent to (but potentially faster than) calling Const.get?
followed by insertIfNew
.
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The number of mappings present in the hash map
Equations
- m.size = m.val.size
Instances For
Returns true
if the hash map contains no mappings.
Note that if your BEq
instance is not reflexive or your Hashable
instance is not
lawful, then it is possible that this function returns false
even though is not possible
to get anything out of the hash map.
Equations
- m.isEmpty = m.val.isEmpty
Instances For
We currently do not provide lemmas for the functions below.
Removes all mappings of the hash map for which the given function returns false
.
Equations
- Std.DHashMap.filter f m = ⟨(Std.DHashMap.Internal.Raw₀.filter f ⟨m.val, ⋯⟩).val, ⋯⟩
Instances For
Monadically computes a value by folding the given function over the mappings in the hash map in some order.
Equations
- Std.DHashMap.foldM f init b = Std.DHashMap.Raw.foldM f init b.val
Instances For
Folds the given function over the mappings in the hash map in some order.
Equations
- Std.DHashMap.fold f init b = Std.DHashMap.Raw.fold f init b.val
Instances For
Carries out a monadic action on each mapping in the hash map in some order.
Equations
- Std.DHashMap.forM f b = Std.DHashMap.Raw.forM f b.val
Instances For
Support for the for
loop construct in do
blocks.
Equations
- Std.DHashMap.forIn f init b = Std.DHashMap.Raw.forIn f init b.val
Instances For
Equations
- Std.DHashMap.instForMSigma = { forM := fun [Monad m] (m_1 : Std.DHashMap α β) (f : (a : α) × β a → m PUnit) => Std.DHashMap.forM (fun (a : α) (b : β a) => f ⟨a, b⟩) m_1 }
Equations
- One or more equations did not get rendered due to their size.
Transforms the hash map into a list of mappings in some order.
Equations
- m.toList = m.val.toList
Instances For
Transforms the hash map into an array of mappings in some order.
Equations
- m.toArray = m.val.toArray
Instances For
Transforms the hash map into a list of mappings in some order.
Equations
Instances For
Transforms the hash map into an array of mappings in some order.
Equations
Instances For
Returns a list of all keys present in the hash map in some order.
Equations
- m.keys = m.val.keys
Instances For
Returns an array of all keys present in the hash map in some order.
Equations
- m.keysArray = m.val.keysArray
Instances For
Returns a list of all values present in the hash map in some order.
Equations
- m.values = m.val.values
Instances For
Returns an array of all values present in the hash map in some order.
Equations
- m.valuesArray = m.val.valuesArray
Instances For
Inserts multiple mappings into the hash map by iterating over the given collection and calling
insert
. If the same key appears multiple times, the last occurrence takes precendence.
Equations
- m.insertMany l = ⟨(Std.DHashMap.Internal.Raw₀.insertMany ⟨m.val, ⋯⟩ l).val.val, ⋯⟩
Instances For
Inserts multiple mappings into the hash map by iterating over the given collection and calling
insert
. If the same key appears multiple times, the last occurrence takes precendence.
Equations
- Std.DHashMap.Const.insertMany m l = ⟨(Std.DHashMap.Internal.Raw₀.Const.insertMany ⟨m.val, ⋯⟩ l).val.val, ⋯⟩
Instances For
Inserts multiple keys with the value ()
into the hash map by iterating over the given collection
and calling insert
. If the same key appears multiple times, the last occurrence takes precedence.
This is mainly useful to implement HashSet.insertMany
, so if you are considering using this,
HashSet
or HashSet.Raw
might be a better fit for you.
Equations
- Std.DHashMap.Const.insertManyUnit m l = ⟨(Std.DHashMap.Internal.Raw₀.Const.insertManyUnit ⟨m.val, ⋯⟩ l).val.val, ⋯⟩
Instances For
Creates a hash map from a list of mappings. If the same key appears multiple times, the last occurrence takes precedence.
Equations
- Std.DHashMap.ofList l = ∅.insertMany l
Instances For
Creates a hash map from a list of mappings. If the same key appears multiple times, the last occurrence takes precedence.
Equations
Instances For
Creates a hash map from a list of keys, associating the value ()
with each key.
This is mainly useful to implement HashSet.ofList
, so if you are considering using this,
HashSet
or HashSet.Raw
might be a better fit for you.
Equations
Instances For
Returns the number of buckets in the internal representation of the hash map. This function may be useful for things like monitoring system health, but it should be considered an internal implementation detail.
Equations
Instances For
Equations
- Std.DHashMap.instRepr = { reprPrec := fun (m : Std.DHashMap α β) (prec : Nat) => Repr.addAppParen (Std.Format.text "Std.DHashMap.ofList " ++ reprArg m.toList) prec }