class Array(T)
- Array(T)
- Reference
- Object
Overview
An Array is an ordered, integer-indexed collection of objects of type T.
Array indexing starts at 0. A negative index is assumed to be relative to the end of the array: -1 indicates the last element, -2 is the next to last element, and so on.
An Array can be created using the usual new method (several are provided), or with an array literal:
Array(Int32).new # => []
[1, 2, 3] # Array(Int32)
[1, "hello", 'x'] # Array(Int32 | String | Char)
See Array literals in the language reference.
An Array can have mixed types, meaning T will be a union of types, but these are determined
when the array is created, either by specifying T or by using an array literal. In the latter
case, T will be set to the union of the array literal elements' types.
When creating an empty array you must always specify T:
[] of Int32 # same as Array(Int32)
[] # syntax error
An Array is implemented using an internal buffer of some capacity
and is reallocated when elements are pushed to it when more capacity
is needed. This is normally known as a dynamic array.
You can use a special array literal syntax with other types too, as long as they define an argless
new method and a << method. Set is one such type:
set = Set{1, 2, 3} # => Set{1, 2, 3}
set.class # => Set(Int32)
The above is the same as this:
set = Set(typeof(1, 2, 3)).new
set << 1
set << 2
set << 3
Included Modules
- Comparable(Array(T))
- Indexable::Mutable(T)
Defined in:
lib/libremiliacr/src/remilib/extensions.crremiaudio/common.cr
Instance Method Summary
-
#[](index : Int, side : RemiAudio::Side) : T
Returns the sample at the given index for the given side.
-
#[]=(index : Int, side : RemiAudio::Side, value : T) : Nil
Sets the sample at the given index for the given side.
-
#deinterleave(destLeft : Array(T), destRight : Array(T)) : Nil
Treats this array as having interleaved left/right data, and splits the internal buffer into destLeft and destRight.
-
#deinterleave : Tuple(Array(T), Array(T))
Treats this array as having interleaved left/right data, and splits the internal buffer into two new
Arrayinstances. -
#eachLeftWithIndex(& : T -> )
Treats this array as having interleaved left/right data, yielding each element that would appear on the left side to the block.
-
#eachRight(& : T -> )
Treats this array as having interleaved left/right data, yielding each element that would appear on the right side to the block.
-
#eachWithSide(& : Tuple(T, RemiAudio::Side) -> )
Treats this array as having interleaved left/right data, yielding each element along with its side.
-
#interleave(other : Array(T), dest : Array(T)) : Array(T)
Interleaves this audio data with other, storing the results in dest.
-
#interleave(other : Array(T)) : Array(T)
Interleaves this audio data with other.
-
#mapSides!(& : Tuple(T, RemiAudio::Side) -> )
Invokes the given block for each sample in
self, along with the side that sample is on. -
#sizePerSide : Int
Returns the side for one side of the internal buffer.
Instance Method Detail
Returns the sample at the given index for the given side. This treats
this array as having interleaved left/right data, and so index must be
less than half of the total size, or less than #sizePerSide.
Sets the sample at the given index for the given side. This treats this
array as having interleaved left/right data, and so index must be less
than half of the total size, or less than #sizePerSide.
Treats this array as having interleaved left/right data, and splits the internal buffer into destLeft and destRight. The size of destLeft and destRight must be exactly equal to the size of this instance. Additionally, this instance must have a size divisible by 2.
If the size checks fail, this raises a RemiAudioError.
The size checks can be disabled by passing -Dremiaudio_wd40 to the
compiler at build time.
Treats this array as having interleaved left/right data, and splits the
internal buffer into two new Array instances. This returns a Tuple
where the 0th element is the left side, and the other is the right side.
Treats this array as having interleaved left/right data, yielding each
element that would appear on the left side to the block. Returns self.
Treats this array as having interleaved left/right data, yielding each
element that would appear on the right side to the block. Returns self.
Treats this array as having interleaved left/right data, yielding each
element along with its side. Returns self.
Interleaves this audio data with other, storing the results in dest.
The size of other must be exactly the same as this instance's #size, and
the size of dest must be exactly twice to the size of this instance, or an
RemiAudioError is raised.
The size checks can be disabled by passing -Dremiaudio_wd40 to the
compiler at build time.
Interleaves this audio data with other. The size of other must be
exactly the same as this instance's #size, or an RemiAudioError is
raised.
The size check can be disabled by passing -Dremiaudio_wd40 to the compiler
at build time.
Invokes the given block for each sample in self, along with the side that
sample is on. This replaces the sample with the value returned by the
block. Returns self.
Returns the side for one side of the internal buffer. This treats this array as having interleaved left/right data.