queue
Abstract Data Type for FIFO Queues
This module implements (double ended) FIFO queues in an efficient manner.
All functions fail with reason badarg
if arguments
are of wrong type, for example queue arguments are not
queues, indexes are not integers, list arguments are
not lists. Improper lists cause internal crashes.
An index out of range for a queue also causes
a failure with reason badarg
.
Some functions, where noted, fail with reason empty
for an empty queue.
The data representing a queue as used by this module should be regarded as opaque by other modules. Any code assuming knowledge of the format is running on thin ice.
All operations has an amortized O(1) running time, except
len/1
, join/2
, split/2
, filter/2
and member/2
that have O(n).
To minimize the size of a queue minimizing
the amount of garbage built by queue operations, the queues
do not contain explicit length information, and that is
why len/1
is O(n). If better performance for this
particular operation is essential, it is easy for
the caller to keep track of the length.
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.
The module has several 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 also 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" contain alternative functions that build less garbage as well as 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. The 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.
Original API
Functions
new() -> queue()
Returns an empty queue.
is_queue(Term :: term()) -> boolean()
Tests if
is a queue and returns true
if so and
false
otherwise.
is_empty(Q :: queue()) -> boolean()
Tests if
is empty and returns true
if so and
false
otherwise.
len(Q :: queue()) -> integer() >= 0
Calculates and returns the length of queue
.
in(Item :: term(), Q1 :: queue()) -> Q2 :: queue()
Inserts
at the rear of queue
.
Returns the resulting queue
.
in_r(Item :: term(), Q1 :: queue()) -> Q2 :: queue()
Inserts
at the front of queue
.
Returns the resulting queue
.
out(Q1 :: queue()) ->
{{value, Item :: term()}, Q2 :: queue()} |
{empty, Q1 :: queue()}
Removes the item at the front of queue
. Returns the
tuple {{value,
, where
is the
item removed and
is the resulting queue. If
is
empty, the tuple {empty,
is returned.
out_r(Q1 :: queue()) ->
{{value, Item :: term()}, Q2 :: queue()} |
{empty, Q1 :: queue()}
Removes the item at the rear of the queue
. Returns the
tuple {{value,
, where
is the
item removed and
is the new queue. If
is
empty, the tuple {empty,
is returned.
from_list(L :: list()) -> queue()
Returns a queue containing the items in
in the
same order; the head item of the list will become the front
item of the queue.
to_list(Q :: queue()) -> list()
Returns a list of the items in the queue in the same order; the front item of the queue will become the head of the list.
reverse(Q1 :: queue()) -> Q2 :: queue()
Returns a queue
that contains the items of
in the reverse order.
split(N :: integer() >= 0, Q1 :: queue()) ->
{Q2 :: queue(), Q3 :: queue()}
Splits
in two. The
front items
are put in
and the rest in
join(Q1 :: queue(), Q2 :: queue()) -> Q3 :: queue()
Returns a queue
that is the result of joining
and
with
in front of
.
filter(Fun, Q1 :: queue()) -> Q2 :: queue()
Fun = fun((Item :: term()) -> boolean() | list())
Returns a queue
that is the result of calling
on all items in
,
in order from front to rear.
If
returns true
, Item
is copied to the result queue. If it returns false
,
is not copied. If it returns a list
the list elements are inserted instead of Item
in the
result queue.
So,
returning [
is thereby
semantically equivalent to returning true
, just
as returning []
is semantically equivalent to
returning false
. But returning a list builds
more garbage than returning an atom.
member(Item :: term(), Q :: queue()) -> boolean()
Returns true
if
matches some element
in
, otherwise false
.
Extended API
Functions
get(Q :: queue()) -> Item :: term()
Returns
at the front of queue
.
Fails with reason empty
if
is empty.
get_r(Q :: queue()) -> Item :: term()
Returns
at the rear of queue
.
Fails with reason empty
if
is empty.
drop(Q1 :: queue()) -> Q2 :: queue()
Returns a queue
that is the result of removing
the front item from
.
Fails with reason empty
if
is empty.
drop_r(Q1 :: queue()) -> Q2 :: queue()
Returns a queue
that is the result of removing
the rear item from
.
Fails with reason empty
if
is empty.
peek(Q :: queue()) -> empty | {value, Item :: term()}
Returns the tuple {value,
where
is the
front item of
, or empty
if
is empty.
peek_r(Q :: queue()) -> empty | {value, Item :: term()}
Returns the tuple {value,
where
is the
rear item of
, or empty
if
is empty.
Okasaki API
Functions
cons(Item :: term(), Q1 :: queue()) -> Q2 :: queue()
Inserts
at the head of queue
. Returns
the new queue
.
head(Q :: queue()) -> Item :: term()
Returns
from the head of queue
.
Fails with reason empty
if
is empty.
tail(Q1 :: queue()) -> Q2 :: queue()
Returns a queue
that is the result of removing
the head item from
.
Fails with reason empty
if
is empty.
snoc(Q1 :: queue(), Item :: term()) -> Q2 :: queue()
Inserts
as the tail item of queue
. Returns
the new queue
.