forked from tendermint/tendermint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmap.go
75 lines (63 loc) · 1.08 KB
/
cmap.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
package common
import "sync"
// CMap is a goroutine-safe map
type CMap struct {
m map[string]interface{}
l sync.Mutex
}
func NewCMap() *CMap {
return &CMap{
m: make(map[string]interface{}),
}
}
func (cm *CMap) Set(key string, value interface{}) {
cm.l.Lock()
cm.m[key] = value
cm.l.Unlock()
}
func (cm *CMap) Get(key string) interface{} {
cm.l.Lock()
val := cm.m[key]
cm.l.Unlock()
return val
}
func (cm *CMap) Has(key string) bool {
cm.l.Lock()
_, ok := cm.m[key]
cm.l.Unlock()
return ok
}
func (cm *CMap) Delete(key string) {
cm.l.Lock()
delete(cm.m, key)
cm.l.Unlock()
}
func (cm *CMap) Size() int {
cm.l.Lock()
size := len(cm.m)
cm.l.Unlock()
return size
}
func (cm *CMap) Clear() {
cm.l.Lock()
cm.m = make(map[string]interface{})
cm.l.Unlock()
}
func (cm *CMap) Keys() []string {
cm.l.Lock()
keys := []string{}
for k := range cm.m {
keys = append(keys, k)
}
cm.l.Unlock()
return keys
}
func (cm *CMap) Values() []interface{} {
cm.l.Lock()
items := []interface{}{}
for _, v := range cm.m {
items = append(items, v)
}
cm.l.Unlock()
return items
}