-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.go
107 lines (91 loc) · 1.83 KB
/
slice.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
package btrgo
func Paginate[V any](x []V, skip int, size int) []V {
if skip > len(x) {
skip = len(x)
}
end := skip + size
if end > len(x) {
end = len(x)
}
return x[skip:end]
}
func PaginateInvert[V any](x []V, skip int, size int) []V {
if size < 0 || skip < 0 {
return []V{}
}
if skip > len(x) {
return []V{}
}
end := len(x) - skip - size
if end < 0 {
end = 0
}
out := make([]V, 0, size)
for i := len(x) - 1 - skip; i >= end; i-- {
out = append(out, x[i])
}
return out
}
// Split slice in chunks of constant size
// based on https://stackoverflow.com/a/67011816
func Chunks[V any](xs []V, chunkSize int) [][]V {
if chunkSize < 1 {
return [][]V{xs}
}
if len(xs) == 0 {
return nil
}
divided := make([][]V, (len(xs)+chunkSize-1)/chunkSize)
prev := 0
i := 0
till := len(xs) - chunkSize
for prev < till {
next := prev + chunkSize
divided[i] = xs[prev:next]
prev = next
i++
}
divided[i] = xs[prev:]
return divided
}
// return unique elements from given slice
// doesn't sort elements, expect output elements in random order
func SliceUnique[V comparable](s []V) []V {
keys := make(map[V]bool, len(s))
list := make([]V, 0, len(s))
for _, entry := range s {
if _, ok := keys[entry]; !ok {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func SliceDiff[V comparable](a, b []V) []V {
m := make(map[V]bool)
diff := make([]V, 0, len(a))
for _, item := range b {
m[item] = true
}
for _, item := range a {
if _, ok := m[item]; !ok {
diff = append(diff, item)
}
}
return diff
}
func SliceDiffSplited[V comparable](a, b []V) (dels, adds []V) {
aMap := map[V]bool{}
for _, v := range a {
aMap[v] = true
}
bMap := map[V]bool{}
for _, v := range b {
bMap[v] = true
delete(aMap, v)
}
for _, v := range a {
delete(bMap, v)
}
return KeysOfMap(aMap), KeysOfMap(bMap)
}