Documentation

Std.Data.HashMap.Basic

class Std.HashMap.LawfulHashable (α : Type u_1) [BEq α] [Hashable α] :

A hash is lawful if elements which compare equal under == have equal hash.

  • hash_eq : ∀ {a b : α}, (a == b) = truehash a = hash b

    Two elements which compare equal under the BEq instance have equal hash.

Instances
    def Std.HashMap.Imp.Buckets (α : Type u) (β : Type v) :
    Type (max 0 u v)

    The bucket array of a HashMap is a nonempty array of AssocLists. (This type is an internal implementation detail of HashMap.)

    Equations
    Instances For
      def Std.HashMap.Imp.Buckets.mk {α : Type u_1} {β : Type u_2} (buckets : optParam Nat 8) (h : autoParam (0 < buckets) _auto✝) :

      Construct a new empty bucket array with the specified capacity.

      Equations
      Instances For
        def Std.HashMap.Imp.Buckets.update {α : Type u_1} {β : Type u_2} (data : Std.HashMap.Imp.Buckets α β) (i : USize) (d : Std.AssocList α β) (h : USize.toNat i < Array.size data.val) :

        Update one bucket in the bucket array with a new value.

        Equations
        Instances For
          noncomputable def Std.HashMap.Imp.Buckets.size {α : Type u_1} {β : Type u_2} (data : Std.HashMap.Imp.Buckets α β) :

          The number of elements in the bucket array. Note: this is marked noncomputable because it is only intended for specification.

          Equations
          Instances For
            @[simp]
            theorem Std.HashMap.Imp.Buckets.update_size {α : Type u_1} {β : Type u_2} (self : Std.HashMap.Imp.Buckets α β) (i : USize) (d : Std.AssocList α β) (h : USize.toNat i < Array.size self.val) :
            @[specialize #[]]
            def Std.HashMap.Imp.Buckets.mapVal {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : αβγ) (self : Std.HashMap.Imp.Buckets α β) :

            Map a function over the values in the map.

            Equations
            Instances For
              structure Std.HashMap.Imp.Buckets.WF {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (buckets : Std.HashMap.Imp.Buckets α β) :

              The well-formedness invariant for the bucket array says that every element hashes to its index (assuming the hash is lawful - otherwise there are no promises about where elements are located).

              Instances For
                structure Std.HashMap.Imp (α : Type u) (β : Type v) :
                Type (max u v)

                HashMap.Imp α β is the internal implementation type of HashMap α β.

                • size : Nat

                  The number of elements stored in the HashMap. We cache this both so that we can implement .size in O(1), and also because we use the size to determine when to resize the map.

                • buckets : Std.HashMap.Imp.Buckets α β

                  The bucket array of the HashMap.

                Instances For
                  @[inline]

                  Given a desired capacity, this returns the number of buckets we should reserve. A "load factor" of 0.75 is the usual standard for hash maps, so we return capacity * 4 / 3.

                  Equations
                  Instances For
                    @[inline]
                    def Std.HashMap.Imp.empty' {α : Type u_1} {β : Type u_2} (buckets : optParam Nat 8) (h : autoParam (0 < buckets) _auto✝) :

                    Constructs an empty hash map with the specified nonzero number of buckets.

                    Equations
                    Instances For
                      def Std.HashMap.Imp.empty {α : Type u_1} {β : Type u_2} (capacity : optParam Nat 0) :

                      Constructs an empty hash map with the specified target capacity.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        def Std.HashMap.Imp.mkIdx {n : Nat} (h : 0 < n) (u : USize) :
                        { u : USize // USize.toNat u < n }

                        Calculates the bucket index from a hash value u.

                        Equations
                        Instances For
                          @[inline]
                          def Std.HashMap.Imp.reinsertAux {α : Type u_1} {β : Type u_2} [Hashable α] (data : Std.HashMap.Imp.Buckets α β) (a : α) (b : β) :

                          Inserts a key-value pair into the bucket array. This function assumes that the data is not already in the array, which is appropriate when reinserting elements into the array after a resize.

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[inline]
                            def Std.HashMap.Imp.foldM {m : Type u_1 → Type u_2} {δ : Type u_1} {α : Type u_3} {β : Type u_4} [Monad m] (f : δαβm δ) (d : δ) (map : Std.HashMap.Imp α β) :
                            m δ

                            Folds a monadic function over the elements in the map (in arbitrary order).

                            Equations
                            Instances For
                              @[inline]
                              def Std.HashMap.Imp.fold {δ : Type u_1} {α : Type u_2} {β : Type u_3} (f : δαβδ) (d : δ) (m : Std.HashMap.Imp α β) :
                              δ

                              Folds a function over the elements in the map (in arbitrary order).

                              Equations
                              Instances For
                                @[inline]
                                def Std.HashMap.Imp.forM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_4} [Monad m] (f : αβm PUnit) (h : Std.HashMap.Imp α β) :

                                Runs a monadic function over the elements in the map (in arbitrary order).

                                Equations
                                Instances For
                                  def Std.HashMap.Imp.findEntry? {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (m : Std.HashMap.Imp α β) (a : α) :
                                  Option (α × β)

                                  Given a key a, returns a key-value pair in the map whose key compares equal to a.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    def Std.HashMap.Imp.find? {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (m : Std.HashMap.Imp α β) (a : α) :

                                    Looks up an element in the map with key a.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      def Std.HashMap.Imp.contains {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (m : Std.HashMap.Imp α β) (a : α) :

                                      Returns true if the element a is in the map.

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        def Std.HashMap.Imp.expand {α : Type u_1} {β : Type u_2} [Hashable α] (size : Nat) (buckets : Std.HashMap.Imp.Buckets α β) :

                                        Copies all the entries from buckets into a new hash map with a larger capacity.

                                        Equations
                                        Instances For
                                          def Std.HashMap.Imp.expand.go {α : Type u_1} {β : Type u_2} [Hashable α] (i : Nat) (source : Array (Std.AssocList α β)) (target : Std.HashMap.Imp.Buckets α β) :

                                          Inner loop of expand. Copies elements source[i:] into target, destroying source in the process.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            @[inline]
                                            def Std.HashMap.Imp.insert {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (m : Std.HashMap.Imp α β) (a : α) (b : β) :

                                            Inserts key-value pair a, b into the map. If an element equal to a is already in the map, it is replaced by b.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For
                                              def Std.HashMap.Imp.erase {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (m : Std.HashMap.Imp α β) (a : α) :

                                              Removes key a from the map. If it does not exist in the map, the map is returned unchanged.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                @[inline]
                                                def Std.HashMap.Imp.mapVal {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : αβγ) (self : Std.HashMap.Imp α β) :

                                                Map a function over the values in the map.

                                                Equations
                                                Instances For
                                                  def Std.HashMap.Imp.modify {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (m : Std.HashMap.Imp α β) (a : α) (f : αββ) :

                                                  Performs an in-place edit of the value, ensuring that the value is used linearly.

                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  Instances For
                                                    @[specialize #[]]
                                                    def Std.HashMap.Imp.filterMap {α : Type u} {β : Type v} {γ : Type w} (f : αβOption γ) (m : Std.HashMap.Imp α β) :

                                                    Applies f to each key-value pair a, b in the map. If it returns some c then a, c is pushed into the new map; else the key is removed from the map.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For
                                                      @[specialize #[]]
                                                      def Std.HashMap.Imp.filterMap.go {α : Type u} {β : Type v} {γ : Type w} (f : αβOption γ) (acc : Std.AssocList α γ) :

                                                      Inner loop of filterMap. Note that this reverses the bucket lists, but this is fine since bucket lists are unordered.

                                                      Equations
                                                      Instances For
                                                        @[inline]
                                                        def Std.HashMap.Imp.filter {α : Type u_1} {β : Type u_2} (f : αβBool) (m : Std.HashMap.Imp α β) :

                                                        Constructs a map with the set of all pairs a, b such that f returns true.

                                                        Equations
                                                        Instances For
                                                          inductive Std.HashMap.Imp.WF {α : Type u_1} [BEq α] [Hashable α] {β : Type u_2} :

                                                          The well-formedness invariant for a hash map. The first constructor is the real invariant, and the others allow us to "cheat" in this file and define insert and erase, which have more complex proofs that are delayed to Std.Data.HashMap.Lemmas.

                                                          Instances For
                                                            def Std.HashMap (α : Type u) (β : Type v) [BEq α] [Hashable α] :
                                                            Type (max 0 u v)

                                                            HashMap α β is a key-value map which stores elements in an array using a hash function to find the values. This allows it to have very good performance for lookups (average O(1) for a perfectly random hash function), but it is not a persistent data structure, meaning that one should take care to use the map linearly when performing updates. Copies are O(n).

                                                            Equations
                                                            Instances For
                                                              @[inline]
                                                              def Std.mkHashMap {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (capacity : optParam Nat 0) :

                                                              Make a new hash map with the specified capacity.

                                                              Equations
                                                              Instances For
                                                                instance Std.HashMap.instInhabitedHashMap {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] :
                                                                Equations
                                                                • Std.HashMap.instInhabitedHashMap = { default := Std.mkHashMap }
                                                                instance Std.HashMap.instEmptyCollectionHashMap {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] :
                                                                Equations
                                                                • Std.HashMap.instEmptyCollectionHashMap = { emptyCollection := Std.mkHashMap }
                                                                @[inline]
                                                                def Std.HashMap.empty {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] :

                                                                Make a new empty hash map.

                                                                Equations
                                                                • Std.HashMap.empty = Std.mkHashMap
                                                                Instances For
                                                                  @[inline]
                                                                  def Std.HashMap.size {α : Type u_1} :
                                                                  {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βNat

                                                                  The number of elements in the hash map.

                                                                  Equations
                                                                  Instances For
                                                                    @[inline]
                                                                    def Std.HashMap.isEmpty {α : Type u_1} :
                                                                    {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βBool

                                                                    Is the map empty?

                                                                    Equations
                                                                    Instances For
                                                                      def Std.HashMap.insert {α : Type u_1} :
                                                                      {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βαβStd.HashMap α β

                                                                      Inserts key-value pair a, b into the map. If an element equal to a is already in the map, it is replaced by b.

                                                                      Equations
                                                                      Instances For
                                                                        @[inline]
                                                                        def Std.HashMap.insert' {α : Type u_1} :
                                                                        {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βαβStd.HashMap α β × Bool

                                                                        Similar to insert, but also returns a boolean flag indicating whether an existing entry has been replaced with a => b.

                                                                        Equations
                                                                        Instances For
                                                                          @[inline]
                                                                          def Std.HashMap.erase {α : Type u_1} :
                                                                          {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βαStd.HashMap α β

                                                                          Removes key a from the map. If it does not exist in the map, the map is returned unchanged.

                                                                          Equations
                                                                          Instances For
                                                                            def Std.HashMap.modify {α : Type u_1} :
                                                                            {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βα(αββ)Std.HashMap α β

                                                                            Performs an in-place edit of the value, ensuring that the value is used linearly. The function f is passed the original key of the entry, along with the value in the map.

                                                                            Equations
                                                                            Instances For
                                                                              @[inline]
                                                                              def Std.HashMap.findEntry? {α : Type u_1} :
                                                                              {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βαOption (α × β)

                                                                              Given a key a, returns a key-value pair in the map whose key compares equal to a.

                                                                              Equations
                                                                              Instances For
                                                                                @[inline]
                                                                                def Std.HashMap.find? {α : Type u_1} :
                                                                                {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βαOption β

                                                                                Looks up an element in the map with key a.

                                                                                Equations
                                                                                Instances For
                                                                                  @[inline]
                                                                                  def Std.HashMap.findD {α : Type u_1} :
                                                                                  {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βαββ

                                                                                  Looks up an element in the map with key a. Returns b₀ if the element is not found.

                                                                                  Equations
                                                                                  Instances For
                                                                                    @[inline]
                                                                                    def Std.HashMap.find! {α : Type u_1} :
                                                                                    {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → [inst : Inhabited β] → Std.HashMap α βαβ

                                                                                    Looks up an element in the map with key a. Panics if the element is not found.

                                                                                    Equations
                                                                                    • One or more equations did not get rendered due to their size.
                                                                                    Instances For
                                                                                      instance Std.HashMap.instGetElemHashMapOptionTrue {α : Type u_1} :
                                                                                      {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → GetElem (Std.HashMap α β) α (Option β) fun (x : Std.HashMap α β) (x : α) => True
                                                                                      Equations
                                                                                      @[inline]
                                                                                      def Std.HashMap.contains {α : Type u_1} :
                                                                                      {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βαBool

                                                                                      Returns true if the element a is in the map.

                                                                                      Equations
                                                                                      Instances For
                                                                                        @[inline]
                                                                                        def Std.HashMap.foldM {α : Type u_1} :
                                                                                        {x : BEq α} → {x_1 : Hashable α} → {m : Type u_2 → Type u_3} → {δ : Type u_2} → {β : Type u_4} → [inst : Monad m] → (δαβm δ)δStd.HashMap α βm δ

                                                                                        Folds a monadic function over the elements in the map (in arbitrary order).

                                                                                        Equations
                                                                                        Instances For
                                                                                          @[inline]
                                                                                          def Std.HashMap.fold {α : Type u_1} :
                                                                                          {x : BEq α} → {x_1 : Hashable α} → {δ : Type u_2} → {β : Type u_3} → (δαβδ)δStd.HashMap α βδ

                                                                                          Folds a function over the elements in the map (in arbitrary order).

                                                                                          Equations
                                                                                          Instances For
                                                                                            @[specialize #[]]
                                                                                            def Std.HashMap.mergeWithM {α : Type u_1} :
                                                                                            {x : BEq α} → {x_1 : Hashable α} → {m : Type (max u_2 u_1) → Type u_3} → {β : Type (max u_2 u_1)} → [inst : Monad m] → (αββm β)Std.HashMap α βStd.HashMap α βm (Std.HashMap α β)

                                                                                            Combines two hashmaps using a monadic function f to combine two values at a key.

                                                                                            Equations
                                                                                            • One or more equations did not get rendered due to their size.
                                                                                            Instances For
                                                                                              @[inline]
                                                                                              def Std.HashMap.mergeWith {α : Type u_1} :
                                                                                              {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → (αβββ)Std.HashMap α βStd.HashMap α βStd.HashMap α β

                                                                                              Combines two hashmaps using function f to combine two values at a key.

                                                                                              Equations
                                                                                              • One or more equations did not get rendered due to their size.
                                                                                              Instances For
                                                                                                @[inline]
                                                                                                def Std.HashMap.forM {α : Type u_1} :
                                                                                                {x : BEq α} → {x_1 : Hashable α} → {m : Type u_2 → Type u_3} → {β : Type u_4} → [inst : Monad m] → (αβm PUnit)Std.HashMap α βm PUnit

                                                                                                Runs a monadic function over the elements in the map (in arbitrary order).

                                                                                                Equations
                                                                                                Instances For
                                                                                                  def Std.HashMap.toList {α : Type u_1} :
                                                                                                  {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βList (α × β)

                                                                                                  Converts the map into a list of key-value pairs.

                                                                                                  Equations
                                                                                                  Instances For
                                                                                                    def Std.HashMap.toArray {α : Type u_1} :
                                                                                                    {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βArray (α × β)

                                                                                                    Converts the map into an array of key-value pairs.

                                                                                                    Equations
                                                                                                    Instances For
                                                                                                      def Std.HashMap.numBuckets {α : Type u_1} :
                                                                                                      {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βNat

                                                                                                      The number of buckets in the hash map.

                                                                                                      Equations
                                                                                                      Instances For
                                                                                                        def Std.HashMap.ofList {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (l : List (α × β)) :

                                                                                                        Builds a HashMap from a list of key-value pairs. Values of duplicated keys are replaced by their respective last occurrences.

                                                                                                        Equations
                                                                                                        Instances For
                                                                                                          def Std.HashMap.ofListWith {α : Type u_1} {β : Type u_2} [BEq α] [Hashable α] (l : List (α × β)) (f : βββ) :

                                                                                                          Variant of ofList which accepts a function that combines values of duplicated keys.

                                                                                                          Equations
                                                                                                          • One or more equations did not get rendered due to their size.
                                                                                                          Instances For