Hash 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 HashMap
over HashMap.Raw
. Lemmas about the operations on Std.Data.HashMap.Raw
are available in the
module Std.Data.HashMap.RawLemmas
.
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
.
Dependent hash maps, in which keys may occur in their values' types, are available as
Std.Data.Raw.DHashMap
.
- inner : Std.DHashMap.Raw α fun (x : α) => β
Internal implementation detail of the hash map
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.HashMap.Raw.empty capacity = { inner := Std.DHashMap.Raw.empty capacity }
Instances For
Equations
- Std.HashMap.Raw.instEmptyCollection = { emptyCollection := Std.HashMap.Raw.empty }
Inserts the given mapping into the map, replacing an existing mapping for the key if there is one.
Equations
- m.insert a b = { inner := m.inner.insert a b }
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 = { inner := m.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
- m.containsThenInsert a b = match m.inner.containsThenInsert a b with | (replaced, r) => (replaced, { inner := r })
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 = match m.inner.containsThenInsertIfNew a b with | (replaced, r) => (replaced, { inner := r })
Instances For
Equivalent to (but potentially faster than) calling get?
followed by insertIfNew
.
Equations
- m.getThenInsertIfNew? a b = match Std.DHashMap.Raw.Const.getThenInsertIfNew? m.inner a b with | (previous, r) => (previous, { inner := r })
Instances For
The notation m[a]?
is preferred over calling this function directly.
Tries to retrieve the mapping for the given key, returning none
if no such mapping is present.
Equations
- m.get? a = Std.DHashMap.Raw.Const.get? m.inner 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 = m.inner.contains a
Instances For
Equations
- Std.HashMap.Raw.instMembershipOfBEqOfHashable = { mem := fun (a : α) (m : Std.HashMap.Raw α β) => a ∈ m.inner }
Equations
- Std.HashMap.Raw.instDecidableMem = inferInstanceAs (Decidable (a ∈ m.inner))
The notation m[a]
or m[a]'h
is preferred over calling this function directly.
Retrieves the mapping for the given key. Ensures that such a mapping exists by requiring a proof of
a ∈ m
.
Equations
- m.get a h = Std.DHashMap.Raw.Const.get m.inner a h
Instances For
Tries to retrieve the mapping for the given key, returning fallback
if no such mapping is present.
Equations
- m.getD a fallback = Std.DHashMap.Raw.Const.getD m.inner a fallback
Instances For
The notation m[a]!
is preferred over calling this function directly.
Tries to retrieve the mapping for the given key, panicking if no such mapping is present.
Equations
- m.get! a = Std.DHashMap.Raw.Const.get! m.inner a
Instances For
Equations
- Std.HashMap.Raw.instGetElem?Mem = GetElem?.mk (fun (m : Std.HashMap.Raw α β) (a : α) => m.get? a) fun [Inhabited β] (m : Std.HashMap.Raw α β) (a : α) => m.get! a
Removes the mapping for the given key if it exists.
Equations
- m.erase a = { inner := m.inner.erase a }
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.inner.isEmpty
Instances For
We currently do not provide lemmas for the functions below.
Updates the values of the hash map by applying the given function to all mappings, keeping
only those mappings where the function returns some
value.
Equations
- Std.HashMap.Raw.filterMap f m = { inner := Std.DHashMap.Raw.filterMap f m.inner }
Instances For
Updates the values of the hash map by applying the given function to all mappings.
Equations
- Std.HashMap.Raw.map f m = { inner := Std.DHashMap.Raw.map f m.inner }
Instances For
Removes all mappings of the hash map for which the given function returns false
.
Equations
- Std.HashMap.Raw.filter f m = { inner := Std.DHashMap.Raw.filter f m.inner }
Instances For
Monadically computes a value by folding the given function over the mappings in the hash map in some order.
Equations
- Std.HashMap.Raw.foldM f init b = Std.DHashMap.Raw.foldM f init b.inner
Instances For
Folds the given function over the mappings in the hash map in some order.
Equations
- Std.HashMap.Raw.fold f init b = Std.DHashMap.Raw.fold f init b.inner
Instances For
Carries out a monadic action on each mapping in the hash map in some order.
Equations
- Std.HashMap.Raw.forM f b = Std.DHashMap.Raw.forM f b.inner
Instances For
Support for the for
loop construct in do
blocks.
Equations
- Std.HashMap.Raw.forIn f init b = Std.DHashMap.Raw.forIn f init b.inner
Instances For
Equations
- Std.HashMap.Raw.instForMProd = { forM := fun [Monad m] (m_1 : Std.HashMap.Raw α β) (f : α × β → m PUnit) => Std.HashMap.Raw.forM (fun (a : α) (b : β) => 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 = Std.DHashMap.Raw.Const.toList m.inner
Instances For
Transforms the hash map into an array of mappings in some order.
Equations
- m.toArray = Std.DHashMap.Raw.Const.toArray m.inner
Instances For
Returns a list of all keys present in the hash map in some order.
Equations
- m.keys = m.inner.keys
Instances For
Returns an array of all keys present in the hash map in some order.
Equations
- m.keysArray = m.inner.keysArray
Instances For
Returns a list of all values present in the hash map in some order.
Equations
- m.values = m.inner.values
Instances For
Returns an array of all values present in the hash map in some order.
Equations
- m.valuesArray = m.inner.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 = { inner := Std.DHashMap.Raw.Const.insertMany m.inner l }
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
- m.insertManyUnit l = { inner := Std.DHashMap.Raw.Const.insertManyUnit m.inner 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
- Std.HashMap.Raw.ofList l = { inner := Std.DHashMap.Raw.Const.ofList l }
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
- Std.HashMap.Raw.unitOfList l = { inner := Std.DHashMap.Raw.Const.unitOfList l }
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.HashMap.Raw.instRepr = { reprPrec := fun (m : Std.HashMap.Raw α β) (prec : Nat) => Repr.addAppParen (Std.Format.text "Std.HashMap.Raw.ofList " ++ reprArg m.toList) prec }
Well-formedness predicate for hash maps. Users of HashMap
will not need to interact with this.
Users of HashMap.Raw
will need to provide proofs of WF
to lemmas and should use lemmas
WF.empty
and WF.insert
(which are always named exactly like the operations they are about) to
show that map operations preserve well-formedness.
- out : m.inner.WF
Internal implementation detail of the hash map
Instances For
Internal implementation detail of the hash map