Skip to content

Commit 0f802a9

Browse files
Merge pull request #121 from Workiva/fix_hashmap_deletes
Fix deletion on collision.
2 parents 9f819b6 + d978195 commit 0f802a9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

hashmap/fastinteger/hashmap.go

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ func (packets packets) delete(key uint64) bool {
7777
return false
7878
}
7979
packets[i] = nil
80+
i = (i + 1) & (uint64(len(packets)) - 1)
81+
for packets[i] != nil {
82+
p := packets[i]
83+
packets[i] = nil
84+
packets.set(p)
85+
i = (i + 1) & (uint64(len(packets)) - 1)
86+
}
8087
return true
8188
}
8289

hashmap/fastinteger/hashmap_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ func TestDeleteAll(t *testing.T) {
121121
}
122122
}
123123

124+
func TestDeleteCollision(t *testing.T) {
125+
// 1, 27, 42 all hash to the same value using our hash function % 32
126+
if hash(1)%32 != 12 || hash(27)%32 != 12 || hash(42)%32 != 12 {
127+
t.Error("test values don't hash to the same value")
128+
}
129+
130+
m := New(32)
131+
m.Set(1, 1)
132+
m.Set(27, 27)
133+
m.Set(42, 42)
134+
135+
m.Delete(27)
136+
value, ok := m.Get(42)
137+
assert.True(t, ok)
138+
assert.Equal(t, uint64(42), value)
139+
}
140+
124141
func BenchmarkInsert(b *testing.B) {
125142
numItems := uint64(1000)
126143

0 commit comments

Comments
 (0)