queues

April 19, 2026 ยท View on GitHub

sink-logo

queues

queues provides two queue implementations: a double-ended queue (Queue) and a priority queue (PriorityQueue).

Queue

Queue supports operations at both ends and is designed to help you manage ordered collections with small functions for:

  • enqueueing values at the front or back
  • dequeueing values from the front or back
  • peeking at either end without removing values
  • inspecting and clearing the queue

Overview

Use Queue when you want double-ended queue behavior that is easy to read, reuse, and test.

It is especially useful when:

  • items should be processed in arrival order
  • you need flexible access at both ends
  • a deque is a better fit than a simple stack or slice

When to use it

Use Queue when:

  • items should be processed in arrival order
  • you need double-ended access
  • a deque is a better fit than a simple stack or slice

Prefer a slice when:

  • you only need access at one end
  • random access by index is required
  • the collection is small and structure does not matter

API reference

Create a queue

MethodPurpose
NewCreates a new double-ended queue

Add values

MethodPurpose
EnqueueAdds a value to the back
EnqueueFrontAdds a value to the front

Remove values

MethodPurpose
DequeueRemoves and returns the front value
DequeueBackRemoves and returns the back value

Inspect without removing

MethodPurpose
PeekReturns the front value without removing it
PeekBackReturns the back value without removing it

Inspect or reset the queue

MethodPurpose
IsEmptyReports whether the queue is empty
SizeReturns the number of elements
ClearRemoves all elements
ValuesReturns all values as a slice

Notes

  • Prefer the method that most clearly expresses your intent.
  • This structure is most useful when both ends of the collection matter.
  • Handle empty queue operations carefully in calling code; Dequeue and Peek return a default value and false when the queue is empty.

Examples

Examples can be found in the test suite.


PriorityQueue

PriorityQueue is a generic min-heap backed priority queue. Elements are ordered by a custom comparator, so it works with any type.

The comparator follows the same contract as slices.SortFunc: return a negative value if a has higher priority than b, positive if b has higher priority, and zero if they are equal.

Overview

Use PriorityQueue when you want elements dequeued in a defined priority order rather than arrival order.

It is especially useful when:

  • tasks or events have varying urgency
  • you need efficient O(log n) insert and removal
  • a sorted slice would be too costly to maintain on each insert

When to use it

Use PriorityQueue when:

  • elements must be processed by priority, not insertion order
  • you need O(1) peek at the highest-priority element
  • the ordering rule can be expressed as a comparator function

Prefer a slice when:

  • you need random access by index
  • the collection is small and a linear scan is acceptable
  • insertion order is all that matters

API reference

Create a priority queue

MethodPurpose
NewPriorityQueueCreates a priority queue pre-populated with the given items
NewPriorityQueueWithDefaultCreates an empty priority queue with the given comparator

Add values

MethodPurpose
EnqueueInserts a new element in O(log n) time

Remove values

MethodPurpose
DequeueRemoves and returns the highest-priority element in O(log n)

Inspect without removing

MethodPurpose
PeekReturns the highest-priority element without removing it in O(1)

Inspect or reset the queue

MethodPurpose
IsEmptyReports whether the priority queue is empty
LenReturns the number of elements
SizeReturns the number of elements (alias for Len)
ClearRemoves all elements
ValuesReturns a snapshot of all elements (unordered)

Notes

  • The comparator determines priority: if comparator(a, b) < 0, then a is dequeued before b.
  • Values returns elements in heap order, not priority order. Use repeated Dequeue calls to retrieve elements in priority order.
  • NewPriorityQueue heapifies the provided slice in O(n) time; the slice does not need to be sorted.
  • Handle empty queue operations carefully; Dequeue and Peek return a default value and false when the queue is empty.

Examples

Examples can be found in the test suite.