forked from babyname/fate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
58 lines (48 loc) · 871 Bytes
/
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
package fate
type IteratorFunc func(v interface{}) error
type iterator struct {
data []interface{}
index int
}
func NewIterator() *iterator {
return &iterator{
data: nil,
index: 0,
}
}
//HasNext check next
func (i *iterator) HasNext() bool {
return i.index < len(i.data)
}
//Next get next
func (i *iterator) Next() interface{} {
defer func() {
i.index++
}()
if i.index < len(i.data) {
return i.data[i.index]
}
return nil
}
//Reset reset index
func (i *iterator) Reset() {
i.index = 0
}
//Add add radical
func (i *iterator) Add(v interface{}) {
i.data = append(i.data, v)
}
//Size iterator data size
func (i *iterator) Size() int {
return len(i.data)
}
//Iterator an default iterator
func (i *iterator) Iterator(f IteratorFunc) error {
i.Reset()
for i.HasNext() {
if err := f(i.Next()); err != nil {
return err
}
}
return nil
}