Struct jagged_array::Jagged2
[−]
[src]
pub struct Jagged2<T> { /* fields omitted */ }
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.
Methods
impl<T> Jagged2<T>
[src]
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);
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));
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][..]));
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);
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][..]);
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);
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);
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);
fn is_empty(&self) -> bool
Return true if the array holds no items.
Semantically equivalent to jagged2.len() == 0
.
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); }
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
impl<T> Index<[usize; 2]> for Jagged2<T>
[src]
type Output = T
The returned type after indexing
fn index(&self, index: [usize; 2]) -> &T
Index into the jagged array. The index is given in (Major, Minor) form,
i.e. (row, column) or (outer, inner).
array[[0, 0]]
is adjacent to array[[0, 1]]
in memory but not
necessarily to array[[1, 0]]
.
impl<T> Drop for Jagged2<T>
[src]
impl<T> Default for Jagged2<T>
[src]
impl<T> Debug for Jagged2<T> where
T: Debug,
[src]
T: Debug,
impl<T, ICol> FromIterator<ICol> for Jagged2<T> where
ICol: IntoIterator<Item = T>,
[src]
ICol: IntoIterator<Item = T>,
fn from_iter<IRow>(row_iter: IRow) -> Self where
IRow: IntoIterator<Item = ICol>,
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
impl<T> Clone for Jagged2<T> where
T: Clone,
[src]
T: Clone,
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T> PartialEq for Jagged2<T> where
T: PartialEq,
[src]
T: PartialEq,
fn eq(&self, other: &Jagged2<T>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl<T> Eq for Jagged2<T> where
T: Eq,
[src]
T: Eq,
impl<T> Hash for Jagged2<T> where
T: Hash,
[src]
T: Hash,
fn hash<H>(&self, state: &mut H) where
H: Hasher,
H: Hasher,
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<T> Serialize for Jagged2<T> where
T: Serialize,
[src]
T: Serialize,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
S: Serializer,
Serialize this value into the given Serde serializer. Read more
impl<'de, T> Deserialize<'de> for Jagged2<T> where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,
fn deserialize<D>(deserializer: D) -> Result<Jagged2<T>, D::Error> where
D: Deserializer<'de>,
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more