github.com/gopherd/gonum@v0.0.4/graph/iterator/lines.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 "github.com/gopherd/gonum/graph" 8 9 // OrderedLines implements the graph.Lines and graph.LineSlicer interfaces. 10 // The iteration order of OrderedLines is the order of lines passed to 11 // NewLineIterator. 12 type OrderedLines struct { 13 idx int 14 lines []graph.Line 15 } 16 17 // NewOrderedLines returns an OrderedLines initialized with the provided lines. 18 func NewOrderedLines(lines []graph.Line) *OrderedLines { 19 return &OrderedLines{idx: -1, lines: lines} 20 } 21 22 // Len returns the remaining number of lines to be iterated over. 23 func (e *OrderedLines) Len() int { 24 if e.idx >= len(e.lines) { 25 return 0 26 } 27 if e.idx <= 0 { 28 return len(e.lines) 29 } 30 return len(e.lines[e.idx:]) 31 } 32 33 // Next returns whether the next call of Line will return a valid line. 34 func (e *OrderedLines) Next() bool { 35 if uint(e.idx)+1 < uint(len(e.lines)) { 36 e.idx++ 37 return true 38 } 39 e.idx = len(e.lines) 40 return false 41 } 42 43 // Line returns the current line of the iterator. Next must have been 44 // called prior to a call to Line. 45 func (e *OrderedLines) Line() graph.Line { 46 if e.idx >= len(e.lines) || e.idx < 0 { 47 return nil 48 } 49 return e.lines[e.idx] 50 } 51 52 // LineSlice returns all the remaining lines in the iterator and advances 53 // the iterator. 54 func (e *OrderedLines) LineSlice() []graph.Line { 55 if e.idx >= len(e.lines) { 56 return nil 57 } 58 idx := e.idx 59 if idx == -1 { 60 idx = 0 61 } 62 e.idx = len(e.lines) 63 return e.lines[idx:] 64 } 65 66 // Reset returns the iterator to its initial state. 67 func (e *OrderedLines) Reset() { 68 e.idx = -1 69 } 70 71 // OrderedWeightedLines implements the graph.Lines and graph.LineSlicer interfaces. 72 // The iteration order of OrderedWeightedLines is the order of lines passed to 73 // NewLineIterator. 74 type OrderedWeightedLines struct { 75 idx int 76 lines []graph.WeightedLine 77 } 78 79 // NewWeightedLineIterator returns an OrderedWeightedLines initialized with the provided lines. 80 func NewOrderedWeightedLines(lines []graph.WeightedLine) *OrderedWeightedLines { 81 return &OrderedWeightedLines{idx: -1, lines: lines} 82 } 83 84 // Len returns the remaining number of lines to be iterated over. 85 func (e *OrderedWeightedLines) Len() int { 86 if e.idx >= len(e.lines) { 87 return 0 88 } 89 if e.idx <= 0 { 90 return len(e.lines) 91 } 92 return len(e.lines[e.idx:]) 93 } 94 95 // Next returns whether the next call of WeightedLine will return a valid line. 96 func (e *OrderedWeightedLines) Next() bool { 97 if uint(e.idx)+1 < uint(len(e.lines)) { 98 e.idx++ 99 return true 100 } 101 e.idx = len(e.lines) 102 return false 103 } 104 105 // WeightedLine returns the current line of the iterator. Next must have been 106 // called prior to a call to WeightedLine. 107 func (e *OrderedWeightedLines) WeightedLine() graph.WeightedLine { 108 if e.idx >= len(e.lines) || e.idx < 0 { 109 return nil 110 } 111 return e.lines[e.idx] 112 } 113 114 // WeightedLineSlice returns all the remaining lines in the iterator and advances 115 // the iterator. 116 func (e *OrderedWeightedLines) WeightedLineSlice() []graph.WeightedLine { 117 if e.idx >= len(e.lines) { 118 return nil 119 } 120 idx := e.idx 121 if idx == -1 { 122 idx = 0 123 } 124 e.idx = len(e.lines) 125 return e.lines[idx:] 126 } 127 128 // Reset returns the iterator to its initial state. 129 func (e *OrderedWeightedLines) Reset() { 130 e.idx = -1 131 }