Debian Codegraph @

queues/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 priorityqueue implements a priority queue backed by binary queue.
6 //
7 // An unbounded priority queue based on a priority queue.
8 // The elements of the priority queue are ordered by a comparator provided at queue construction time.
9 //
10 // The heap of this queue is the least/smallest element with respect to the specified ordering.
11 // If multiple elements are tied for least value, the heap is one of those elements arbitrarily.
12 //
13 // Structure is not thread safe.
14 //
15 // References: https://en.wikipedia.org/wiki/Priority_queue
16 package priorityqueue
17
18 import (
19 "cmp"
20 "fmt"
21 "strings"
22
23 "github.com/emirpasic/gods/v2/queues"
24 "github.com/emirpasic/gods/v2/trees/binaryheap"
25 "github.com/emirpasic/gods/v2/utils"
26 )
27
28 // Assert Queue implementation
29 var _ queues.Queue[int] = (*Queue[int])(nil)
30
31 // Queue holds elements in an array-list
32 type Queue[T comparable] struct {
33 heap *binaryheap.Heap[T]
34 Comparator utils.Comparator[T]
35 }
36
37 func New[T cmp.Ordered]() *Queue[T] {
38 return NewWith[T](cmp.Compare[T])
39 }
40
41 // NewWith instantiates a new empty queue with the custom comparator.
42 func NewWith[T comparable](comparator utils.Comparator[T]) *Queue[T] {
43 return &Queue[T]{heap: binaryheap.NewWith(comparator), Comparator: comparator}
44 }
45
46 // Enqueue adds a value to the end of the queue
47 func (queue *Queue[T]) Enqueue(value T) {
48 queue.heap.Push(value)
49 }
50
51 // Dequeue removes first element of the queue and returns it, or nil if queue is empty.
52 // Second return parameter is true, unless the queue was empty and there was nothing to dequeue.
53 func (queue *Queue[T]) Dequeue() (value T, ok bool) {
54 return queue.heap.Pop()
55 }
56
57 // Peek returns top element on the queue without removing it, or nil if queue is empty.
58 // Second return parameter is true, unless the queue was empty and there was nothing to peek.
59 func (queue *Queue[T]) Peek() (value T, ok bool) {
60 return queue.heap.Peek()
61 }
62
63 // Empty returns true if queue does not contain any elements.
64 func (queue *Queue[T]) Empty() bool {
65 return queue.heap.Empty()
66 }
67
68 // Size returns number of elements within the queue.
69 func (queue *Queue[T]) Size() int {
70 return queue.heap.Size()
71 }
72
73 // Clear removes all elements from the queue.
74 func (queue *Queue[T]) Clear() {
75 queue.heap.Clear()
76 }
77
78 // Values returns all elements in the queue.
79 func (queue *Queue[T]) Values() []T {
80 return queue.heap.Values()
81 }
82
83 // String returns a string representation of container
84 func (queue *Queue[T]) String() string {
85 str := "PriorityQueue\n"
86 values := make([]string, queue.heap.Size(), queue.heap.Size())
87 for index, value := range queue.heap.Values() {
88 values[index] = fmt.Sprintf("%v", value)
89 }
90 str += strings.Join(values, ", ")
91 return str
92 }