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