-
Notifications
You must be signed in to change notification settings - Fork 134
/
map.go
70 lines (60 loc) · 1.36 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
package util
func GetOrDefault(m map[string]float64, key string, def float64) float64 {
v, ok := m[key]
if ok {
return v
}
return def
}
func MergeMaps(a map[string]string, b map[string]string) map[string]string {
result := make(map[string]string)
for k, v := range a {
result[k] = v
}
for k, v := range b {
result[k] = v
}
return result
}
func DeepCopy(a map[string]string) map[string]string {
if a == nil {
return nil
}
result := make(map[string]string)
for k, v := range a {
result[k] = v
}
return result
}
func Equal(a map[string]string, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for key, value := range a {
if comparativeValue, present := b[key]; !present || value != comparativeValue {
return false
}
}
return true
}
func FilterKeys(a map[string]string, keys []string) map[string]string {
if a == nil {
return nil
}
result := make(map[string]string)
for _, key := range keys {
if val, exists := a[key]; exists {
result[key] = val
}
}
return result
}
// InverseMap creates a new map where each key: value pair is swapped
// If the same value is present multiple times, a random one will be selected (depending on map key-value iteration)
func InverseMap[K comparable, V comparable](a map[K]V) map[V]K {
result := make(map[V]K)
for key, value := range a {
result[value] = key
}
return result
}