File tree Expand file tree Collapse file tree 1 file changed +67
-0
lines changed Expand file tree Collapse file tree 1 file changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ package graph
2+
3+ import (
4+ "container/heap"
5+ )
6+
7+ type prioQueue struct {
8+ heap []int // vertices in heap order
9+ index []int // index of each vertex in the heap
10+ cost []int64
11+ }
12+
13+ func emptyQueue (cost []int64 ) * prioQueue {
14+ return & prioQueue {
15+ index : make ([]int , len (cost )),
16+ cost : cost ,
17+ }
18+ }
19+
20+ func newQueue (cost []int64 ) * prioQueue {
21+ n := len (cost )
22+ h := & prioQueue {
23+ heap : make ([]int , n ),
24+ index : make ([]int , n ),
25+ cost : cost ,
26+ }
27+ for i := range h .heap {
28+ h .heap [i ] = i
29+ h .index [i ] = i
30+ }
31+ return h
32+ }
33+
34+ func (m * prioQueue ) Len () int { return len (m .heap ) }
35+
36+ func (m * prioQueue ) Less (i , j int ) bool {
37+ return m.cost [m.heap [i ]] < m.cost [m.heap [j ]]
38+ }
39+
40+ func (m * prioQueue ) Swap (i , j int ) {
41+ m .heap [i ], m .heap [j ] = m .heap [j ], m .heap [i ]
42+ m.index [m.heap [i ]] = i
43+ m.index [m.heap [j ]] = j
44+ }
45+
46+ func (pq * prioQueue ) Push (x interface {}) {
47+ n := len (pq .heap )
48+ v := x .(int )
49+ pq .heap = append (pq .heap , v )
50+ pq .index [v ] = n
51+ }
52+
53+ func (m * prioQueue ) Pop () interface {} {
54+ n := len (m .heap ) - 1
55+ v := m .heap [n ]
56+ m .index [v ] = - 1
57+ m .heap = m .heap [:n ]
58+ return v
59+ }
60+
61+ func (m * prioQueue ) Update (v int ) {
62+ heap .Fix (m , m .index [v ])
63+ }
64+
65+ func (m * prioQueue ) Contains (v int ) bool {
66+ return m .index [v ] >= 0
67+ }
You can’t perform that action at this time.
0 commit comments