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)

See Array literals in the language reference.

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

Defined in:

lib/views/src/views/array_view.cr
chem/core_ext/array.cr
chem/register_format.cr

Constructors

Instance Method Summary

Instance methods inherited from module Indexable(T)

sentence(io : IO, separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ", & : T, IO -> ) : Nil
sentence(io : IO, separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ") : Nil
sentence(separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ", & : T -> ) : String
sentence(separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ") : String
sentence

Instance methods inherited from module Enumerable(T)

average(weights : Indexable(Number))
average(weights : Indexable(Number), & : T -> _)
average
, mean
mean(& : T -> _)
mean

Constructor Detail

def self.from_mol2(input : IO | Path | String, indexes : Array(Int)) : self #

Creates a new array of Chem::Structure with the entries at indexes encoded in input using the Chem::Mol2 file format. Arguments are fowarded to Chem::Mol2::Reader.open.


[View source]
def self.from_mol2(input : IO | Path | String) : self #

Creates a new array of Chem::Structure with the entries encoded in input using the Chem::Mol2 file format. Arguments are fowarded to Chem::Mol2::Reader.open.


[View source]
def self.from_pdb(input : IO | Path | String, alt_loc : Char | Nil = nil, chains : Enumerable(Char) | String | Nil = nil, guess_bonds : Bool = false, het : Bool = true) : self #

Creates a new array of Chem::Structure with the entries encoded in input using the Chem::PDB file format. Arguments are fowarded to Chem::PDB::Reader.open.


[View source]
def self.from_pdb(input : IO | Path | String, indexes : Array(Int), alt_loc : Char | Nil = nil, chains : Enumerable(Char) | String | Nil = nil, guess_bonds : Bool = false, het : Bool = true) : self #

Creates a new array of Chem::Structure with the entries at indexes encoded in input using the Chem::PDB file format. Arguments are fowarded to Chem::PDB::Reader.open.


[View source]
def self.from_sdf(input : IO | Path | String, indexes : Array(Int)) : self #

Creates a new array of Chem::Structure with the entries at indexes encoded in input using the Chem::SDF file format. Arguments are fowarded to Chem::SDF::Reader.open.


[View source]
def self.from_sdf(input : IO | Path | String) : self #

Creates a new array of Chem::Structure with the entries encoded in input using the Chem::SDF file format. Arguments are fowarded to Chem::SDF::Reader.open.


[View source]
def self.from_xyz(input : IO | Path | String, guess_bonds : Bool = false, guess_names : Bool = false) : self #

Creates a new array of Chem::Structure with the entries encoded in input using the Chem::XYZ file format. Arguments are fowarded to Chem::XYZ::Reader.open.


[View source]
def self.from_xyz(input : IO | Path | String, indexes : Array(Int), guess_bonds : Bool = false, guess_names : Bool = false) : self #

Creates a new array of Chem::Structure with the entries at indexes encoded in input using the Chem::XYZ file format. Arguments are fowarded to Chem::XYZ::Reader.open.


[View source]
def self.read(input : IO | Path | String, format : String) : self #

Returns the entries encoded in the specified file using format. Raises ArgumentError if format is invalid.


[View source]
def self.read(input : IO | Path | String, format : Chem::Format) : self #

Returns the entries encoded in the specified file using format. Raises ArgumentError if format cannot read the element type or it is write only.


[View source]
def self.read(path : Path | String) : self #

Returns the entries encoded in the specified file. The file format is chosen based on the filename (see Chem::Format#from_filename). Raises ArgumentError if the file format cannot be determined.


[View source]

Instance Method Detail

def sort(range : Range(Int, Int)) : self #

[View source]
def sort(range : Range(Int, Int), &block : T, T -> Int32 | Nil) : self #

[View source]
def sort!(range : Range(Int, Int)) : self #

[View source]
def sort!(range : Range(Int, Int), &block : T, T -> Int32 | Nil) : self #

[View source]
def to_mol2(output : IO | Path | String) : Nil #

Writes the elements to output using the Chem::Mol2 file format. Arguments are fowarded to Chem::Mol2::Writer.open.


[View source]
def to_mol2 : String #

Returns a string representation of the elements encoded in the Chem::Mol2 file format. Arguments are fowarded to Chem::Mol2::Writer.open.


[View source]
def to_pdb(bonds : Chem::PDB::Writer::BondOptions = Chem::PDB::Writer::BondOptions.flags(Het, Disulfide), renumber : Bool = true, ter_on_fragment : Bool = false) : String #

Returns a string representation of the elements encoded in the Chem::PDB file format. Arguments are fowarded to Chem::PDB::Writer.open.


[View source]
def to_pdb(output : IO | Path | String, bonds : Chem::PDB::Writer::BondOptions = Chem::PDB::Writer::BondOptions.flags(Het, Disulfide), renumber : Bool = true, ter_on_fragment : Bool = false) : Nil #

Writes the elements to output using the Chem::PDB file format. Arguments are fowarded to Chem::PDB::Writer.open.


[View source]
def to_xyz(extended : Bool = false, fields : Array(String) = [] of String) : String #

Returns a string representation of the elements encoded in the Chem::XYZ file format. Arguments are fowarded to Chem::XYZ::Writer.open.


[View source]
def to_xyz(output : IO | Path | String, extended : Bool = false, fields : Array(String) = [] of String) : Nil #

Writes the elements to output using the Chem::XYZ file format. Arguments are fowarded to Chem::XYZ::Writer.open.


[View source]
def write(output : IO | Path | String, format : String) : Nil #

Writes the elements to output using format. Raises ArgumentError if format is invalid.


[View source]
def write(output : IO | Path | String, format : Chem::Format) : Nil #

Writes the elements to output using format. Raises ArgumentError if format cannot write the element type or it is read only.


[View source]
def write(path : Path | String) : Nil #

Writes the elements to the specified file. The file format is chosen based on the filename (see Chem::Format#from_filename). Raises ArgumentError if the file format cannot be determined.


[View source]