This module provides (double-ended) FIFO queues in an efficient manner.
All functions fail with reason
Some functions, where noted, fail with reason
The data representing a queue as used by this module is to be
regarded as opaque by other modules. In abstract terms, the
representation is a composite type of existing Erlang terms. See
note on
All operations have an amortized O(1) running time, except
Queues are double-ended. The mental picture of a queue is a line of people (items) waiting for their turn. The queue front is the end with the item that has waited the longest. The queue rear is the end an item enters when it starts to wait. If instead using the mental picture of a list, the front is called head and the rear is called tail.
Entering at the front and exiting at the rear are reverse operations on the queue.
This module has three sets of interface functions: the "Original API", the "Extended API", and the "Okasaki API".
The "Original API" and the "Extended API" both use the mental picture of a waiting line of items. Both have reverse operations suffixed "_r".
The "Original API" item removal functions return compound terms with both the removed item and the resulting queue. The "Extended API" contains alternative functions that build less garbage and functions for just inspecting the queue ends. Also the "Okasaki API" functions build less garbage.
The "Okasaki API" is inspired by "Purely Functional Data Structures" by Chris Okasaki. It regards queues as lists. This API is by many regarded as strange and avoidable. For example, many reverse operations have lexically reversed names, some with more readable but perhaps less understandable aliases.
As returned by
Returns
Example:
1> Queue = queue:from_list([1,2,3,4,5]). 2> queue:all(fun (E) -> E > 3 end, Queue). false 3> queue:all(fun (E) -> E > 0 end, Queue). true
Returns
Example:
1> Queue = queue:from_list([1,2,3,4,5]). 2> queue:any(fun (E) -> E > 10 end, Queue). false 3> queue:any(fun (E) -> E > 3 end, Queue). true
Returns a copy of
Example:
1> Queue = queue:from_list([1,2,3,4,5]). 2> Queue1 = queue:delete(3, Queue). 3> queue:member(3, Queue1). false
Returns a copy of
Example:
1> Queue = queue:from_list([1,2,3,4,3,5]). 2> Queue1 = queue:delete_r(3, Queue). 3> queue:to_list(Queue1). [1,2,3,4,5]
Returns a copy of
Example:
1> Queue = queue:from_list([100,1,2,3,4,5]). 2> Queue1 = queue:delete_with(fun (E) -> E > 0, Queue). 3> queue:to_list(Queue1). [1,2,3,4,5]
Returns a copy of
Example:
1> Queue = queue:from_list([1,2,3,4,5,100]). 2> Queue1 = queue:delete_with(fun (E) -> E > 10, Queue). 3> queue:to_list(Queue1). [1,2,3,4,5]
Returns a queue
If
Example 1:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> Queue1 = queue:filter(fun (E) -> E > 2 end, Queue). {[5],[3,4]} 3> queue:to_list(Queue1). [3,4,5]
So,
Example 2:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> Queue1 = queue:filter(fun (E) -> [E, E+1] end, Queue). {[6,5,5,4,4,3],[1,2,2,3]} 3> queue:to_list(Queue1). [1,2,2,3,3,4,4,5,5,6]
Returns a queue
If
Example 1:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> Queue1 = queue:filtermap(fun (E) -> E > 2 end, Queue). {[5],[3,4]} 3> queue:to_list(Queue1). [3,4,5] 4> Queue1 = queue:filtermap(fun (E) -> {true, E+100} end, Queue). {"ihg","ef"} 5> queue:to_list(Queue1). "efghi
Calls
Example:
1> queue:fold(fun(X, Sum) -> X + Sum end, 0, queue:from_list([1,2,3,4,5])). 15 2> queue:fold(fun(X, Prod) -> X * Prod end, 1, queue:from_list([1,2,3,4,5])). 120
Returns a queue containing the items in
Inserts
Example:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> Queue1 = queue:in(100, Queue). {[100,5,4,3],[1,2]} 3> queue:to_list(Queue1). [1,2,3,4,5,100]
Inserts
Example:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> Queue1 = queue:in_r(100, Queue). {[5,4,3],[100,1,2]} 3> queue:to_list(Queue1). [100,1,2,3,4,5]
Tests if
Tests if
Returns a queue
Example:
1> Queue1 = queue:from_list([1,3]). {[3],[1]} 2> Queue2 = queue:from_list([2,4]). {[4],[2]} 3> queue:to_list(queue:join(Queue1, Queue2)). [1,3,2,4]
Calculates and returns the length of queue
Returns
Returns an empty queue.
Removes the item at the front of queue
Example:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> {{value, 1=Item}, Queue1} = queue:out(Queue). {{value,1},{[5,4,3],[2]}} 3> queue:to_list(Queue1). [2,3,4,5]
Removes the item at the rear of queue
Example:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> {{value, 5=Item}, Queue1} = queue:out_r(Queue). {{value,5},{[4,3],[1,2]}} 3> queue:to_list(Queue1). [1,2,3,4]
Returns a queue
Splits
Returns a list of the items in the queue in the same order; the front item of the queue becomes the head of the list.
Example:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> List == queue:to_list(Queue). true
Returns a queue
Fails with reason
Example:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> Queue = queue:drop(Queue). {[5,4,3],[2]} 3> queue:to_list(Queue1). [2,3,4,5]
Returns a queue
Fails with reason
Example:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> Queue = queue:drop_r(Queue). {[4,3],[1,2]} 3> queue:to_list(Queue1). [1,2,3,4]
Returns
Fails with reason
Example 1:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> 1 == queue:get(Queue). true
Returns
Fails with reason
Example 1:
1> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 2> 5 == queue:get_r(Queue). true
Returns tuple
Example 1:
1> queue:peek(queue:new()). empty 2> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 3> queue:peek(Queue). {value, 1}
Returns tuple
Example 1:
1> queue:peek_r(queue:new()). empty 2> Queue = queue:from_list([1,2,3,4,5]). {[5,4,3],[1,2]} 3> queue:peek_r(Queue). {value, 5}
Inserts
Example:
1> Queue = queue:cons(0, queue:from_list([1,2,3])). {[3,2],[0,1]} 2> queue:to_list(Queue). [0,1,2,3]
Returns the tail item of queue
Fails with reason
Example 1:
1> queue:daeh(queue:from_list([1,2,3])). 3
Returns
Fails with reason
Example 1:
1> queue:head(queue:from_list([1,2,3])). 1
Returns a queue
Fails with reason
Example:
1> Queue = queue:init(queue:from_list([1,2,3])). {[2],[1]} 2> queue:to_list(Queue). [1,2]
Returns a queue
Fails with reason
The name
Returns the tail item of queue
Fails with reason
Example:
1> queue:last(queue:from_list([1,2,3])). 3
Returns a queue
Fails with reason
Example:
1> Queue = queue:liat(queue:from_list([1,2,3])). {[2],[1]} 2> queue:to_list(Queue). [1,2]
Inserts
Example:
1> Queue = queue:snoc(queue:from_list([1,2,3]), 4). {[4,3,2],[1]} 2> queue:to_list(Queue). [1,2,3,4]
Returns a queue
Fails with reason