mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(hashmap): preserve collision chains and bucket indexes (#408)
This commit is contained in:
1 parent
9c401c4043
commit
03695c886c
2 files changed
+190
-49
No files matched your search
@@ -0,0 +1,138 @@
|
|||||||
|
package datastructure_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
type collisionKey string
|
||||||
|
|
||||||
|
func TestHashMapCollisionStorage(t *testing.T) {
|
||||||
|
cases := map[string]func(*testing.T){
|
||||||
|
"distinct colliding keys": func(t *testing.T) {
|
||||||
|
h := hashmap.NewHashMapWithCapacity(0, 1)
|
||||||
|
keys := []any{1, "1", collisionKey("1")}
|
||||||
|
for i, k := range keys {
|
||||||
|
h.Put(k, i+10)
|
||||||
|
}
|
||||||
|
for i, k := range keys {
|
||||||
|
if h.Get(k) != i+10 || !h.Contains(k) {
|
||||||
|
t.Fatalf("lost key %T(%v): %v", k, k, h.Get(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h.Size() != 3 {
|
||||||
|
t.Fatalf("size=%d", h.Size())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"overwrite cardinality": func(t *testing.T) {
|
||||||
|
h := hashmap.NewHashMap()
|
||||||
|
h.Put("same", 1)
|
||||||
|
h.Put("same", 2)
|
||||||
|
if h.Size() != 1 || !reflect.DeepEqual(h.Keys(), []any{"same"}) || !reflect.DeepEqual(h.Values(), []any{2}) {
|
||||||
|
t.Fatalf("overwrite size/keys/values: %d %v %v", h.Size(), h.Keys(), h.Values())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete absent colliding key": func(t *testing.T) {
|
||||||
|
h := hashmap.NewHashMap()
|
||||||
|
h.Put(1, "kept")
|
||||||
|
h.Delete("1")
|
||||||
|
if h.Size() != 1 || h.Get(1) != "kept" {
|
||||||
|
t.Fatalf("absent delete removed another key: size=%d get=%v", h.Size(), h.Get(1))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete every chain position": func(t *testing.T) {
|
||||||
|
for _, remove := range []any{1, "1", collisionKey("1")} {
|
||||||
|
h := hashmap.NewHashMapWithCapacity(0, 4)
|
||||||
|
keys := []any{1, "1", collisionKey("1")}
|
||||||
|
for i, k := range keys {
|
||||||
|
h.Put(k, i)
|
||||||
|
}
|
||||||
|
h.Delete(remove)
|
||||||
|
if h.Contains(remove) || h.Size() != 2 {
|
||||||
|
t.Fatalf("deletion failed: %T size=%d", remove, h.Size())
|
||||||
|
}
|
||||||
|
for i, k := range keys {
|
||||||
|
if !reflect.DeepEqual(k, remove) && h.Get(k) != i {
|
||||||
|
t.Fatalf("delete lost other key: %T", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete preserves bucket indexes": func(t *testing.T) {
|
||||||
|
h := hashmap.NewHashMap()
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
h.Put(fmt.Sprintf("key-%d", i), i)
|
||||||
|
}
|
||||||
|
for i := 0; i < 100; i += 2 {
|
||||||
|
h.Delete(fmt.Sprintf("key-%d", i))
|
||||||
|
}
|
||||||
|
for i := 1; i < 100; i += 2 {
|
||||||
|
if h.Get(fmt.Sprintf("key-%d", i)) != i {
|
||||||
|
t.Fatalf("lost untouched bucket key %d", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h.Size() != 50 {
|
||||||
|
t.Fatalf("size=%d", h.Size())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"resize and enumerate chains": func(t *testing.T) {
|
||||||
|
h := hashmap.NewHashMapWithCapacity(0, 1)
|
||||||
|
h.Put(1, 1)
|
||||||
|
h.Put("1", 2)
|
||||||
|
h.Put(collisionKey("1"), 3)
|
||||||
|
for i := 2; i <= 64; i++ {
|
||||||
|
h.Put(i, i)
|
||||||
|
}
|
||||||
|
seen := map[string]any{}
|
||||||
|
h.Iterate(func(k, v any) { seen[fmt.Sprintf("%T:%v", k, k)] = v })
|
||||||
|
if h.Size() != 66 || len(seen) != 66 || len(h.Keys()) != 66 || len(h.Values()) != 66 {
|
||||||
|
t.Fatalf("size=%d enumerated=%d", h.Size(), len(seen))
|
||||||
|
}
|
||||||
|
if seen["string:1"] != 2 || h.Get(collisionKey("1")) != 3 {
|
||||||
|
t.Fatal("collision chain lost during growth")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"filter colliding values": func(t *testing.T) {
|
||||||
|
h := hashmap.NewHashMapWithCapacity(0, 4)
|
||||||
|
h.Put(1, 1)
|
||||||
|
h.Put("1", 2)
|
||||||
|
h.Put(collisionKey("1"), 3)
|
||||||
|
f := h.FilterByValue(func(v any) bool { return v.(int)%2 == 1 })
|
||||||
|
if f == nil || f.Size() != 2 || f.Get(1) != 1 || f.Get(collisionKey("1")) != 3 || f.Contains("1") {
|
||||||
|
t.Fatal("filter skipped or corrupted collision chain")
|
||||||
|
}
|
||||||
|
if h.FilterByValue(func(any) bool { return false }) != nil {
|
||||||
|
t.Fatal("empty-filter nil contract changed")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"zero capacity and deep equal keys": func(t *testing.T) {
|
||||||
|
h := hashmap.NewHashMapWithCapacity(0, 0)
|
||||||
|
if h.Get("missing") != nil || h.Contains("missing") {
|
||||||
|
t.Fatal("empty lookup")
|
||||||
|
}
|
||||||
|
h.Delete("missing")
|
||||||
|
h.Put([]int{1, 2}, "first")
|
||||||
|
h.Put([]int{1, 2}, "second")
|
||||||
|
if h.Get([]int{1, 2}) != "second" || h.Size() != 1 {
|
||||||
|
t.Fatal("deep equal overwrite failed")
|
||||||
|
}
|
||||||
|
h.Delete([]int{1, 2})
|
||||||
|
if h.Size() != 0 {
|
||||||
|
t.Fatal("deep equal delete failed")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for name, fn := range cases {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
t.Fatalf("public map operation panicked: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
fn(t)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,6 +44,9 @@ func NewHashMapWithCapacity(size, capacity uint64) *HashMap {
|
|||||||
|
|
||||||
// Get return the value of given key in hashmap
|
// Get return the value of given key in hashmap
|
||||||
func (hm *HashMap) Get(key any) any {
|
func (hm *HashMap) Get(key any) any {
|
||||||
|
if hm.size == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
hashValue := hm.hash(key)
|
hashValue := hm.hash(key)
|
||||||
node := hm.table[hashValue]
|
node := hm.table[hashValue]
|
||||||
for node != nil {
|
for node != nil {
|
||||||
@@ -66,41 +69,55 @@ func (hm *HashMap) GetOrDefault(key any, defaultValue any) any {
|
|||||||
|
|
||||||
// Put new key value in hashmap
|
// Put new key value in hashmap
|
||||||
func (hm *HashMap) Put(key any, value any) {
|
func (hm *HashMap) Put(key any, value any) {
|
||||||
hm.putValue(hm.hash(key), key, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hm *HashMap) putValue(hash uint64, key, value any) {
|
|
||||||
if hm.capacity == 0 {
|
if hm.capacity == 0 {
|
||||||
hm.capacity = defaultMapCapacity
|
hm.capacity = defaultMapCapacity
|
||||||
hm.table = make([]*mapNode, defaultMapCapacity)
|
hm.table = make([]*mapNode, defaultMapCapacity)
|
||||||
}
|
}
|
||||||
|
hm.putValue(hm.hash(key), key, value)
|
||||||
|
}
|
||||||
|
|
||||||
node := hm.table[hash]
|
func (hm *HashMap) putValue(hash uint64, key, value any) {
|
||||||
if node == nil {
|
for node := hm.table[hash]; node != nil; node = node.next {
|
||||||
hm.table[hash] = newMapNode(key, value)
|
if reflect.DeepEqual(node.key, key) {
|
||||||
} else if node.key == key {
|
node.value = value
|
||||||
hm.table[hash] = newMapNodeWithNext(key, value, node)
|
return
|
||||||
} else {
|
}
|
||||||
hm.resize()
|
|
||||||
hm.putValue(hash, value, value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if hm.size >= hm.capacity {
|
||||||
|
hm.resize()
|
||||||
|
hash = hm.hash(key)
|
||||||
|
}
|
||||||
|
hm.table[hash] = newMapNodeWithNext(key, value, hm.table[hash])
|
||||||
hm.size++
|
hm.size++
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete item by given key in hashmap
|
// Delete item by given key in hashmap
|
||||||
func (hm *HashMap) Delete(key any) {
|
func (hm *HashMap) Delete(key any) {
|
||||||
hash := hm.hash(key)
|
if hm.size == 0 {
|
||||||
node := hm.table[hash]
|
|
||||||
if node == nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
hash := hm.hash(key)
|
||||||
hm.table = append(hm.table[:hash], hm.table[hash+1:]...)
|
var previous *mapNode
|
||||||
hm.size--
|
for node := hm.table[hash]; node != nil; node = node.next {
|
||||||
|
if reflect.DeepEqual(node.key, key) {
|
||||||
|
if previous == nil {
|
||||||
|
hm.table[hash] = node.next
|
||||||
|
} else {
|
||||||
|
previous.next = node.next
|
||||||
|
}
|
||||||
|
hm.size--
|
||||||
|
return
|
||||||
|
}
|
||||||
|
previous = node
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contains checks if given key is in hashmap or not
|
// Contains checks if given key is in hashmap or not
|
||||||
func (hm *HashMap) Contains(key any) bool {
|
func (hm *HashMap) Contains(key any) bool {
|
||||||
|
if hm.size == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
node := hm.table[hm.hash(key)]
|
node := hm.table[hm.hash(key)]
|
||||||
for node != nil {
|
for node != nil {
|
||||||
if reflect.DeepEqual(node.key, key) {
|
if reflect.DeepEqual(node.key, key) {
|
||||||
@@ -113,12 +130,10 @@ func (hm *HashMap) Contains(key any) bool {
|
|||||||
|
|
||||||
// Iterate executes iteratee funcation for every key and value pair of hashmap (random order)
|
// Iterate executes iteratee funcation for every key and value pair of hashmap (random order)
|
||||||
func (hm *HashMap) Iterate(iteratee func(key, value any)) {
|
func (hm *HashMap) Iterate(iteratee func(key, value any)) {
|
||||||
if hm.size > 0 {
|
for _, node := range hm.table {
|
||||||
for i := 0; i < len(hm.table); i++ {
|
for node != nil {
|
||||||
item := hm.table[i]
|
iteratee(node.key, node.value)
|
||||||
if item != nil {
|
node = node.next
|
||||||
iteratee(item.key, item.value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,17 +143,14 @@ func (hm *HashMap) Iterate(iteratee func(key, value any)) {
|
|||||||
// otherwise it returns the HashMap with selected values.
|
// otherwise it returns the HashMap with selected values.
|
||||||
func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap {
|
func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap {
|
||||||
var filteredHM *HashMap
|
var filteredHM *HashMap
|
||||||
if hm.size > 0 {
|
hm.Iterate(func(key, value any) {
|
||||||
for i := 0; i < len(hm.table); i++ {
|
if perdicate(value) {
|
||||||
item := hm.table[i]
|
if filteredHM == nil {
|
||||||
if item != nil && perdicate(item.value) {
|
filteredHM = NewHashMap()
|
||||||
if filteredHM == nil {
|
|
||||||
filteredHM = NewHashMap()
|
|
||||||
}
|
|
||||||
filteredHM.Put(item.key, item.value)
|
|
||||||
}
|
}
|
||||||
|
filteredHM.Put(key, value)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
return filteredHM
|
return filteredHM
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,18 +184,16 @@ func (hm *HashMap) Values() []any {
|
|||||||
|
|
||||||
func (hm *HashMap) resize() {
|
func (hm *HashMap) resize() {
|
||||||
hm.capacity <<= 1
|
hm.capacity <<= 1
|
||||||
|
|
||||||
tempTable := hm.table
|
tempTable := hm.table
|
||||||
|
|
||||||
hm.table = make([]*mapNode, hm.capacity)
|
hm.table = make([]*mapNode, hm.capacity)
|
||||||
|
for _, node := range tempTable {
|
||||||
for i := 0; i < len(tempTable); i++ {
|
for node != nil {
|
||||||
node := tempTable[i]
|
next := node.next
|
||||||
if node == nil {
|
hash := hm.hash(node.key)
|
||||||
continue
|
node.next = hm.table[hash]
|
||||||
|
hm.table[hash] = node
|
||||||
|
node = next
|
||||||
}
|
}
|
||||||
|
|
||||||
hm.table[hm.hash(node.key)] = node
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,13 +211,6 @@ func (hm *HashMap) hash(key any) uint64 {
|
|||||||
return (hm.capacity - 1) & (hashValue ^ (hashValue >> 16))
|
return (hm.capacity - 1) & (hashValue ^ (hashValue >> 16))
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMapNode(key, value any) *mapNode {
|
|
||||||
return &mapNode{
|
|
||||||
key: key,
|
|
||||||
value: value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newMapNodeWithNext(key, value any, next *mapNode) *mapNode {
|
func newMapNodeWithNext(key, value any, next *mapNode) *mapNode {
|
||||||
return &mapNode{
|
return &mapNode{
|
||||||
key: key,
|
key: key,
|
||||||
|
|||||||
Reference in new issue
Block a user