Documentation

Std.Data.HashMap.Raw

structure Std.HashMap.Raw (α : Type u) (β : Type v) :
Type (max u v)

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
    @[inline]
    def Std.HashMap.Raw.empty {α : Type u} {β : Type v} (capacity : Nat := 8) :

    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.

    Instances For
      Equations
      • Std.HashMap.Raw.instEmptyCollection = { emptyCollection := Std.HashMap.Raw.empty }
      Equations
      • Std.HashMap.Raw.instInhabited = { default := }
      @[inline]
      def Std.HashMap.Raw.insert {α : Type u} {β : Type v} [beq : BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) (b : β) :

      Inserts the given mapping into the map. If there is already a mapping for the given key, then both key and value will be replaced.

      Note: this replacement behavior is true for HashMap, DHashMap, HashMap.Raw and DHashMap.Raw. The insert function on HashSet and HashSet.Raw behaves differently: it will return the set unchanged if a matching key is already present.

      Instances For
        @[inline]
        def Std.HashMap.Raw.insertIfNew {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) (b : β) :

        If there is no mapping for the given key, inserts the given mapping into the map. Otherwise, returns the map unaltered.

        Instances For
          @[inline]
          def Std.HashMap.Raw.containsThenInsert {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) (b : β) :

          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.

          Instances For
            @[inline]
            def Std.HashMap.Raw.containsThenInsertIfNew {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) (b : β) :

            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
              @[inline]
              def Std.HashMap.Raw.getThenInsertIfNew? {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) (b : β) :

              Equivalent to (but potentially faster than) calling get? followed by insertIfNew.

              Equations
              Instances For
                @[inline]
                def Std.HashMap.Raw.get? {α : Type u} {β : Type v} [beq : BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) :

                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.

                Instances For
                  @[inline]
                  def Std.HashMap.Raw.contains {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) :

                  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
                    instance Std.HashMap.Raw.instDecidableMem {α : Type u} {β : Type v} [BEq α] [Hashable α] {m : Std.HashMap.Raw α β} {a : α} :
                    @[inline]
                    def Std.HashMap.Raw.get {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) (h : a m) :
                    β

                    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
                    Instances For
                      @[inline]
                      def Std.HashMap.Raw.getD {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) (fallback : β) :
                      β

                      Tries to retrieve the mapping for the given key, returning fallback if no such mapping is present.

                      Instances For
                        @[inline]
                        def Std.HashMap.Raw.get! {α : Type u} {β : Type v} [BEq α] [Hashable α] [Inhabited β] (m : Std.HashMap.Raw α β) (a : α) :
                        β

                        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
                        Instances For
                          instance Std.HashMap.Raw.instGetElem?Mem {α : Type u} {β : Type v} [BEq α] [Hashable α] :
                          GetElem? (Std.HashMap.Raw α β) α β fun (m : Std.HashMap.Raw α β) (a : α) => a m
                          Equations
                          @[inline]
                          def Std.HashMap.Raw.getKey? {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (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
                            @[inline]
                            def Std.HashMap.Raw.getKey {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) (h : a m) :
                            α

                            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
                            • m.getKey a h = m.inner.getKey a h
                            Instances For
                              @[inline]
                              def Std.HashMap.Raw.getKeyD {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a fallback : α) :
                              α

                              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
                              • m.getKeyD a fallback = m.inner.getKeyD a fallback
                              Instances For
                                @[inline]
                                def Std.HashMap.Raw.getKey! {α : Type u} {β : Type v} [BEq α] [Hashable α] [Inhabited α] (m : Std.HashMap.Raw α β) (a : α) :
                                α

                                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
                                • m.getKey! a = m.inner.getKey! a
                                Instances For
                                  @[inline]
                                  def Std.HashMap.Raw.erase {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) (a : α) :

                                  Removes the mapping for the given key if it exists.

                                  Instances For
                                    @[inline]
                                    def Std.HashMap.Raw.size {α : Type u} {β : Type v} (m : Std.HashMap.Raw α β) :

                                    The number of mappings present in the hash map

                                    Equations
                                    • m.size = m.inner.size
                                    Instances For
                                      @[inline]
                                      def Std.HashMap.Raw.isEmpty {α : Type u} {β : Type v} (m : Std.HashMap.Raw α β) :

                                      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
                                        @[inline]
                                        def Std.HashMap.Raw.keys {α : Type u} {β : Type v} (m : Std.HashMap.Raw α β) :
                                        List α

                                        Returns a list of all keys present in the hash map in some order.

                                        Instances For

                                          We currently do not provide lemmas for the functions below.

                                          @[inline]
                                          def Std.HashMap.Raw.filterMap {α : Type u} {β : Type v} {γ : Type w} (f : αβOption γ) (m : Std.HashMap.Raw α β) :

                                          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
                                          Instances For
                                            @[inline]
                                            def Std.HashMap.Raw.map {α : Type u} {β : Type v} {γ : Type w} (f : αβγ) (m : Std.HashMap.Raw α β) :

                                            Updates the values of the hash map by applying the given function to all mappings.

                                            Equations
                                            Instances For
                                              @[inline]
                                              def Std.HashMap.Raw.filter {α : Type u} {β : Type v} (f : αβBool) (m : Std.HashMap.Raw α β) :

                                              Removes all mappings of the hash map for which the given function returns false.

                                              Instances For
                                                @[inline]
                                                def Std.HashMap.Raw.foldM {α : Type u} {β : Type v} {m : Type w → Type w} [Monad m] {γ : Type w} (f : γαβm γ) (init : γ) (b : Std.HashMap.Raw α β) :
                                                m γ

                                                Monadically computes a value by folding the given function over the mappings in the hash map in some order.

                                                Instances For
                                                  @[inline]
                                                  def Std.HashMap.Raw.fold {α : Type u} {β : Type v} {γ : Type w} (f : γαβγ) (init : γ) (b : Std.HashMap.Raw α β) :
                                                  γ

                                                  Folds the given function over the mappings in the hash map in some order.

                                                  Equations
                                                  Instances For
                                                    @[inline]
                                                    def Std.HashMap.Raw.forM {α : Type u} {β : Type v} {m : Type w → Type w} [Monad m] (f : αβm PUnit) (b : Std.HashMap.Raw α β) :

                                                    Carries out a monadic action on each mapping in the hash map in some order.

                                                    Equations
                                                    Instances For
                                                      @[inline]
                                                      def Std.HashMap.Raw.forIn {α : Type u} {β : Type v} {m : Type w → Type w} [Monad m] {γ : Type w} (f : αβγm (ForInStep γ)) (init : γ) (b : Std.HashMap.Raw α β) :
                                                      m γ

                                                      Support for the for loop construct in do blocks.

                                                      Equations
                                                      Instances For
                                                        instance Std.HashMap.Raw.instForMProd {α : Type u} {β : Type v} {m : Type w → Type w} :
                                                        ForM m (Std.HashMap.Raw α β) (α × β)
                                                        Equations
                                                        instance Std.HashMap.Raw.instForInProd {α : Type u} {β : Type v} {m : Type w → Type w} :
                                                        ForIn m (Std.HashMap.Raw α β) (α × β)
                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        @[inline]
                                                        def Std.HashMap.Raw.toList {α : Type u} {β : Type v} (m : Std.HashMap.Raw α β) :
                                                        List (α × β)

                                                        Transforms the hash map into a list of mappings in some order.

                                                        Instances For
                                                          @[inline]
                                                          def Std.HashMap.Raw.toArray {α : Type u} {β : Type v} (m : Std.HashMap.Raw α β) :
                                                          Array (α × β)

                                                          Transforms the hash map into an array of mappings in some order.

                                                          Instances For
                                                            @[inline]
                                                            def Std.HashMap.Raw.keysArray {α : Type u} {β : Type v} (m : Std.HashMap.Raw α β) :

                                                            Returns an array of all keys present in the hash map in some order.

                                                            Instances For
                                                              @[inline]
                                                              def Std.HashMap.Raw.values {α : Type u} {β : Type v} (m : Std.HashMap.Raw α β) :
                                                              List β

                                                              Returns a list of all values present in the hash map in some order.

                                                              Equations
                                                              • m.values = m.inner.values
                                                              Instances For
                                                                @[inline]
                                                                def Std.HashMap.Raw.valuesArray {α : Type u} {β : Type v} (m : Std.HashMap.Raw α β) :

                                                                Returns an array of all values present in the hash map in some order.

                                                                Equations
                                                                • m.valuesArray = m.inner.valuesArray
                                                                Instances For
                                                                  @[inline]
                                                                  def Std.HashMap.Raw.insertMany {α : Type u} {β : Type v} [BEq α] [Hashable α] {ρ : Type w} [ForIn Id ρ (α × β)] (m : Std.HashMap.Raw α β) (l : ρ) :

                                                                  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 precedence.

                                                                  Note: this precedence behavior is true for HashMap, DHashMap, HashMap.Raw and DHashMap.Raw. The insertMany function on HashSet and HashSet.Raw behaves differently: it will prefer the first appearance.

                                                                  Instances For
                                                                    @[inline]
                                                                    def Std.HashMap.Raw.insertManyIfNewUnit {α : Type u} [BEq α] [Hashable α] {ρ : Type w} [ForIn Id ρ α] (m : Std.HashMap.Raw α Unit) (l : ρ) :

                                                                    Inserts multiple keys with the value () into the hash map by iterating over the given collection and calling insertIfNew. If the same key appears multiple times, the first 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.

                                                                    Instances For
                                                                      @[inline]
                                                                      def Std.HashMap.Raw.ofList {α : Type u} {β : Type v} [BEq α] [Hashable α] (l : List (α × β)) :

                                                                      Creates a hash map from a list of mappings. If the same key appears multiple times, the last occurrence takes precedence.

                                                                      Equations
                                                                      Instances For
                                                                        @[inline]
                                                                        def Std.HashMap.Raw.union {α : Type u} {β : Type v} [BEq α] [Hashable α] (m₁ m₂ : Std.HashMap.Raw α β) :

                                                                        Computes the union of the given hash maps, by traversing m₂ and inserting its elements into m₁.

                                                                        Equations
                                                                        Instances For
                                                                          @[inline]

                                                                          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.

                                                                          Instances For
                                                                            @[inline]

                                                                            Creates a hash map from an array of keys, associating the value () with each key.

                                                                            This is mainly useful to implement HashSet.ofArray, so if you are considering using this, HashSet or HashSet.Raw might be a better fit for you.

                                                                            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
                                                                                instance Std.HashMap.Raw.instRepr {α : Type u} {β : Type v} [Repr α] [Repr β] :
                                                                                structure Std.HashMap.Raw.WF {α : Type u} {β : Type v} [BEq α] [Hashable α] (m : Std.HashMap.Raw α β) :

                                                                                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
                                                                                  theorem Std.HashMap.Raw.WF.empty {α : Type u} {β : Type v} [BEq α] [Hashable α] {c : Nat} :
                                                                                  theorem Std.HashMap.Raw.WF.emptyc {α : Type u} {β : Type v} [BEq α] [Hashable α] :
                                                                                  .WF
                                                                                  theorem Std.HashMap.Raw.WF.insert {α : Type u} {β : Type v} [BEq α] [Hashable α] {m : Std.HashMap.Raw α β} {a : α} {b : β} (h : m.WF) :
                                                                                  (m.insert a b).WF
                                                                                  theorem Std.HashMap.Raw.WF.insertIfNew {α : Type u} {β : Type v} [BEq α] [Hashable α] {m : Std.HashMap.Raw α β} {a : α} {b : β} (h : m.WF) :
                                                                                  (m.insertIfNew a b).WF
                                                                                  theorem Std.HashMap.Raw.WF.containsThenInsert {α : Type u} {β : Type v} [BEq α] [Hashable α] {m : Std.HashMap.Raw α β} {a : α} {b : β} (h : m.WF) :
                                                                                  (m.containsThenInsert a b).snd.WF
                                                                                  theorem Std.HashMap.Raw.WF.containsThenInsertIfNew {α : Type u} {β : Type v} [BEq α] [Hashable α] {m : Std.HashMap.Raw α β} {a : α} {b : β} (h : m.WF) :
                                                                                  (m.containsThenInsertIfNew a b).snd.WF
                                                                                  theorem Std.HashMap.Raw.WF.getThenInsertIfNew? {α : Type u} {β : Type v} [BEq α] [Hashable α] {m : Std.HashMap.Raw α β} {a : α} {b : β} (h : m.WF) :
                                                                                  (m.getThenInsertIfNew? a b).snd.WF
                                                                                  theorem Std.HashMap.Raw.WF.erase {α : Type u} {β : Type v} [BEq α] [Hashable α] {m : Std.HashMap.Raw α β} {a : α} (h : m.WF) :
                                                                                  (m.erase a).WF
                                                                                  theorem Std.HashMap.Raw.WF.filter {α : Type u} {β : Type v} [BEq α] [Hashable α] {m : Std.HashMap.Raw α β} {f : αβBool} (h : m.WF) :
                                                                                  theorem Std.HashMap.Raw.WF.insertMany {α : Type u} {β : Type v} [BEq α] [Hashable α] {ρ : Type w} [ForIn Id ρ (α × β)] {m : Std.HashMap.Raw α β} {l : ρ} (h : m.WF) :
                                                                                  (m.insertMany l).WF
                                                                                  theorem Std.HashMap.Raw.WF.insertManyIfNewUnit {α : Type u} [BEq α] [Hashable α] {ρ : Type w} [ForIn Id ρ α] {m : Std.HashMap.Raw α Unit} {l : ρ} (h : m.WF) :
                                                                                  (m.insertManyIfNewUnit l).WF
                                                                                  theorem Std.HashMap.Raw.WF.ofList {α : Type u} {β : Type v} [BEq α] [Hashable α] {l : List (α × β)} :