W3cubDocs

/Crystal

struct StaticArray(T, N)

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

Defined in:

static_array.cr

Constructors

  • .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.

  • .new(value : T)

    Creates a new static array filled with the given value.

Instance Method Summary

Macro Summary

Instance methods inherited from module Indexable(T)

[](index : Int) [], []?(index : Int) []?, bsearch(&block : T -> Bool) bsearch, bsearch_index(&block : T, Int32 -> Bool) bsearch_index, dig(index : Int, *subindexes) dig, dig?(index : Int, *subindexes) dig?, each
each(*, within range : Range, &)
each(&)
each(*, start : Int, count : Int, &) each
, each_index(*, start : Int, count : Int, &)
each_index(&) : Nil
each_index each_index
, empty? empty?, equals?(other : Indexable, &)
equals?(other, &) equals?
, fetch(index, default)
fetch(index : Int, &) fetch
, first(&) first, hash(hasher) hash, index(object, offset : Int = 0)
index(offset : Int = 0, &) index
, join(separator = "") join, last
last(&) last
, last? last?, reverse_each(&) : Nil
reverse_each reverse_each
, rindex(value, offset = size - 1)
rindex(offset = size - 1, &) rindex
, sample(random = Random::DEFAULT) sample, size size, to_a to_a, unsafe_fetch(index : Int) unsafe_fetch, values_at(*indexes : Int) values_at

Instance methods inherited from module Enumerable(T)

all?(&)
all?(pattern)
all? all?
, any?(&)
any?(pattern)
any? any?
, chunks(&block : T -> U) forall U chunks, compact_map(&) compact_map, count(&)
count(item) count
, cycle(&)
cycle(n, &) cycle
, each(&block : T -> _) each, each_cons(count : Int, reuse = false, &) each_cons, each_cons_pair(& : T, T -> _) : Nil each_cons_pair, each_slice(count : Int, reuse = false, &) each_slice, each_with_index(offset = 0, &) each_with_index, each_with_object(obj, &) each_with_object, empty? empty?, find(if_none = nil, &) find, first(&)
first(count : Int)
first first
, first? first?, flat_map(&block : T -> Array(U) | Iterator(U) | U) forall U flat_map, grep(pattern) grep, group_by(&block : T -> U) forall U group_by, in_groups_of(size : Int, filled_up_with : U = nil) forall U
in_groups_of(size : Int, filled_up_with : U = nil, reuse = false, &) forall U in_groups_of
, includes?(obj) includes?, index(&)
index(obj) index
, index_by(&block : T -> U) forall U index_by, join(separator = "", & : T -> )
join(separator, io : IO, &)
join(io : IO, separator = "", & : T, IO -> )
join(separator = "")
join(separator, io : IO)
join(io : IO, separator = "") join
, map(&block : T -> U) forall U map, map_with_index(offset = 0, &block : T, Int32 -> U) forall U map_with_index, max max, max? max?, max_by(&block : T -> U) forall U max_by, max_by?(&block : T -> U) forall U max_by?, max_of(&block : T -> U) forall U max_of, max_of?(&block : T -> U) forall U max_of?, min min, min? min?, min_by(&block : T -> U) forall U min_by, min_by?(&block : T -> U) forall U min_by?, min_of(&block : T -> U) forall U min_of, min_of?(&block : T -> U) forall U min_of?, minmax minmax, minmax? minmax?, minmax_by(&block : T -> U) forall U minmax_by, minmax_by?(&block : T -> U) forall U minmax_by?, minmax_of(&block : T -> U) forall U minmax_of, minmax_of?(&block : T -> U) forall U minmax_of?, none?
none?(pattern)
none?(&) none?
, one?(&)
one?(pattern)
one? one?
, partition(&) partition, product
product(initial : Number)
product(initial : Number, &)
product(&) product
, reduce(memo, &)
reduce(&) reduce
, reduce?(&) reduce?, reject(pattern)
reject(type : U.class) forall U
reject(&block : T -> ) reject
, select(pattern)
select(type : U.class) forall U
select(&block : T -> ) select
, size size, skip(count : Int) skip, skip_while(&) skip_while, sum(initial)
sum
sum(initial, &)
sum(&) sum
, take_while(&) take_while, tally : Hash(T, Int32) tally, to_a to_a, to_h
to_h(&block : T -> Tuple(K, V)) forall K, V to_h
, to_set to_set, zip(*others : Indexable | Iterable | Iterator, &)
zip(*others : Indexable | Iterable | Iterator) zip
, zip?(*others : Indexable | Iterable | Iterator, &)
zip?(*others : Indexable | Iterable | Iterator) zip?

