-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
40 lines (35 loc) · 1.06 KB
/
iter.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
package tl
// Iter defines an iterator interface.
type Iter[T any] interface {
// Next is called to iterate to the next element.
//
// Returns true if a next element is available, false otherwise.
Next() bool
// Get returns the current element.
Get() T
// GetPtr returns a pointer to the current element.
GetPtr() *T
}
// IterDrop implements an iterator which current element can be dropped.
type IterDrop[T any] interface {
Iter[T]
// Drop removes the current element from the iterator.
Drop()
}
// IterBidir defines a bidirectional iterator.
type IterBidir[T any] interface {
Iter[T]
// Back is like next but for going backwards. It moves the iterator to the previous position.
// For some implementations that might mean going in reverse mode (not going backwards).
Back() bool
}
// IterDropBidir merges IterBidir and IterDrop in one interface.
type IterDropBidir[T any] interface {
IterDrop[T]
IterBidir[T]
}
// Advance advances iter a number of `count` positions.
func Advance[T any](iter Iter[T], count int) {
for i := 0; i < count && iter.Next(); i++ {
}
}