github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/multi/multi.go (about)

     1  // Copyright ©2014 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  package multi
     6  
     7  import "github.com/jingcheng-WU/gonum/graph"
     8  
     9  // Node here is a duplication of simple.Node
    10  // to avoid needing to import both packages.
    11  
    12  // Node is a simple graph node.
    13  type Node int64
    14  
    15  // ID returns the ID number of the node.
    16  func (n Node) ID() int64 {
    17  	return int64(n)
    18  }
    19  
    20  // Edge is a collection of multigraph edges sharing end points.
    21  type Edge struct {
    22  	F, T graph.Node
    23  
    24  	graph.Lines
    25  }
    26  
    27  // From returns the from-node of the edge.
    28  func (e Edge) From() graph.Node { return e.F }
    29  
    30  // To returns the to-node of the edge.
    31  func (e Edge) To() graph.Node { return e.T }
    32  
    33  // ReversedEdge returns a new Edge with the F and T fields
    34  // swapped.
    35  func (e Edge) ReversedEdge() graph.Edge { return Edge{F: e.T, T: e.F} }
    36  
    37  // Line is a multigraph edge.
    38  type Line struct {
    39  	F, T graph.Node
    40  
    41  	UID int64
    42  }
    43  
    44  // From returns the from-node of the line.
    45  func (l Line) From() graph.Node { return l.F }
    46  
    47  // To returns the to-node of the line.
    48  func (l Line) To() graph.Node { return l.T }
    49  
    50  // ReversedLine returns a new Line with the F and T fields
    51  // swapped. The UID of the new Line is the same as the
    52  // UID of the receiver. The Lines within the Edge are
    53  // not altered.
    54  func (l Line) ReversedLine() graph.Line { l.F, l.T = l.T, l.F; return l }
    55  
    56  // ID returns the ID of the line.
    57  func (l Line) ID() int64 { return l.UID }
    58  
    59  // WeightedEdge is a collection of weighted multigraph edges sharing end points.
    60  type WeightedEdge struct {
    61  	F, T graph.Node
    62  
    63  	graph.WeightedLines
    64  
    65  	// WeightFunc calculates the aggregate
    66  	// weight of the lines in Lines. If
    67  	// WeightFunc is nil, the sum of weights
    68  	// is used as the edge weight.
    69  	// The graph.WeightedLines can be expected
    70  	// to be positioned at the first line of
    71  	// the iterator on entry and must be
    72  	// Reset before exit.
    73  	// WeightFunc must accept a nil input.
    74  	WeightFunc func(graph.WeightedLines) float64
    75  }
    76  
    77  // From returns the from-node of the edge.
    78  func (e WeightedEdge) From() graph.Node { return e.F }
    79  
    80  // To returns the to-node of the edge.
    81  func (e WeightedEdge) To() graph.Node { return e.T }
    82  
    83  // ReversedEdge returns a new Edge with the F and T fields
    84  // swapped. The Lines within the WeightedEdge are not
    85  // altered.
    86  func (e WeightedEdge) ReversedEdge() graph.Edge { e.F, e.T = e.T, e.F; return e }
    87  
    88  // Weight returns the weight of the edge. Weight uses WeightFunc
    89  // field to calculate the weight, so the WeightedLines field is
    90  // expected to be positioned at the first line and is reset before
    91  // Weight returns.
    92  func (e WeightedEdge) Weight() float64 {
    93  	if e.WeightFunc != nil {
    94  		return e.WeightFunc(e.WeightedLines)
    95  	}
    96  	if e.WeightedLines == nil {
    97  		return 0
    98  	}
    99  	var w float64
   100  	for e.Next() {
   101  		w += e.WeightedLine().Weight()
   102  	}
   103  	e.WeightedLines.Reset()
   104  	return w
   105  }
   106  
   107  // WeightedLine is a weighted multigraph edge.
   108  type WeightedLine struct {
   109  	F, T graph.Node
   110  	W    float64
   111  
   112  	UID int64
   113  }
   114  
   115  // From returns the from-node of the line.
   116  func (l WeightedLine) From() graph.Node { return l.F }
   117  
   118  // To returns the to-node of the line.
   119  func (l WeightedLine) To() graph.Node { return l.T }
   120  
   121  // ReversedLine returns a new Line with the F and T fields
   122  // swapped. The UID and W of the new Line are the same as the
   123  // UID and W of the receiver.
   124  func (l WeightedLine) ReversedLine() graph.Line { l.F, l.T = l.T, l.F; return l }
   125  
   126  // ID returns the ID of the line.
   127  func (l WeightedLine) ID() int64 { return l.UID }
   128  
   129  // Weight returns the weight of the edge.
   130  func (l WeightedLine) Weight() float64 { return l.W }