github.com/hzck/speedroute@v0.0.0-20201115191102-403b7d0e443f/model/node.go (about) 1 package model 2 3 import "math" 4 5 // Node stores all information about nodes like neighboring edges, rewards and if it's revisitable. 6 type Node struct { 7 fromEdges []*Edge 8 toEdges []*Edge 9 id string 10 rewards map[*Reward]int 11 minPathLeft int 12 revisitable bool 13 } 14 15 // FromEdges returns all edges which has this node as a from node. 16 func (node *Node) FromEdges() []*Edge { 17 return node.fromEdges 18 } 19 20 // AddFromEdge adds an edge which has this node as a from edge. 21 func (node *Node) AddFromEdge(edge *Edge) { 22 node.fromEdges = append(node.fromEdges, edge) 23 } 24 25 // ToEdges returns all edges which has this node as a to node. 26 func (node *Node) ToEdges() []*Edge { 27 return node.toEdges 28 } 29 30 // AddToEdge adds an edge which has this node as a to edge. 31 func (node *Node) AddToEdge(edge *Edge) { 32 node.toEdges = append(node.toEdges, edge) 33 } 34 35 // ID returns node id. 36 func (node *Node) ID() string { 37 return node.id 38 } 39 40 // Rewards returns the node rewards as a map with quantity. 41 func (node *Node) Rewards() map[*Reward]int { 42 return node.rewards 43 } 44 45 // AddReward sets a node reward with quantity. 46 func (node *Node) AddReward(reward *Reward, quantity int) { 47 node.rewards[reward] = quantity 48 } 49 50 // MinPathLeft returns minimum path left to end node or math.MaxInt32 if not known. 51 func (node *Node) MinPathLeft() int { 52 return node.minPathLeft 53 } 54 55 // SetMinPathLeft sets the minimum path left to end node. 56 func (node *Node) SetMinPathLeft(mpl int) { 57 node.minPathLeft = mpl 58 } 59 60 // Revisitable returns true if the node is revisitable 61 func (node *Node) Revisitable() bool { 62 return node.revisitable 63 } 64 65 // CreateNode takes id and revisitable as paramteres, returning a pointer to the new node 66 func CreateNode(id string, revisit bool) *Node { 67 node := new(Node) 68 node.id = id 69 node.revisitable = revisit 70 node.rewards = make(map[*Reward]int) 71 node.minPathLeft = math.MaxInt32 72 return node 73 } 74 75 // DijkstraPrio is a list of node pointers and implements heap interface 76 type DijkstraPrio []*Node 77 78 // Len returns length of DijkstraPrio 79 func (dp DijkstraPrio) Len() int { 80 return len(dp) 81 } 82 83 // Less checks which node has shorter minimum path left to end node 84 func (dp DijkstraPrio) Less(i, j int) bool { 85 return dp[i].MinPathLeft() < dp[j].MinPathLeft() 86 } 87 88 // Swap switches places in the DijkstraPrio list 89 func (dp DijkstraPrio) Swap(i, j int) { 90 dp[i], dp[j] = dp[j], dp[i] 91 } 92 93 // Push adds a node into DijkstraPrio list to the correct place 94 func (dp *DijkstraPrio) Push(x interface{}) { 95 *dp = append(*dp, x.(*Node)) 96 } 97 98 // Pop returns the node with the shortest minimum path left to end node 99 func (dp *DijkstraPrio) Pop() interface{} { 100 old := *dp 101 n := len(old) 102 x := old[n-1] 103 *dp = old[0 : n-1] 104 return x 105 }