Debian Codegraph @

graph/iterator/nodes_map_safe.go

raw symbols SCIP
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 //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 // Nodes implements the graph.Nodes interfaces.
17 // The iteration order of Nodes is randomized.
18 type Nodes struct {
19 iter reflect.MapIter
20 pos, len int
21 curr graph.Node
22 value reflect.Value
23 nodes reflect.Value
24 }
25
26 // NewNodes returns a Nodes initialized with the provided nodes, a
27 // map of node IDs to graph.Nodes. No check is made that the keys
28 // match the graph.Node IDs, and the map keys are not used.
29 //
30 // Behavior of the Nodes is unspecified if nodes is mutated after
31 // the call to NewNodes.
32 func NewNodes(nodes map[int64]graph.Node) *Nodes {
33 rv := reflect.ValueOf(nodes)
34 n := &Nodes{nodes: rv, len: len(nodes)}
35 n.iter.Reset(rv)
36 n.value = reflect.ValueOf(&n.curr).Elem()
37 return n
38 }
39
40 // Len returns the remaining number of nodes to be iterated over.
41 func (n *Nodes) Len() int {
42 return n.len - n.pos
43 }
44
45 // Next returns whether the next call of Node will return a valid node.
46 func (n *Nodes) Next() bool {
47 if n.pos >= n.len {
48 return false
49 }
50 ok := n.iter.Next()
51 if ok {
52 n.pos++
53 n.value.SetIterValue(&n.iter)
54 }
55 return ok
56 }
57
58 // Node returns the current node of the iterator. Next must have been
59 // called prior to a call to Node.
60 func (n *Nodes) Node() graph.Node {
61 return n.curr
62 }
63
64 // Reset returns the iterator to its initial state.
65 func (n *Nodes) Reset() {
66 n.curr = nil
67 n.pos = 0
68 n.iter.Reset(n.nodes)
69 }
70
71 // NodeSlice returns all the remaining nodes in the iterator and advances
72 // the iterator. The order of nodes within the returned slice is not
73 // specified.
74 func (n *Nodes) NodeSlice() []graph.Node {
75 if n.Len() == 0 {
76 return nil
77 }
78 nodes := make([]graph.Node, 0, n.Len())
79 for n.iter.Next() {
80 n.value.SetIterValue(&n.iter)
81 nodes = append(nodes, n.curr)
82 }
83 n.pos = n.len
84 return nodes
85 }
86
87 // NodesByEdge implements the graph.Nodes interfaces.
88 // The iteration order of Nodes is randomized.
89 type NodesByEdge struct {
90 iter reflect.MapIter
91 pos, len int
92 edges reflect.Value
93 curr graph.Node
94 nodes map[int64]graph.Node
95 }
96
97 // NewNodesByEdge returns a NodesByEdge initialized with the
98 // provided nodes, a map of node IDs to graph.Nodes, and the set
99 // of edges, a map of to-node IDs to graph.Edge, that can be
100 // traversed to reach the nodes that the NodesByEdge will iterate
101 // over. No check is made that the keys match the graph.Node IDs,
102 // and the map keys are not used.
103 //
104 // Behavior of the NodesByEdge is unspecified if nodes or edges
105 // is mutated after the call to NewNodes.
106 func NewNodesByEdge(nodes map[int64]graph.Node, edges map[int64]graph.Edge) *NodesByEdge {
107 rv := reflect.ValueOf(edges)
108 n := &NodesByEdge{nodes: nodes, len: len(edges), edges: rv}
109 n.iter.Reset(rv)
110 return n
111 }
112
113 // NewNodesByWeightedEdge returns a NodesByEdge initialized with the
114 // provided nodes, a map of node IDs to graph.Nodes, and the set
115 // of edges, a map of to-node IDs to graph.WeightedEdge, that can be
116 // traversed to reach the nodes that the NodesByEdge will iterate
117 // over. No check is made that the keys match the graph.Node IDs,
118 // and the map keys are not used.
119 //
120 // Behavior of the NodesByEdge is unspecified if nodes or edges
121 // is mutated after the call to NewNodes.
122 func NewNodesByWeightedEdge(nodes map[int64]graph.Node, edges map[int64]graph.WeightedEdge) *NodesByEdge {
123 rv := reflect.ValueOf(edges)
124 n := &NodesByEdge{nodes: nodes, len: len(edges), edges: rv}
125 n.iter.Reset(rv)
126 return n
127 }
128
129 // NewNodesByLines returns a NodesByEdge initialized with the
130 // provided nodes, a map of node IDs to graph.Nodes, and the set
131 // of lines, a map to-node IDs to map of graph.Line, that can be
132 // traversed to reach the nodes that the NodesByEdge will iterate
133 // over. No check is made that the keys match the graph.Node IDs,
134 // and the map keys are not used.
135 //
136 // Behavior of the NodesByEdge is unspecified if nodes or lines
137 // is mutated after the call to NewNodes.
138 func NewNodesByLines(nodes map[int64]graph.Node, lines map[int64]map[int64]graph.Line) *NodesByEdge {
139 rv := reflect.ValueOf(lines)
140 n := &NodesByEdge{nodes: nodes, len: len(lines), edges: rv}
141 n.iter.Reset(rv)
142 return n
143 }
144
145 // NewNodesByWeightedLines returns a NodesByEdge initialized with the
146 // provided nodes, a map of node IDs to graph.Nodes, and the set
147 // of lines, a map to-node IDs to map of graph.WeightedLine, that can be
148 // traversed to reach the nodes that the NodesByEdge will iterate
149 // over. No check is made that the keys match the graph.Node IDs,
150 // and the map keys are not used.
151 //
152 // Behavior of the NodesByEdge is unspecified if nodes or lines
153 // is mutated after the call to NewNodes.
154 func NewNodesByWeightedLines(nodes map[int64]graph.Node, lines map[int64]map[int64]graph.WeightedLine) *NodesByEdge {
155 rv := reflect.ValueOf(lines)
156 n := &NodesByEdge{nodes: nodes, len: len(lines), edges: rv}
157 n.iter.Reset(rv)
158 return n
159 }
160
161 // Len returns the remaining number of nodes to be iterated over.
162 func (n *NodesByEdge) Len() int {
163 return n.len - n.pos
164 }
165
166 // Next returns whether the next call of Node will return a valid node.
167 func (n *NodesByEdge) Next() bool {
168 if n.pos >= n.len {
169 return false
170 }
171 ok := n.iter.Next()
172 if ok {
173 n.pos++
174 n.curr = n.nodes[n.iter.Key().Int()]
175 }
176 return ok
177 }
178
179 // Node returns the current node of the iterator. Next must have been
180 // called prior to a call to Node.
181 func (n *NodesByEdge) Node() graph.Node {
182 return n.curr
183 }
184
185 // Reset returns the iterator to its initial state.
186 func (n *NodesByEdge) Reset() {
187 n.curr = nil
188 n.pos = 0
189 n.iter.Reset(n.edges)
190 }
191
192 // NodeSlice returns all the remaining nodes in the iterator and advances
193 // the iterator. The order of nodes within the returned slice is not
194 // specified.
195 func (n *NodesByEdge) NodeSlice() []graph.Node {
196 if n.Len() == 0 {
197 return nil
198 }
199 nodes := make([]graph.Node, 0, n.Len())
200 for n.iter.Next() {
201 n.curr = n.nodes[n.iter.Key().Int()]
202 nodes = append(nodes, n.curr)
203 }
204 n.pos = n.len
205 return nodes
206 }