-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterators.go
251 lines (226 loc) · 5.52 KB
/
iterators.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package iterator
import "github.com/pkg/errors"
// Helper Functions
type PredicateFunc func(item interface{}) (bool, error)
// Creates a wrapper-iterator over the original that will filter elements according to the filter function specified
func Filter(iter Iterator, test PredicateFunc) Iterator {
return &DefaultIterator{
ComputeNext: func() (interface{}, bool, error) {
for iter.HasNext() {
ret, err := iter.Next()
if err != nil {
return nil, true, err
}
ok, err := test(ret) // valid predicate
if ok {
return ret, false, nil
}
if err != nil { // predicate error
return nil, false, err
}
}
return nil, true, nil
},
closer: func() error {
return iter.Close()
},
}
}
// Specific case of Filter that returns a wrapper-iterator over the original that will return only the non nil items
func FilterNonNil(it Iterator) Iterator {
return Filter(it, func(item interface{}) (bool, error) {
return item != nil, nil
})
}
type TransformFunc func(item interface{}) (interface{}, error)
// Creates an wrapper-iterator over the original that will transform elements according to the filter function specified
func Transform(iter Iterator, fn TransformFunc) Iterator {
return &DefaultIterator{
ComputeNext: func() (interface{}, bool, error) {
for iter.HasNext() {
ret, err := iter.Next()
if err != nil {
return nil, false, err
}
nextFn, err := fn(ret)
return nextFn, false, err
}
return nil, true, nil
},
closer: func() (e error) {
return iter.Close()
},
}
}
// Creates an wrapper-iterator over the original that will skip the first 'numberOfElementsToSkip' items
func Skip(it Iterator, howMany int) Iterator {
return &DefaultIterator{
ComputeNext: func() (interface{}, bool, error) {
for howMany > 0 {
hasNext:= it.HasNext()
if !hasNext {
return nil, true, nil
}
_, _ = it.Next()
howMany--
}
hasNext := it.HasNext()
if !hasNext {
return nil, true, nil
}
ret, err := it.Next()
if err != nil {
return nil, true, err
}
return ret, false, nil
},
closer: func() (e error) {
return it.Close()
},
}
}
// Creates an wrapper-iterator over the original that will iterate until there are no more items or the 'upperBound' is reached.
func Limit(it Iterator, upperBound int) Iterator {
items := 0
return &DefaultIterator{
ComputeNext: func() (interface{}, bool, error) {
if items == upperBound {
return nil, true, nil
}
hasNext := it.HasNext()
if !hasNext {
return nil, true, nil
}
ret, err := it.Next()
if err != nil {
return nil, true, err
}
items = items + 1
return ret, false, nil
},
closer: func() (e error) {
return it.Close()
},
}
}
// Appends multiple iterators together exposing them as a single virtual iterator.
func Concat(iterators ...Iterator) Iterator {
var currentIteratorIdx = 0
var iterator = iterators[0]
return &DefaultIterator{
ComputeNext: func() (interface{}, bool, error) {
for {
hasNext := iterator.HasNext()
if !hasNext {
_ = iterator.Close()
currentIteratorIdx ++
if currentIteratorIdx < len(iterators) {
iterator = iterators[currentIteratorIdx]
continue
}
return nil, true, nil
}
next, err := iterator.Next()
if err != nil {
return nil, true, err
}
return next, false, nil
}
},
closer: func() (e error) {
var err error
for _, it := range iterators {
tmpErr := it.Close()
if tmpErr != nil {
if err != nil {
err = tmpErr
} else {
err = errors.Wrap(err, tmpErr.Error())
}
}
}
return err
},
}
}
type CompareFunc func(item1 interface{}, item2 interface{}) int
// Merge combines multiple sorted iterators into a single sorted iterator.
func Merge(compareFn CompareFunc, iterators ...Iterator) Iterator {
return &DefaultIterator{
ComputeNext: func() (interface{}, bool, error) {
for {
ret, err := selectMin(compareFn, iterators...)
if err != nil {
return nil, true, err
}
if ret == nil {
return nil, true, nil
}
return ret, false, nil
}
},
closer: func() (e error) {
var err error
for _, it := range iterators {
tmpErr := it.Close()
if tmpErr != nil {
if err != nil {
err = tmpErr
} else {
err = errors.Wrap(err, tmpErr.Error())
}
}
}
return err
},
}
}
// EqualsFunc returns true is items are equal
type EqualsFunc func(item1 interface{}, item2 interface{}) bool
// Dedup eliminates duplicates
func Dedup(it Iterator, equalsFn EqualsFunc) Iterator {
var prev interface{}
return &DefaultIterator{
ComputeNext: func() (interface{}, bool, error) {
for it.HasNext() {
ret, err := it.Next()
if err != nil {
return nil, true, err
}
if prev == nil || !equalsFn(prev, ret) {
prev = ret
return ret, false, nil
}
}
return nil, true, nil
},
closer: func() (e error) {
return it.Close()
},
}
}
func selectMin(compareFn CompareFunc, iterators ...Iterator) (interface{}, error) {
var selected int
var current interface{}
for i, it := range iterators {
hasNext := it.HasNext()
if hasNext {
peek, err := it.Peek()
if err != nil {
return nil, err
}
if current == nil {
current = peek
selected = i
} else if compareFn(current, peek) > 0 { // The peek is lower than the current selection
current = peek
selected = i
}
}
}
if current != nil {
_, _ = iterators[selected].Next()
return current, nil
}
return nil, nil
}