| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package iterator |
| 9 |
|
| 10 |
import ( |
| 11 |
"reflect" |
| 12 |
|
| 13 |
"gonum.org/v1/gonum/graph" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
type Lines struct { |
| 19 |
iter reflect.MapIter |
| 20 |
pos, len int |
| 21 |
curr graph.Line |
| 22 |
value reflect.Value |
| 23 |
lines reflect.Value |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
func NewLines(lines map[int64]graph.Line) *Lines { |
| 33 |
rv := reflect.ValueOf(lines) |
| 34 |
l := &Lines{lines: rv, len: len(lines)} |
| 35 |
l.iter.Reset(rv) |
| 36 |
l.value = reflect.ValueOf(&l.curr).Elem() |
| 37 |
return l |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func (l *Lines) Len() int { |
| 42 |
return l.len - l.pos |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func (l *Lines) Next() bool { |
| 47 |
if l.pos >= l.len { |
| 48 |
return false |
| 49 |
} |
| 50 |
ok := l.iter.Next() |
| 51 |
if ok { |
| 52 |
l.pos++ |
| 53 |
l.value.SetIterValue(&l.iter) |
| 54 |
} |
| 55 |
return ok |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func (l *Lines) Line() graph.Line { |
| 61 |
return l.curr |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
func (l *Lines) Reset() { |
| 66 |
l.curr = nil |
| 67 |
l.pos = 0 |
| 68 |
l.iter.Reset(l.lines) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
func (l *Lines) LineSlice() []graph.Line { |
| 75 |
if l.Len() == 0 { |
| 76 |
return nil |
| 77 |
} |
| 78 |
lines := make([]graph.Line, 0, l.Len()) |
| 79 |
for l.iter.Next() { |
| 80 |
l.value.SetIterValue(&l.iter) |
| 81 |
lines = append(lines, l.curr) |
| 82 |
} |
| 83 |
l.pos = l.len |
| 84 |
return lines |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
type WeightedLines struct { |
| 90 |
iter reflect.MapIter |
| 91 |
pos, len int |
| 92 |
curr graph.WeightedLine |
| 93 |
value reflect.Value |
| 94 |
lines reflect.Value |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
func NewWeightedLines(lines map[int64]graph.WeightedLine) *WeightedLines { |
| 104 |
rv := reflect.ValueOf(lines) |
| 105 |
l := &WeightedLines{lines: rv, len: len(lines)} |
| 106 |
l.iter.Reset(rv) |
| 107 |
l.value = reflect.ValueOf(&l.curr).Elem() |
| 108 |
return l |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
func (l *WeightedLines) Len() int { |
| 113 |
return l.len - l.pos |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
func (l *WeightedLines) Next() bool { |
| 118 |
if l.pos >= l.len { |
| 119 |
return false |
| 120 |
} |
| 121 |
ok := l.iter.Next() |
| 122 |
if ok { |
| 123 |
l.pos++ |
| 124 |
l.value.SetIterValue(&l.iter) |
| 125 |
} |
| 126 |
return ok |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
func (l *WeightedLines) WeightedLine() graph.WeightedLine { |
| 132 |
return l.curr |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func (l *WeightedLines) Reset() { |
| 137 |
l.curr = nil |
| 138 |
l.pos = 0 |
| 139 |
l.iter.Reset(l.lines) |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
func (l *WeightedLines) WeightedLineSlice() []graph.WeightedLine { |
| 146 |
if l.Len() == 0 { |
| 147 |
return nil |
| 148 |
} |
| 149 |
lines := make([]graph.WeightedLine, 0, l.Len()) |
| 150 |
for l.iter.Next() { |
| 151 |
l.value.SetIterValue(&l.iter) |
| 152 |
lines = append(lines, l.curr) |
| 153 |
} |
| 154 |
l.pos = l.len |
| 155 |
return lines |
| 156 |
} |