N Circular Array
An n-dimensional circular array.
Features
- Fixed dimension arrays of any size.
- Element insertion to the front or back of any dimension.
- Indexing, range and slicing operations.
- Performant operations for sequentual
Copytype elements.
Usage
// A 1-dimensional circular array of 6 elements.
let mut array = new;
array.push_front;
assert_eq!;
array.push_back;
assert_eq!;
// A 2-dimensional circular array of 3^2 elements.
let mut array = new;
// Push to the front of axis 0.
array.push_front;
assert_eq!;
// Push to the back of axis 1.
array.push_back;
assert_eq!;
Mutation
n_circular_array allows for mutating single elements, or inserting any number
of slices to an axis. Insertion operations expect elements arranged as a row-major
slice. That is, insertion of two columns arranged as a row-major contiguous
slice would be the elements of column one, interspersed by those of column two.
This is the default behaviour when slicing into ndarray or nalgebra arrays.
// A 2-dimensional circular array of 3^2 elements.
let mut array = new;
// Push two columns to the front of axis 0.
array.push_front;
// Mutate the last element of the array (equivalent to `array.get_mut([2, 2])`).
assert_eq!;
array = 99;
assert_eq!;
See [CircularArrayMut].
Indexing
n_circular_array allows for elements to be accessed by index or slice.
// A 2-dn_n_imensional circular array of 3 * 3 * 2 elements.
let mut array = new;
// Get the first element of axis 2 (equivalent to `array.get([0, 0, 2])`).
assert_eq!;
// Get the second and third row.
assert_eq!;
// Get the third row of the second slice of axis 2.
assert_eq!;
See [CircularArrayIndex] and [CircularArrayIndexMut].
Resizing/Reshaping
No resizing or reshaping operations are offered, however the same functionality
can be achieved by iterating and collecting into a new array. No method is
offered for this functionality to make the performance implications explicit.
// A 2-dimensional circular array of 3 * 3 * 2 elements.
let mut array = new;
// Insert a row at index 0.
array.push_front;
assert_eq!;
assert_eq!;
// Iterate over index 1 of axis 2 into a new array of size [3, 3].
let iter = array.iter_slice.cloned;
let array_2 = from_iter;
assert_eq!;
assert_eq!;
Performance
The inner dimensions of any n > 1 array are impacted the most by cache locality
(or a lack thereof). Wrapping contigous slices over the bounds of an axis further
reduces cache locality. Where possible, an array should be oriented in which the
majority of operations are performed on the outermost dimension(s). n_circular_array
will take contiguous slices of memory where possible. For elements that implement
Copy, this can result in an insertion of as little as a single call to copy_from_slice.
License: MIT OR Apache-2.0