aboutsummaryrefslogtreecommitdiff
path: root/internal/commitquery/priority_queue.go
blob: d6dbf54fc95f312e37a9e1b9c945dc9bdf13165a (about) (plain) (blame)
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 commitquery

import "container/heap"

// priorityQueue orders internal nodes using one query context's comparator.
type priorityQueue struct {
	ctx   *Context
	items []NodeIndex
}

// newPriorityQueue builds one empty priority queue over one query context.
func newPriorityQueue(ctx *Context) *priorityQueue {
	queue := &priorityQueue{ctx: ctx}
	heap.Init(queue)

	return queue
}

// Len reports the number of queued items.
func (queue *priorityQueue) Len() int {
	return len(queue.items)
}

// Less reports whether one heap slot sorts ahead of another.
func (queue *priorityQueue) Less(left, right int) bool {
	return queue.ctx.Compare(queue.items[left], queue.items[right]) > 0
}

// Swap exchanges two heap slots.
func (queue *priorityQueue) Swap(left, right int) {
	queue.items[left], queue.items[right] = queue.items[right], queue.items[left]
}

// Push appends one heap element.
func (queue *priorityQueue) Push(item any) {
	idx, ok := item.(NodeIndex)
	if !ok {
		panic("commitquery: heap push item is not a NodeIndex")
	}

	queue.items = append(queue.items, idx)
}

// Pop removes one heap element.
func (queue *priorityQueue) Pop() any {
	last := len(queue.items) - 1
	item := queue.items[last]
	queue.items = queue.items[:last]

	return item
}

// PushNode inserts one internal node.
func (queue *priorityQueue) PushNode(idx NodeIndex) {
	heap.Push(queue, idx)
}

// PopNode removes the highest-priority internal node.
func (queue *priorityQueue) PopNode() NodeIndex {
	item := heap.Pop(queue)

	idx, ok := item.(NodeIndex)
	if !ok {
		panic("commitquery: heap pop item is not a NodeIndex")
	}

	return idx
}