-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpriority_queue.go
68 lines (57 loc) · 1.41 KB
/
priority_queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package extractor
import (
"container/heap"
"fmt"
)
type sortingStrategy int
const (
count sortingStrategy = iota
countTimesVolume
)
type priorityQueue struct {
boxes []*box
sortingStrategy sortingStrategy
}
func newBoxes(strategy sortingStrategy) *priorityQueue {
boxes := &priorityQueue{make([]*box, 0), strategy}
heap.Init(boxes)
return boxes
}
func (boxes priorityQueue) Len() int {
return len(boxes.boxes)
}
func (boxes priorityQueue) Less(i, j int) bool {
switch boxes.sortingStrategy {
case count:
return boxes.boxes[i].count() > boxes.boxes[j].count()
case countTimesVolume:
return boxes.boxes[i].count()*boxes.boxes[i].volume() > boxes.boxes[j].count()*boxes.boxes[j].volume()
default:
return boxes.boxes[i].count() > boxes.boxes[j].count()
}
}
func (boxes priorityQueue) Swap(i, j int) {
boxes.boxes[i], boxes.boxes[j] = boxes.boxes[j], boxes.boxes[i]
boxes.boxes[i].index = i
boxes.boxes[j].index = j
}
func (boxes *priorityQueue) Pop() interface{} {
old := *boxes
n := len(old.boxes)
item := old.boxes[n-1]
item.index = -1
(*boxes).boxes = old.boxes[0 : n-1]
return item
}
func (boxes *priorityQueue) Push(x interface{}) {
n := len((*boxes).boxes)
item := x.(*box)
item.index = n
(*boxes).boxes = append((*boxes).boxes, item)
}
func (boxes priorityQueue) print() {
fmt.Println(fmt.Sprintf("Len: %d", boxes.Len()))
for i := 0; i < boxes.Len(); i++ {
boxes.boxes[i].print()
}
}