-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfast_map.go
162 lines (144 loc) · 3.08 KB
/
fast_map.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
package fast_hashmap
import (
"container/list"
)
func hashCode(str string) int {
h := 0
bytes := []byte(str)
for i := 0; i < len(bytes); i++ {
h = h*31 + int(bytes[i])
}
return h
}
func indexFor(h int, capacity int) int {
return h & (capacity - 1)
}
type MapWithStringKey interface {
Put(key string, v interface{})
Get(key string) (interface{}, bool)
}
type Entry struct {
Key string
Value interface{}
lenOfKey int
}
type BucketWithSlice struct {
entries []*Entry
}
type BucketWithList struct {
entryList *list.List
}
type FasterMap struct {
buckets []*BucketWithSlice
length int
capacity int
numOfCollisions int
}
type FastMap struct {
buckets []*BucketWithList
length int
capacity int
}
func calTableCapacity(length int) int {
capacity := 1
for capacity < length*3 {
capacity <<= 1
}
return capacity
}
func NewFasterMap(length int) *FasterMap {
capacity := calTableCapacity(length)
buckets := make([]*BucketWithSlice, capacity, capacity)
return &FasterMap{
buckets,
length,
capacity,
0,
}
}
func (m *FasterMap) Put(key string, v interface{}) {
isFirstBucket := false
hCode := hashCode(key)
pos := indexFor(hCode, m.capacity)
bucket := m.buckets[pos]
if bucket == nil {
entries := make([]*Entry, 0, 10)
m.buckets[pos] = &BucketWithSlice{entries}
isFirstBucket = true
}
for _, entry := range m.buckets[pos].entries {
if entry.Key == key {
entry.Value = v
return
}
}
m.buckets[pos].entries = append(m.buckets[pos].entries, &Entry{key, v, len(key)})
if !isFirstBucket {
m.numOfCollisions += 1
}
}
func (m *FasterMap) Get(key string) (interface{}, bool) {
hCode := hashCode(key)
pos := indexFor(hCode, m.capacity)
bucket := m.buckets[pos]
if bucket == nil {
return nil, false
}
if len(bucket.entries) == 1 {
return bucket.entries[0].Value, true
}
var lenOfKey int
for _, entry := range bucket.entries {
lenOfKey = len(key)
if lenOfKey == entry.lenOfKey && entry.Key == key {
return entry.Value, true
}
}
panic("Fatal for hash bucket")
}
func NewFastMap(length int) *FastMap {
capacity := calTableCapacity(length)
buckets := make([]*BucketWithList, capacity, capacity)
return &FastMap{
buckets,
length,
capacity,
}
}
func (m *FastMap) Put(key string, v interface{}) {
hCode := hashCode(key)
pos := indexFor(hCode, m.capacity)
bucket := m.buckets[pos]
if bucket == nil {
m.buckets[pos] = &BucketWithList{list.New()}
}
for i := m.buckets[pos].entryList.Front(); i != nil; i = i.Next() {
entry, ok := (i.Value).(*Entry)
if !ok {
panic("Wrong entry")
}
if entry.Key == key {
entry.Value = v
return
}
}
m.buckets[pos].entryList.PushBack(&Entry{key, v, len(key)})
}
func (m *FastMap) Get(key string) (interface{}, bool) {
hCode := hashCode(key)
pos := indexFor(hCode, m.capacity)
bucket := m.buckets[pos]
if bucket == nil {
return nil, false
}
for i := bucket.entryList.Front(); i != nil; i = i.Next() {
entry, ok := (i.Value).(*Entry)
if !ok {
panic("Wrong entry")
}
if entry.Key == key {
return entry.Value, true
}
}
panic("Fatal for hash bucket")
}