ets_deque v0.2.0 EtsDeque View Source

EtsDeque is an Elixir implementation of a double-ended queue (deque), using Erlang's ETS library as a backing store.

Using ETS ensures that all functions in the EtsDeque module execute in amortized O(1) time with a minimum of memory allocations, offering bounded or unbounded operation with high performance and favorable RAM usage.

Using ETS also means that EtsDeque is not a purely functional data structure, and is not suitable for direct concurrent usage in multiple processes. Use the EtsDeque.Server GenServer if you would like safe access to an EtsDeque from multiple processes.

You can push items onto, pop items from, or peek at items from the head or tail of the queue. Additionally, any item can be accessed or replaced by its index using at/2 and replace_at/3.

EtsQueue implements Elixir's Access behaviour and Enumerable and Collectable protocols, so code like deque[0] and Enum.count(deque) and Enum.into([1, 2, 3], EtsDeque.new()) works as it should.

Example

iex> deque = EtsDeque.new(3)
iex> {:ok, deque} = EtsDeque.push_head(deque, :moe)
iex> {:ok, deque} = EtsDeque.push_tail(deque, :larry)
iex> {:ok, deque} = EtsDeque.push_tail(deque, :curly)
iex> :error = EtsDeque.push_head(deque, :shemp)  ## deque is full
iex> {:ok, :curly, deque} = EtsDeque.pop_tail(deque)
iex> {:ok, deque} = EtsDeque.push_tail(deque, :shemp)
iex> Enum.to_list(deque)
[:moe, :larry, :shemp]

Link to this section Summary

Functions

Returns the item at the given index, where index 0 is the head. Returns :error if index is out of bounds.

Returns the item at the given index, where index 0 is the head. Raises ArgumentError if index is out of bounds.

Returns the number of items in the given deque. Equivalent to deque.length.

Creates a deque, optionally limited to a given size.

Returns the item at the head of the queue, or :error if the queue is empty.

Returns the item at the head of the queue, or raises ArgumentError if the queue is empty.

Returns the item at the tail of the queue, or :error if the queue is empty.

Returns the item at the tail of the queue, or raises ArgumentError if the queue is empty.

Removes the item at the head of the queue, returning it along with the updated deque. Returns :error if queue is empty.

Removes the item at the head of the queue, returning it along with the updated deque. Raises ArgumentError if queue is empty.

Removes the item at the tail of the queue, returning it along with the updated deque. Returns :error if queue is empty.

Removes the item at the tail of the queue, returning it along with the updated deque. Raises ArgumentError if queue is empty.

Adds an item onto the head of the queue. Returns the updated deque, or :error if the queue is full.

Adds an item onto the head of the queue. Returns the updated deque, or raises ArgumentError if the queue is full.

Adds an item onto the tail of the queue. Returns the updated deque, or :error if the queue is full.

Adds an item onto the tail of the queue. Returns the updated deque, or raises ArgumentError if the queue is full.

Replaces the item at the given index, returning the updated deque. Returns :error if index is out of bounds.

Replaces the item at the given index, returning the updated deque. Raises ArgumentError if index is out of bounds.

Returns the maximum capacity of the given deque. Equivalent to deque.size.

Link to this section Types

Link to this type

t()

View Source
t() :: %EtsDeque{head: term(), length: term(), size: term(), table: term()}

Link to this section Functions

Link to this function

at(deque, index)

View Source
at(t(), non_neg_integer()) :: {:ok, any()} | :error

Returns the item at the given index, where index 0 is the head. Returns :error if index is out of bounds.

Returns the item at the given index, where index 0 is the head. Raises ArgumentError if index is out of bounds.

Returns the number of items in the given deque. Equivalent to deque.length.

Link to this function

new(size \\ :infinity)

View Source
new(non_neg_integer() | :infinity) :: t()

Creates a deque, optionally limited to a given size.

Link to this function

peek_head(deque)

View Source
peek_head(t()) :: {:ok, any()} | :error

Returns the item at the head of the queue, or :error if the queue is empty.

Link to this function

peek_head!(deque)

View Source
peek_head!(t()) :: any()

Returns the item at the head of the queue, or raises ArgumentError if the queue is empty.

Link to this function

peek_tail(deque)

View Source
peek_tail(t()) :: {:ok, any()} | :error

Returns the item at the tail of the queue, or :error if the queue is empty.

Link to this function

peek_tail!(deque)

View Source
peek_tail!(t()) :: any()

Returns the item at the tail of the queue, or raises ArgumentError if the queue is empty.

Link to this function

pop_head(deque)

View Source
pop_head(t()) :: {:ok, any(), t()} | :error

Removes the item at the head of the queue, returning it along with the updated deque. Returns :error if queue is empty.

Link to this function

pop_head!(deque)

View Source
pop_head!(t()) :: {any(), t()}

Removes the item at the head of the queue, returning it along with the updated deque. Raises ArgumentError if queue is empty.

Link to this function

pop_tail(deque)

View Source
pop_tail(t()) :: {:ok, any(), t()} | :error

Removes the item at the tail of the queue, returning it along with the updated deque. Returns :error if queue is empty.

Link to this function

pop_tail!(deque)

View Source
pop_tail!(t()) :: {any(), t()}

Removes the item at the tail of the queue, returning it along with the updated deque. Raises ArgumentError if queue is empty.

Link to this function

push_head(deque, item)

View Source
push_head(t(), any()) :: {:ok, t()} | :error

Adds an item onto the head of the queue. Returns the updated deque, or :error if the queue is full.

Link to this function

push_head!(deque, item)

View Source
push_head!(t(), any()) :: t()

Adds an item onto the head of the queue. Returns the updated deque, or raises ArgumentError if the queue is full.

Link to this function

push_tail(deque, item)

View Source
push_tail(t(), any()) :: {:ok, t()} | :error

Adds an item onto the tail of the queue. Returns the updated deque, or :error if the queue is full.

Link to this function

push_tail!(deque, item)

View Source
push_tail!(t(), any()) :: t()

Adds an item onto the tail of the queue. Returns the updated deque, or raises ArgumentError if the queue is full.

Link to this function

replace_at(deque, index, item)

View Source
replace_at(t(), non_neg_integer(), any()) :: {:ok, t()} | :error

Replaces the item at the given index, returning the updated deque. Returns :error if index is out of bounds.

Link to this function

replace_at!(deque, index, item)

View Source
replace_at!(t(), non_neg_integer(), any()) :: t()

Replaces the item at the given index, returning the updated deque. Raises ArgumentError if index is out of bounds.

Link to this function

size(deque)

View Source
size(t()) :: non_neg_integer() | :infinity

Returns the maximum capacity of the given deque. Equivalent to deque.size.