module Indexable(T)
Overview
A container that allows accessing elements via a numeric index.
Indexing starts at 0
. A negative index is assumed to be
relative to the end of the container: -1
indicates the last element,
-2
is the next to last element, and so on.
Types including this module are typically Array
-like types.
Stability guarantees
Several methods in Indexable
, such as #bsearch
and #cartesian_product
,
require the collection to be stable; that is, calling #each(&)
over and
over again should always yield the same elements, provided the collection is
not mutated between the calls. In particular, #each(&)
itself should not
mutate the collection throughout the loop. Stability of an Indexable
is
guaranteed if the following criteria are met:
#unsafe_fetch
and#size
do not mutate the collection#each(&)
and#each_index(&)
are not overridden
The standard library assumes that all including types of Indexable
are
always stable. It is undefined behavior to implement an Indexable
that is
not stable or only conditionally stable.
Included Modules
- Collection(Int32, T)
- Digable(Int32)
- Enumerable(T)
- Iterable(T)
Defined in:
collection/core_ext.crInstance Method Summary
-
#each_pair(&) : Nil
Must yield this structure's key, value or index, element pairs.
-
#each_pair : Iterator(Tuple(Int32, T))
Provides an
Iterator
for each pair inself
. -
#traverse(*prefix : *T, &) : Nil forall T
Traverse the structure depth first yielding a tuple of the path through the structure and the leaf value for every element to the passed block.
-
#traverse(*prefix : *T) : Iterator forall T
Provide an iterator that may be used to traverse a nested structure.
Instance methods inherited from module Collection(Int32, T)
[](key_or_index : X) : Y
[],
[]?(key_or_index : X) : Y | Nil
[]?,
dig(key : X, *subkeys)
dig,
dig?(key : X, *subkeys)
dig?,
each_pair(&block : Tuple(X, Y) -> ) : Nileach_pair : Iterator(Tuple(X, Y)) each_pair, nested? : Bool nested?
Instance Method Detail
Must yield this structure's key, value or index, element pairs.
Provides an Iterator
for each pair in self
.
NOTE when the base collection provides it own iterator implementation, this method should be overridden to use this directly, this is a generalised implementation and likely sub-optimal in many cases.
Traverse the structure depth first yielding a tuple of the path through the structure and the leaf value for every element to the passed block.
The paths contain a tuple of the keys or indicies used across
intermediate structures, similar to what would be passed to #dig
.
Provide an iterator that may be used to traverse a nested structure.