/
etreeiterator.go
83 lines (71 loc) · 2.3 KB
/
etreeiterator.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
// *****************************************************************************
// Copyright(c) 2021 MASA Group
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// *****************************************************************************
package ecore
type treeIterator struct {
object interface{}
data []EIterator
root bool
getChildren func(interface{}) EIterator
}
func newTreeIterator(object interface{}, root bool, getChildren func(interface{}) EIterator) *treeIterator {
return &treeIterator{object: object, root: root, getChildren: getChildren}
}
func newEAllContentsIterator(object EObject) *treeIterator {
return &treeIterator{object: object, root: false, getChildren: func(o interface{}) EIterator {
return o.(EObject).EContents().Iterator()
}}
}
func (it *treeIterator) HasNext() bool {
if it.data == nil && !it.root {
return it.hasAnyChildren()
} else {
return it.hasMoreChildren()
}
}
func (it *treeIterator) hasAnyChildren() bool {
current := it.getChildren(it.object)
it.data = append(it.data, current)
return current.HasNext()
}
func (it *treeIterator) hasMoreChildren() bool {
return it.data == nil || len(it.data) != 0 && it.data[len(it.data)-1].HasNext()
}
func (it *treeIterator) Next() interface{} {
if it.data == nil {
// Yield that mapping, create a stack, and add it to the stack.
current := it.getChildren(it.object)
it.data = append(it.data, current)
if it.root {
return it.object
}
}
// Get the top iterator, retrieve it's result, and record it as the one to which
// remove will be delegated.
current := it.data[len(it.data)-1]
result := current.Next()
// If the result about to be returned has children...
iterator := it.getChildren(result)
if iterator.HasNext() {
// Add iterator to the stack.
it.data = append(it.data, iterator)
} else {
// While the current iterator has no next...
for !current.HasNext() {
// Pop it from the stack.
it.data = it.data[:len(it.data)-1]
// If the stack is empty, we're done.
if len(it.data) == 0 {
break
}
// Get the next one down and then test it for has next.
current = it.data[len(it.data)-1]
}
}
return result
}