StaticArray(T, N)
Inherits Indexable::Mutable / Indexable / Enumerable / Iterable / Comparable / Value / Object
A fixed-size, stack allocated array.
StaticArray is a generic type with type argument T specifying the type of
its elements and N the fixed size. For example StaticArray(Int32, 3)
is a static array of Int32 with three elements.
Instantiations of this static array type:
StaticArray(Int32, 3).new(42) # => StaticArray[42, 42, 42]
StaticArray(Int32, 3).new { |i| i * 2 } # => StaticArray[0, 2, 4]
StaticArray[0, 8, 15] # => StaticArray[0, 8, 15]
This type can also be expressed as Int32[3] (only in type grammar). A typical use
case is in combination with uninitialized:
ints = uninitialized Int32[3]
ints[0] = 0
ints[1] = 8
ints[2] = 15
For number types there is also Number.static_array which can be used to initialize
a static array:
Int32.static_array(0, 8, 15) # => StaticArray[0, 8, 15]
The generic argument type N is a special case in the type grammar as it
doesn't specify a type but a size. Its value can be an Int32 literal or
constant.
Constructors
Creates a new static array and invokes the block once for each index of the array, assigning the block's value in that index.
StaticArray(Int32, 3).new { |i| i * 2 } # => StaticArray[0, 2, 4]
Creates a new static array filled with the given value.
StaticArray(Int32, 3).new(42) # => StaticArray[42, 42, 42]
Instance methods
The comparison operator. Returns 0 if the two objects are equal,
a negative number if this object is considered less than other,
a positive number if this object is considered greater than other,
or nil if the two objects are not comparable.
Subclasses define this method to provide class-specific ordering.
The comparison operator is usually used to sort values:
# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]
# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
Equality. Returns true if each element in self is equal to each
corresponding element in other.
array = StaticArray(Int32, 3).new 0 # => StaticArray[0, 0, 0]
array2 = StaticArray(Int32, 3).new 0 # => StaticArray[0, 0, 0]
array3 = StaticArray(Int32, 3).new 1 # => StaticArray[1, 1, 1]
array == array2 # => true
array == array3 # => false
Equality with another object. Always returns false.
array = StaticArray(Int32, 3).new 0 # => StaticArray[0, 0, 0]
array == nil # => false
Replaces count or less (if there aren't enough) elements starting at the
given start index with value. Returns self.
Negative values of start count from the end of the container.
Raises IndexError if the start index is out of range.
Raises ArgumentError if count is negative.
array = [1, 2, 3, 4, 5]
array.fill(9, 2, 2) # => [1, 2, 9, 9, 5]
array # => [1, 2, 9, 9, 5]
Replaces the elements within the given range with value. Returns self.
Negative indices count backward from the end of the container.
Raises IndexError if the starting index is out of range.
array = [1, 2, 3, 4, 5]
array.fill(9, 2..3) # => [1, 2, 9, 9, 5]
array # => [1, 2, 9, 9, 5]
Replaces every element in self with the given value. Returns self.
array = [1, 2, 3, 4]
array.fill(2) # => [2, 2, 2, 2]
array # => [2, 2, 2, 2]
Returns the index of the first appearance of object in self
starting from the given offset, or nil if object is not in self.
[1, 2, 3, 1, 2, 3].index(2, offset: 2) # => 4
Returns a new static array where elements are mapped by the given block.
array = StaticArray[1, 2.5, "a"]
array.map &.to_s # => StaticArray["1", "2.5", "a"]
Like map, but the block gets passed both the element and its index.
Accepts an optional offset parameter, which tells it to start counting from there.
Shifts all elements of self to the left n times. Returns self.
a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a1.rotate!
a2.rotate!(1)
a3.rotate!(3)
a1 # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
a2 # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
a3 # => [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
Returns the size of self
array = StaticArray(Int32, 3).new { |i| i + 1 }
array.size # => 3
Returns a new instance with all elements sorted based on the return value of
their comparison method T#<=> (see Comparable#<=>), using a stable sort algorithm.
a = StaticArray[3, 1, 2]
a.sort # => StaticArray[1, 2, 3]
a # => StaticArray[3, 1, 2]
See Indexable::Mutable#sort! for details on the sorting mechanism.
Raises ArgumentError if the comparison between any two elements returns nil.
Returns a new instance with all elements sorted based on the comparator in the given block, using a stable sort algorithm.
a = StaticArray[3, 1, 2]
b = a.sort { |a, b| b <=> a }
b # => StaticArray[3, 2, 1]
a # => StaticArray[3, 1, 2]
See Indexable::Mutable#sort!(&block : T, T -> U) for details on the sorting mechanism.
Raises ArgumentError if for any two elements the block returns nil.=
Sorts all elements in self based on the return value of the comparison
method T#<=> (see Comparable#<=>), using a stable sort algorithm.
a = [3, 1, 2]
a.sort!
a # => [1, 2, 3]
This sort operation modifies self. See #sort for a non-modifying option
that allocates a new instance.
See Slice#sort! for details on the implementation.
Raises ArgumentError if the comparison between any two elements returns nil.
Sorts all elements in self based on the comparator in the given block, using
a stable sort algorithm.
The block must implement a comparison between two elements a and b,
where a < b returns -1, a == b returns 0, and a > b returns 1.
The comparison operator <=> can be used for this.
a = [3, 1, 2]
# This is a reverse sort (forward sort would be `a <=> b`)
a.sort! { |a, b| b <=> a }
a # => [3, 2, 1]
This sort operation modifies self. See #sort(&block : T, T -> U) for a
non-modifying option that allocates a new instance.
See Slice#sort!(&block : T, T -> U) for details on the implementation.
Raises ArgumentError if for any two elements the block returns nil.
Returns a new instance with all elements sorted by the output value of the
block. The output values are compared via the comparison method T#<=>
(see Comparable#<=>), using a stable sort algorithm.
a = StaticArray["apple", "pear", "fig"]
b = a.sort_by { |word| word.size }
b # => StaticArray["fig", "pear", "apple"]
a # => StaticArray["apple", "pear", "fig"]
If stability is expendable, #unstable_sort_by(&block : T -> _) provides a
performance advantage over stable sort.
See Indexable::Mutable#sort_by!(&block : T -> _) for details on the sorting mechanism.
Raises ArgumentError if the comparison between any two comparison values returns nil.
Sorts all elements in self by the output value of the
block. The output values are compared via the comparison method #<=>
(see Comparable#<=>), using a stable sort algorithm.
a = %w(apple pear fig)
a.sort_by! { |word| word.size }
a # => ["fig", "pear", "apple"]
This sort operation modifies self. See #sort_by(&block : T -> _) for a
non-modifying option that allocates a new instance.
If stability is expendable, #unstable_sort_by!(&block : T -> _) provides a
performance advantage over stable sort.
See #sort!(&block : T -> _) for details on the sorting mechanism.
Raises ArgumentError if the comparison between any two comparison values returns nil.
Appends a string representation of this static array to the given IO.
array = StaticArray(Int32, 3).new { |i| i + 1 }
array.to_s # => "StaticArray[1, 2, 3]"
Returns a slice that points to the elements of this static array. Changes made to the returned slice also affect this static array.
array = StaticArray(Int32, 3).new(2)
slice = array.to_slice # => Slice[2, 2, 2]
slice[0] = 3
array # => StaticArray[3, 2, 2]
Returns a pointer to this static array's data.
ary = StaticArray(Int32, 3).new(42)
ary.to_unsafe[0] # => 42
Returns the element at the given index, without doing any bounds check.
Indexable makes sure to invoke this method with index in 0...size,
so converting negative indices to positive ones is not needed here.
Clients never invoke this method directly. Instead, they access
elements with #[](index) and #[]?(index).
This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.
Sets the element at the given index to value, without doing any bounds check.
Indexable::Mutable makes sure to invoke this method with index in
0...size, so converting negative indices to positive ones is not needed
here.
Clients never invoke this method directly. Instead, they modify elements
with #[]=(index, value).
This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.
Returns a new instance with all elements sorted based on the return value of
their comparison method T#<=> (see Comparable#<=>), using an unstable sort algorithm.
a = StaticArray[3, 1, 2]
a.unstable_sort # => StaticArray[1, 2, 3]
a # => StaticArray[3, 1, 2]
See Indexable::Mutable#unstable_sort! for details on the sorting mechanism.
Raises ArgumentError if the comparison between any two elements returns nil.
Returns a new instance with all elements sorted based on the comparator in the given block, using an unstable sort algorithm.
a = StaticArray[3, 1, 2]
b = a.unstable_sort { |a, b| b <=> a }
b # => StaticArray[3, 2, 1]
a # => StaticArray[3, 1, 2]
See Indexable::Mutable#unstable_sort!(&block : T, T -> U) for details on the sorting mechanism.
Raises ArgumentError if for any two elements the block returns nil.
Sorts all elements in self based on the return value of the comparison
method T#<=> (see Comparable#<=>), using an unstable sort algorithm.
a = [3, 1, 2]
a.unstable_sort!
a # => [1, 2, 3]
This sort operation modifies self. See #unstable_sort for a non-modifying
option that allocates a new instance.
See Slice#unstable_sort! for details on the implementation.
Raises ArgumentError if the comparison between any two elements returns nil.
Sorts all elements in self based on the comparator in the given block,
using an unstable sort algorithm.
The block must implement a comparison between two elements a and b,
where a < b returns -1, a == b returns 0, and a > b returns 1.
The comparison operator <=> can be used for this.
a = [3, 1, 2]
# This is a reverse sort (forward sort would be `a <=> b`)
a.unstable_sort! { |a, b| b <=> a }
a # => [3, 2, 1]
This sort operation modifies self. See #unstable_sort(&block : T, T -> U)
for a non-modifying option that allocates a new instance.
See Slice#unstable_sort!(&block : T, T -> U) for details on the implementation.
Raises ArgumentError if for any two elements the block returns nil.
Returns a new instance with all elements sorted by the output value of the
block. The output values are compared via the comparison method #<=>
(see Comparable#<=>), using an unstable sort algorithm.
a = StaticArray["apple", "pear", "fig"]
b = a.unstable_sort_by { |word| word.size }
b # => StaticArray["fig", "pear", "apple"]
a # => StaticArray["apple", "pear", "fig"]
If stability is necessary, use #sort_by(&block : T -> _) instead.
See Indexable::Mutable#unstable_sort!(&block : T -> _) for details on the sorting mechanism.
Raises ArgumentError if the comparison between any two comparison values returns nil.
Sorts all elements in self by the output value of the
block. The output values are compared via the comparison method #<=>
(see Comparable#<=>), using an unstable sort algorithm.
a = %w(apple pear fig)
a.unstable_sort_by! { |word| word.size }
a # => ["fig", "pear", "apple"]
This sort operation modifies self. See #unstable_sort_by(&block : T -> _)
for a non-modifying option that allocates a new instance.
If stability is necessary, use #sort_by!(&block : T -> _) instead.
See #unstable_sort!(&block : T -> _) for details on the sorting mechanism.
Raises ArgumentError if the comparison between any two comparison values returns nil.
Macros
Creates a new StaticArray with the given args. The type of the
static array will be the union of the type of the given args,
and its size will be the number of elements in args.
ary = StaticArray[1, 'a']
ary[0] # => 1
ary[1] # => 'a'
ary.class # => StaticArray(Char | Int32, 2)
Number.static_arrayis a convenient alternative for designating a specific numerical item type.