-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathmap.go
96 lines (82 loc) · 1.56 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package util
import (
"sort"
"sync"
)
type Map[K comparable, V any] struct {
sync.Map
}
func (m *Map[K, V]) Add(k K, v V) bool {
_, loaded := m.LoadOrStore(k, v)
return !loaded
}
func (m *Map[K, V]) Set(k K, v V) {
m.Store(k, v)
}
func (m *Map[K, V]) Has(k K) (ok bool) {
_, ok = m.Load(k)
return
}
func (m *Map[K, V]) Len() (l int) {
m.Map.Range(func(k, v interface{}) bool {
l++
return true
})
return
}
func (m *Map[K, V]) Get(k K) (result V) {
v, ok := m.Load(k)
if !ok {
return
}
return v.(V)
}
func (m *Map[K, V]) Delete(k K) (v V, ok bool) {
var r any
if r, ok = m.Map.LoadAndDelete(k); ok {
v = r.(V)
}
return
}
func (m *Map[K, V]) ToList() (r []V) {
m.Map.Range(func(k, v interface{}) bool {
r = append(r, v.(V))
return true
})
return
}
func MapList[K comparable, V any, R any](m *Map[K, V], f func(K, V) R) (r []R) {
m.Map.Range(func(k, v interface{}) bool {
r = append(r, f(k.(K), v.(V)))
return true
})
return
}
func (m *Map[K, V]) Range(f func(K, V)) {
m.Map.Range(func(k, v interface{}) bool {
f(k.(K), v.(V))
return true
})
}
func (m *Map[K, V]) RangeSorted(f func(K, V), compare func(K, K) bool) {
// Collect keys and values
var pairs []struct {
key K
val V
}
m.Map.Range(func(k, v interface{}) bool {
pairs = append(pairs, struct {
key K
val V
}{k.(K), v.(V)})
return true
})
// Sort by keys using the compare function
sort.Slice(pairs, func(i, j int) bool {
return compare(pairs[i].key, pairs[j].key)
})
// Apply function in sorted order
for _, pair := range pairs {
f(pair.key, pair.val)
}
}