This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
iter.go
154 lines (130 loc) · 3.52 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
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
package uast
// PathIter iterates node paths.
type PathIter interface {
// Next returns the next node path or nil if the are no more nodes.
Next() Path
}
// PathIter iterates node paths, optionally stepping to avoid visiting children
// of some nodes.
type PathStepIter interface {
PathIter
// If Step is called, children of the last node returned by Next() will
// not be visited.
Step()
}
func newSliceIter(elements ...Path) PathIter {
return &sliceIter{elements: elements}
}
func newNodeSliceIter(prefix Path, nodes ...*Node) PathIter {
paths := make([]Path, 0, len(nodes))
for _, n := range nodes {
paths = append(paths, prefix.Child(n))
}
return newSliceIter(paths...)
}
type sliceIter struct {
idx int
elements []Path
}
func (i *sliceIter) Next() Path {
if i.idx >= len(i.elements) {
return nil
}
n := i.elements[i.idx]
i.idx++
return n
}
type orderPathIter struct {
stack []PathIter
last Path
}
// NewOrderPathIter creates an iterator that iterates all tree nodes (by default it
// will use preorder traversal but will switch to inorder or postorder if the Infix and
// Postfix roles are found).
func NewOrderPathIter(p Path) PathStepIter {
return &orderPathIter{
stack: []PathIter{newSliceIter(p)},
}
}
const (
preOrder = iota
inOrder
postOrder
)
func getNextIterType(n *Node) int {
var order int
for _, r := range n.Roles {
switch r {
case Infix:
order = inOrder
case Postfix:
order = postOrder
default:
order = preOrder
}
}
return order
}
// Make a copy of the Node removing the children. Used to
// add nodes with the InOrder or PostOrder roles to the stack
// when their children have been already added
func noChildrenNodeCopy(n *Node) *Node {
noChildrenNode := *n
noChildrenNode.Children = nil
return &noChildrenNode
}
// Adds to the orderPathIter stack with the right order depending on
// the order Role with (if set) can be Infix, Postfix or Prefix. Defaults to Preorder
// if the order Role is not set. This also updates i.last.
func (i *orderPathIter) addToStackWithOrder(n *Node) {
hasChildren := len(n.Children) > 0
iterType := getNextIterType(n)
if iterType == inOrder && hasChildren {
// Right
i.stack = append(i.stack, newNodeSliceIter(i.last, n.Children[1]))
// Relator
i.stack = append(i.stack, newNodeSliceIter(i.last, noChildrenNodeCopy(n)))
// left
i.stack = append(i.stack, newNodeSliceIter(i.last, n.Children[0]))
} else if iterType == postOrder && hasChildren {
// Children
i.stack = append(i.stack, newNodeSliceIter(i.last, noChildrenNodeCopy(n)))
// Relator
i.stack = append(i.stack, newNodeSliceIter(i.last, n.Children...))
} else if hasChildren {
// no order role or (default) preOrder
// (children not added to the stack):
i.stack = append(i.stack, newNodeSliceIter(i.last, n.Children...))
}
}
func (i *orderPathIter) Next() Path {
for {
if !i.last.IsEmpty() {
n := i.last.Node()
i.addToStackWithOrder(n)
}
if len(i.stack) == 0 {
break
}
cur := i.stack[len(i.stack)-1]
p := cur.Next()
if p.IsEmpty() {
i.stack = i.stack[:len(i.stack)-1]
continue
}
i.last = p
n := p.Node()
// Check if the item has the role inOrder or postOrder and have children; in that
// case skip it since the children and the (childless) copy of the node have already
// been added in addToStackWithOrder in the correct order
iterType := getNextIterType(n)
if (iterType == inOrder || iterType == postOrder) && n.Children != nil {
continue
}
return p
}
return NewPath()
}
func (i *orderPathIter) Step() {
i.last = nil
}