-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
71 lines (61 loc) · 1.04 KB
/
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
package dmap
import (
"sync"
)
type Map struct {
e []entry
s uint
}
type entry struct {
m map[string][]byte
l sync.RWMutex
}
func NewMap() *Map {
m := new(Map)
m.e = make([]entry, 256)
for i := 0; i < 256; i++ {
m.e[i].m = make(map[string][]byte)
}
return m
}
func (m *Map) Put(key string, value []byte) {
idx := index(key)
m.e[idx].l.Lock()
defer m.e[idx].l.Unlock()
m.e[idx].m[key] = value
}
func (m *Map) Get(key string) []byte {
idx := index(key)
m.e[idx].l.RLock()
defer m.e[idx].l.RUnlock()
return m.e[idx].m[key]
}
func (m *Map) Clear() {
for i := 0; i < 256; i++ {
m.e[i].l.Lock()
for k := range m.e[i].m {
delete(m.e[i].m, k)
}
m.e[i].l.Unlock()
}
}
func (m *Map) Delete(key string) {
idx := index(key)
m.e[idx].l.Lock()
defer m.e[idx].l.Unlock()
delete(m.e[idx].m, key)
}
func (m *Map) Size() int {
var size int
for i := 0; i < 256; i++ {
m.e[i].l.RLock()
size += len(m.e[i].m)
m.e[i].l.RUnlock()
}
return size
}
func index(key string) uint8 {
// c := []byte(key)
// TODO try CRC
return key[0]
}