# Heap.PQ(Heap v1.0.0) View Source

A proirity queue implemetation.

The priority of each elements can be customized when `enqueue`.

# Link to this section Summary

## Functions

Dequeue and pick the smallest priority item.

Returns an empty priority queue.

Returns whether the queue is empty.

Enqueue an item into queue with optional priority. If priority is not set, the item itself is considered as the priority.

# dequeue(pq)

View Source

Dequeue and pick the smallest priority item.

If everything is ok, it returns {:ok, new_queue, picked_item}, otherwise it returns {:error, reason}.

## Examples

``````iex> {:ok, _, item} = Heap.PQ.empty() |> Heap.PQ.enqueue(1, -1) |> Heap.PQ.enqueue(3, -3) |> Heap.PQ.enqueue(-3, 3) |> Heap.PQ.dequeue()
iex> item
3``````

# empty()

View Source

Returns an empty priority queue.

## Examples

``````iex> Heap.PQ.empty() |> Heap.PQ.empty?()
true``````

# empty?(pq)

View Source

Returns whether the queue is empty.

# enqueue(pq, item, priority \\ nil)

View Source

Enqueue an item into queue with optional priority. If priority is not set, the item itself is considered as the priority.

Returns a new queue with item enqueued.

## Examples

``````iex> {:ok, _, item} = Heap.PQ.empty() |> Heap.PQ.enqueue(1, -1) |> Heap.PQ.enqueue(3, -3) |> Heap.PQ.enqueue(-3, 3) |> Heap.PQ.dequeue()
iex> item
3``````