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