pub struct Jagged2<T> { /* private fields */ }Expand description
2-dimensional jagged array type. It’s equivalent to a
Box<Box<[mut T]>>, but where all array data is stored contiguously
and fewer allocations are performed.
Note that no dimension of the array can be modified after creation.
Jagged arrays can be created via the Jagged2Builder type
or the from_iter method.
Implementations§
Source§impl<T> Jagged2<T>
impl<T> Jagged2<T>
Sourcepub fn get(&self, index: [usize; 2]) -> Option<&T>
pub fn get(&self, index: [usize; 2]) -> Option<&T>
Index into the jagged array. The index is given in (Major, Minor) form, i.e. (row, column) or (outer, inner).
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
let a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
vec![],
vec![5, 6],
]);
assert_eq!(a.get([1, 0]), Some(&4));
assert_eq!(a.get([2, 0]), None);Sourcepub fn get_mut(&mut self, index: [usize; 2]) -> Option<&mut T>
pub fn get_mut(&mut self, index: [usize; 2]) -> Option<&mut T>
Index into the jagged array. The index is given in (Major, Minor) form, i.e. (row, column) or (outer, inner).
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
let mut a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
vec![],
vec![5, 6],
]);
assert_eq!(a.get([1, 0]), Some(&4));
*a.get_mut([1, 0]).unwrap() = 11;
assert_eq!(a.get([1, 0]), Some(&11));Sourcepub fn get_row(&self, row: usize) -> Option<&[T]>
pub fn get_row(&self, row: usize) -> Option<&[T]>
Retrieve the given row as a contiguous slice of memory.
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
let a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
vec![],
vec![5, 6],
]);
assert_eq!(a.get_row(3), Some(&[5, 6][..]));Sourcepub fn get_row_mut(&mut self, row: usize) -> Option<&mut [T]>
pub fn get_row_mut(&mut self, row: usize) -> Option<&mut [T]>
Retrieve the given row as a contiguous slice of mutable memory.
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
let mut a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
vec![],
vec![5, 6],
]);
assert_eq!(a.get_row_mut(3), Some(&mut[5, 6][..]));
a.get_row_mut(3).unwrap()[1] = 11;
assert_eq!(a[[3, 1]], 11);Sourcepub fn as_flat_slice(&self) -> &[T]
pub fn as_flat_slice(&self) -> &[T]
Return a slice over the entire storage area.
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
let a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
vec![],
vec![5, 6],
]);
assert_eq!(a.as_flat_slice(), &[1, 2, 3, 4, 5, 6][..]);Sourcepub fn as_flat_slice_mut(&mut self) -> &mut [T]
pub fn as_flat_slice_mut(&mut self) -> &mut [T]
Return a mutable slice over the entire storage area.
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
let mut a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
vec![],
vec![5, 6],
]);
assert_eq!(a.as_flat_slice()[3], 4);
a.as_flat_slice_mut()[3] = 33;
assert_eq!(a[[1, 0]], 33);Sourcepub fn flat_len(&self) -> usize
pub fn flat_len(&self) -> usize
Return the total number of T held in the array.
This is generally a constant-time operation, but if T is a zero-sized type
then the time complexity is proportional to the number of rows in the array.
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
let a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
vec![],
vec![5, 6],
]);
assert_eq!(a.flat_len(), 6);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the number of rows held in the array.
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
let a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
vec![],
vec![5, 6],
]);
assert_eq!(a.len(), 4);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return true if the array holds no items.
Semantically equivalent to jagged2.len() == 0.
Sourcepub fn stream<'a>(&'a self) -> Stream<'a, T>
pub fn stream<'a>(&'a self) -> Stream<'a, T>
Create a streaming iterator over the rows of this array.
Lifetime restrictions prevent implementing std::iter::Iterator for this
type, however a streaming iterator provides similar features except that
the lifetime of the items it yields is tied to the lifetime of the iterator
itself.
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
use streaming_iterator::StreamingIterator;
let a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
]);
let mut iter = a.stream();
while let Some(row) = iter.next() {
println!("row: {:?}", row);
}Sourcepub fn into_boxed_slice(self) -> Box<[T]>
pub fn into_boxed_slice(self) -> Box<[T]>
Consumes self and returns the underlying storage
(identical to as_flat_slice_mut, but owned).
The slice can optionally be turned into a vector by calling
slice::into_vec() on the result.
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for Jagged2<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Jagged2<T>where
T: Deserialize<'de>,
Source§fn deserialize<D>(deserializer: D) -> Result<Jagged2<T>, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Jagged2<T>, D::Error>where
D: Deserializer<'de>,
Source§impl<T, ICol> FromIterator<ICol> for Jagged2<T>where
ICol: IntoIterator<Item = T>,
impl<T, ICol> FromIterator<ICol> for Jagged2<T>where
ICol: IntoIterator<Item = T>,
Source§fn from_iter<IRow>(row_iter: IRow) -> Selfwhere
IRow: IntoIterator<Item = ICol>,
fn from_iter<IRow>(row_iter: IRow) -> Selfwhere
IRow: IntoIterator<Item = ICol>,
Allow construction from any type that behaves like [[T]].
§Example
use std::iter::FromIterator;
use jagged_array::Jagged2;
let a = Jagged2::from_iter(vec![
vec![1, 2, 3],
vec![4],
vec![],
vec![5, 6],
]);
assert_eq!(a.len(), 4); // 4 rows
assert_eq!(a.as_flat_slice(), &[1, 2, 3, 4, 5, 6][..]); // contiguous view
assert_eq!(a.get_row(3), Some(&[5, 6][..])); // third row
assert_eq!(a[[0, 1]], 2); // first row, second column