forked from lightninglabs/neutrino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workqueue.go
57 lines (49 loc) · 1.52 KB
/
workqueue.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
package query
// Task is an interface that has a method for returning their index in the
// work queue.
type Task interface {
// Index returns this Task's index in the work queue.
Index() uint64
}
// workQueue is struct implementing the heap interface, and is used to keep a
// list of remaining queryTasks in order.
type workQueue struct {
tasks []Task
}
// Len returns the number of nodes in the priority queue.
//
// NOTE: This is part of the heap.Interface implementation.
func (w *workQueue) Len() int { return len(w.tasks) }
// Less returns whether the item in the priority queue with index i should sort
// before the item with index j.
//
// NOTE: This is part of the heap.Interface implementation.
func (w *workQueue) Less(i, j int) bool {
return w.tasks[i].Index() < w.tasks[j].Index()
}
// Swap swaps the nodes at the passed indices in the priority queue.
//
// NOTE: This is part of the heap.Interface implementation.
func (w *workQueue) Swap(i, j int) {
w.tasks[i], w.tasks[j] = w.tasks[j], w.tasks[i]
}
// Push add x as elemement Len().
//
// NOTE: This is part of the heap.Interface implementation.
func (w *workQueue) Push(x interface{}) {
w.tasks = append(w.tasks, x.(Task))
}
// Pop removes and returns element Len()-1.
//
// NOTE: This is part of the heap.Interface implementation.
func (w *workQueue) Pop() interface{} {
n := len(w.tasks)
x := w.tasks[n-1]
w.tasks[n-1] = nil
w.tasks = w.tasks[0 : n-1]
return x
}
// Peek returns the first item in the queue.
func (w *workQueue) Peek() interface{} {
return w.tasks[0]
}