lqueue v0.1.0 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_length
The tuple 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
The max number of elements in the queue
Functions
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
Checks if the queue is empty
Filters the queue using a filtering function. The function is supposed to return truthy values for the elements that should be kept in the queue
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_length
of
the queue, those that can’t fit in the queue 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 number of elements in the queue
Returns the max length of the queue
Checks if the given element is in the queue
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
Reverses the order of the elements in the queue
Converts the queue to a list
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_length
The tuple 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.
The max number of elements in the queue.
Functions
Drops the front element of the queue. When the queue is empty, it is not changed.
Examples
iex> [] |> LQueue.from_list(5) |> LQueue.drop() |> LQueue.to_list()
[]
iex> [1, 2, 3] |> LQueue.from_list(5) |> LQueue.drop() |>
...> LQueue.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() |> LQueue.to_list()
[]
iex> [1, 2, 3] |> LQueue.from_list(5) |> LQueue.drop_rear() |>
...> LQueue.to_list()
[1, 2]
Checks if the queue is empty.
Examples
iex> LQueue.new(5) |> LQueue.empty?()
true
iex> [1, 2, 3] |> LQueue.from_list(3) |> LQueue.empty?()
false
Filters the queue using a filtering function. The function is supposed to return truthy values for the elements that should be kept in the queue.
Examples
iex> [1, 2, 3] |> LQueue.from_list(3) |> LQueue.filter(& &1 > 2) |>
...> LQueue.to_list
[3]
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_length
of
the queue, those that can’t fit in the queue are discarded.
Examples
iex> [] |> LQueue.from_list(3) |> LQueue.to_list()
[]
iex> [1, 2, 3] |> LQueue.from_list(3) |> LQueue.to_list()
[1, 2, 3]
iex> [1, 2, 3, 4, 5] |> LQueue.from_list(3) |> LQueue.to_list()
[1, 2, 3]
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 number of elements in the queue
Examples
iex> LQueue.new(10) |> LQueue.length()
0
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.length()
2
Returns the max length of the queue.
Examples
iex> LQueue.new(10) |> LQueue.max_length()
10
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.max_length()
2
Checks if the given element is in the queue.
Examples
iex> [1, 2, 3] |> LQueue.from_list(3) |> LQueue.member?("abc")
false
iex> [1, 2, 3] |> LQueue.from_list(3) |> LQueue.member?(2)
true
Creates a new limited lenght queue.
Examples
iex> LQueue.new(1)
{0, 1, [], []}
iex> LQueue.new(5)
{0, 5, [], []}
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, {0, 5, [], []}}
iex> lqueue |> LQueue.to_list() == []
true
iex> {1, lqueue} = [1, 2] |> LQueue.from_list(2) |> LQueue.pop()
{1, {1, 2, [], [2]}}
iex> lqueue |> LQueue.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, {0, 5, [], []}}
iex> lqueue |> LQueue.to_list() == []
true
iex> {2, lqueue} = [1, 2] |> LQueue.from_list(2) |> LQueue.pop_rear()
{2, {1, 2, [1], []}}
iex> lqueue |> LQueue.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) |>
...> LQueue.to_list()
[1, 2, 10]
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.push(10) |> LQueue.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) |>
...> LQueue.to_list()
[5, 1, 2]
iex> [1, 2] |> LQueue.from_list(2) |> LQueue.push_front(5) |>
...> LQueue.to_list()
[5, 1]