-
Notifications
You must be signed in to change notification settings - Fork 13
/
map.go
75 lines (60 loc) · 1.46 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
72
73
74
/*=============================================================================
# FileName: map.go
# Author: sunminghong, [email protected], http://weibo.com/5d13
# Team: http://1201.us
# LastChange: 2014-01-01 19:12:23
# History:
=============================================================================*/
package helper
import (
"sync"
)
type LGMap struct {
lock *sync.RWMutex
bm map[int]interface{}
}
func NewLGMap() *LGMap {
return &LGMap{
lock: new(sync.RWMutex),
bm: make(map[int]interface{}),
}
}
//Get from maps return the k's value
func (m *LGMap) Get(k int) (interface{},bool) {
//m.lock.RLock()
//defer m.lock.RUnlock()
val, ok := m.bm[k]
return val,ok
}
// if the key is already in the map and changes nothing.
func (m *LGMap) Set(k int, v interface{}) {
m.lock.Lock()
defer m.lock.Unlock()
if val, ok := m.bm[k]; !ok {
m.bm[k] = v
} else if val != v {
m.bm[k] = v
}
}
func (m *LGMap) All() map[int]interface{} {
return m.bm
}
// Returns true if k is exist in the map.
func (m *LGMap) Check(k int) bool {
//m.lock.RLock()
//defer m.lock.RUnlock()
if _, ok := m.bm[k]; !ok {
return false
}
return true
}
func (m *LGMap) Delete(k int) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.bm, k)
}
func (m *LGMap) Clear() {
m.lock.Lock()
defer m.lock.Unlock()
m.bm = make(map[int]interface{})
}