-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti_map.go
86 lines (73 loc) · 1.65 KB
/
multi_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
package maps
// NewMultiMap creates a new multi map
func NewMultiMap() *MultiMap {
return &MultiMap{
internalMap: make(map[string][]interface{}),
}
}
// MultiMap map
type MultiMap struct {
internalMap map[string][]interface{}
}
// Set set to map
func (c *MultiMap) Set(key string, values []interface{}) {
c.internalMap[key] = values
}
// Append append to map
func (c *MultiMap) Append(key string, value interface{}) {
values, ok := c.internalMap[key]
if !ok {
values = []interface{}{}
}
values = append(values, value)
c.internalMap[key] = values
}
// Get get from map
func (c *MultiMap) Get(key string) ([]interface{}, bool) {
value, ok := c.internalMap[key]
return value, ok
}
// Remove remove from map
func (c *MultiMap) Remove(key string) {
delete(c.internalMap, key)
}
// ContainsKey contains key in map
func (c *MultiMap) ContainsKey(key string) bool {
_, ok := c.Get(key)
return ok
}
// ContainsEntry concurrent contains entry in map
func (c *MultiMap) ContainsEntry(key string, value interface{}) bool {
existingValues, ok := c.Get(key)
if !ok {
return false
}
for _, existingValue := range existingValues {
if existingValue == value {
return true
}
}
return false
}
// Size size of map
func (c *MultiMap) Size() int {
return len(c.internalMap)
}
// IsEmpty check of map's emptiness
func (c *MultiMap) IsEmpty() bool {
return c.Size() == 0
}
// Keys retrieval of keys from map
func (c *MultiMap) Keys() []string {
keys := make([]string, len(c.internalMap))
i := 0
for key := range c.internalMap {
keys[i] = key
i++
}
return keys
}
// Clear map
func (c *MultiMap) Clear() {
c.internalMap = make(map[string][]interface{})
}