github.com/koko1123/flow-go-1@v0.29.6/module/forest/vertex.go (about)

     1  package forest
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/model/flow"
     5  )
     6  
     7  type Vertex interface {
     8  	// VertexID returns the vertex's ID (in most cases its hash)
     9  	VertexID() flow.Identifier
    10  	// Level returns the vertex's level
    11  	Level() uint64
    12  	// Parent returns the returns the parents (level, ID)
    13  	Parent() (flow.Identifier, uint64)
    14  }
    15  
    16  // VertexIterator is a stateful iterator for VertexList.
    17  // Internally operates directly on the Vertex Containers
    18  // It has one-element look ahead for skipping empty vertex containers.
    19  type VertexIterator struct {
    20  	data VertexList
    21  	idx  int
    22  	next Vertex
    23  }
    24  
    25  func (it *VertexIterator) preLoad() {
    26  	for it.idx < len(it.data) {
    27  		v := it.data[it.idx].vertex
    28  		it.idx++
    29  		if v != nil {
    30  			it.next = v
    31  			return
    32  		}
    33  	}
    34  	it.next = nil
    35  }
    36  
    37  // NextVertex returns the next Vertex or nil if there is none
    38  func (it *VertexIterator) NextVertex() Vertex {
    39  	res := it.next
    40  	it.preLoad()
    41  	return res
    42  }
    43  
    44  // HasNext returns true if and only if there is a next Vertex
    45  func (it *VertexIterator) HasNext() bool {
    46  	return it.next != nil
    47  }
    48  
    49  func newVertexIterator(vertexList VertexList) VertexIterator {
    50  	it := VertexIterator{
    51  		data: vertexList,
    52  	}
    53  	it.preLoad()
    54  	return it
    55  }