-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathiterator.go
177 lines (165 loc) · 4.2 KB
/
iterator.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package iter
// Iter represents an iterator, just an alias of any.
type Iter[T any] interface{}
type (
// Reader is a readable iterator.
Reader[T any] interface {
Read() T
}
// Writer is a writable iterator.
Writer[T any] interface {
Write(T)
}
// ReadWriter is an interface that groups Reader and Writer.
ReadWriter[T any] interface {
Reader[T]
Writer[T]
}
)
// Comparable represents an iterator that can be compared.
type Comparable[It Iter[any]] interface {
Eq(It) bool
}
// ForwardMovable represents iterators that can move forward.
type ForwardMovable[It Iter[any]] interface {
Next() It
}
// BackwardMovable represents iterators that can move backward.
type BackwardMovable[It Iter[any]] interface {
Prev() It
}
// InputIter is a readable and forward movable iterator.
type InputIter[T any, It Iter[T]] interface {
Reader[T]
ForwardMovable[It]
Comparable[It]
}
// OutputIter is a writable and ForwardMovable iterator.
//
// It may not implement the incremental interface, in which case the increment
// logic is done in Write().
type OutputIter[T any] interface {
Writer[T]
}
type (
// ForwardIter is an iterator that moves forward.
ForwardIter[T any, It Iter[T]] interface {
ForwardMovable[It]
Comparable[It]
AllowMultiplePass() // a marker indicates it can be multiple passed.
}
// ForwardReader is an interface that groups ForwardIter and Reader.
ForwardReader[T any, It Iter[T]] interface {
ForwardIter[T, It]
Reader[T]
}
// ForwardWriter is an interface that groups ForwardIter and Writer.
ForwardWriter[T any, It Iter[T]] interface {
ForwardIter[T, It]
Writer[T]
}
// ForwardReadWriter is an interface that groups ForwardIter and
// ReadWriter.
ForwardReadWriter[T any, It Iter[T]] interface {
ForwardIter[T, It]
ReadWriter[T]
}
)
type (
// BidiIter is an iterator that moves both forward or backward.
BidiIter[T any, It Iter[T]] interface {
ForwardIter[T, It]
BackwardMovable[It]
}
// BidiReader is an interface that groups BidiIter and Reader.
BidiReader[T any, It Iter[T]] interface {
BidiIter[T, It]
Reader[T]
}
// BidiWriter is an interface that groups BidiIter and Writer.
BidiWriter[T any, It Iter[T]] interface {
BidiIter[T, It]
Writer[T]
}
// BidiReadWriter is an interface that groups BidiIter and ReadWriter.
BidiReadWriter[T any, It Iter[T]] interface {
BidiIter[T, It]
ReadWriter[T]
}
)
type (
// RandomIter is a random access iterator.
RandomIter[T any, It Iter[T]] interface {
BidiIter[T, It]
AdvanceN(n int) It
Distance(It) int
Less(It) bool
}
// RandomReader is an interface that groups RandomIter and Reader.
RandomReader[T any, It Iter[T]] interface {
RandomIter[T, It]
Reader[T]
}
// RandomWriter is an interface that groups RandomIter and Writer.
RandomWriter[T any, It Iter[T]] interface {
RandomIter[T, It]
Writer[T]
}
// RandomReadWriter is an interface that groups RandomIter and
// ReadWriter.
RandomReadWriter[T any, It Iter[T]] interface {
RandomIter[T, It]
ReadWriter[T]
}
)
// Distance returns the distance of two iterators.
func Distance[T any, It Iter[T]](first, last It) int {
ifirst, ilast := any(first), any(last)
if f, ok := ifirst.(RandomIter[T, It]); ok {
if l, ok := ilast.(It); ok {
return f.Distance(l)
}
}
if f, ok := ifirst.(ForwardIter[T, It]); ok {
if l, ok := ilast.(It); ok {
var d int
for ; !f.Eq(l); f = any(f.Next()).(ForwardIter[T, It]) {
d++
}
return d
}
}
if f, ok := ifirst.(InputIter[T, It]); ok {
var d int
for ; !f.Eq(last); f = any(f.Next()).(InputIter[T, It]) {
d++
}
return d
}
panic("cannot get distance")
}
// AdvanceN moves an iterator by step N.
func AdvanceN[T any, It Iter[T]](it It, n int) It {
if it2, ok := any(it).(RandomIter[T, It]); ok {
return it2.AdvanceN(n)
}
if it2, ok := any(it).(ForwardIter[T, It]); ok && n >= 0 {
for ; n > 0; n-- {
it2 = (any)(it2.Next()).(ForwardIter[T, It])
}
return it2.(It)
}
if it2, ok := any(it).(InputIter[T, It]); ok && n >= 0 {
for ; n > 0; n-- {
it2 = any(it2.Next()).(InputIter[T, It])
}
return it2.(It)
}
if it2, ok := any(it).(BidiIter[T, It]); ok && n <= 0 {
for ; n < 0; n++ {
it2 = any(it2.Prev()).(BidiIter[T, It])
}
return it2.(It)
}
panic("cannot advance")
}