-
Notifications
You must be signed in to change notification settings - Fork 1
/
maps.go
186 lines (175 loc) · 4.19 KB
/
maps.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
package easy
// DiffMaps returns a new map which contains elements which present in m,
// but not present in others.
//
// If length of m is zero, it returns nil.
func DiffMaps[M ~map[K]V, K comparable, V any](m M, others ...M) M {
out := make(M)
for k, v := range m {
found := false
for _, b := range others {
if _, ok := b[k]; ok {
found = true
break
}
}
if !found {
out[k] = v
}
}
return out
}
// DiffMapsInplace removes elements that present in others from m.
func DiffMapsInplace[M ~map[K]V, K comparable, V any](m M, others ...M) M {
for k := range m {
for _, m1 := range others {
if _, ok := m1[k]; ok {
delete(m, k)
break
}
}
}
return m
}
// FilterMaps iterates the given maps, it calls predicate(k, v) for each
// key value in the maps and returns a new map of key value pairs for
// which predicate(k, v) returns true.
func FilterMaps[M ~map[K]V, K comparable, V any](predicate func(k K, v V) bool, maps ...M) M {
if len(maps) == 0 {
return nil
}
out := make(M, len(maps[0]))
for _, x := range maps {
for k, v := range x {
if predicate(k, v) {
out[k] = v
}
}
}
return out
}
// MergeMaps returns a new map containing all key values present in given maps.
func MergeMaps[M ~map[K]V, K comparable, V any](maps ...M) M {
var length int
for _, m := range maps {
length += len(m)
}
dst := make(M, length)
for _, m := range maps {
for k, v := range m {
dst[k] = v
}
}
return dst
}
// MergeMapsTo adds key values present in others to the dst map.
// If dst is a nil map, it creates a new map and returns it.
func MergeMapsTo[M ~map[K]V, K comparable, V any](dst M, others ...M) M {
if len(others) == 0 {
return dst
}
if dst == nil {
dst = make(M, len(others[0]))
}
for _, m := range others {
for k, v := range m {
dst[k] = v
}
}
return dst
}
// MergeMapsToPtr is similar to MergeMapsTo, but it accepts a pointer as dst,
// if dst points to a nil map, it creates a new map and assigns it to dst.
// If dst is a nil pointer, it panics.
func MergeMapsToPtr[M ~map[K]V, K comparable, V any](dst *M, others ...M) {
if dst == nil {
panic("easy.MergeMapsToPtr: dst must not be nil")
}
*dst = MergeMapsTo(*dst, others...)
}
// Keys returns the keys of the map m.
// The keys will be in an indeterminate order.
//
// Optionally, a filter function can be given to make it returning
// only keys for which filter(k, v) returns true.
func Keys[M ~map[K]V, K comparable, V any](m M, filter ...func(K, V) bool) []K {
var f func(K, V) bool
if len(filter) > 0 {
f = filter[0]
}
keys := make([]K, 0, len(m))
if f == nil {
for k := range m {
keys = append(keys, k)
}
} else {
for k, v := range m {
if f(k, v) {
keys = append(keys, k)
}
}
}
return keys
}
// Values returns the values of the map m.
// The values will be in an indeterminate order.
//
// Optionally, a filter function can be given to make it returning
// only values for which filter(k, v) returns true.
func Values[M ~map[K]V, K comparable, V any](m M, filter ...func(K, V) bool) []V {
var f func(K, V) bool
if len(filter) > 0 {
f = filter[0]
}
values := make([]V, 0, len(m))
if f == nil {
for _, v := range m {
values = append(values, v)
}
} else {
for k, v := range m {
if f(k, v) {
values = append(values, v)
}
}
}
return values
}
// CopyMap copies a map to be a new one.
// optionalSize optionally specifies the size of the new map.
func CopyMap[M ~map[K]V, K comparable, V any](m M, optionalSize ...int) M {
copySize := len(m)
if len(optionalSize) > 0 && optionalSize[0] > copySize {
copySize = optionalSize[0]
}
out := make(M, copySize)
for k, v := range m {
out[k] = v
}
return out
}
// SplitMap splits a large map to batches, it returns a slice
// of type []M whose elements are subset of the given map.
func SplitMap[M ~map[K]V, K comparable, V any](m M, batchSize int) []M {
if len(m) == 0 {
return nil
}
if len(m) <= batchSize {
return []M{m}
}
cnt := (len(m) + batchSize - 1) / batchSize
out := make([]M, cnt)
for i := range out {
if i < len(out)-1 {
out[i] = make(M, batchSize)
} else {
out[i] = make(M, len(m)%batchSize)
}
}
i := 0
for k, v := range m {
out[i/batchSize][k] = v
i++
}
return out
}