Expand description
N-dimensional arrays
Julia has a generic array type, Array{T, N}. These arrays are column-major, N-dimensional
arrays that can hold elements of type T.
jlrs provides a flexible base type that wraps instances of this type, ArrayBase. This type
has two generics: a type constructor T, and a constant isize rank N. You shouldn’t use
this type directly, but use the four available type aliases instead: Array,
TypedArray, RankedArray, and TypedRankedArray. If Typed is missing from the
name, the element type T is set to Unknown, if Ranked is missing the the rank N is
set to -1.
There are several special aliases: Vector, VectorAny and TypedVector (rank 1), and
Matrix and TypedMatrix (rank 2).
§Converting between array types
The methods ArrayBase::set_rank and ArrayBase::set_type can be used to set the two
generic parameters, ArrayBase::forget_rank and ArrayBase::forget_type lets you set
them to -1 and Unknown respectively.
§Constructing new arrays
Many methods that construct new arrays exist, they can be divided into several groups:
-
new: Constructs a new array whose storage is managed by Julia. -
from_slice: Constructs a new array whose storage is borrowed from Rust. -
from_vec: Constructs a new array whose storage is moved from Rust. -
from_slice_cloned: Constructs a new array whose storage is managed by Julia, the elements are initialized by cloning a slice. -
from_slice_copied: Constructs a new array whose storage is managed by Julia, the elements are initialized by copying a slice.
These methods exist as named for typed arrays, the element type is constructed from the
provided type parameter. Untyped arrays have *_for methods like new_for that take the
element type as an argument. All these methods have unsafe, unchecked variants like
new_for_unchecked.
One limitation of arrays that are backed by Rust data is that Julia is not able to reallocate
this array. Functions that can reallocate, like push!, will throw an exception if they are
called with such an array.
In addition to these generic constructors there are two specialized constructors:
TypedVector<u8> can be constructed with from_bytes, which behaves as from_slice_copied
does. VectorAny can be constructed with new_any, which behaves as new does.
§Array data
In order to access the content of an array an accessor must be created first. There are several kinds of accessors to account for the different ways the elements can be laid out in memory.
Elements are either stored inline in the backing storage or as references. They are stored
inline if they are immutable, concrete types. Unions of isbits types, i.e. immutable,
concrete types which contain no references to other Julia data, are also stored inline. In
this last case a type tag is stored for each element after the elements themselves. In all
other cases, the elements are stored as references, i.e. as Option<WeakValue>.
For several reasons, some more technical than others, it’s useful to distinguish between
isbits and “non-bits” immutable types. Similarly, for elements that are stored as references
it can be useful to distinguish between arbitrary Values and more specific managed types.
It’s also perfectly valid to not make any assumptions about the layout and only work with
Values, allocating new Julia data whenever necessary.
Putting all of this together, we end up with the following accessors: BitsAccessor,
InlineAccessor, BitsUnionAccessor, ValueAccessor, ManagedAccessor, and
IndeterminateAccessor. There are also mutable variants of all of these accessors.
Depending on the element type parameter T and the traits it implements, it can be possible
to infer that a certain accessor must be used. If this is the case, a method to create
that accessor without performing any checks will be available. An example is
ArrayBase::bits_data, which is only available if T: IsBits + ConstructType. If this
information can’t be inferred from T, try_* and *_unchecked variants are available.
§Tracking
It’s very easy to accidentally create multiple mutable accessors to the same array. In order
to prevent this, you can track an array. You can either track an array exclusively or allow
multiple shared references with ArrayBase::track_exclusive and
ArrayBase::track_shared respectively. This dynamically enforces borrowing rules at
runtime, but is limited. While it is thread-safe and even works across multiple packages, the
tracking mechanism is unaware of how the data is used inside Julia. It won’t protect you from
accessing an array that is currently being mutated by some Julia task running in the
background. Tracking is also relatively expensive; if you can guarantee you are the only user
of an array, e.g. you’ve just allocated it, you should avoid tracking the array.
Modules§
- data
- The data of a Julia array.
- dimensions
- N-dimensional indexing.
- tracked
Structs§
- Array
Base - Wrapper type for an array of rank
Nwhose element type isT.
Enums§
- How
- How an array has been allocated
- Unknown
- Marker type used to indicate the element type of an array is unknown.
Traits§
- Construct
Typed Array - Constructor methods for typed arrays.
Type Aliases§
- Array
- An array with an unknown element type and unknown rank.
- Array
Base Data ArrayorWeakArray, depending on the target typeT.- Array
Base Result JuliaResult<Array>orWeakJuliaResult<WeakArray>, depending on the target typeT.- Array
Data - Array
Result - Array
Ret - Matrix
- An array with an unknown element type of rank 2.
- Matrix
Data - Matrix
Result - Matrix
Ret - Ranked
Array - An array with an unknown element type and known rank.
- Ranked
Array Data - Ranked
Array Result - Ranked
Array Ret - Typed
Array - An array with a known element type and unknown rank.
- Typed
Array Data - Typed
Array Result - Typed
Array Ret - Typed
Matrix - An array with a known element type of rank 2.
- Typed
Matrix Data - Typed
Matrix Result - Typed
Matrix Ret - Typed
Ranked Array - An array with a known element type and known rank.
- Typed
Ranked Array Data - Typed
Ranked Array Result - Typed
Ranked Array Ret - Typed
Vector - An array with a known element type of rank 1.
- Typed
Vector Data - Typed
Vector Result - Typed
Vector Ret - Vector
- An array with an unknown element type of rank 1.
- Vector
Any - An array with an unknown element type of rank 1.
- Vector
AnyData - Vector
AnyResult - Vector
AnyRet - Vector
Data - Vector
Result - Vector
Ret - Weak
Array - Weak
Matrix - Weak
Ranked Array - Weak
Typed Array - Weak
Typed Matrix - Weak
Typed Ranked Array - Weak
Typed Vector - Weak
Vector - Weak
Vector Any