-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchunks.go
68 lines (55 loc) · 1.48 KB
/
chunks.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
package ranges
type chunkResult[T any] struct {
size int
takeResult takeResult[T]
}
func (cr *chunkResult[T]) Empty() bool {
return cr.takeResult.ir.Empty()
}
func (cr *chunkResult[T]) Front() InputRange[T] {
return &cr.takeResult
}
func (cr *chunkResult[T]) PopFront() {
for !cr.takeResult.Empty() {
cr.takeResult.PopFront()
}
cr.takeResult = takeResult[T]{cr.size, cr.takeResult.ir}
}
type chunkForwardResult[T any] struct {
size int
takeResult takeForwardResult[T]
}
func (cr *chunkForwardResult[T]) Empty() bool {
return cr.takeResult.ir.Empty()
}
func (cr *chunkForwardResult[T]) Front() ForwardRange[T] {
return &cr.takeResult
}
func (cr *chunkForwardResult[T]) PopFront() {
for !cr.takeResult.Empty() {
cr.takeResult.PopFront()
}
cr.takeResult = takeForwardResult[T]{takeResult[T]{cr.size, cr.takeResult.ir}}
}
func (cr *chunkForwardResult[T]) Save() ForwardRange[ForwardRange[T]] {
return &chunkForwardResult[T]{
cr.size,
takeForwardResult[T]{
takeResult[T]{
cr.size,
cr.takeResult.ir.(ForwardRange[T]).Save(),
},
},
}
}
// Chunks returns sub-ranges that are at most `size` in length.
func Chunks[T any](r InputRange[T], size int) InputRange[InputRange[T]] {
return &chunkResult[T]{size, takeResult[T]{size, r}}
}
// `ChunksF` is `Chunks` where the range can be saved.
func ChunksF[T any](r ForwardRange[T], size int) ForwardRange[ForwardRange[T]] {
return &chunkForwardResult[T]{
size,
takeForwardResult[T]{takeResult[T]{size, r}},
}
}