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 section Functions
at(deque, index)
View Sourceat(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
.
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.
replace_at(deque, index, item)
View Sourcereplace_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.
replace_at!(deque, index, item)
View Sourcereplace_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.
Returns the maximum capacity of the given deque. Equivalent to deque.size
.