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

t()

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

The current number of elements in the queue.

The number is an integer between 0 and max_count

Link to this type

element() View Source
element() :: term()

The queue can hold any type of elements.

Link to this type

max_count() View Source
max_count() :: pos_integer()

The max number of elements in the queue.

Link to this type

t() View Source
t() :: %LQueue{
  count: count(),
  f: [element()],
  max_count: max_count(),
  r: [element()]
}

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

Link to this function

clear(lqueue) View Source
clear(t()) :: t()

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
Link to this function

drop(lqueue) View Source
drop(t()) :: t()

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]
Link to this function

drop_rear(lqueue) View Source
drop_rear(t()) :: t()

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]
Link to this function

from_list(list, max_count) View Source
from_list([element()], max_count()) :: t()

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
Link to this function

get(lqueue) View Source
get(t()) :: 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
Link to this function

get_rear(lqueue) View Source
get_rear(t()) :: 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
Link to this function

max_count(lqueue) View Source
max_count(t()) :: max_count()

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 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: []}
Link to this function

pop(lqueue) View Source
pop(t()) :: {element() | nil, t()}

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
Link to this function

pop_rear(lqueue) View Source
pop_rear(t()) :: {element() | nil, t()}

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
Link to this function

push(lqueue, element) View Source
push(t(), element()) :: t()

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]
Link to this function

push_front(lqueue, element) View Source
push_front(t(), element()) :: t()

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]