-
Notifications
You must be signed in to change notification settings - Fork 6
/
generic.go
60 lines (48 loc) · 1.17 KB
/
generic.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
package equals
type Comparable[T any] interface {
Equals(T) bool
}
func GenericPointer[T any](left, right *T, equals func(left, right T) bool) bool {
if left != right {
if left == nil || right == nil {
return false
}
if !equals(*left, *right) {
return false
}
}
return true
}
func GenericArray[T any](left, right []T, equals func(left, right T) bool) bool {
if len(left) != len(right) {
return false
}
for i, l := range left {
if !equals(l, right[i]) {
return false
}
}
return true
}
func GenericMap[T any](left, right map[string]T, equals func(left, right T) bool) bool {
if len(left) != len(right) {
return false
}
for k, lv := range left {
rv, ok := right[k]
if !ok || !equals(lv, rv) {
return false
}
}
return true
}
func GenericArrayPointer[T any](left, right *[]T, equals func(left, right T) bool) bool {
return GenericPointer(left, right, func(left, right []T) bool {
return GenericArray(left, right, equals)
})
}
func GenericMapPointer[T any](left, right *map[string]T, equals func(left, right T) bool) bool {
return GenericPointer(left, right, func(left, right map[string]T) bool {
return GenericMap(left, right, equals)
})
}