Instance methods inherited from module Iterable(T)

chunk(reuse = false, &block : T -> U) forall U chunk, chunk_while(reuse : Bool | Array(T) = false, &block : T, T -> B) forall B chunk_while, cycle(n)
cycle cycle
, each each, each_cons(count : Int, reuse = false) each_cons, each_slice(count : Int, reuse = false) each_slice, each_with_index(offset = 0) each_with_index, each_with_object(obj) each_with_object, slice_after(reuse : Bool | Array(T) = false, &block : T -> B) forall B
slice_after(pattern, reuse : Bool | Array(T) = false) slice_after
, slice_before(reuse : Bool | Array(T) = false, &block : T -> B) forall B
slice_before(pattern, reuse : Bool | Array(T) = false) slice_before
, slice_when(reuse : Bool | Array(T) = false, &block : T, T -> B) forall B slice_when

Instance methods inherited from struct Value

==(other : JSON::Any)
==(other : YAML::Any)
==(other) ==
, dup dup

Instance methods inherited from class Object

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other) ===
, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash hash
, in?(*values : Object) : Bool
in?(collection) : Bool in?
, inspect : String
inspect(io : IO) : Nil inspect
, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO)
to_json to_json
, to_pretty_json(io : IO, indent : String = " ")
to_pretty_json(indent : String = " ") to_pretty_json
, to_s : String
to_s(io : IO) : Nil to_s
, to_yaml(io : IO)
to_yaml to_yaml
, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

Class methods inherited from class Object

from_json(string_or_io, root : String)
from_json(string_or_io) from_json
, from_yaml(string_or_io : String | IO) from_yaml

Constructor Detail

def self.new(&block : Int32 -> T)Source

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]

def self.new(value : T)Source

Creates a new static array filled with the given value.

StaticArray(Int32, 3).new(42) # => StaticArray[42, 42, 42]

Instance Method Detail

def ==(other : StaticArray)Source

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

def ==(other)Source

Equality with another object. Always returns false.

array = StaticArray(Int32, 3).new 0 # => StaticArray[0, 0, 0]
array == nil                        # => false

def []=(index : Int, value : T)Source

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

def []=(value : T)Source

Fills the array by substituting all elements with the given value.

array = StaticArray(Int32, 3).new { |i| i + 1 }
array.[]= 2 # => nil
array       # => StaticArray[2, 2, 2]

def cloneSource

Returns a new StaticArray where each element is cloned from elements in self.

def map(&block : T -> U) forall USource

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"]

def map!(&)Source

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]

def map_with_index(offset = 0, &block : T, Int32 -> U) forall USource

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.

def map_with_index!(offset = 0, &block : T, Int32 -> T)Source

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.

def pretty_print(pp)Source

def reverse!Source

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]

def shuffle!(random = Random::DEFAULT)Source

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]

def sizeSource

Returns the size of self

array = StaticArray(Int32, 3).new { |i| i + 1 }
array.size # => 3

def to_s(io : IO) : NilSource

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]"

def to_sliceSource

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]

def to_unsafe : Pointer(T)Source

Returns a pointer to this static array's data.

ary = StaticArray(Int32, 3).new(42)
ary.to_unsafe[0] # => 42

def unsafe_fetch(index : Int)Source

Description copied from module Indexable(T)

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.

def update(index : Int, &)Source

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

Macro Detail

macro [](*args)Source

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.

© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/StaticArray.html