/
random_map.go
170 lines (126 loc) · 3.33 KB
/
random_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package randommap
import (
"math/rand"
"sync"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type randomMapEntry struct {
key interface{}
value interface{}
keyIndex int
}
// RandomMap defines a map with extended ability to return a random entry.
type RandomMap struct {
rawMap map[interface{}]*randomMapEntry
keys []interface{}
size int
mutex sync.RWMutex
}
// New creates a new random map
func New() *RandomMap {
return &RandomMap{
rawMap: make(map[interface{}]*randomMapEntry),
keys: make([]interface{}, 0),
}
}
// Set associates the specified value with the specified key.
// If the association already exists, it updates the value.
func (rmap *RandomMap) Set(key interface{}, value interface{}) (updated bool) {
rmap.mutex.Lock()
if entry, exists := rmap.rawMap[key]; exists {
if entry.value != value {
entry.value = value
updated = true
}
} else {
rmap.rawMap[key] = &randomMapEntry{
key: key,
value: value,
keyIndex: rmap.size,
}
updated = true
rmap.keys = append(rmap.keys, key)
rmap.size++
}
rmap.mutex.Unlock()
return
}
// Get returns the value to which the specified key is mapped.
func (rmap *RandomMap) Get(key interface{}) (result interface{}, exists bool) {
rmap.mutex.RLock()
if entry, entryExists := rmap.rawMap[key]; entryExists {
result = entry.value
exists = entryExists
}
rmap.mutex.RUnlock()
return
}
// Delete removes the mapping for the specified key in the map.
func (rmap *RandomMap) Delete(key interface{}) (result interface{}, exists bool) {
rmap.mutex.RLock()
if _, entryExists := rmap.rawMap[key]; entryExists {
rmap.mutex.RUnlock()
rmap.mutex.Lock()
if entry, entryExists := rmap.rawMap[key]; entryExists {
delete(rmap.rawMap, key)
rmap.size--
if entry.keyIndex != rmap.size {
oldKey := entry.keyIndex
movedKey := rmap.keys[rmap.size]
rmap.rawMap[movedKey].keyIndex = oldKey
rmap.keys[oldKey] = movedKey
}
rmap.keys = rmap.keys[:rmap.size]
result = entry.value
exists = true
}
rmap.mutex.Unlock()
} else {
rmap.mutex.RUnlock()
}
return
}
// Size returns the number of key-value mappings in the map.
func (rmap *RandomMap) Size() (result int) {
rmap.mutex.RLock()
result = rmap.size
rmap.mutex.RUnlock()
return
}
// ForEach iterates through the elements in the map and calls the consumer function for each element.
func (rmap *RandomMap) ForEach(consumer func(key interface{}, value interface{})) {
rmap.mutex.RLock()
defer rmap.mutex.RUnlock()
for _, key := range rmap.keys {
consumer(key, rmap.rawMap[key].value)
}
}
// RandomKey returns a random key from the map.
func (rmap *RandomMap) RandomKey() (result interface{}) {
rmap.mutex.RLock()
defer rmap.mutex.RUnlock()
return rmap.randomKey()
}
// RandomEntry returns a random value from the map.
func (rmap *RandomMap) RandomEntry() (result interface{}) {
rmap.mutex.RLock()
if rmap.size >= 1 {
result = rmap.rawMap[rmap.randomKey()].value
}
rmap.mutex.RUnlock()
return
}
// Keys returns the list of keys stored in the RandomMap.
func (rmap *RandomMap) Keys() (result []interface{}) {
rmap.mutex.RLock()
defer rmap.mutex.RUnlock()
result = make([]interface{}, rmap.size)
copy(result, rmap.keys)
return
}
func (rmap *RandomMap) randomKey() (result interface{}) {
return rmap.keys[rand.Intn(rmap.size)]
}