struct StaticArray(T, N)
inherits Value
¶
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.
Included modules
Indexable
Class methods¶
.new(&block : Int32 -> T)
¶
(&block : Int32 -> T)
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]
.new(value : T)
¶
(value : T)
Creates a new static array filled with the given value.
StaticArray(Int32, 3).new(42) # => StaticArray[42, 42, 42]
Methods¶
#==(other : StaticArray)
¶
(other : StaticArray)
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
#==(other)
¶
(other)
Equality with another object. Always returns false
.
array = StaticArray(Int32, 3).new 0 # => StaticArray[0, 0, 0]
array == nil # => false
#[]=(index : Int, value : T)
¶
(index : Int, value : T)
Sets the given value at the given index.
Negative indices can be used to start counting from the end of the array.
Raises IndexError
if trying to set an element outside the array's range.
array = StaticArray(Int32, 3).new { |i| i + 1 } # => StaticArray[1, 2, 3]
array[2] = 2 # => 2
array # => StaticArray[1, 2, 2]
array[4] = 4 # raises IndexError
#fill(value : T) : self
¶
(value : T) : self
Replaces every element in self
with the given value. Returns self
.
array = StaticArray(Int32, 3).new 0 # => StaticArray[0, 0, 0]
array.fill(2) # => StaticArray[2, 2, 2]
array # => StaticArray[2, 2, 2]
#fill(& : Int32 -> T) : self
¶
(& : Int32 -> T) : self
Yields each index of self
to the given block and then assigns
the block's value in that position. Returns self
.
array = StaticArray[2, 1, 1, 1]
array.fill { |i| i * i } # => StaticArray[0, 1, 4, 9]
array # => StaticArray[0, 1, 4, 9]
#map(&block : T -> U) forall U
¶
(&block : T -> U) forall U
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"]
#map!
¶
Invokes the given block for each element of self
, replacing the element
with the value returned by the block. Returns self
.
array = StaticArray(Int32, 3).new { |i| i + 1 }
array.map! { |x| x*x } # => StaticArray[1, 4, 9]
#map_with_index(offset = 0, &block : T, Int32 -> U) forall U
¶
(offset = 0, &block : T, Int32 -> U) forall U
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.
#map_with_index!(offset = 0, &block : T, Int32 -> T)
¶
(offset = 0, &block : T, Int32 -> T)
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.
#reverse!
¶
Reverses the elements of this array in-place, then returns self
.
array = StaticArray(Int32, 3).new { |i| i + 1 }
array.reverse! # => StaticArray[3, 2, 1]
#shuffle!(random = Random::DEFAULT)
¶
(random = Random::DEFAULT)
Modifies self
by randomizing the order of elements in the array
using the given random number generator. Returns self
.
a = StaticArray(Int32, 3).new { |i| i + 1 } # => StaticArray[1, 2, 3]
a.shuffle!(Random.new(42)) # => StaticArray[3, 2, 1]
a # => StaticArray[3, 2, 1]
#size
¶
Returns the size of self
array = StaticArray(Int32, 3).new { |i| i + 1 }
array.size # => 3
#to_s(io : IO) : Nil
¶
(io : IO) : 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]"
#to_slice
¶
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]
#to_unsafe : Pointer(T)
¶
: Pointer(T)
Returns a pointer to this static array's data.
ary = StaticArray(Int32, 3).new(42)
ary.to_unsafe[0] # => 42
#unsafe_fetch(index : Int)
¶
(index : Int)
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.
#update(index : Int
¶
(index : Int
Yields the current element at the given index and updates the value
at the given index with the block's value.
Raises IndexError
if trying to set an element outside the array's range.
array = StaticArray(Int32, 3).new { |i| i + 1 } # => StaticArray[1, 2, 3]
array.update(1) { |x| x * 2 } # => 4
array # => StaticArray[1, 4, 3]
array.update(5) { |x| x * 2 } # raises IndexError
Macros¶
[](*args)
¶
(*args)
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)
See also: Number.static_array
.