Debian Codegraph @

examples/priorityqueue/priorityqueue.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8 "cmp"
9
10 pq "github.com/emirpasic/gods/v2/queues/priorityqueue"
11 )
12
13 // Element is an entry in the priority queue
14 type Element struct {
15 name string
16 priority int
17 }
18
19 // Comparator function (sort by element's priority value in descending order)
20 func byPriority(a, b Element) int {
21 return -cmp.Compare(a.priority, b.priority) // "-" descending order
22 }
23
24 // PriorityQueueExample to demonstrate basic usage of BinaryHeap
25 func main() {
26 a := Element{name: "a", priority: 1}
27 b := Element{name: "b", priority: 2}
28 c := Element{name: "c", priority: 3}
29
30 queue := pq.NewWith(byPriority) // empty
31 queue.Enqueue(a) // {a 1}
32 queue.Enqueue(c) // {c 3}, {a 1}
33 queue.Enqueue(b) // {c 3}, {b 2}, {a 1}
34 _ = queue.Values() // [{c 3} {b 2} {a 1}]
35 _, _ = queue.Peek() // {c 3} true
36 _, _ = queue.Dequeue() // {c 3} true
37 _, _ = queue.Dequeue() // {b 2} true
38 _, _ = queue.Dequeue() // {a 1} true
39 _, _ = queue.Dequeue() // <nil> false (nothing to dequeue)
40 queue.Clear() // empty
41 _ = queue.Empty() // true
42 _ = queue.Size() // 0
43 }