Documentation

Std.Data.ExtHashMap.Basic

Extensional hash maps #

This module develops the type Std.ExtHashMap of extensional hash maps. Dependent hash maps are defined in Std.Data.ExtDHashMap.

Lemmas about the operations on Std.ExtHashMap are available in the module Std.Data.ExtHashMap.Lemmas.

structure Std.ExtHashMap (α : Type u) (β : Type v) [BEq α] [Hashable α] :
Type (max u v)

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.

In contrast to regular hash maps, Std.ExtHashMap offers several extensionality lemmas and therefore has more lemmas about equality of hash maps. This however also makes it lose the ability to iterate freely over hash maps.

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.HashMap.Raw and Std.HashMap.Raw.WF unbundle the invariant from the hash map. When in doubt, prefer HashMap or ExtHashMap over HashMap.Raw.

Dependent hash maps, in which keys may occur in their values' types, are available as Std.ExtDHashMap in the module Std.Data.ExtDHashMap.

  • inner : ExtDHashMap α fun (x : α) => β

    Internal implementation detail of the hash map

Instances For
    @[inline]
    def Std.ExtHashMap.emptyWithCapacity {α : Type u} {β : Type v} [BEq α] [Hashable α] (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.

    Equations
    Instances For
      instance Std.ExtHashMap.instInhabited {α : Type u} {β : Type v} [BEq α] [Hashable α] :
      Equations
      @[inline]
      def Std.ExtHashMap.insert {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (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.

      Equations
      Instances For
        instance Std.ExtHashMap.instSingletonProdOfEquivBEqOfLawfulHashable {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] :
        Singleton (α × β) (ExtHashMap α β)
        Equations
        instance Std.ExtHashMap.instInsertProdOfEquivBEqOfLawfulHashable {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] :
        Insert (α × β) (ExtHashMap α β)
        Equations
        instance Std.ExtHashMap.instLawfulSingletonProd {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] :
        LawfulSingleton (α × β) (ExtHashMap α β)
        @[inline]
        def Std.ExtHashMap.insertIfNew {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (a : α) (b : β) :

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

        Equations
        Instances For
          @[inline]
          def Std.ExtHashMap.containsThenInsert {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (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.

          Equations
          Instances For
            @[inline]
            def Std.ExtHashMap.containsThenInsertIfNew {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (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
            Instances For
              @[inline]
              def Std.ExtHashMap.getThenInsertIfNew? {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (a : α) (b : β) :

              Checks whether a key is present in a map, returning the associate 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.

              Equations
              Instances For
                @[inline]
                def Std.ExtHashMap.get? {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (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.

                Equations
                Instances For
                  @[inline]
                  def Std.ExtHashMap.contains {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (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
                  Instances For
                    @[inline]
                    def Std.ExtHashMap.get {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (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.ExtHashMap.getD {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (a : α) (fallback : β) :
                      β

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

                      Equations
                      Instances For
                        @[inline]
                        def Std.ExtHashMap.get! {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] [Inhabited β] (m : ExtHashMap α β) (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.ExtHashMap.instGetElem?Mem {α : Type u} {β : Type v} [BEq α] [Hashable α] [EquivBEq α] [LawfulHashable α] :
                          GetElem? (ExtHashMap α β) α β fun (m : ExtHashMap α β) (a : α) => a m
                          Equations
                          • One or more equations did not get rendered due to their size.
                          @[inline]
                          def Std.ExtHashMap.getKey? {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (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.

                          Equations
                          Instances For
                            @[inline]
                            def Std.ExtHashMap.getKey {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (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
                            Instances For
                              @[inline]
                              def Std.ExtHashMap.getKeyD {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (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
                              Instances For
                                @[inline]
                                def Std.ExtHashMap.getKey! {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] [Inhabited α] (m : ExtHashMap α β) (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
                                Instances For
                                  @[inline]
                                  def Std.ExtHashMap.erase {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (a : α) :

                                  Removes the mapping for the given key if it exists.

                                  Equations
                                  Instances For
                                    @[inline]
                                    def Std.ExtHashMap.size {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) :

                                    The number of mappings present in the hash map

                                    Equations
                                    Instances For
                                      @[inline]
                                      def Std.ExtHashMap.isEmpty {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) :

                                      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
                                      Instances For
                                        @[inline]
                                        def Std.ExtHashMap.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.ExtHashMap.unitOfList {α : Type u} [BEq α] [Hashable α] (l : List α) :

                                          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
                                            @[inline]
                                            def Std.ExtHashMap.filter {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (f : αβBool) (m : ExtHashMap α β) :

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

                                            Equations
                                            Instances For
                                              @[inline]
                                              def Std.ExtHashMap.map {α : Type u} {β : Type v} {γ : Type w} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (f : αβγ) (m : ExtHashMap α β) :

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

                                              Equations
                                              Instances For
                                                @[inline]
                                                def Std.ExtHashMap.filterMap {α : Type u} {β : Type v} {γ : Type w} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (f : αβOption γ) (m : ExtHashMap α β) :

                                                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.ExtHashMap.modify {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (a : α) (f : ββ) :

                                                  Modifies in place the value associated with a given key.

                                                  This function ensures that the value is used linearly.

                                                  Equations
                                                  Instances For
                                                    @[inline]
                                                    def Std.ExtHashMap.alter {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] (m : ExtHashMap α β) (a : α) (f : Option βOption β) :

                                                    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
                                                      @[inline]
                                                      def Std.ExtHashMap.insertMany {α : Type u} {β : Type v} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] {ρ : Type w} [ForIn Id ρ (α × β)] (m : ExtHashMap α β) (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.

                                                      Equations
                                                      Instances For
                                                        @[inline]
                                                        def Std.ExtHashMap.insertManyIfNewUnit {α : Type u} {x✝ : BEq α} {x✝¹ : Hashable α} [EquivBEq α] [LawfulHashable α] {ρ : Type w} [ForIn Id ρ α] (m : ExtHashMap α 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.

                                                        Equations
                                                        Instances For
                                                          @[inline]
                                                          def Std.ExtHashMap.unitOfArray {α : Type u} [BEq α] [Hashable α] (l : Array α) :

                                                          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.

                                                          Equations
                                                          Instances For