lqueue v1.2.0 LQueue View Source
Functions that work on the double-ended queue with limited length.
[1, 2, 3]
queue has the front at the element 1
and the rear at 3
.
By pushing a new element to the queue (4
), and assuming that the max
length of the queue is 3, we will get [2, 3, 4]
(the push/2
function).
Also, it is possible to push to the front of the queue. In this case, by
pushing 4
to the front (push_front/2
) we will get [4, 1, 2]
.
Due to efficiency reasons, the limited length queue is implemented as two lists - the front and the rear list. The rear end is reversed and becomes the new front when the front is empty.
Link to this section Summary
Types
The current number of elements in the queue
The queue can hold any type of elements
The max number of elements in the queue
The structure representing the limited length queue
Functions
Removes all the elements from the queue
Drops the front element of the queue
Drops the rear element of the queue
Converts the list to a queue
Checks if the queue is full
Gets the front element of the queue
Gets the rear element of the queue
Returns the max number of elements the queue can hold
Creates a new limited length queue
Pops an element from the front of the queue
Pops an element from the rear of the queue
Pushes a new element to the rear of the queue
Pushes a new element to the front of the queue
Link to this section Types
count()
View Source
count() :: non_neg_integer()
count() :: non_neg_integer()
The current number of elements in the queue.
The number is an integer between 0
and max_count
element()
View Source
element() :: term()
element() :: term()
The queue can hold any type of elements.
max_count()
View Source
max_count() :: pos_integer()
max_count() :: pos_integer()
The max number of elements in the queue.
The structure representing the limited length queue.
It includes the current number of elements, max number of elements, rear list and front list. Note that this structure should be considered as opaque by other modules.
Link to this section Functions
clear(lqueue) View Source
Removes all the elements from the queue.
Examples
iex> [] |> LQueue.from_list(5) |> LQueue.clear() |> Enum.to_list == []
true
iex> [1, 2, 3] |> LQueue.from_list(5) |> LQueue.clear() |>
...> Enum.to_list == []
true
drop(lqueue) View Source
Drops the front element of the queue.
When the queue is empty, it is not changed.
Examples
iex> [] |> LQueue.from_list(5) |> LQueue.drop() |> Enum.to_list()
[]
iex> [1, 2, 3] |> LQueue.from_list(5) |> LQueue.drop() |>
...> Enum.to_list()
[2, 3]
drop_rear(lqueue) View Source
Drops the rear element of the queue.
When the queue is empty, it is not changed.
Examples
iex> [] |> LQueue.from_list(5) |> LQueue.drop() |> Enum.to_list()
[]
iex> [1, 2, 3] |> LQueue.from_list(5) |> LQueue.drop_rear() |>
...> Enum.to_list()
[1, 2]
from_list(list, max_count) View Source
Converts the list to a queue.
The elements are pushed into the queue starting with the list head. If the
list has more elements than the max_count
of the queue, those that can't
fit in the queue (at the front) are discarded.
Examples
iex> [] |> LQueue.from_list(3) |> Enum.to_list()
[]
iex> [1, 2, 3] |> LQueue.from_list(3) |> Enum.to_list()
[1, 2, 3]
iex> [1, 2, 3, 4, 5] |> LQueue.from_list(3) |> Enum.to_list()
[3, 4, 5]
full?(lqueue) View Source
Checks if the queue is full.
Examples
iex> [1, 2] |> LQueue.from_list(3) |> LQueue.full?()
false
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.full?()
true
get(lqueue) View Source
Gets the front element of the queue.
It does not change the queue. When the queue is empty, nil
is returned.
Examples
iex> [] |> LQueue.from_list(5) |> LQueue.get()
nil
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.get()
1
get_rear(lqueue) View Source
Gets the rear element of the queue.
It does not change the queue. When the queue is empty, nil
is returned.
Examples
iex> [] |> LQueue.from_list(5) |> LQueue.get_rear()
nil
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.get_rear()
2
max_count(lqueue) View Source
Returns the max number of elements the queue can hold.
Examples
iex> LQueue.new(10) |> LQueue.max_count()
10
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.max_count()
2
new(max_count) View Source
Creates a new limited length queue.
Examples
iex> LQueue.new(1)
%LQueue{count: 0, max_count: 1, r: [], f: []}
iex> LQueue.new(5)
%LQueue{count: 0, max_count: 5, r: [], f: []}
pop(lqueue) View Source
Pops an element from the front of the queue.
If the queue is empty, nil
is returned.
Examples
iex> {nil, lqueue} = [] |> LQueue.from_list(5) |> LQueue.pop()
{nil, %LQueue{count: 0, max_count: 5, r: [], f: []}}
iex> lqueue |> Enum.to_list() == []
true
iex> {1, lqueue} = [1, 2] |> LQueue.from_list(2) |> LQueue.pop()
{1, %LQueue{count: 1, max_count: 2, r: [], f: [2]}}
iex> lqueue |> Enum.to_list() == [2]
true
pop_rear(lqueue) View Source
Pops an element from the rear of the queue.
If the queue is empty, nil
is returned.
Examples
iex> {nil, lqueue} = [] |> LQueue.from_list(5) |> LQueue.pop_rear()
{nil, %LQueue{count: 0, max_count: 5, r: [], f: []}}
iex> lqueue |> Enum.to_list() == []
true
iex> {2, lqueue} = [1, 2] |> LQueue.from_list(2) |> LQueue.pop_rear()
{2, %LQueue{count: 1, max_count: 2, r: [1], f: []}}
iex> lqueue |> Enum.to_list() == [1]
true
push(lqueue, element) View Source
Pushes a new element to the rear of the queue.
When pushing to a full queue, the front element will be discarded.
Examples
iex> [1, 2] |> LQueue.from_list(3) |> LQueue.push(10) |>
...> Enum.to_list()
[1, 2, 10]
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.push(10) |> Enum.to_list()
[2, 10]
push_front(lqueue, element) View Source
Pushes a new element to the front of the queue.
When pushing to a full queue, the rear element will be discarded.
Examples
iex> [1, 2] |> LQueue.from_list(3) |> LQueue.push_front(5) |>
...> Enum.to_list()
[5, 1, 2]
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.push_front(5) |>
...> Enum.to_list()
[5, 1]