This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/
hash_table.go
127 lines (113 loc) · 2.35 KB
/
hash_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* go-mysqlstack
* xelabs.org
*
* Copyright (c) XeLabs
* GPL License
*
*/
package common
import (
"bytes"
"hash/fnv"
)
// hash64a used to get bucket.
func hash64a(data []byte) uint64 {
h := fnv.New64a()
h.Write(data)
return h.Sum64()
}
type entry struct {
// key slice.
key []byte
// value interface.
value []interface{}
// point to the next entry.
next *entry
}
func (e *entry) put(key []byte, value interface{}) *entry {
if e == nil {
return &entry{key, []interface{}{value}, nil}
}
if bytes.Equal(e.key, key) {
e.value = append(e.value, value)
return e
}
e.next = e.next.put(key, value)
return e
}
func (e *entry) get(key []byte) (bool, []interface{}) {
if e == nil {
return false, nil
} else if bytes.Equal(e.key, key) {
return true, e.value
} else {
return e.next.get(key)
}
}
// HashTable the hash table.
type HashTable struct {
// stores value for a given key.
hashEntry []*entry
// k: bucket. v: index in the hashEntry.
hashMap map[uint64]int
// size of entrys.
size int
}
// NewHashTable create hash table.
func NewHashTable() *HashTable {
return &HashTable{
hashMap: make(map[uint64]int),
size: 0,
}
}
// Size used to get the hashtable size.
func (h *HashTable) Size() int {
return h.size
}
// Put puts the key/value pairs to the HashTable.
func (h *HashTable) Put(key []byte, value interface{}) {
var table *entry
bucket := hash64a(key)
index, ok := h.hashMap[bucket]
if !ok {
table = &entry{key, []interface{}{value}, nil}
h.hashMap[bucket] = len(h.hashEntry)
h.hashEntry = append(h.hashEntry, table)
} else {
h.hashEntry[index] = h.hashEntry[index].put(key, value)
}
h.size++
}
// Get gets the values of the "key".
func (h *HashTable) Get(key []byte) (bool, []interface{}) {
bucket := hash64a(key)
index, ok := h.hashMap[bucket]
if !ok {
return false, nil
}
return h.hashEntry[index].get(key)
}
// Iterator used to iterate the HashTable.
type Iterator func() (key []byte, value []interface{}, next Iterator)
// Next used to iterate the HashTable.
func (h *HashTable) Next() Iterator {
var e *entry
var iter Iterator
table := h.hashEntry
i := -1
iter = func() (key []byte, val []interface{}, next Iterator) {
for e == nil {
i++
if i >= len(table) {
return nil, nil, nil
}
e = table[i]
}
key = e.key
val = e.value
e = e.next
return key, val, iter
}
return iter
}