-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
61 lines (46 loc) · 957 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
59
60
61
package node
type Iterator interface {
HasNext() bool
Next() (key, value Node)
}
type mapNodeIterator struct {
content []Node
}
func MapNodeIterator(content []Node) Iterator {
return &mapNodeIterator{
content: content,
}
}
func (i *mapNodeIterator) HasNext() bool {
return len(i.content) >= 1
}
func (i *mapNodeIterator) Next() (key, value Node) {
key = i.content[0]
if len(i.content) == 1 {
i.content = i.content[:0]
return key, EmptyNode{}
}
value = i.content[1]
i.content = i.content[2:]
return key, value
}
type IndexedIterator struct {
iter Iterator
keyIndex int
}
func NewIndexedIterator(iter Iterator) *IndexedIterator {
return &IndexedIterator{
iter: iter,
keyIndex: -2,
}
}
func (i *IndexedIterator) HasNext() bool {
return i.iter.HasNext()
}
func (i *IndexedIterator) Next() (key, value Node) {
i.keyIndex += 2
return i.iter.Next()
}
func (i *IndexedIterator) Index() int {
return i.keyIndex
}