diff --git a/src/Std/Data/DHashMap/Basic.lean b/src/Std/Data/DHashMap/Basic.lean index 94308f93c85b..89d5d9e294b2 100644 --- a/src/Std/Data/DHashMap/Basic.lean +++ b/src/Std/Data/DHashMap/Basic.lean @@ -75,8 +75,9 @@ instance [BEq α] [Hashable α] : Inhabited (DHashMap α β) where (b : β a) : DHashMap α β := ⟨Raw₀.insert ⟨m.1, m.2.size_buckets_pos⟩ a b, .insert₀ m.2⟩ -instance : Singleton (Σ a, β a) (DHashMap α β) := ⟨fun ⟨a, b⟩ => DHashMap.empty.insert a b⟩ -instance : Insert (Σ a, β a) (DHashMap α β) := ⟨fun ⟨a, b⟩ s => s.insert a b⟩ +instance : Singleton ((a : α) × β a) (DHashMap α β) := ⟨fun ⟨a, b⟩ => DHashMap.empty.insert a b⟩ + +instance : Insert ((a : α) × β a) (DHashMap α β) := ⟨fun ⟨a, b⟩ s => s.insert a b⟩ @[inline, inherit_doc Raw.insertIfNew] def insertIfNew (m : DHashMap α β) (a : α) (b : β a) : DHashMap α β := diff --git a/src/Std/Data/DHashMap/Raw.lean b/src/Std/Data/DHashMap/Raw.lean index 34a04564cec6..d119ee310633 100644 --- a/src/Std/Data/DHashMap/Raw.lean +++ b/src/Std/Data/DHashMap/Raw.lean @@ -65,6 +65,12 @@ Inserts the given mapping into the map, replacing an existing mapping for the ke (Raw₀.insert ⟨m, h⟩ a b).1 else m -- will never happen for well-formed inputs +instance [BEq α] [Hashable α] : Singleton ((a : α) × β a) (Raw α β) := + ⟨fun ⟨a, b⟩ => Raw.empty.insert a b⟩ + +instance [BEq α] [Hashable α] : Insert ((a : α) × β a) (Raw α β) := + ⟨fun ⟨a, b⟩ s => s.insert a b⟩ + /-- If there is no mapping for the given key, inserts the given mapping into the map. Otherwise, returns the map unaltered. @@ -399,6 +405,12 @@ occurrence takes precedence. -/ @[inline] def ofList [BEq α] [Hashable α] (l : List ((a : α) × β a)) : Raw α β := insertMany ∅ l +/-- Computes the union of the given hash maps, by traversing `m₂` and inserting its elements into `m₁`. -/ +@[inline] def union [BEq α] [Hashable α] (m₁ m₂ : Raw α β) : Raw α β := + m₂.fold (init := m₁) fun acc x => acc.insert x + +instance [BEq α] [Hashable α] : Union (Raw α β) := ⟨union⟩ + @[inline, inherit_doc Raw.ofList] def Const.ofList {β : Type v} [BEq α] [Hashable α] (l : List (α × β)) : Raw α (fun _ => β) := Const.insertMany ∅ l diff --git a/src/Std/Data/HashMap/Basic.lean b/src/Std/Data/HashMap/Basic.lean index d676bdd696a1..24ba2e12b976 100644 --- a/src/Std/Data/HashMap/Basic.lean +++ b/src/Std/Data/HashMap/Basic.lean @@ -77,6 +77,7 @@ instance [BEq α] [Hashable α] : Inhabited (HashMap α β) where ⟨m.inner.insert a b⟩ instance : Singleton (α × β) (HashMap α β) := ⟨fun ⟨a, b⟩ => HashMap.empty.insert a b⟩ + instance : Insert (α × β) (HashMap α β) := ⟨fun ⟨a, b⟩ s => s.insert a b⟩ @[inline, inherit_doc DHashMap.insertIfNew] def insertIfNew (m : HashMap α β) diff --git a/src/Std/Data/HashMap/Raw.lean b/src/Std/Data/HashMap/Raw.lean index afaf81004185..0bdc7dae59a3 100644 --- a/src/Std/Data/HashMap/Raw.lean +++ b/src/Std/Data/HashMap/Raw.lean @@ -74,6 +74,10 @@ set_option linter.unusedVariables false in (a : α) (b : β) : Raw α β := ⟨m.inner.insert a b⟩ +instance [BEq α] [Hashable α] : Singleton (α × β) (Raw α β) := ⟨fun ⟨a, b⟩ => Raw.empty.insert a b⟩ + +instance [BEq α] [Hashable α] : Insert (α × β) (Raw α β) := ⟨fun ⟨a, b⟩ s => s.insert a b⟩ + @[inline, inherit_doc DHashMap.Raw.insertIfNew] def insertIfNew [BEq α] [Hashable α] (m : Raw α β) (a : α) (b : β) : Raw α β := ⟨m.inner.insertIfNew a b⟩ @@ -231,10 +235,20 @@ m.inner.values (l : List (α × β)) : Raw α β := ⟨DHashMap.Raw.Const.ofList l⟩ +/-- Computes the union of the given hash maps, by traversing `m₂` and inserting its elements into `m₁`. -/ +@[inline] def union [BEq α] [Hashable α] (m₁ m₂ : Raw α β) : Raw α β := + m₂.fold (init := m₁) fun acc x => acc.insert x + +instance [BEq α] [Hashable α] : Union (Raw α β) := ⟨union⟩ + @[inline, inherit_doc DHashMap.Raw.Const.unitOfList] def unitOfList [BEq α] [Hashable α] (l : List α) : Raw α Unit := ⟨DHashMap.Raw.Const.unitOfList l⟩ +@[inline, inherit_doc DHashMap.Raw.Const.unitOfArray] def unitOfArray [BEq α] [Hashable α] + (l : Array α) : Raw α Unit := + ⟨DHashMap.Raw.Const.unitOfArray l⟩ + @[inherit_doc DHashMap.Raw.Internal.numBuckets] def Internal.numBuckets (m : Raw α β) : Nat := DHashMap.Raw.Internal.numBuckets m.inner diff --git a/src/Std/Data/HashSet/Basic.lean b/src/Std/Data/HashSet/Basic.lean index dedad907249b..762ea2f11306 100644 --- a/src/Std/Data/HashSet/Basic.lean +++ b/src/Std/Data/HashSet/Basic.lean @@ -78,6 +78,7 @@ equal (with regard to `==`) to the given element, then the hash set is returned ⟨m.inner.insertIfNew a ()⟩ instance : Singleton α (HashSet α) := ⟨fun a => HashSet.empty.insert a⟩ + instance : Insert α (HashSet α) := ⟨fun a s => s.insert a⟩ /-- diff --git a/src/Std/Data/HashSet/Raw.lean b/src/Std/Data/HashSet/Raw.lean index 88cab2ef3fc6..cf1edf9b10cd 100644 --- a/src/Std/Data/HashSet/Raw.lean +++ b/src/Std/Data/HashSet/Raw.lean @@ -78,6 +78,10 @@ equal (with regard to `==`) to the given element, then the hash set is returned @[inline] def insert [BEq α] [Hashable α] (m : Raw α) (a : α) : Raw α := ⟨m.inner.insertIfNew a ()⟩ +instance [BEq α] [Hashable α] : Singleton α (Raw α) := ⟨fun a => Raw.empty.insert a⟩ + +instance [BEq α] [Hashable α] : Insert α (Raw α) := ⟨fun a s => s.insert a⟩ + /-- Checks whether an element is present in a set and inserts the element if it was not found. If the hash set already contains an element that is equal (with regard to `==`) to the given @@ -213,6 +217,20 @@ in the collection will be present in the returned hash set. @[inline] def ofList [BEq α] [Hashable α] (l : List α) : Raw α := ⟨HashMap.Raw.unitOfList l⟩ +/-- +Creates a hash set from an array of elements. Note that unlike repeatedly calling `insert`, if the +collection contains multiple elements that are equal (with regard to `==`), then the last element +in the collection will be present in the returned hash set. +-/ +@[inline] def ofArray [BEq α] [Hashable α] (l : Array α) : Raw α := + ⟨HashMap.Raw.unitOfArray l⟩ + +/-- Computes the union of the given hash sets, by traversing `m₂` and inserting its elements into `m₁`. -/ +@[inline] def union [BEq α] [Hashable α] (m₁ m₂ : Raw α) : Raw α := + m₂.fold (init := m₁) fun acc x => acc.insert x + +instance [BEq α] [Hashable α] : Union (Raw α) := ⟨union⟩ + /-- Returns the number of buckets in the internal representation of the hash set. This function may be useful for things like monitoring system health, but it should be considered an internal diff --git a/tests/lean/run/hashmap.lean b/tests/lean/run/hashmap.lean index 774c9692068a..936705c8bcd7 100644 --- a/tests/lean/run/hashmap.lean +++ b/tests/lean/run/hashmap.lean @@ -382,6 +382,10 @@ def addKeyToState (k : Nat) : StateM Nat PUnit := do #guard_msgs in #eval m.toArray +/-- info: Std.HashSet.Raw.ofList [1000000000, 2, 1, 16] -/ +#guard_msgs in +#eval m ∪ {16, 16} + /-- info: [1000000000, 2, 1, 16] -/ #guard_msgs in #eval (m.insertMany [16, 16]).toList