View Source ExArray (ex_array v0.1.3)

A wrapper module for Erlang's array.

Link to this section Summary

Types

Represents an :array from Erlang. The representation is not documented and is subject to change without notice.

Defines the array default value.

Defines the array index.

Defines the number maximum of values in the array.

Defines the possible options.

Defines the options accepted by new/1.

Represents a keyword list where the index is the key and the value is the element.

Defines the number of values in the array.

t()

Defines the module typespec

Defines the array value.

Functions

Gets the value used for uninitialized entries.

Check if two arrays are equal using ===.

Fixes the size of the array. This prevents it from growing automatically upon insertion.

Folds the elements of the array using the given function and initial accumulator value. The elements are visited in order from the lowest index to the highest.

Folds the elements of the array right-to-left using the given function and initial accumulator value. The elements are visited in order from the highest index to the lowest.

Converts an Erlang's array to an array. All properties (size, elements, default value, fixedness) of the original array are preserved.

Converts a list to an extendible array. default is used as the value for uninitialized entries of the array.

Converts an ordered list of pairs {index, value} to a corresponding extendible array. default is used as the value for uninitialized entries of the array.

Gets the value of entry index. If index is not a nonnegative integer, or if the array has fixed size and index is larger than the maximum index, the call raises ArgumentError.

Returns true if arr appears to be an array, otherwise false. Note that the check is only shallow; there is no guarantee that arr is a well-formed array representation even if this function returns true.

Checks if the array has fixed size. Returns true if the array is fixed, otherwise false.

Maps the given function onto each element of the array. The elements are visited in order from the lowest index to the highest.

Creates a new, extendible array with initial size zero. The default value is nil.

Creates a new fixed array according to the given options. By default, the array is extendible and has initial size zero. The default value is nil, if not specified.

Makes the array resizable.

Resets entry index to the default value for the array. If the value of entry index is the default value the array will be returned unchanged. Reset will never change size of the array. Shrinking can be done explicitly by calling resize/2.

Changes the size of the array to that reported by sparse_size/1. If the given array has fixed size, the resulting array will also have fixed size.

Changes the size of the array. If size is not a nonnegative integer, the call raises ArgumentError. If the given array has fixed size, the resulting array will also have fixed size.

Sets entry index of the array to val. If index is not a nonnegative integer, or if the array has fixed size and index is larger than the maximum index, the call raises ArgumentError.

Gets the number of entries in the array. Entries are numbered from 0 to size(array)-1; hence, this is also the index of the first entry that is guaranteed to not have been previously set.

Folds the elements of the array using the given function and initial accumulator value, skipping default-valued entries. The elements are visited in order from the lowest index to the highest.

Folds the elements of the array right-to-left using the given function and initial accumulator value, skipping default-valued entries. The elements are visited in order from the highest index to the lowest.

Maps the given function onto each element of the array, skipping default-valued entries. The elements are visited in order from the lowest index to the highest.

Gets the number of entries in the array up until the last non-default valued entry. In other words, returns index+1 if index is the last non-default valued entry in the array, or zero if no such entry exists.

Converts the array to a list, skipping default-valued entries.

Converts the array to an ordered list of pairs {index, value}, skipping default-valued entries.

Converts the array to its underlying Erlang's array.

Converts the array to a list.

Converts the array to an ordered list of pairs {index, value}.

Link to this section Types

@opaque array()

Represents an :array from Erlang. The representation is not documented and is subject to change without notice.

@type default() :: term()

Defines the array default value.

@type index() :: non_neg_integer()

Defines the array index.

@type max() :: non_neg_integer()

Defines the number maximum of values in the array.

@type option() ::
  {:fixed, boolean()}
  | :fixed
  | {:default, term()}
  | {:size, non_neg_integer()}
  | (size :: non_neg_integer())

Defines the possible options.

@type options() :: option() | [option()]

Defines the options accepted by new/1.

@opaque orddict()

Represents a keyword list where the index is the key and the value is the element.

@type size() :: non_neg_integer()

Defines the number of values in the array.

@type t() :: %ExArray{arr: array()}

Defines the module typespec

@type value() :: term()

Defines the array value.

Link to this section Functions

@spec default(t()) :: default()

Gets the value used for uninitialized entries.

Link to this function

equal?(struct1, struct2)

View Source
@spec equal?(t(), t()) :: boolean()

Check if two arrays are equal using ===.

@spec fix(t()) :: t()

Fixes the size of the array. This prevents it from growing automatically upon insertion.

Link to this function

foldl(ex_array, acc, fun)

View Source
@spec foldl(t(), acc, (index(), value(), acc -> acc)) :: acc when acc: var

Folds the elements of the array using the given function and initial accumulator value. The elements are visited in order from the lowest index to the highest.

If fun is not a function, the call raises ArgumentError.

Link to this function

foldr(ex_array, acc, fun)

View Source
@spec foldr(t(), acc, (index(), value(), acc -> acc)) :: acc when acc: var

Folds the elements of the array right-to-left using the given function and initial accumulator value. The elements are visited in order from the highest index to the lowest.

If fun is not a function, the call raises ArgumentError.

Link to this function

from_erlang_array(erl_array)

View Source
@spec from_erlang_array(array()) :: t()

Converts an Erlang's array to an array. All properties (size, elements, default value, fixedness) of the original array are preserved.

If erl_array is not an Erlang's array, the call raises ArgumentError.

Link to this function

from_list(list, default \\ nil)

View Source
@spec from_list(list(), any()) :: t()

