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