class Array(T)

Overview

An Array is an ordered, integer-indexed collection of objects of type T.

Array indexing starts at 0. A negative index is assumed to be relative to the end of the array: -1 indicates the last element, -2 is the next to last element, and so on.

An Array can be created using the usual .new method (several are provided), or with an array literal:

Array(Int32).new  # => []
[1, 2, 3]         # Array(Int32)
[1, "hello", 'x'] # Array(Int32 | String | Char)

An Array can have mixed types, meaning T will be a union of types, but these are determined when the array is created, either by specifying T or by using an array literal. In the latter case, T will be set to the union of the array literal elements' types.

When creating an empty array you must always specify T:

[] of Int32 # same as Array(Int32)
[]          # syntax error

An Array is implemented using an internal buffer of some capacity and is reallocated when elements are pushed to it when more capacity is needed. This is normally known as a dynamic array.

You can use a special array literal syntax with other types too, as long as they define an argless .new method and a << method. Set is one such type:

set = Set{1, 2, 3} # => Set{1, 2, 3}
set.class          # => Set(Int32)

The above is the same as this:

set = Set(typeof(1, 2, 3)).new
set << 1
set << 2
set << 3

Included Modules

Direct Known Subclasses

Defined in:

point.cr
array/new.cr
collection/change.cr
collection/compress.cr
collection/convex_hull.cr
collection/unique_permutation.cr
math/gcd.cr

Constructors

Instance Method Summary

Instance methods inherited from module Comparable(Array(T))

max(x : T) max, min(x : T) min

Instance methods inherited from module Indexable(T)

[](point : Point) [], []?(point : Point) []?, each_pair(&)
each_pair
each_pair
, fetch(point : Point, &) fetch

Instance methods inherited from module Enumerable(T)

accumulate(init : U) : Array(U) forall U
accumulate : Array(T)
accumulate(init : U, &block : U, T -> U) : Array(U) forall U
accumulate(&block : T, T -> T) : Array(T)
accumulate
, mex : T mex, mex_sorted : T mex_sorted, tally(*, default : Int32) : Hash(T, Int32) tally, unique : self
unique(&) : self
unique

Constructor Detail

def self.new(sizes : Tuple(*I), initial_value) forall I #

[View source]
def self.new(sizes : Tuple(*I), &) forall I #

[View source]

Instance Method Detail

def []=(point : Point, value) #

[View source]
def chmax(i : Int, value : T) #

[View source]
def chmax(indexes : Tuple, value) #

[View source]
def chmin(i : Int, value : T) #

[View source]
def chmin(indexes : Tuple, value) #

[View source]
def compress(values : Array(T), *, index : Int = 0) #

[View source]
def compress(*, index : Int = 0) : Array(Int32) #

[View source]
def convex_hull : Array(Int32) #

Returns convex hull of points that consist of (value, index).

[4, 2, 3, 4, 1, 2, 1, 3, 2, 4].convex_hull # => [4, 2, 1, 1, 2, 4]
  | 0 1 2 3 4 5 6 7 8 9
--+---------------------
4 | o     x           o
3 |     x         x
2 |   o       x     o
1 |         o   o

[View source]
def convex_hull_with_index : Array(Tuple(T, Int32)) #

Returns convex hull of points that consist of (value, index).

[4, 2, 3, 4, 1, 2, 1, 3, 2, 4].convex_hull_with_index # => [{4, 0}, {2, 1}, {1, 4}, {1, 6}, {2, 8}, {4, 9}]
  | 0 1 2 3 4 5 6 7 8 9
--+---------------------
4 | o     x           o
3 |     x         x
2 |   o       x     o
1 |         o   o

[View source]
def each_unique_permutation(reuse : Bool = false) #

[View source]
def each_unique_permutation(reuse : Bool = false, &) : Nil #

[View source]
def gcd_mobius #

a[i] = Sum_{n | i} result[i]


[View source]
def gcd_mobius! #

a[i] = Sum_{n | i} result[i] (inplace)


[View source]
def gcd_zeta #

result[i] = Sum_{n | i} a[i]


[View source]
def gcd_zeta! #

result[i] = Sum_{n | i} a[i] (inplace)


[View source]
def next_permutation! : Bool #

[View source]
def next_permutation? : Array(T)? #

[View source]
def unique_permutations : Array(Array(T)) #

[View source]