gonum.org/v1/gonum@v0.14.0/graph/iterator/edges.go (about)

     1  // Copyright ©2018 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package iterator
     6  
     7  import "gonum.org/v1/gonum/graph"
     8  
     9  // OrderedEdges implements the graph.Edges and graph.EdgeSlicer interfaces.
    10  // The iteration order of OrderedEdges is the order of edges passed to
    11  // NewEdgeIterator.
    12  type OrderedEdges struct {
    13  	idx   int
    14  	edges []graph.Edge
    15  }
    16  
    17  // NewOrderedEdges returns an OrderedEdges initialized with the provided edges.
    18  func NewOrderedEdges(edges []graph.Edge) *OrderedEdges {
    19  	return &OrderedEdges{idx: -1, edges: edges}
    20  }
    21  
    22  // Len returns the remaining number of edges to be iterated over.
    23  func (e *OrderedEdges) Len() int {
    24  	if e.idx >= len(e.edges) {
    25  		return 0
    26  	}
    27  	if e.idx <= 0 {
    28  		return len(e.edges)
    29  	}
    30  	return len(e.edges[e.idx:])
    31  }
    32  
    33  // Next returns whether the next call of Edge will return a valid edge.
    34  func (e *OrderedEdges) Next() bool {
    35  	if uint(e.idx)+1 < uint(len(e.edges)) {
    36  		e.idx++
    37  		return true
    38  	}
    39  	e.idx = len(e.edges)
    40  	return false
    41  }
    42  
    43  // Edge returns the current edge of the iterator. Next must have been
    44  // called prior to a call to Edge.
    45  func (e *OrderedEdges) Edge() graph.Edge {
    46  	if e.idx >= len(e.edges) || e.idx < 0 {
    47  		return nil
    48  	}
    49  	return e.edges[e.idx]
    50  }
    51  
    52  // EdgeSlice returns all the remaining edges in the iterator and advances
    53  // the iterator.
    54  func (e *OrderedEdges) EdgeSlice() []graph.Edge {
    55  	if e.idx >= len(e.edges) {
    56  		return nil
    57  	}
    58  	idx := e.idx
    59  	if idx == -1 {
    60  		idx = 0
    61  	}
    62  	e.idx = len(e.edges)
    63  	return e.edges[idx:]
    64  }
    65  
    66  // Reset returns the iterator to its initial state.
    67  func (e *OrderedEdges) Reset() {
    68  	e.idx = -1
    69  }
    70  
    71  // OrderedWeightedEdges implements the graph.Edges and graph.EdgeSlicer interfaces.
    72  // The iteration order of OrderedWeightedEdges is the order of edges passed to
    73  // NewEdgeIterator.
    74  type OrderedWeightedEdges struct {
    75  	idx   int
    76  	edges []graph.WeightedEdge
    77  }
    78  
    79  // NewOrderedWeightedEdges returns an OrderedWeightedEdges initialized with the provided edges.
    80  func NewOrderedWeightedEdges(edges []graph.WeightedEdge) *OrderedWeightedEdges {
    81  	return &OrderedWeightedEdges{idx: -1, edges: edges}
    82  }
    83  
    84  // Len returns the remaining number of edges to be iterated over.
    85  func (e *OrderedWeightedEdges) Len() int {
    86  	if e.idx >= len(e.edges) {
    87  		return 0
    88  	}
    89  	if e.idx <= 0 {
    90  		return len(e.edges)
    91  	}
    92  	return len(e.edges[e.idx:])
    93  }
    94  
    95  // Next returns whether the next call of WeightedEdge will return a valid edge.
    96  func (e *OrderedWeightedEdges) Next() bool {
    97  	if uint(e.idx)+1 < uint(len(e.edges)) {
    98  		e.idx++
    99  		return true
   100  	}
   101  	e.idx = len(e.edges)
   102  	return false
   103  }
   104  
   105  // WeightedEdge returns the current edge of the iterator. Next must have been
   106  // called prior to a call to WeightedEdge.
   107  func (e *OrderedWeightedEdges) WeightedEdge() graph.WeightedEdge {
   108  	if e.idx >= len(e.edges) || e.idx < 0 {
   109  		return nil
   110  	}
   111  	return e.edges[e.idx]
   112  }
   113  
   114  // WeightedEdgeSlice returns all the remaining edges in the iterator and advances
   115  // the iterator.
   116  func (e *OrderedWeightedEdges) WeightedEdgeSlice() []graph.WeightedEdge {
   117  	if e.idx >= len(e.edges) {
   118  		return nil
   119  	}
   120  	idx := e.idx
   121  	if idx == -1 {
   122  		idx = 0
   123  	}
   124  	e.idx = len(e.edges)
   125  	return e.edges[idx:]
   126  }
   127  
   128  // Reset returns the iterator to its initial state.
   129  func (e *OrderedWeightedEdges) Reset() {
   130  	e.idx = -1
   131  }