simple.go 1.7 KB

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