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