-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
51 lines (46 loc) · 1.26 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
// Copyright 2023 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Written by Changkun Ou <changkun.de>
package iter
// NewBatchFromSlice returns a batch iterator over a slice.
// Note that the slice is not copied and the caller must not modify it.
// To use the returned iterator, here is an example:
//
// it := iter.NewBatchFromSlice[T](s, 42)
// for batch, ok := it.Next(); ok; batch, ok = it.Next() {
// // do something with batch
// }
func NewBatchFromSlice[T any](s []T, batchSize int) Iter[[]T] {
return &sliceIter[T]{s: s, batchSize: batchSize}
}
type sliceIter[T any] struct {
s []T
batchSize int
i int
}
// Next implements Iter[T] and returns a batch of elements.
func (si *sliceIter[T]) Next() (ss []T, ok bool) {
if si.i >= len(si.s) {
return []T{}, false
}
end := si.i + si.batchSize
if end > len(si.s) {
end = len(si.s)
}
ss = si.s[si.i:end]
si.i = end
ok = true
return
}
// ToSlice returns a slice containing all the elements in an iterator.
func BatchToSlice[E any](it Iter[[]E]) []E {
var r []E
for batch, ok := it.Next(); ok; batch, ok = it.Next() {
for i := range batch {
r = append(r, batch[i])
}
}
return r
}