2012-05-24 22:02:44 +04:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012, Matt T. Proud
|
|
|
|
All rights reserved.
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file.
|
|
|
|
*/
|
2012-05-20 01:59:25 +04:00
|
|
|
|
|
|
|
package utility
|
|
|
|
|
|
|
|
type Item struct {
|
|
|
|
Priority int64
|
2013-01-19 17:48:30 +04:00
|
|
|
Value interface{}
|
2012-05-20 01:59:25 +04:00
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
|
|
|
type PriorityQueue []*Item
|
|
|
|
|
|
|
|
func (q PriorityQueue) Len() int {
|
|
|
|
return len(q)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q PriorityQueue) Less(i, j int) bool {
|
|
|
|
return q[i].Priority > q[j].Priority
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q PriorityQueue) Swap(i, j int) {
|
|
|
|
q[i], q[j] = q[j], q[i]
|
|
|
|
q[i].index = i
|
|
|
|
q[j].index = j
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *PriorityQueue) Push(x interface{}) {
|
|
|
|
queue := *q
|
|
|
|
size := len(queue)
|
|
|
|
queue = queue[0 : size+1]
|
|
|
|
item := x.(*Item)
|
|
|
|
item.index = size
|
|
|
|
queue[size] = item
|
|
|
|
*q = queue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *PriorityQueue) Pop() interface{} {
|
|
|
|
queue := *q
|
|
|
|
size := len(queue)
|
|
|
|
item := queue[size-1]
|
|
|
|
item.index = -1
|
|
|
|
*q = queue[0 : size-1]
|
|
|
|
return item
|
|
|
|
}
|