github.com/hzck/speedroute@v0.0.0-20201115191102-403b7d0e443f/model/edge.go (about) 1 package model 2 3 import "sort" 4 5 // Edge contains pointers to from and to nodes as well as a list of weights. 6 type Edge struct { 7 from *Node 8 to *Node 9 weights []*Weight 10 } 11 12 // From returns a pointer for the edge's from node. 13 func (edge *Edge) From() *Node { 14 return edge.from 15 } 16 17 // To returns a pointer for the edge's to node. 18 func (edge *Edge) To() *Node { 19 return edge.to 20 } 21 22 // Weights returns a list of the edge's weights or a default weight if no weights are set. 23 func (edge *Edge) Weights() []*Weight { 24 if len(edge.weights) == 0 { 25 return []*Weight{CreateWeight(0)} 26 } 27 return edge.weights 28 } 29 30 // AddWeight adds a weight to the edge. 31 func (edge *Edge) AddWeight(weight *Weight) { 32 edge.weights = append(edge.weights, weight) 33 sort.Sort(ByTime(edge.weights)) 34 } 35 36 // CreateEdge takes from and to node pointers and returns a pointer to a new edge. 37 func CreateEdge(from, to *Node) *Edge { 38 edge := new(Edge) 39 edge.from = from 40 from.AddFromEdge(edge) 41 edge.to = to 42 to.AddToEdge(edge) 43 return edge 44 } 45 46 // FastestTime returns the fastest possible time this edge's weights can provide. 47 func (edge *Edge) FastestTime() int { 48 if len(edge.weights) == 0 { 49 return 0 50 } 51 return edge.weights[0].Time() 52 }