-
-
Notifications
You must be signed in to change notification settings - Fork 485
/
diff.go
43 lines (33 loc) · 874 Bytes
/
diff.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
package diff
// Stat returnes the number of items added to and removed from the first to
// the second list.
func Stat[K comparable](l, r []K) (int, int) {
added, removed := List(l, r)
return len(added), len(removed)
}
// List returns two lists, the first one contains the items that were added from left
// to right, the second one contains the items that were removed from left to right.
func List[K comparable](l, r []K) ([]K, []K) {
ml := listToMap(l)
mr := listToMap(r)
var added []K
for k := range mr {
if _, found := ml[k]; !found {
added = append(added, k)
}
}
var removed []K
for k := range ml {
if _, found := mr[k]; !found {
removed = append(removed, k)
}
}
return added, removed
}
func listToMap[K comparable](l []K) map[K]struct{} {
m := make(map[K]struct{}, len(l))
for _, e := range l {
m[e] = struct{}{}
}
return m
}