pub struct Array<M, T> { /* private fields */ }Expand description
A reference to a fixed-length array of values stored on the trail.
The type parameter T is the type of value stored on the trail, and the type parameter M
represents how the value is stored on the trail. An Array<Backtrackable, T> is stored on the
trail in backtrackable memory, whereas an Array<NonBacktrackable, T> is stored on the trail
in non-backtrackable memory.
Instead of using Array directly, it’s often easier to use the type definitions
BacktrackableArray and
NonBacktrackableArray.
Implementations§
Source§impl<M, T> Array<M, T>where
M: StorageMode,
T: Bytes,
impl<M, T> Array<M, T>where
M: StorageMode,
T: Bytes,
Sourcepub fn new(
builder: &mut TrailBuilder,
vals: impl IntoIterator<Item = T>,
) -> Self
pub fn new( builder: &mut TrailBuilder, vals: impl IntoIterator<Item = T>, ) -> Self
Creates a new Array with the given values.
The Array is usable after the TrailBuilder used to create it is finished.
§Examples
use contrail::{BacktrackableArray, TrailBuilder};
let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 5..10);
let trail = builder.finish();
// the array is usable now
assert_eq!(array.get(&trail, 2), 7);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the array.
§Examples
use contrail::{BacktrackableArray, TrailBuilder};
let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 0..8);
assert_eq!(array.len(), 8);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the length of the array is equal to 0.
§Examples
use contrail::{BacktrackableArray, TrailBuilder};
let mut builder = TrailBuilder::new();
let empty = BacktrackableArray::new(&mut builder, 0..0);
let not_empty = BacktrackableArray::new(&mut builder, 0..1);
assert_eq!(empty.is_empty(), true);
assert_eq!(not_empty.is_empty(), false);Sourcepub fn iter<'t>(&self, trail: &'t Trail) -> ArrayIter<'t, M, T> ⓘ
pub fn iter<'t>(&self, trail: &'t Trail) -> ArrayIter<'t, M, T> ⓘ
Returns an iterator over the elements of the array.
§Examples
use contrail::{BacktrackableArray, TrailBuilder};
let mut builder = TrailBuilder::new();
let odds = BacktrackableArray::new(&mut builder, (0..10).map(|x| 2 * x + 1));
let trail = builder.finish();
for odd in odds.iter(&trail) {
assert_eq!(odd % 2, 1);
}Sourcepub fn get(&self, trail: &Trail, i: usize) -> T
pub fn get(&self, trail: &Trail, i: usize) -> T
Gets the value of the array at the given index.
§Panics
Panics if the index is out of bounds.
§Examples
use contrail::{BacktrackableArray, Trail, TrailBuilder};
let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 0..10);
let mut trail = builder.finish();
assert_eq!(array.get(&trail, 4), 4);Sourcepub fn set(&self, trail: &mut Trail, i: usize, new_val: T)
pub fn set(&self, trail: &mut Trail, i: usize, new_val: T)
Sets the value of the array at the given index.
§Panics
Panics if the index is out of bounds.
§Examples
use contrail::{BacktrackableArray, Trail, TrailBuilder};
let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 0..10);
let mut trail = builder.finish();
assert_eq!(array.get(&trail, 4), 4);
array.set(&mut trail, 4, -23);
assert_eq!(array.get(&trail, 4), -23);Sourcepub fn update(&self, trail: &mut Trail, i: usize, f: impl FnOnce(T) -> T)
pub fn update(&self, trail: &mut Trail, i: usize, f: impl FnOnce(T) -> T)
Updates the value of the array at the given index using the given update function.
§Panics
Panics if the index is out of bounds.
§Examples
use contrail::{BacktrackableArray, Trail, TrailBuilder};
let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 0..10);
let mut trail = builder.finish();
assert_eq!(array.get(&trail, 4), 4);
array.update(&mut trail, 4, |x| x * x);
assert_eq!(array.get(&trail, 4), 16);Sourcepub fn swap(&self, trail: &mut Trail, i: usize, j: usize)
pub fn swap(&self, trail: &mut Trail, i: usize, j: usize)
Swaps the two values at the given indices of the array in memory.
§Panics
Panics if either of the indices are out of bounds.
§Examples
use contrail::{BacktrackableArray, TrailBuilder};
let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, vec!['r', 'u', 't', 's']);
let mut trail = builder.finish();
assert_eq!(array.get(&trail, 2), 't');
assert_eq!(array.get(&trail, 3), 's');
array.swap(&mut trail, 2, 3);
assert_eq!(array.get(&trail, 2), 's');
assert_eq!(array.get(&trail, 3), 't');