1
2
3
4
5 package a
6
7
8
9 func SliceEqual[Elem comparable](s1, s2 []Elem) bool {
10 if len(s1) != len(s2) {
11 return false
12 }
13 for i, v1 := range s1 {
14 v2 := s2[i]
15 if v1 != v2 {
16 isNaN := func(f Elem) bool { return f != f }
17 if !isNaN(v1) || !isNaN(v2) {
18 return false
19 }
20 }
21 }
22 return true
23 }
24
25
26
27 func Keys[K comparable, V any](m map[K]V) []K {
28 r := make([]K, 0, len(m))
29 for k := range m {
30 r = append(r, k)
31 }
32 return r
33 }
34
35
36
37 func Values[K comparable, V any](m map[K]V) []V {
38 r := make([]V, 0, len(m))
39 for _, v := range m {
40 r = append(r, v)
41 }
42 return r
43 }
44
45
46
47 func Equal[K, V comparable](m1, m2 map[K]V) bool {
48 if len(m1) != len(m2) {
49 return false
50 }
51 for k, v1 := range m1 {
52 if v2, ok := m2[k]; !ok || v1 != v2 {
53 return false
54 }
55 }
56 return true
57 }
58
59
60 func Copy[K comparable, V any](m map[K]V) map[K]V {
61 r := make(map[K]V, len(m))
62 for k, v := range m {
63 r[k] = v
64 }
65 return r
66 }
67
68
69
70 func Add[K comparable, V any](m1, m2 map[K]V) {
71 for k, v := range m2 {
72 m1[k] = v
73 }
74 }
75
76
77
78 func Sub[K comparable, V any](m1, m2 map[K]V) {
79 for k := range m2 {
80 delete(m1, k)
81 }
82 }
83
84
85
86 func Intersect[K comparable, V any](m1, m2 map[K]V) {
87 for k := range m1 {
88 if _, ok := m2[k]; !ok {
89 delete(m1, k)
90 }
91 }
92 }
93
94
95 func Filter[K comparable, V any](m map[K]V, f func(K, V) bool) {
96 for k, v := range m {
97 if !f(k, v) {
98 delete(m, k)
99 }
100 }
101 }
102
103
104 func TransformValues[K comparable, V any](m map[K]V, f func(V) V) {
105 for k, v := range m {
106 m[k] = f(v)
107 }
108 }
109
View as plain text