-
Notifications
You must be signed in to change notification settings - Fork 153
/
16_map_hash.go
182 lines (165 loc) · 4.23 KB
/
16_map_hash.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital
// networks and storage devices to detect accidental changes to raw data.
//
// https://github.com/golang/go/blob/master/src/hash/crc32/crc32.go
// https://github.com/golang/go/blob/master/src/runtime/hashmap.go
// A map is just a hash table. The data is arranged
// into an array of buckets. Each bucket contains up to
// 8 key/value pairs. The low-order bits of the hash are
// used to select a bucket. Each bucket contains a few
// high-order bits of each hash to distinguish the entries
// within a single bucket.
//
// If more than 8 keys hash to a bucket, we chain on
// extra buckets.
//
// When the hashtable grows, we allocate a new array
// of buckets twice as big. Buckets are incrementally
// copied from the old bucket array to the new bucket array.
package main
import (
"bytes"
"fmt"
"hash/crc32"
"strings"
)
func main() {
// hash table using array
ht := newHashTable()
for _, elem := range strings.Split("aaaaaaaaaaabbbbcdeftghiklmnopr", "") {
ht.insert([]byte(elem))
}
for _, bucket := range ht.bucketSlice {
fmt.Println(bucket)
}
/*
&{true [[102] [98] [101] [97] [100] [116] [103] [99]]}
&{true [[109] [105] [110] [112] [111] [107] [108] [104]]}
&{false [[] [] [] [] [] [114] [] []]}
*/
fmt.Println(ht.search([]byte("f"))) // true
fmt.Println(ht.search([]byte("x"))) // false
}
func hashFuncCrc32(val []byte) uint32 {
// crc64.Checksum(val, crc64.MakeTable(crc64.ISO))
return crc32.Checksum(val, crc32.MakeTable(crc32.IEEE))
}
func hashFunc(val []byte) uint32 {
return checksum(val, makePolyTable(crc32.IEEE))
}
// polyTable is a 256-word table representing the polynomial for efficient processing.
type polyTable [256]uint32
func makePolyTable(poly uint32) *polyTable {
t := new(polyTable)
for i := 0; i < 256; i++ {
crc := uint32(i)
for j := 0; j < 8; j++ {
if crc&1 == 1 {
crc = (crc >> 1) ^ poly
} else {
crc >>= 1
}
}
t[i] = crc
}
return t
}
// checksum returns the CRC-32 checksum of data
// using the polynomial represented by the polyTable.
func checksum(data []byte, tab *polyTable) uint32 {
crc := ^uint32(0)
for _, v := range data {
crc = tab[byte(crc)^v] ^ (crc >> 8)
}
return ^crc
}
const (
bucketCntBits = 3
bucketCnt = 1 << bucketCntBits // 8, Maximum number of key/value pairs a bucket can hold
)
type hashTable struct {
bucketSlice []*bucket
}
func newHashTable() *hashTable {
table := new(hashTable)
// table.bucketSlice = make([]*bucket, hashTableSize)
table.bucketSlice = []*bucket{}
return table
}
type bucket struct {
wrapped bool // already wrapped around from end of bucket array to beginning
data [bucketCnt][]byte
// type byteData []byte
// []byte == []uint8
}
func newBucket() *bucket {
newBucket := &bucket{}
newBucket.wrapped = false
newBucket.data = [bucketCnt][]byte{}
return newBucket
}
func (h *hashTable) search(val []byte) bool {
if len(h.bucketSlice) == 0 {
return false
}
probeIdx := hashFunc(val) % uint32(bucketCnt)
for _, bucket := range h.bucketSlice {
// check the probeIdx
if bucket.data[probeIdx] != nil {
if bytes.Equal(bucket.data[probeIdx], val) {
return true
}
}
// linear probe
for idx, elem := range bucket.data {
if uint32(idx) == probeIdx {
continue
}
if bytes.Equal(elem, val) {
return true
}
}
}
return false
}
// hashFunc -> probeIdx -> linear probe to fill up bucket
func (h *hashTable) insert(val []byte) {
if h.search(val) {
return
}
if len(h.bucketSlice) == 0 {
h.bucketSlice = append(h.bucketSlice, newBucket())
}
probeIdx := hashFunc(val) % uint32(bucketCnt)
isInserted := false
Loop:
for _, bucket := range h.bucketSlice {
// if the bucket is already full, skip it
if bucket.wrapped {
continue
}
// if the index is not taken yet, map it
if bucket.data[probeIdx] == nil {
bucket.data[probeIdx] = val
isInserted = true
break
}
// linear probe
for idx, elem := range bucket.data {
if uint32(idx) == probeIdx {
continue
}
if elem == nil {
bucket.data[idx] = val
isInserted = true
break Loop
}
}
bucket.wrapped = true
}
if !isInserted {
nb := newBucket()
nb.data[probeIdx] = val
h.bucketSlice = append(h.bucketSlice, nb)
}
}