Compare commits

...

2 Commits

Author SHA1 Message Date
Kim Morrison
29e37332cd fix and tests 2024-10-02 12:19:09 +10:00
Kim Morrison
959e4f7478 chore: Singleton/Insert/Union instances for HashMap/Set 2024-10-02 12:00:49 +10:00
5 changed files with 33 additions and 2 deletions

View File

@@ -248,7 +248,7 @@ def getInteractiveGoals (p : Lsp.PlainGoalParams) : RequestM (RequestTask (Optio
let goals ci.runMetaM {} (do
let goals := List.toArray <| if useAfter then ti.goalsAfter else ti.goalsBefore
let goals goals.mapM Widget.goalToInteractive
return {goals}
return goals
)
-- compute the goal diff
ciAfter.runMetaM {} (do

View File

@@ -75,6 +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
@[inline, inherit_doc Raw.insertIfNew] def insertIfNew (m : DHashMap α β)
(a : α) (b : β a) : DHashMap α β :=
Raw₀.insertIfNew m.1, m.2.size_buckets_pos a b, .insertIfNew₀ m.2
@@ -261,6 +264,12 @@ instance [BEq α] [Hashable α] : ForIn m (DHashMap α β) ((a : α) × β a) wh
DHashMap α β :=
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₂ : DHashMap α β) : DHashMap α β :=
m₂.fold (init := m₁) fun acc x => acc.insert x
instance [BEq α] [Hashable α] : Union (DHashMap α β) := union
@[inline, inherit_doc Raw.Const.ofList] def Const.ofList {β : Type v} [BEq α] [Hashable α]
(l : List (α × β)) : DHashMap α (fun _ => β) :=
Const.insertMany l

View File

@@ -76,6 +76,9 @@ instance [BEq α] [Hashable α] : Inhabited (HashMap α β) where
(b : β) : HashMap α β :=
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 α β)
(a : α) (b : β) : HashMap α β :=
m.inner.insertIfNew a b
@@ -251,6 +254,12 @@ instance [BEq α] [Hashable α] {m : Type w → Type w} : ForIn m (HashMap α β
HashMap α β :=
DHashMap.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₂ : HashMap α β) : HashMap α β :=
m₂.fold (init := m₁) fun acc x => acc.insert x
instance [BEq α] [Hashable α] : Union (HashMap α β) := union
@[inline, inherit_doc DHashMap.Const.unitOfList] def unitOfList [BEq α] [Hashable α] (l : List α) :
HashMap α Unit :=
DHashMap.Const.unitOfList l

View File

@@ -77,6 +77,9 @@ equal (with regard to `==`) to the given element, then the hash set is returned
@[inline] def insert (m : HashSet α) (a : α) : HashSet α :=
m.inner.insertIfNew a ()
instance : Singleton α (HashSet α) := fun a => HashSet.empty.insert a
instance : Insert α (HashSet α) := 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
@@ -225,10 +228,12 @@ in the collection will be present in the returned hash set.
@[inline] def ofArray [BEq α] [Hashable α] (l : Array α) : HashSet α :=
HashMap.unitOfArray l
/-- Computes the union of the given hash sets. -/
/-- Computes the union of the given hash sets, by traversing `m₂` and inserting its elements into `m₁`. -/
@[inline] def union [BEq α] [Hashable α] (m₁ m₂ : HashSet α) : HashSet α :=
m₂.fold (init := m₁) fun acc x => acc.insert x
instance [BEq α] [Hashable α] : Union (HashSet α) := 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

View File

@@ -332,6 +332,10 @@ def addValueToState (_ : Nat) (v : Nat) : StateM Nat PUnit := do
#guard_msgs in
#eval HashMap.ofList [(1, 2), (1, 4)]
/-- info: Std.HashMap.ofList [(37, 37), (4, 5), (3, 6), (2, 4), (1, 2)] -/
#guard_msgs in
#eval m {(4, 5), (37, 37)}
end HashMap
namespace HashSet.Raw
@@ -432,6 +436,10 @@ def addKeyToState (k : Nat) : StateM Nat PUnit := do
#guard_msgs in
#eval m.toArray
/-- info: Std.HashSet.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