Converts a list to an extendible array. default is used as the value for uninitialized entries of the array.

If list is not a proper list, the call raises ArgumentError.

Link to this function

from_orddict(orddict, default \\ nil)

View Source
@spec from_orddict(orddict(), any()) :: t()

Converts an ordered list of pairs {index, value} to a corresponding extendible array. default is used as the value for uninitialized entries of the array.

If orddict is not a proper, ordered list of pairs whose first elements are nonnegative integers, the call raises ArgumentError.

@spec get(t(), index()) :: value()

Gets the value of entry index. If index is not a nonnegative integer, or if the array has fixed size and index is larger than the maximum index, the call raises ArgumentError.

@spec is_array(any()) :: boolean()

Returns true if arr appears to be an array, otherwise false. Note that the check is only shallow; there is no guarantee that arr is a well-formed array representation even if this function returns true.

@spec is_fix(t()) :: boolean()

Checks if the array has fixed size. Returns true if the array is fixed, otherwise false.

@spec map(t(), (index(), value() -> any())) :: t()

Maps the given function onto each element of the array. The elements are visited in order from the lowest index to the highest.

If fun is not a function, the call raises ArgumentError.

@spec new() :: t()

Creates a new, extendible array with initial size zero. The default value is nil.

examples

Examples

iex> ExArray.new()
#ExArray<[], fixed=false, default=nil>
@spec new(options()) :: t()

Creates a new fixed array according to the given options. By default, the array is extendible and has initial size zero. The default value is nil, if not specified.

options is a single term or a list of terms, selected from the following:

  • n or {:size, n}
    • Specifies the initial size of the array; this also implies {:fixed, true}.
    • If n is not a non-negative integer, the call raises ArgumentError.
  • {:fixed, true}
    • Creates a fixed-size array.
  • {:default, value}
    • Sets the default value for the array to value.

examples

Examples

iex> ExArray.new(5)
#ExArray<[nil, nil, nil, nil, nil], fixed=true, default=nil>

iex> ExArray.new(size: 5)
#ExArray<[nil, nil, nil, nil, nil], fixed=true, default=nil>

iex> ExArray.new(fixed: true)
#ExArray<[], fixed=true, default=nil>

iex> ExArray.new(default: 0)
#ExArray<[], fixed=false, default=0>

iex> ExArray.new(size: 5, fixed: true, default: 0)
#ExArray<[0, 0, 0, 0, 0], fixed=true, default=0>
@spec relax(t()) :: t()

Makes the array resizable.

@spec reset(t(), index()) :: t()

Resets entry index to the default value for the array. If the value of entry index is the default value the array will be returned unchanged. Reset will never change size of the array. Shrinking can be done explicitly by calling resize/2.

If index is not a nonnegative integer, or if the array has fixed size and index is larger than the maximum index, the call raises ArgumentError.

@spec resize(t()) :: t()

Changes the size of the array to that reported by sparse_size/1. If the given array has fixed size, the resulting array will also have fixed size.

@spec resize(t(), non_neg_integer()) :: t()

Changes the size of the array. If size is not a nonnegative integer, the call raises ArgumentError. If the given array has fixed size, the resulting array will also have fixed size.

Link to this function

set(ex_array, index, value)

View Source
@spec set(t(), index(), value()) :: t()

Sets entry index of the array to val. If index is not a nonnegative integer, or if the array has fixed size and index is larger than the maximum index, the call raises ArgumentError.

@spec size(t()) :: non_neg_integer()

Gets the number of entries in the array. Entries are numbered from 0 to size(array)-1; hence, this is also the index of the first entry that is guaranteed to not have been previously set.

Link to this function

sparse_foldl(ex_array, acc, fun)

View Source
@spec sparse_foldl(t(), acc, (index(), value(), acc -> acc)) :: acc when acc: var

Folds the elements of the array using the given function and initial accumulator value, skipping default-valued entries. The elements are visited in order from the lowest index to the highest.

If fun is not a function, the call raises ArgumentError.

Link to this function

sparse_foldr(ex_array, acc, fun)

View Source
@spec sparse_foldr(t(), acc, (index(), value(), acc -> acc)) :: acc when acc: var

Folds the elements of the array right-to-left using the given function and initial accumulator value, skipping default-valued entries. The elements are visited in order from the highest index to the lowest.

If fun is not a function, the call raises ArgumentError.

Link to this function

sparse_map(ex_array, fun)

View Source
@spec sparse_map(t(), (index(), value() -> any())) :: t()

Maps the given function onto each element of the array, skipping default-valued entries. The elements are visited in order from the lowest index to the highest.

If fun is not a function, the call raises ArgumentError.

@spec sparse_size(t()) :: non_neg_integer()

Gets the number of entries in the array up until the last non-default valued entry. In other words, returns index+1 if index is the last non-default valued entry in the array, or zero if no such entry exists.

Link to this function

sparse_to_list(ex_array)

View Source
@spec sparse_to_list(t()) :: list()

Converts the array to a list, skipping default-valued entries.

Link to this function

sparse_to_orddict(ex_array)

View Source
@spec sparse_to_orddict(t()) :: orddict()

Converts the array to an ordered list of pairs {index, value}, skipping default-valued entries.

Link to this function

to_erlang_array(ex_array)

View Source
@spec to_erlang_array(t()) :: array()

Converts the array to its underlying Erlang's array.

@spec to_list(t()) :: list()

Converts the array to a list.

@spec to_orddict(t()) :: orddict()

Converts the array to an ordered list of pairs {index, value}.