gonum.org/v1/gonum@v0.14.0/graph/simple/simple.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 simple 6 7 import ( 8 "math" 9 10 "gonum.org/v1/gonum/graph" 11 ) 12 13 // Node is a simple graph node. 14 type Node int64 15 16 // ID returns the ID number of the node. 17 func (n Node) ID() int64 { 18 return int64(n) 19 } 20 21 func newSimpleNode(id int) graph.Node { 22 return Node(id) 23 } 24 25 // Edge is a simple graph edge. 26 type Edge struct { 27 F, T graph.Node 28 } 29 30 // From returns the from-node of the edge. 31 func (e Edge) From() graph.Node { return e.F } 32 33 // To returns the to-node of the edge. 34 func (e Edge) To() graph.Node { return e.T } 35 36 // ReversedLine returns a new Edge with the F and T fields 37 // swapped. 38 func (e Edge) ReversedEdge() graph.Edge { return Edge{F: e.T, T: e.F} } 39 40 // WeightedEdge is a simple weighted graph edge. 41 type WeightedEdge struct { 42 F, T graph.Node 43 W float64 44 } 45 46 // From returns the from-node of the edge. 47 func (e WeightedEdge) From() graph.Node { return e.F } 48 49 // To returns the to-node of the edge. 50 func (e WeightedEdge) To() graph.Node { return e.T } 51 52 // ReversedLine returns a new Edge with the F and T fields 53 // swapped. The weight of the new Edge is the same as 54 // the weight of the receiver. 55 func (e WeightedEdge) ReversedEdge() graph.Edge { return WeightedEdge{F: e.T, T: e.F, W: e.W} } 56 57 // Weight returns the weight of the edge. 58 func (e WeightedEdge) Weight() float64 { return e.W } 59 60 // isSame returns whether two float64 values are the same where NaN values 61 // are equalable. 62 func isSame(a, b float64) bool { 63 return a == b || (math.IsNaN(a) && math.IsNaN(b)) 64 } 65 66 type edgeSetter interface { 67 SetEdge(e graph.Edge) 68 } 69 70 type weightedEdgeSetter interface { 71 SetWeightedEdge(e graph.WeightedEdge) 72 }