PexQueue v0.1.0 PexQueue View Source

A Queue that uses the First In, First Out (FIFO) method, unless provided with a function to prioritise items.

An example of using the prioritise function might be to order from highest to lowest numbers, regardless of when they are added to the queue. For example, we have this queue:

{:ok, pid} = PexQueue.start_link(fn first, second -> first < second end)
PexQueue.enqueue(pid, 2)
PexQueue.enqueue(pid, 7)
PexQueue.enqueue(pid, 5)

Queue state: [7, 5, 2]

When we add 6 to the queue, it results in:

PexQueue.enqueue(pid, 6)

Queue state: [7, 6, 5, 2]

This is in contrast to a queue with a provided prioritisation function, which would return items in the order they were added.

{:ok, pid} = PexQueue.start_link()
PexQueue.enqueue(pid, 2)
PexQueue.enqueue(pid, 7)
PexQueue.enqueue(pid, 5)

# Queue state: [2, 5, 7]

PexQueue.dequeue(pid) # 2
PexQueue.dequeue(pid) # 5
PexQueue.dequeue(pid) # 7

Link to this section Summary

Functions

Returns the length of the queue

Removes an item from the queue

Adds an item to the queue

Returns the first item in the queue, without altering the queue state

Link to this section Functions

Returns the length of the queue

Removes an item from the queue

Adds an item to the queue

Returns the first item in the queue, without altering the queue state

Link to this function start_link(prioritise \\ fn _, _ -> true end) View Source
start_link((... -> any())) :: {:ok, pid()}

Start a new Priority Queue