struct StaticArray(T, N)
- StaticArray(T, N)
- Value
- Object
Overview
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
- Comparable(StaticArray(T, N))
- Indexable::Mutable(T)
Defined in:
base58/extensions/static_array.crInstance Method Summary
-
#strip(left : Bool = false, right : Bool = true)
A static array has a fixed size.
Instance Method Detail
A static array has a fixed size. If a value smaller than the size of the static array is inserted into it, there may be trailing zeros after the data. This method removes those trailing zeros, and optionally any leading zeros, and returns a Slice containing the stripped data.
Example:
ary = StaticArray[0, 1, 2, 3, 4, 0]
ary.strip # => StaticArray[0, 1, 2, 3, 4]
ary.strip(left: false, right: false) # => StaticArray[0, 1, 2, 3, 4, 0]
ary.strip(left: true, right: false) # => StaticArray[1, 2, 3, 4, 0]
ary.strip(left: false, right: true) # => StaticArray[0, 1, 2, 3, 4]
ary.strip(left: true) # => StaticArray[1, 2, 3, 4]
ary.strip(left: true, right: true) # => StaticArray[1, 2, 3, 4]