-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapsafestrings.go
89 lines (78 loc) · 1.73 KB
/
mapsafestrings.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
package shoset
import (
// "fmt"
// "fmt"
"sync"
)
// MapSafeStrings : simple key map safe for goroutines...
type MapSafeStrings struct {
m map[string]map[string]bool
sync.Mutex
}
// NewMapSafeStrings : constructor
func NewMapSafeStrings() *MapSafeStrings {
m := new(MapSafeStrings)
m.m = make(map[string]map[string]bool)
return m
}
// func (m *MapSafeStrings) String() string {
// var descr string
// for key, lNames := range m.m {
// descr = descr + fmt.Sprintf("%s key : %s, lName : ", descr, key)
// for lName := range lNames {
// descr = fmt.Sprintf("%s %s", descr, lName)
// }
// descr = descr + "} \n\t\t\t"
// }
// return descr
// }
// Get : Get a value from a MapSafeStrings
func (m *MapSafeStrings) Get(key string) map[string]bool {
m.Lock()
defer m.Unlock()
return m.m[key]
}
func (m *MapSafeStrings) Set(key, value string) {
m.Lock()
defer m.Unlock()
if m.m[key] == nil {
m.m[key] = make(map[string]bool)
}
m.m[key][value] = true
}
// Delete : delete a value in a MapSafeStrings
func (m *MapSafeStrings) Delete(key string) {
m.Lock()
_, ok := m.m[key]
if ok {
delete(m.m, key)
}
m.Unlock()
}
// Iterate : iterate through MapSafeStrings Values using a function
func (m *MapSafeStrings) Iterate(iter func(string, map[string]bool)) {
m.Lock()
for key, val := range m.m {
iter(key, val)
}
m.Unlock()
}
// Len : return length of the map
func (m *MapSafeStrings) Len() int {
return len(m.m)
}
func (m *MapSafeStrings) Keys(key string) []string {
m.Lock()
defer m.Unlock()
return m._keys(key)
}
func (m *MapSafeStrings) _keys(key string) []string {
lNamesByType := m.m[key]
lNames := make([]string, m.Len())
i := 0
for lName := range lNamesByType {
lNames[i] = lName
i++
}
return lNames[:i]
}