-
Notifications
You must be signed in to change notification settings - Fork 178
/
vertex.go
55 lines (48 loc) · 1.15 KB
/
vertex.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
package forest
import (
"github.com/onflow/flow-go/model/flow"
)
type Vertex interface {
// VertexID returns the vertex's ID (in most cases its hash)
VertexID() flow.Identifier
// Level returns the vertex's level
Level() uint64
// Parent returns the returns the parents (level, ID)
Parent() (flow.Identifier, uint64)
}
// VertexIterator is a stateful iterator for VertexList.
// Internally operates directly on the Vertex Containers
// It has one-element look ahead for skipping empty vertex containers.
type VertexIterator struct {
data VertexList
idx int
next Vertex
}
func (it *VertexIterator) preLoad() {
for it.idx < len(it.data) {
v := it.data[it.idx].vertex
it.idx++
if v != nil {
it.next = v
return
}
}
it.next = nil
}
// NextVertex returns the next Vertex or nil if there is none
func (it *VertexIterator) NextVertex() Vertex {
res := it.next
it.preLoad()
return res
}
// HasNext returns true if and only if there is a next Vertex
func (it *VertexIterator) HasNext() bool {
return it.next != nil
}
func newVertexIterator(vertexList VertexList) VertexIterator {
it := VertexIterator{
data: vertexList,
}
it.preLoad()
return it
}