struct StaticArray(T, N)
Overview
A fixed-size, stack allocated array.
Included Modules
Defined in:
static_array.crClass Method Summary
-
.new(value : T)
Creates a new static array filled with the given value.
-
.new(&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.
Instance Method Summary
-
#==(other)
Equality with another object.
-
#==(other : StaticArray)
Equality.
-
#[](index : Int)
Returns the element at the given index.
-
#[]=(value : T)
Fills the array by substituting all elements with the given value
-
#[]=(index : Int, value : T)
Sets the given value at the given index.
-
#each(&block)
Calls the given block once for each element in
self
, passing that element as a parameter -
#hash
Returns a hash code based on
self
's size and elements. -
#map!(&block)
Invokes the given block for each element of
self
, replacing the element with the value returned by the block. -
#reverse!
Reverses the elements of this array in-place, then returns
self
-
#shuffle!(random = Random::DEFAULT)
Modifies
self
by randomizing the order of elements in the array using the given random number generator. -
#size
Returns the size of
self
-
#to_s(io : IO)
Appends a string representation of this static array to the given IO.
-
#to_slice
Returns a slice that points to the elements of this static array.
-
#to_unsafe : Pointer(T)
Returns a pointer to this static array's data.
-
#update(index : Int, &block)
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. -
#values_at(*indexes : Int)
Returns a tuple populated with the elements at the given indexes.
Macro Summary
-
[](*args)
Create a new
StaticArray
with the given args.
Instance methods inherited from module Enumerable(T)
all?all?(&block) all?, any?
any?(&block) any?, compact_map(&block) compact_map, count(item)
count(&block) count, cycle(&block)
cycle(n, &block) cycle, each(&block : T -> _) each, each_cons(count : Int, &block) each_cons, each_slice(count : Int, &block) each_slice, each_with_index(offset = 0, &block) each_with_index, each_with_object(obj, &block) each_with_object, find(if_none = nil, &block) find, first
first(count : Int) first, first? first?, flat_map(&block : T -> Array(U)) flat_map, grep(pattern) grep, group_by(&block : T -> U) group_by, in_groups_of(size : Int, filled_up_with : U = nil, &block)
in_groups_of(size : Int, filled_up_with : U = nil) in_groups_of, includes?(obj) includes?, index(obj)
index(&block) index, index_by(&block : T -> U) index_by, join(separator = "")
join(separator, io, &block)
join(separator = "", &block)
join(separator, io) join, map(&block : T -> U) map, map_with_index(&block : T, Int32 -> U) map_with_index, max max, max? max?, max_by(&block : T -> U) max_by, max_by?(&block : T -> U) max_by?, max_of(&block : T -> U) max_of, max_of?(&block : T -> U) max_of?, min min, min? min?, min_by(&block : T -> U) min_by, min_by?(&block : T -> U) min_by?, min_of(&block : T -> U) min_of, min_of?(&block : T -> U) min_of?, minmax minmax, minmax? minmax?, minmax_by(&block : T -> U) minmax_by, minmax_by?(&block : T -> U) minmax_by?, minmax_of(&block : T -> U) minmax_of, minmax_of?(&block : T -> U) minmax_of?, none?(&block)
none? none?, one?(&block) one?, partition(&block) partition, product(initial : Number, &block)
product
product(initial : Number)
product(&block) product, reduce(memo, &block)
reduce(&block) reduce, reject(&block : T -> ) reject, select(&block : T -> ) select, size size, skip(count : Int) skip, skip_while(&block) skip_while, sum
sum(initial)
sum(&block)
sum(initial, &block) sum, take_while(&block) take_while, to_a to_a, to_h to_h, to_set to_set
Instance methods inherited from struct Value
==(other)
==
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO) inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
from_json(string_or_io) : self
from_json,
from_yaml(string : String) : self
from_yaml,
hash
hash,
inspect(io)
inspect,
name : String
name,
to_s(io)
to_s,
|(other : U.class)
|
Class Method Detail
Creates a new static array filled with the given value.
StaticArray(Int32, 3).new(42) # => [42, 42, 42]
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 } # => [0, 2, 4]
Instance Method Detail
Equality with another object. Always returns false.
array = StaticArray(Int32, 3).new 0 # => [0, 0, 0]
array == nil # => false
Equality. Returns true if each element in self
is equal to each
corresponding element in other.
array = StaticArray(Int32, 3).new 0 # => [0, 0, 0]
array2 = StaticArray(Int32, 3).new 0 # => [0, 0, 0]
array3 = StaticArray(Int32, 3).new 1 # => [1, 1, 1]
array == array2 # => true
array == array3 # => false
Returns the element 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 } # => [1 ,2 ,3]
array[0] # => 1
array[1] # => 2
array[2] # => 3
array[4] # => IndexError
Fills the array by substituting all elements with the given value
array = StaticArray(Int32, 3).new { |i| i+1 }
array[]= 2 # => [2, 2, 2]
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 } # => [1, 2, 3]
array[2] = 2 # => [1, 2, 2]
array[4] = 4 # => IndexError
Calls the given block once for each element in self
, passing that element as a parameter
array = StaticArray(Int32, 3).new 0 # => [0, 0, 0]
puts array.each { |x| print x, " -- " } # => 0 -- 0 -- 0 -- 3
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 } # => [1, 4, 9]
Reverses the elements of this array in-place, then returns self
array = StaticArray(Int32, 3).new { |i| i + 1 }
array.reverse! # => [3, 2, 1]
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 } # => [1, 2, 3]
a.shuffle!(Random.new(42)) # => [3, 2, 1]
a # => [3, 2, 1]
Returns the size of self
array = StaticArray(Int32, 3).new { |i| i + 1 }
array.size # => 3
Appends a string representation of this static array to the given IO.
array = StaticArray(Int32, 3).new { |i| i + 1 }
array.to_s # => "[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 # => [2, 2, 2]
slice[0] = 3
array # => [3, 2, 2]
Returns a pointer to this static array's data.
ary = StaticArray(Int32, 3).new(42)
ary.to_unsafe[0] # => 42
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 } # => [1, 2, 3]
array.update(1) { |x| x * 2 } # => [1, 4, 3]
array.update(5) { |x| x * 2 } # => IndexError
Returns a tuple populated with the elements at the given indexes. Raises if any index is invalid.
a = StaticArray(Int32, 4).new { |i| i + 1 }
a.values_at(0, 2) # => {1, 3}
Macro Detail
Create 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
.