class Array(T)
- Array(T)
- Reference
- Object
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
- Comparable(Array(T))
- Indexable::Mutable(T)
Defined in:
lib/views/src/views/array_view.crchem/core_ext/array.cr
chem/register_format.cr
Constructors
-
.from_dcd(input : IO | Path | String, indexes : Array(Int)) : self
Creates a new array of
Chem::Spatial::Positions3with the entries at indexes encoded in input using theChem::DCDfile format. -
.from_dcd(input : IO | Path | String) : self
Creates a new array of
Chem::Spatial::Positions3with the entries encoded in input using theChem::DCDfile format. -
.from_mol2(input : IO | Path | String, indexes : Array(Int)) : self
Creates a new array of
Chem::Structurewith the entries at indexes encoded in input using theChem::Mol2file format. -
.from_mol2(input : IO | Path | String) : self
Creates a new array of
Chem::Structurewith the entries encoded in input using theChem::Mol2file format. -
.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::Structurewith the entries encoded in input using theChem::PDBfile format. -
.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::Structurewith the entries at indexes encoded in input using theChem::PDBfile format. -
.from_sdf(input : IO | Path | String, indexes : Array(Int)) : self
Creates a new array of
Chem::Structurewith the entries at indexes encoded in input using theChem::SDFfile format. -
.from_sdf(input : IO | Path | String) : self
Creates a new array of
Chem::Structurewith the entries encoded in input using theChem::SDFfile format. -
.from_xyz(input : IO | Path | String, guess_bonds : Bool = false, guess_names : Bool = false) : self
Creates a new array of
Chem::Structurewith the entries encoded in input using theChem::XYZfile format. -
.from_xyz(input : IO | Path | String, indexes : Array(Int), guess_bonds : Bool = false, guess_names : Bool = false) : self
Creates a new array of
Chem::Structurewith the entries at indexes encoded in input using theChem::XYZfile format. -
.read(input : IO | Path | String, format : String) : self
Returns the entries encoded in the specified file using format.
-
.read(input : IO | Path | String, format : Chem::Format) : self
Returns the entries encoded in the specified file using format.
-
.read(path : Path | String) : self
Returns the entries encoded in the specified file.
Instance Method Summary
- #sort(range : Range(Int, Int)) : self
- #sort(range : Range(Int, Int), &block : T, T -> Int32 | Nil) : self
- #sort!(range : Range(Int, Int)) : self
- #sort!(range : Range(Int, Int), &block : T, T -> Int32 | Nil) : self
-
#to_dcd(title : String | Nil = nil) : String
Returns a string representation of the elements encoded in the
Chem::DCDfile format. -
#to_dcd(output : IO | Path | String, title : String | Nil = nil) : Nil
Writes the elements to output using the
Chem::DCDfile format. -
#to_mol2(output : IO | Path | String) : Nil
Writes the elements to output using the
Chem::Mol2file format. -
#to_mol2 : String
Returns a string representation of the elements encoded in the
Chem::Mol2file format. -
#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::PDBfile format. -
#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::PDBfile format. -
#to_sdf(variant : Chem::Mol::Variant = :v2000) : String
Returns a string representation of the elements encoded in the
Chem::SDFfile format. -
#to_sdf(output : IO | Path | String, variant : Chem::Mol::Variant = :v2000) : Nil
Writes the elements to output using the
Chem::SDFfile format. -
#to_xyz(extended : Bool = false, fields : Array(String) = [] of String) : String
Returns a string representation of the elements encoded in the
Chem::XYZfile format. -
#to_xyz(output : IO | Path | String, extended : Bool = false, fields : Array(String) = [] of String) : Nil
Writes the elements to output using the
Chem::XYZfile format. -
#write(output : IO | Path | String, format : String) : Nil
Writes the elements to output using format.
-
#write(output : IO | Path | String, format : Chem::Format) : Nil
Writes the elements to output using format.
-
#write(path : Path | String) : Nil
Writes the elements to the specified file.
Instance methods inherited from module Indexable(T)
[](idxs : Tuple) : Tuple[](idxs : Enumerable(Int)) : Array(T)
[](*idxs : Int) : Tuple [], 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)
===(atom : Chem::Atom) : Bool===(chain : Chem::Chain) : Bool
===(residue : Chem::Residue) : Bool ===, average(weights : Indexable(Number))
average(weights : Indexable(Number), & : T -> _) average, find(pattern, if_none default = nil) find, find!(pattern) find!, mean
mean(& : T -> _) mean
Constructor Detail
Creates a new array of Chem::Spatial::Positions3 with the entries at
indexes encoded in input using the Chem::DCD file
format. Arguments are forwarded to Chem::DCD::Reader.open.
Creates a new array of Chem::Spatial::Positions3 with the entries
encoded in input using the Chem::DCD file format.
Arguments are forwarded to Chem::DCD::Reader.open.
Creates a new array of Chem::Structure with the entries at
indexes encoded in input using the Chem::Mol2 file
format. Arguments are forwarded to Chem::Mol2::Reader.open.
Creates a new array of Chem::Structure with the entries
encoded in input using the Chem::Mol2 file format.
Arguments are forwarded to Chem::Mol2::Reader.open.
Creates a new array of Chem::Structure with the entries
encoded in input using the Chem::PDB file format.
Arguments are forwarded to Chem::PDB::Reader.open.
Creates a new array of Chem::Structure with the entries at
indexes encoded in input using the Chem::PDB file
format. Arguments are forwarded to Chem::PDB::Reader.open.
Creates a new array of Chem::Structure with the entries at
indexes encoded in input using the Chem::SDF file
format. Arguments are forwarded to Chem::SDF::Reader.open.
Creates a new array of Chem::Structure with the entries
encoded in input using the Chem::SDF file format.
Arguments are forwarded to Chem::SDF::Reader.open.
Creates a new array of Chem::Structure with the entries
encoded in input using the Chem::XYZ file format.
Arguments are forwarded to Chem::XYZ::Reader.open.
Creates a new array of Chem::Structure with the entries at
indexes encoded in input using the Chem::XYZ file
format. Arguments are forwarded to Chem::XYZ::Reader.open.
Returns the entries encoded in the specified file using format.
Raises ArgumentError if format is invalid.
Returns the entries encoded in the specified file using format.
Raises ArgumentError if format cannot read the element type or
it is write only.
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.
Instance Method Detail
Returns a string representation of the elements encoded in
the Chem::DCD file format. Arguments are forwarded to
Chem::DCD::Writer.open.
Writes the elements to output using the Chem::DCD file
format. Arguments are forwarded to Chem::DCD::Writer.open.
Writes the elements to output using the Chem::Mol2 file
format. Arguments are forwarded to Chem::Mol2::Writer.open.
Returns a string representation of the elements encoded in
the Chem::Mol2 file format. Arguments are forwarded to
Chem::Mol2::Writer.open.
Returns a string representation of the elements encoded in
the Chem::PDB file format. Arguments are forwarded to
Chem::PDB::Writer.open.
Writes the elements to output using the Chem::PDB file
format. Arguments are forwarded to Chem::PDB::Writer.open.
Returns a string representation of the elements encoded in
the Chem::SDF file format. Arguments are forwarded to
Chem::SDF::Writer.open.
Writes the elements to output using the Chem::SDF file
format. Arguments are forwarded to Chem::SDF::Writer.open.
Returns a string representation of the elements encoded in
the Chem::XYZ file format. Arguments are forwarded to
Chem::XYZ::Writer.open.
Writes the elements to output using the Chem::XYZ file
format. Arguments are forwarded to Chem::XYZ::Writer.open.
Writes the elements to output using format. Raises
ArgumentError if format is invalid.
Writes the elements to output using format. Raises
ArgumentError if format cannot write the element type or it is
read only.
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.