-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
50 lines (45 loc) · 1.38 KB
/
common.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
package sliceutils
// Creates a set out of slice elements. Duplicates are discarded.
func makeSet[T comparable](slice []T) map[T]struct{} {
uniques := make(map[T]struct{})
for _, val := range slice {
uniques[val] = struct{}{}
}
return uniques
}
// Returns the zero value of type T.
func zeroValue[T any]() T {
var t T
return t
}
// Slice division generator is used to evenly divide a slice into sub-slices
// which could be processed in parallel. All sub-slices are non-overlapping.
type sliceDivGen struct {
// Minimum number of elements per division.
minDivLen int
// Number of divisions which have `minDivLen + 1` elements.
firstPartDivs int
}
// Creates a new slice division generator. Takes parameter `length` as length
// of the slice and `divs` as the number times to divide the slice.
//
// Panics if `divs` is zero.
func newSliceDivGen(length, divs int) sliceDivGen {
return sliceDivGen{
minDivLen: length / divs,
firstPartDivs: length % divs,
}
}
// Gets the offset in the original slice and length of the sub-slice for given
// sub-slice index.
//
// `divIdx` is expected to be less than the number of divisions.
func (sdg sliceDivGen) get(divIdx int) (int, int) {
if divIdx < sdg.firstPartDivs {
offset := (sdg.minDivLen + 1) * divIdx
return offset, sdg.minDivLen + 1
} else {
offset := divIdx*sdg.minDivLen + sdg.firstPartDivs
return offset, sdg.minDivLen
}
}