-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiter.go
59 lines (50 loc) · 1.44 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package gtl
// Iterator defines an interface for iterative objects.
type Iterator[T any] interface {
// Next increments the iterator.
Next() bool
// Advance advances the cursor `n` steps. Returns false if `n` overflows.
Advance(n int) bool
// Get returns the value held in the iterator.
Get() T
// Ptr returns a pointer to T.
Ptr() *T
}
// Iter implements the Iterator[T] interface.
//
// The iterator might not be directly used by the user, but as a mean
// to iterate over gtl defined data structures.
//
// T defines the object held by the Iterator.
// T2 is any object that keeps the previous state in order to
// help the iterator to move forward. For example, a vector might use as T2
// an integer, to be used as index for accessing the next element.
type Iter[T, T2 any] struct {
v *T
prev T2
index T2
next func(T2) (*T, T2)
advance func(T2, int) (*T, T2)
}
// Index returns the index used in the iterator.
func (it *Iter[T, T2]) Index() T2 {
return it.prev
}
// Next advances the iterator pointer.
func (it *Iter[T, T2]) Next() bool {
it.prev = it.index
it.v, it.index = it.next(it.index)
return it.v != nil
}
func (it *Iter[T, T2]) Advance(n int) bool {
it.v, it.index = it.advance(it.index, n)
return it.v != nil
}
// Get returns the value held in the iterator.
func (it *Iter[T, T2]) Get() T {
return *it.v
}
// Ptr returns a pointer to the value held in the iterator.
func (it *Iter[T, T2]) Ptr() *T {
return it.v
}