Skip to content

Commit

Permalink
Slightly more efficient hashmap remove
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Apr 13, 2022
1 parent 7383fd2 commit b42c923
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions hashmap/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (m *Map[K, V]) Remove(key K) {
idx := hash & (m.capacity - 1)

for m.entries[idx].filled && !m.ops.equals(m.entries[idx].key, key) {
idx = (idx + 1) % m.capacity
idx = (idx + 1) & (m.capacity - 1)
}

if !m.entries[idx].filled {
Expand All @@ -148,13 +148,13 @@ func (m *Map[K, V]) Remove(key K) {

m.remove(idx)

idx = (idx + 1) % m.capacity
idx = (idx + 1) & (m.capacity - 1)
for m.entries[idx].filled {
krehash := m.entries[idx].key
vrehash := m.entries[idx].value
m.remove(idx)
m.Put(krehash, vrehash)
idx = (idx + 1) % m.capacity
idx = (idx + 1) & (m.capacity - 1)
}

// halves the array if it is 12.5% full or less
Expand Down
2 changes: 1 addition & 1 deletion hashmap/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func checkeq[K any, V comparable](cm *hashmap.Map[K, V], get func(k K) (V, bool)

func TestCrossCheck(t *testing.T) {
stdm := make(map[uint64]uint32)
cowm := hashmap.New[uint64, uint32](1, g.Equals[uint64], g.HashUint64)
cowm := hashmap.New[uint64, uint32](uint64(rand.Intn(1024)), g.Equals[uint64], g.HashUint64)

const nops = 1000

Expand Down

0 comments on commit b42c923

Please sign in to comment.