-
Notifications
You must be signed in to change notification settings - Fork 0
/
min-priority-queue.go
44 lines (37 loc) · 1.11 KB
/
min-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
package datastructures
// MinPriorityQueue represents a min-priority queue data
// structure.
type MinPriorityQueue struct {
minHeap *MinHeap
}
// NewMinPriorityQueue returns a min-priority queue data structure.
func NewMinPriorityQueue() *MinPriorityQueue {
return &MinPriorityQueue{
minHeap: NewMinHeap(),
}
}
// Enqueue adds an item to the priority queue.
func (q *MinPriorityQueue) Enqueue(item float64) *MinPriorityQueue {
q.minHeap.Insert(item)
return q
}
// Dequeue removes the smallest item from the priority queue.
func (q *MinPriorityQueue) Dequeue() (float64, error) {
return q.minHeap.Poll()
}
// Size returns the size of the priority queue.
func (q *MinPriorityQueue) Size() int {
return q.minHeap.Size()
}
// Contains returns true if the item is in the queue; else false.
func (q *MinPriorityQueue) Contains(item float64) bool {
return q.minHeap.Contains(item)
}
// Iterate iterates through the queue and executes the callback function
// f for each iteration.
func (q *MinPriorityQueue) Iterate(f func(index int, item float64)) {
list := q.minHeap.GetList()
for k, v := range list {
f(k, v)
}
}