-
Notifications
You must be signed in to change notification settings - Fork 11
/
async_pairwise_sequence.go
54 lines (42 loc) · 1.21 KB
/
async_pairwise_sequence.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
package co
type AsyncPairwiseSequence[R any, T []R] struct {
*asyncSequence[T]
previousIterator Iterator[R]
}
func NewAsyncPairwiseSequence[R any, T []R](it AsyncSequenceable[R]) *AsyncPairwiseSequence[R, T] {
a := &AsyncPairwiseSequence[R, T]{
previousIterator: it.iterator(),
}
a.asyncSequence = NewAsyncSequence[T](a)
return a
}
func (c *AsyncPairwiseSequence[R, T]) iterator() Iterator[T] {
it := &asyncPairwiseSequenceIterator[R, T]{
AsyncPairwiseSequence: c,
}
it.asyncSequenceIterator = NewAsyncSequenceIterator[T](it)
return it
}
type asyncPairwiseSequenceIterator[R any, T []R] struct {
*asyncSequenceIterator[T]
*AsyncPairwiseSequence[R, T]
previousData Optional[R]
}
func (it *asyncPairwiseSequenceIterator[R, T]) preflight() {
if it.previousData.valid {
return
}
for op := it.previousIterator.next(); op.valid; op = it.previousIterator.next() {
it.previousData = *op
break
}
}
func (it *asyncPairwiseSequenceIterator[R, T]) next() *Optional[T] {
it.preflight()
previousData := it.previousData.data
for op := it.previousIterator.next(); op.valid; op = it.previousIterator.next() {
it.previousData = *op
return OptionalOf(T{previousData, op.data})
}
return NewOptionalEmpty[T]()
}