-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.go
77 lines (61 loc) · 1.23 KB
/
chunk.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
package generics
// Chunk will uniformly distribute the given over n bins/buckets.
// Note that buckets are slices referencing the same memory.
func Chunk[T any](items []T, n int) [][]T {
if n <= 0 {
return nil
}
if n == 1 {
return [][]T{items}
}
bins := make([][]T, n)
baseCount := len(items) / n
leftover := len(items) % n
idx := 0
for i := 0; i < n; i += 1 {
count := baseCount
if leftover > 0 {
count += 1
leftover -= 1
}
bins[i] = items[idx : idx+count]
idx += count
}
return bins
}
// ChunkMap will uniformly distribute the given map over n bins/buckets.
func ChunkMap[K comparable, V any](items map[K]V, n int) []map[K]V {
if n <= 0 {
return nil
}
if n == 1 {
return []map[K]V{items}
}
baseCount := len(items) / n
leftover := len(items) % n
// Pass 1 - preallocate the map memory
bins := make([]map[K]V, n)
for i := range bins {
count := baseCount
if i < leftover {
count += 1
}
bins[i] = make(map[K]V, count)
}
// Pass 2 - distribute the items.
binIdx := 0
curCount := 0
for k, v := range items {
count := baseCount
if binIdx < leftover {
count += 1
}
bins[binIdx][k] = v
curCount += 1
if curCount == count {
curCount = 0
binIdx += 1
}
}
return bins
}