Documentation

Std.Data.HashMap.Basic

class Std.HashMap.LawfulHashable (α : Type u_1) [inst : BEq α] [inst : Hashable α] :
  • Two elements which compare equal under the BEq instance have equal hash.

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

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

Instances
    def Std.HashMap.Imp.Bucket (α : Type u) (β : Type v) :
    Type (max0uv)

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

    Equations
    def Std.HashMap.Imp.Bucket.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
    def Std.HashMap.Imp.Bucket.update {α : Type u_1} {β : Type u_2} (data : Std.HashMap.Imp.Bucket α β) (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
    noncomputable def Std.HashMap.Imp.Bucket.size {α : Type u_1} {β : Type u_2} (data : Std.HashMap.Imp.Bucket α β) :

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

    Equations
    @[specialize #[]]
    def Std.HashMap.Imp.Bucket.mapVal {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : αβγ) (self : Std.HashMap.Imp.Bucket α β) :

    Map a function over the values in the map.

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

    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 (maxuv)
      • 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.

        size : Nat
      • The bucket array of the HashMap.

        buckets : Std.HashMap.Imp.Bucket α β

      HashMap.Imp α β is the internal implementation type of 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
        @[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
        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.
        def Std.HashMap.Imp.mkIdx {n : Nat} (h : 0 < n) (u : USize) :
        { u // USize.toNat u < n }

        Calculates the bucket index from a hash value u.

        Equations
        @[inline]
        def Std.HashMap.Imp.reinsertAux {α : Type u_1} {β : Type u_2} [inst : Hashable α] (data : Std.HashMap.Imp.Bucket α β) (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.
        @[inline]
        def Std.HashMap.Imp.foldM {m : Type u_1 → Type u_2} {δ : Type u_1} {α : Type u_3} {β : Type u_4} [inst : Monad m] (f : δαβm δ) (d : δ) (map : Std.HashMap.Imp α β) :
        m δ

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

        Equations
        @[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
        @[inline]
        def Std.HashMap.Imp.forM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_4} [inst : Monad m] (f : αβm PUnit) (h : Std.HashMap.Imp α β) :

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

        Equations
        def Std.HashMap.Imp.findEntry? {α : Type u_1} {β : Type u_2} [inst : BEq α] [inst : 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.
        def Std.HashMap.Imp.find? {α : Type u_1} {β : Type u_2} [inst : BEq α] [inst : 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.
        def Std.HashMap.Imp.contains {α : Type u_1} {β : Type u_2} [inst : BEq α] [inst : 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.
        def Std.HashMap.Imp.expand {α : Type u_1} {β : Type u_2} [inst : Hashable α] (size : Nat) (buckets : Std.HashMap.Imp.Bucket α β) :

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

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

        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.
        @[inline]
        def Std.HashMap.Imp.insert {α : Type u_1} {β : Type u_2} [inst : BEq α] [inst : 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.
        def Std.HashMap.Imp.erase {α : Type u_1} {β : Type u_2} [inst : BEq α] [inst : 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.
        @[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
        @[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.
        @[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
        @[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
        inductive Std.HashMap.Imp.WF {α : Type u_1} [inst : BEq α] [inst : 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
          theorem Std.HashMap.Imp.WF.empty {α : Type u_1} {β : Type u_2} {n : Nat} [inst : BEq α] [inst : Hashable α] :
          def Std.HashMap (α : Type u) (β : Type v) [inst : BEq α] [inst : Hashable α] :
          Type (max0uv)

          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
          @[inline]
          def Std.mkHashMap {α : Type u_1} {β : Type u_2} [inst : BEq α] [inst : Hashable α] (capacity : optParam Nat 0) :

          Make a new hash map with the specified capacity.

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

          Make a new empty hash map.

          Equations
          • Std.HashMap.empty = Std.mkHashMap
          @[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
          @[inline]
          def Std.HashMap.isEmpty {α : Type u_1} :
          {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → Std.HashMap α βBool

          Is the map empty?

          Equations
          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
          @[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↦ b.

          Equations
          @[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
          @[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
          @[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
          @[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
          @[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
          instance Std.HashMap.instGetElemHashMapOptionTrue {α : Type u_1} :
          {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → GetElem (Std.HashMap α β) α (Option β) fun x x => True
          Equations
          • Std.HashMap.instGetElemHashMapOptionTrue = { getElem := fun m k x => Std.HashMap.find? m k }
          @[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
          @[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
          @[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
          @[specialize #[]]
          def Std.HashMap.mergeWithM {α : Type u_1} :
          {x : BEq α} → {x_1 : Hashable α} → {m : Type (maxu_2u_1) → Type u_3} → {β : Type (maxu_2u_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.
          @[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.
          @[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
          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
          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
          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
          def Std.HashMap.ofList {α : Type u_1} :
          {x : BEq α} → {x_1 : Hashable α} → {β : Type u_2} → List (α × β)Std.HashMap α β

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

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

          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.