-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapkeyvalue.go
428 lines (353 loc) · 10.1 KB
/
mapkeyvalue.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package r9e
import (
"reflect"
"sort"
"sync"
)
type mapKeyValueOptions struct {
size int
}
// MapKeyValueOptions are the options for MapKeyValue container.
type MapKeyValueOptions func(*mapKeyValueOptions)
// WithCapacity sets the initial capacity allocation of the MapKeyValue container.
func WithCapacity(size int) MapKeyValueOptions {
return func(kv *mapKeyValueOptions) {
kv.size = size
}
}
// MapKeyValue is a generic key-value store container that is thread-safe.
// This use a golang native map data structure as underlying data structure and a mutex to
// protect the data.
type MapKeyValue[K comparable, T any] struct {
mu sync.RWMutex
data map[K]T
}
// kv is a helper struct to sort the values of the MapKeyValue container.
type kv[K comparable, T any] struct {
key K
value T
}
// NewMapKeyValue returns a new MapKeyValue container.
func NewMapKeyValue[K comparable, T any](options ...MapKeyValueOptions) *MapKeyValue[K, T] {
kvo := mapKeyValueOptions{}
for _, opt := range options {
opt(&kvo)
}
return &MapKeyValue[K, T]{
data: make(map[K]T, kvo.size),
}
}
// Set sets the value associated with the key.
func (r *MapKeyValue[K, T]) Set(key K, value T) {
r.mu.Lock()
defer r.mu.Unlock()
r.data[key] = value
}
// GetAndCheck returns the value associated with the key if this exist also a
// boolean value if this exist of not.
func (r *MapKeyValue[K, T]) GetAndCheck(key K) (T, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
value, ok := r.data[key]
return value, ok
}
// Get returns the value associated with the key.
// If the key does not exist, return zero value of the type.
func (r *MapKeyValue[K, T]) Get(key K) T {
r.mu.RLock()
defer r.mu.RUnlock()
return r.data[key]
}
// GetAnDelete returns the value associated with the key and delete it if the key exist
// if the key doesn't exist return the given key value false
func (r *MapKeyValue[K, T]) GetAnDelete(key K) (T, bool) {
r.mu.Lock()
defer r.mu.Unlock()
current, loaded := r.data[key]
if loaded {
delete(r.data, key)
}
return current, loaded
}
// Delete deletes the value associated with the key.
func (r *MapKeyValue[K, T]) Delete(key K) {
r.mu.Lock()
defer r.mu.Unlock()
delete(r.data, key)
}
// Clear deletes all key-value pairs stored in the container.
func (r *MapKeyValue[K, T]) Clear() {
r.mu.Lock()
defer r.mu.Unlock()
r.data = make(map[K]T, 0)
}
// Size returns the number of key-value pairs stored in the container.
func (r *MapKeyValue[K, T]) Size() int {
r.mu.RLock()
defer r.mu.RUnlock()
return len(r.data)
}
// IsEmpty returns true if the container is empty.
func (r *MapKeyValue[K, T]) IsEmpty() bool {
return r.Size() == 0
}
// IsFull returns true if the container has elements.
func (r *MapKeyValue[K, T]) IsFull() bool {
return r.Size() != 0
}
// ContainsKey returns true if the key is in the container.
func (r *MapKeyValue[K, T]) ContainsKey(key K) bool {
r.mu.RLock()
defer r.mu.RUnlock()
_, ok := r.data[key]
return ok
}
// ContainsValue returns true if the value is in the container.
func (r *MapKeyValue[K, T]) ContainsValue(value T) bool {
r.mu.RLock()
defer r.mu.RUnlock()
for _, v := range r.data {
if reflect.DeepEqual(v, value) {
return true
}
}
return false
}
// Get returns the key value associated with the key.
func (r *MapKeyValue[K, T]) Key(key K) K {
r.mu.RLock()
defer r.mu.RUnlock()
if _, ok := r.data[key]; ok {
return key
}
var empty K
return empty
}
// Keys returns all keys stored in the container.
func (r *MapKeyValue[K, T]) Keys() []K {
r.mu.RLock()
defer r.mu.RUnlock()
keys := make([]K, 0, len(r.data))
for key := range r.data {
keys = append(keys, key)
}
return keys
}
// Values returns all values stored in the container.
func (r *MapKeyValue[K, T]) Values() []T {
r.mu.RLock()
defer r.mu.RUnlock()
values := make([]T, 0, len(r.data))
for _, value := range r.data {
values = append(values, value)
}
return values
}
// ForEach calls the given function for each key-value pair in the container.
func (r *MapKeyValue[K, T]) ForEach(fn func(key K, value T)) {
r.mu.RLock()
defer r.mu.RUnlock()
for key, value := range r.data {
fn(key, value)
}
}
// ForEachKey calls the given function for each key in the container.
func (r *MapKeyValue[K, T]) ForEachKey(fn func(key K)) {
r.mu.RLock()
defer r.mu.RUnlock()
for key := range r.data {
fn(key)
}
}
// ForEachValue calls the given function for each value in the container.
func (r *MapKeyValue[K, T]) ForEachValue(fn func(value T)) {
r.mu.RLock()
defer r.mu.RUnlock()
for _, value := range r.data {
fn(value)
}
}
// Clone returns a new MapKeyValue with a copy of the underlying data.
func (r *MapKeyValue[K, T]) Clone() *MapKeyValue[K, T] {
r.mu.RLock()
defer r.mu.RUnlock()
clone := NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key, value := range r.data {
clone.Set(key, value)
}
return clone
}
// CloneAndClear returns a new MapKeyValue with a copy of the underlying data and clears the container.
func (r *MapKeyValue[K, T]) CloneAndClear() *MapKeyValue[K, T] {
r.mu.RLock()
defer r.mu.RUnlock()
clone := NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key, value := range r.data {
clone.Set(key, value)
}
r.data = make(map[K]T)
return clone
}
// DeepEqual returns true if the given kv is deep equal to the MapKeyValue container
func (r *MapKeyValue[K, T]) DeepEqual(kv *MapKeyValue[K, T]) bool {
r.mu.RLock()
defer r.mu.RUnlock()
if r.Size() != kv.Size() {
return false
}
for key, value := range r.data {
if !reflect.DeepEqual(value, kv.Get(key)) {
return false
}
}
return true
}
// Map returns a new MapKeyValue after applying the given function fn to each key-value pair.
func (r *MapKeyValue[K, T]) Map(fn func(key K, value T) (newKey K, newValue T)) *MapKeyValue[K, T] {
r.mu.RLock()
defer r.mu.RUnlock()
m := NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key, value := range r.data {
newKey, newValue := fn(key, value)
m.Set(newKey, newValue)
}
return m
}
// MapKey returns a new MapKeyValue after applying the given function fn to each key.
func (r *MapKeyValue[K, T]) MapKey(fn func(key K) K) *MapKeyValue[K, T] {
r.mu.RLock()
defer r.mu.RUnlock()
m := NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key := range r.data {
newKey := fn(key)
m.Set(newKey, r.data[key])
}
return m
}
// MapValue returns a new MapKeyValue after applying the given function fn to each value.
func (r *MapKeyValue[K, T]) MapValue(fn func(value T) T) *MapKeyValue[K, T] {
r.mu.RLock()
defer r.mu.RUnlock()
m := NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key, value := range r.data {
newValue := fn(value)
m.Set(key, newValue)
}
return m
}
// Filter returns a new MapKeyValue after applying the given function fn to each key-value pair.
func (r *MapKeyValue[K, T]) Filter(fn func(key K, value T) bool) *MapKeyValue[K, T] {
r.mu.RLock()
defer r.mu.RUnlock()
m := NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key, value := range r.data {
if fn(key, value) {
m.Set(key, value)
}
}
return m
}
// FilterKey returns a new MapKeyValue after applying the given function fn to each key.
func (r *MapKeyValue[K, T]) FilterKey(fn func(key K) bool) *MapKeyValue[K, T] {
r.mu.RLock()
defer r.mu.RUnlock()
m := NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key := range r.data {
if fn(key) {
m.Set(key, r.data[key])
}
}
return m
}
// FilterValue returns a new MapKeyValue after applying the given function fn to each value.
func (r *MapKeyValue[K, T]) FilterValue(fn func(value T) bool) *MapKeyValue[K, T] {
r.mu.RLock()
defer r.mu.RUnlock()
m := NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key, value := range r.data {
if fn(value) {
m.Set(key, value)
}
}
return m
}
// Partition returns two new MapKeyValue. One with all the elements that satisfy the predicate and
// another with the rest. The predicate is applied to each element.
func (r *MapKeyValue[K, T]) Partition(fn func(key K, value T) bool) (match, others *MapKeyValue[K, T]) {
r.mu.RLock()
defer r.mu.RUnlock()
match = NewMapKeyValue[K, T](WithCapacity(r.Size()))
others = NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key, value := range r.data {
if fn(key, value) {
match.Set(key, value)
} else {
others.Set(key, value)
}
}
return
}
// PartitionKey returns two new MapKeyValue. One with all the elements that satisfy the predicate and
// another with the rest. The predicate is applied to each key.
func (r *MapKeyValue[K, T]) PartitionKey(fn func(key K) bool) (match, others *MapKeyValue[K, T]) {
r.mu.RLock()
defer r.mu.RUnlock()
match = NewMapKeyValue[K, T](WithCapacity(r.Size()))
others = NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key := range r.data {
if fn(key) {
match.Set(key, r.data[key])
} else {
others.Set(key, r.data[key])
}
}
return
}
// PartitionValue returns two new MapKeyValue. One with all the elements that satisfy the predicate and
// another with the rest. The predicate is applied to each value.
func (r *MapKeyValue[K, T]) PartitionValue(fn func(value T) bool) (match, others *MapKeyValue[K, T]) {
r.mu.RLock()
defer r.mu.RUnlock()
match = NewMapKeyValue[K, T](WithCapacity(r.Size()))
others = NewMapKeyValue[K, T](WithCapacity(r.Size()))
for key, value := range r.data {
if fn(value) {
match.Set(key, value)
} else {
others.Set(key, value)
}
}
return
}
// SortKeys returns a []*K (keys) after sorting the keys using the given sortFn function.
func (r *MapKeyValue[K, T]) SortKeys(sortFn func(key1, key2 K) bool) []*K {
r.mu.RLock()
defer r.mu.RUnlock()
keys := r.Keys()
sort.Slice(keys, func(i, j int) bool {
return sortFn(keys[i], keys[j])
})
m := make([]*K, len(keys))
for i, key := range keys {
k := key
m[i] = &k
}
return m
}
// SortValues returns a []*T (values) after sorting the values using given function sortFn.
func (r *MapKeyValue[K, T]) SortValues(sortFn func(value1, value2 T) bool) []*T {
r.mu.RLock()
defer r.mu.RUnlock()
kvs := make([]*kv[K, T], 0, r.Size())
for key, value := range r.data {
kvs = append(kvs, &kv[K, T]{key, value})
}
sort.Slice(kvs, func(i, j int) bool {
return sortFn(kvs[i].value, kvs[j].value)
})
m := make([]*T, len(kvs))
for i, pair := range kvs {
m[i] = &pair.value
}
return m
}