View Source ExArray (ex_array v0.1.1)
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.
Represents a keyword list where the index is the key and the value is the element.
Defines the number of values in the array.
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.
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
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.
If fun
is not a function, the call raises ArgumentError
.
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
.
Converts an Erlang's array to an array. All properties (size, elements, default value, fixedness) of the original array are preserved.
If erl_arr
is not an Erlang's array, the call raises ArgumentError
.
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
.
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
.
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.
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>
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 raisesArgumentError
.
- Specifies the initial size of the array; this also implies
{:fixed, true}
- Creates a fixed-size array.
{:default, value}
- Sets the default value for the array to
value
.
- Sets the default value for the array to
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>
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
.
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
.
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.
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.
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
.
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
.
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.
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}
.