View Source ExArray.Array (exarray v0.1.1)

An ExArray.Array is a data structure that contains a collection of elements.

Elements are stored in a Map with their index as the key. This allows for fast random access to elements by index, but slower insertion and deletion operations, unless the element is being added or deleted to the end of the Array.

Summary

Functions

Adds the given element to the end of the Array

Adds the given element to the Array at the given index.

Adds the given element to the Array at the given index.

Returns {:ok, element} if the given index is within the bounds of the Array, otherwise returns {:error, :out_of_bounds}.

Returns the element at the given index if it exists, otherwise raises an ArgumentError

Creates an ExArray.Array with a length of zero, containing no elements.

Creates an ExArray.Array with the given list of elements.

Removes the last element from the Array and returns the resulting Array.

Removes the element at the given index from the Array and returns the resulting Array.

Removes the element at the given index from the Array and returns the resulting Array.

Returns a List of all the elements in the Array. Ordering is retained.

Types

@type t() :: %ExArray.Array{contents: map(), length: non_neg_integer()}

Functions

@spec add(t(), any()) :: t()

Adds the given element to the end of the Array

Examples

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.add(4)
%ExArray.Array{length: 4, contents: %{0 => 1, 1 => 2, 2 => 3, 3 => 4}}
Link to this function

add_at(array, index, element)

View Source
@spec add_at(t(), integer(), any()) :: {:ok, t()} | {:error, :out_of_bounds}

Adds the given element to the Array at the given index.

If the given index is greater than the length of the Array, an {:error, :out_of_bounds} tuple is returned. If the given index is negative, it will be evaluated as an offset from the end of the Array.

If an element currently exists at the given index, it, and all following elements, are shifted right.

Examples

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.add_at(1, 4)
{:ok, %ExArray.Array{length: 4, contents: %{0 => 1, 1 => 4, 2 => 2, 3 => 3}}}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.add_at(-2, 4)
{:ok, %ExArray.Array{length: 4, contents: %{0 => 1, 1 => 4, 2 => 2, 3 => 3}}}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.add_at(4, 4)
{:error, :out_of_bounds}
Link to this function

add_at!(array, index, element)

View Source
@spec add_at!(t(), integer(), any()) :: t()

Adds the given element to the Array at the given index.

If the given index is greater than the length of the Array, an ArgumentError is raised. If the given index is negative, it will be evaluated as an offset from the end of the Array.

If an element currently exists at the given index, it, and all following elements, are shifted right.t()

Examples

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.add_at!(1, 4)
%ExArray.Array{length: 4, contents: %{0 => 1, 1 => 4, 2 => 2, 3 => 3}}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.add_at!(-2, 4)
%ExArray.Array{length: 4, contents: %{0 => 1, 1 => 4, 2 => 2, 3 => 3}}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.add_at!(4, 4)
** (ArgumentError) Index 4 is out of bounds for length 3
@spec get(t(), integer()) :: {:ok, any()} | {:error, :out_of_bounds}

Returns {:ok, element} if the given index is within the bounds of the Array, otherwise returns {:error, :out_of_bounds}.

If the given index is negative, it will be evaluated as an offset from the end of the Array.

Examples

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.get(1)
{:ok, 2}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.get(-1)
{:ok, 3}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.get(4)
{:error, :out_of_bounds}
@spec get!(t(), integer()) :: any()

Returns the element at the given index if it exists, otherwise raises an ArgumentError

If the given index is negative, it will be evaluated as an offset from the end of the Array.

Examples

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.get!(1)
2

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.get!(-1)
3

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.get!(4)
** (ArgumentError) Index 4 is out of bounds for length 3
@spec new() :: t()

Creates an ExArray.Array with a length of zero, containing no elements.

Examples

iex> ExArray.Array.new()
%ExArray.Array{length: 0, contents: %{}}
@spec new([any()]) :: t()

Creates an ExArray.Array with the given list of elements.

Examples

iex> ExArray.Array.new([1, 2, 3])
%ExArray.Array{length: 3, contents: %{0 => 1, 1 => 2, 2 => 3}}
@spec remove(t()) :: t()

Removes the last element from the Array and returns the resulting Array.

If the array is empty, the original Array is returned.

Examples

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.remove()
%ExArray.Array{length: 2, contents: %{0 => 1, 1 => 2}}

iex> ExArray.Array.new([]) |> ExArray.Array.remove()
%ExArray.Array{length: 0, contents: %{}}
@spec remove_at(t(), integer()) :: {:ok, t()} | {:error, :out_of_bounds}

Removes the element at the given index from the Array and returns the resulting Array.

If the given index is invalid, an {:error, :out_of_bounds} tuple is returned. If the given index is negative, it will be evaluated as an offset from the end of the Array.

Examples

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.remove_at(1)
{:ok, %ExArray.Array{length: 2, contents: %{0 => 1, 1 => 3}}}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.remove_at(-1)
{:ok, %ExArray.Array{length: 2, contents: %{0 => 1, 1 => 2}}}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.remove_at(4)
{:error, :out_of_bounds}
Link to this function

remove_at!(array, index)

View Source
@spec remove_at!(t(), integer()) :: t()

Removes the element at the given index from the Array and returns the resulting Array.

If the given index is invalid, an ArgumentError is raised. If the given index is negative, it will be evaluated as an offset from the end of the Array.

Examples

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.remove_at!(1)
%ExArray.Array{length: 2, contents: %{0 => 1, 1 => 3}}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.remove_at!(-1)
%ExArray.Array{length: 2, contents: %{0 => 1, 1 => 2}}

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.remove_at!(4)
** (ArgumentError) Index 4 is out of bounds for length 3
@spec to_list(t()) :: [any()]

Returns a List of all the elements in the Array. Ordering is retained.

Examples

iex> ExArray.Array.new([1, 2, 3]) |> ExArray.Array.to_list()
[1, 2, 3]