Debian Codegraph @

graph/iterator/lines_map_safe.go

raw symbols SCIP
1 // Copyright ©2021 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 //go:build safe
6 // +build safe
7
8 package iterator
9
10 import (
11 "reflect"
12
13 "gonum.org/v1/gonum/graph"
14 )
15
16 // Lines implements the graph.Lines interfaces.
17 // The iteration order of Lines is randomized.
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 // NewLines returns a Lines initialized with the provided lines, a
27 // map of line IDs to graph.Lines. No check is made that the keys
28 // match the graph.Line IDs, and the map keys are not used.
29 //
30 // Behavior of the Lines is unspecified if lines is mutated after
31 // the call to NewLines.
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 // Len returns the remaining number of lines to be iterated over.
41 func (l *Lines) Len() int {
42 return l.len - l.pos
43 }
44
45 // Next returns whether the next call of Line will return a valid line.
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 // Line returns the current line of the iterator. Next must have been
59 // called prior to a call to Line.
60 func (l *Lines) Line() graph.Line {
61 return l.curr
62 }
63
64 // Reset returns the iterator to its initial state.
65 func (l *Lines) Reset() {
66 l.curr = nil
67 l.pos = 0
68 l.iter.Reset(l.lines)
69 }
70
71 // LineSlice returns all the remaining lines in the iterator and advances
72 // the iterator. The order of lines within the returned slice is not
73 // specified.
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 // WeightedLines implements the graph.WeightedLines interfaces.
88 // The iteration order of WeightedLines is randomized.
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 // NewWeightedLines returns a WeightedLines initialized with the provided lines, a
98 // map of line IDs to graph.WeightedLines. No check is made that the keys
99 // match the graph.WeightedLine IDs, and the map keys are not used.
100 //
101 // Behavior of the WeightedLines is unspecified if lines is mutated after
102 // the call to NewWeightedLines.
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 // Len returns the remaining number of lines to be iterated over.
112 func (l *WeightedLines) Len() int {
113 return l.len - l.pos
114 }
115
116 // Next returns whether the next call of WeightedLine will return a valid line.
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 // WeightedLine returns the current line of the iterator. Next must have been
130 // called prior to a call to WeightedLine.
131 func (l *WeightedLines) WeightedLine() graph.WeightedLine {
132 return l.curr
133 }
134
135 // Reset returns the iterator to its initial state.
136 func (l *WeightedLines) Reset() {
137 l.curr = nil
138 l.pos = 0
139 l.iter.Reset(l.lines)
140 }
141
142 // WeightedLineSlice returns all the remaining lines in the iterator and advances
143 // the iterator. The order of lines within the returned slice is not
144 // specified.
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 }