Jagged2

Struct Jagged2 

Source
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>

Source

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);
Source

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));
Source

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][..]));
Source

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);
Source

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][..]);
Source

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);
Source

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);
Source

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);
Source

pub fn is_empty(&self) -> bool

Return true if the array holds no items. Semantically equivalent to jagged2.len() == 0.

Source

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);
}
Source

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<T> Clone for Jagged2<T>
where T: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Jagged2<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Jagged2<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Drop for Jagged2<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, ICol> FromIterator<ICol> for Jagged2<T>
where ICol: IntoIterator<Item = T>,

Source§

fn from_iter<IRow>(row_iter: IRow) -> Self
where 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
Source§

impl<T> Hash for Jagged2<T>
where T: Hash,

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Index<[usize; 2]> for Jagged2<T>

Source§

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]].

Source§

type Output = T

The returned type after indexing.
Source§

impl<T> Into<Jagged2<T>> for Jagged2Builder<T>

Source§

fn into(self) -> Jagged2<T>

Converts this type into the (usually inferred) input type.
Source§

impl<T> PartialEq for Jagged2<T>
where T: PartialEq,

Source§

fn eq(&self, other: &Jagged2<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for Jagged2<T>
where T: Serialize,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> Eq for Jagged2<T>
where T: Eq,

Auto Trait Implementations§

§

impl<T> Freeze for Jagged2<T>

§

impl<T> RefUnwindSafe for Jagged2<T>
where T: RefUnwindSafe,

§

impl<T> !Send for Jagged2<T>

§

impl<T> !Sync for Jagged2<T>

§

impl<T> Unpin for Jagged2<T>

§

impl<T> UnwindSafe for Jagged2<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,