Skip to content

Commit

Permalink
perf: fix linearity in (HashSet|HashMap).erase
Browse files Browse the repository at this point in the history
  • Loading branch information
hargoniX committed Apr 12, 2024
1 parent 2e3d523 commit 321f8c0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Lean/Data/HashMap.lean
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ def erase [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : HashMapImp α
| ⟨ size, buckets ⟩ =>
let ⟨i, h⟩ := mkIdx (hash a) buckets.property
let bkt := buckets.val[i]
if bkt.contains a then ⟨size - 1, buckets.update i (bkt.erase a) h⟩
else m
if bkt.contains a then
⟨size - 1, buckets.update i (bkt.erase a) h⟩
else
⟨size, buckets⟩

inductive WellFormed [BEq α] [Hashable α] : HashMapImp α β → Prop where
| mkWff : ∀ n, WellFormed (mkHashMapImp n)
Expand Down
6 changes: 4 additions & 2 deletions src/Lean/Data/HashSet.lean
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ def erase [BEq α] [Hashable α] (m : HashSetImp α) (a : α) : HashSetImp α :=
| ⟨ size, buckets ⟩ =>
let ⟨i, h⟩ := mkIdx (hash a) buckets.property
let bkt := buckets.val[i]
if bkt.contains a then ⟨size - 1, buckets.update i (bkt.erase a) h⟩
else m
if bkt.contains a then
⟨size - 1, buckets.update i (bkt.erase a) h⟩
else
⟨size, buckets⟩

inductive WellFormed [BEq α] [Hashable α] : HashSetImp α → Prop where
| mkWff : ∀ n, WellFormed (mkHashSetImp n)
Expand Down

0 comments on commit 321f8c0

Please sign in to comment.