lqueue v1.0.1 LQueue
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.
Summary
Types
The queue can hold any type of elements
The current number of elements in the queue. The number is an integer between
0
and max_count
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
Functions
Removes all the elements from the queue
Drops the front element of the queue. When the queue is empty, it is not changed
Drops the rear element of the queue. When the queue is empty, it is not changed
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
Checks if the queue is full
Gets the front element of the queue. It does not change the queue. When the
queue is empty, nil
is returned
Gets the rear element of the queue. It does not change the queue. When the
queue is empty, nil
is returned
Returns the max number of elements the queue can hold
Creates a new limited lenght queue
Pops an element from the front of the queue. If the queue is empty, nil
is
returned
Pops an element from the rear of the queue. If the queue is empty, nil
is
returned
Pushes a new element to the rear of the queue. When pushing to a full queue, the front element will be discarded
Pushes a new element to the front of the queue. When pushing to a full queue, the rear element will be discarded
Types
The queue can hold any type of elements.
The current number of elements in the queue. The number is an integer between
0
and max_count
The max number of elements in the queue.
Functions
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
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]
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]
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]
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
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
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
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
Creates a new limited lenght queue.
Examples
iex> LQueue.new(1)
%LQueue{len: 0, m_len: 1, r: [], f: []}
iex> LQueue.new(5)
%LQueue{len: 0, m_len: 5, r: [], f: []}
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{len: 0, m_len: 5, r: [], f: []}}
iex> lqueue |> Enum.to_list() == []
true
iex> {1, lqueue} = [1, 2] |> LQueue.from_list(2) |> LQueue.pop()
{1, %LQueue{len: 1, m_len: 2, r: [], f: [2]}}
iex> lqueue |> Enum.to_list() == [2]
true
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{len: 0, m_len: 5, r: [], f: []}}
iex> lqueue |> Enum.to_list() == []
true
iex> {2, lqueue} = [1, 2] |> LQueue.from_list(2) |> LQueue.pop_rear()
{2, %LQueue{len: 1, m_len: 2, r: [1], f: []}}
iex> lqueue |> Enum.to_list() == [1]
true
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]
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]