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

element()
element :: any

The queue can hold any type of elements.

length()
length :: non_neg_integer

The current number of elements in the queue. The number is an integer between 0 and max_length

lqueue()

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.

max_length()
max_length :: pos_integer

The max number of elements in the queue.

Functions

drop(lqueue)
drop(lqueue) :: lqueue

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]
drop_rear(lqueue)
drop_rear(lqueue) :: lqueue

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]
empty?(lqueue)
empty?(lqueue) :: boolean

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
filter(arg, fun)
filter(lqueue, (element -> boolean)) :: lqueue

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]
from_list(list, m_len)
from_list([element], max_length) :: lqueue

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]
full?(arg1)
full?(lqueue) :: boolean

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(arg)
get(lqueue) :: element | nil

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(arg)
get_rear(lqueue) :: element | nil

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
length(arg)
length(lqueue) :: length

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
max_length(arg)
max_length(lqueue) :: max_length

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
member?(arg, elem)
member?(lqueue, element) :: boolean

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
new(m_len)
new(max_length) :: lqueue

Creates a new limited lenght queue.

Examples

iex> LQueue.new(1)
{0, 1, [], []}

iex> LQueue.new(5)
{0, 5, [], []}
pop(lqueue)
pop(lqueue) :: {element | nil, lqueue}

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
pop_rear(lqueue)
pop_rear(lqueue) :: {element | nil, lqueue}

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
push(arg, elem)
push(lqueue, element) :: lqueue

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]
push_front(arg, elem)
push_front(lqueue, element) :: lqueue

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]
reverse(arg)
reverse(lqueue) :: lqueue

Reverses the order of the elements in the queue.

Examples

iex> [] |> LQueue.from_list(3) |> LQueue.reverse() |> LQueue.to_list()
[]

iex> [1, 2, 3] |> LQueue.from_list(5) |> LQueue.reverse() |>
...> LQueue.to_list()
[3, 2, 1]
to_list(arg)
to_list(lqueue) :: [element]

Converts the queue to a list.

Examples

iex> LQueue.new(5) |> LQueue.to_list()
[]

iex> [1, 2] |> LQueue.from_list(4) |> LQueue.to_list()
[1, 2]