PriorityQueue

GitHub   Edit on GitHub

A mutable priority queue implementation. A priority queue is a data structure that maintains elements in a priority order. Elements with higher priority are served before elements with lower priority when extracting from the priority queue.

Added in 0.5.3 No other changes yet.
1
import PriorityQueue from "priorityqueue"

Types

Type declarations included in the PriorityQueue module.

PriorityQueue.PriorityQueue

1
type PriorityQueue<a>

Mutable data structure which maintains a priority order for its elements.


Values

Functions for working with PriorityQueues.

PriorityQueue.makeSized

Added in 0.5.3 No other changes yet.
1
makeSized : (Number, ((a, a) -> Number)) -> PriorityQueue<a>

Creates a new priority queue with a given internal storage size and a comparator function, which is used to determine priority of elements. The comparator function takes two elements and must return 0 if both share priority, a positive number if the first has greater priority, and a negative number if the first has less priority.

Generally, you won’t need to care about the storage size of your priority queue and can use PriorityQueue.make() instead.

Parameters:

param type description
size Number The initial storage size of the priority queue
comp (a, a) -> Number The comparator function used to indicate priority order

Returns:

type description
PriorityQueue<a> An empty priority queue

PriorityQueue.make

Added in 0.5.3 No other changes yet.
1
make : ((a, a) -> Number) -> PriorityQueue<a>

Creates a new priority queue with a comparator function, which is used to determine priority of elements. The comparator function takes two elements and must return 0 if both share priority, a positive number if the first has greater priority, and a negative number if the first has less priority.

Parameters:

param type description
comp (a, a) -> Number The comparator function used to indicate priority order

Returns:

type description
PriorityQueue<a> An empty priority queue

Examples:

1
PriorityQueue.make(compare) // creates a min priority queue of numbers using the compare pervasive
1
PriorityQueue.make((a, b) => String.length(b) - String.length(a)) // creates a priority queue by string length (longest to shortest)

PriorityQueue.size

Added in 0.5.3 No other changes yet.
1
size : PriorityQueue<a> -> Number

Gets the number of elements in a priority queue.

Parameters:

param type description
pq PriorityQueue<a> The priority queue to inspect

Returns:

type description
Number The number of elements in the priority queue

PriorityQueue.isEmpty

Added in 0.5.3 No other changes yet.
1
isEmpty : PriorityQueue<a> -> Bool

Determines if the priority queue contains no elements.

Parameters:

param type description
pq PriorityQueue<a> The priority queue to check

Returns:

type description
Bool true if the priority queue is empty and false otherwise

PriorityQueue.push

Added in 0.5.3 No other changes yet.
1
push : (a, PriorityQueue<a>) -> Void

Adds a new element to the priority queue.

Parameters:

param type description
val a The value to add into the priority queue
pq PriorityQueue<a> The priority queue to update

PriorityQueue.peek

Added in 0.5.3 No other changes yet.
1
peek : PriorityQueue<a> -> Option<a>

Retrieves the highest priority element in the priority queue. It is not removed from the queue.

Parameters:

param type description
pq PriorityQueue<a> The priority queue to inspect

Returns:

type description
Option<a> Some(value) containing the highest priority element or None if the priority queue is empty

PriorityQueue.pop

Added in 0.5.3 No other changes yet.
1
pop : PriorityQueue<a> -> Option<a>

Removes and retrieves the highest priority element in the priority queue.

Parameters:

param type description
pq PriorityQueue<a> The priority queue to inspect

Returns:

type description
Option<a> Some(value) containing the highest priority element or None if the priority queue is empty

PriorityQueue.drain

Added in 0.5.3 No other changes yet.
1
drain : PriorityQueue<a> -> List<a>

Clears the priority queue and produces a list of all of the elements in the priority queue in priority order.

Parameters:

param type description
pq PriorityQueue<a> The priority queue to drain

Returns:

type description
List<a> A list of all elements in the priority in priority order

PriorityQueue.fromList

Added in 0.5.3 No other changes yet.
1
fromList : (List<a>, ((a, a) -> Number)) -> PriorityQueue<a>

Constructs a new priority queue initialized with the elements in the list using a custom comparator function, which is used to determine priority of elements. The comparator function takes two elements and must return 0 if both share priority, a positive number if the first has greater priority, and a negative number if the first has less priority.

Parameters:

param type description
list List<a> A list of values used to initialize the priority queue
comp (a, a) -> Number A comparator function used to assign priority to elements

Returns:

type description
PriorityQueue<a> A priority queue containing the elements from the list
This is a notification!