pub struct JaggedVec<T> { /* private fields */ }
Expand description

An extensible (ie: can add more rows) jagged array.

Note: Unlike JaggedArray, this implementation can have 0 rows.

Refer to the JaggedArray “Design” section for more details.

Implementations§

source§

impl<T> JaggedVec<T>

source

pub fn push_row(&mut self, row: impl IntoIterator<Item = T>) -> &mut Self

Add row at the end of the matrix.

Example
use datazoo::JaggedVec;

let mut jagged = JaggedVec::empty();
jagged
    .push_row([])
    .push_row([0, 1, 2])
    .push_row([3])
    .push_row([7, 8])
    .push_row([9])
    .push_row([])
    .push_row([11, 23]);
assert_eq!(
    jagged.into_vecs(),
    vec![
        vec![],
        vec![0, 1, 2],
        vec![3],
        vec![7, 8],
        vec![9],
        vec![],
        vec![11, 23],
    ],
);
source

pub fn push(&mut self, elem: T)

Add an element to the last row, or create a first row if none exist yet.

Example
use datazoo::JaggedVec;

let mut jagged = JaggedVec::empty();
jagged.push_row([0, 1, 2]).push_row([3]);
jagged.push(4);
assert_eq!(jagged.into_vecs(), vec![vec![0, 1, 2], vec![3, 4]]);
source

pub fn extend_last_row(&mut self, elems: impl IntoIterator<Item = T>)

Add multiple elements to the last row.

Example
use datazoo::JaggedVec;

let mut jagged = JaggedVec::empty();
jagged.push_row([0, 1, 2]).push_row([3]);
jagged.extend_last_row([4, 5, 6]);
assert_eq!(jagged.into_vecs(), vec![vec![0, 1, 2], vec![3, 4, 5, 6]]);
source

pub fn clear(&mut self)

Remove all rows from this JaggedVec.

source

pub fn pop_row(&mut self) -> Option<PoppedRow<'_, T>>

Remove the last row from the matrix, returning it.

Note that the returned value holds a reference to the jagged vec, which will prevent using this JaggedVec until the returned PoppedRow is dropped.

Example
use datazoo::JaggedVec;

let mut jagged = JaggedVec::empty();
jagged.push_row([0, 1, 2]).push_row([3]).push_row([4, 5, 6, 7]);
let popped = jagged.pop_row();
assert_eq!(popped.as_deref(), Some(&[4, 5, 6, 7][..]));

drop(popped); // need to drop `popped`, otherwise can't access `jagged`.

assert_eq!(jagged.get_row(2), None);
assert_eq!(jagged.clone().into_vecs(), vec![vec![0, 1, 2], vec![3]]);
jagged.pop_row();
jagged.pop_row();
assert_eq!(jagged.height(), 0);
source

pub fn len(&self) -> usize

How many cells are contained in this JaggedVec.

source

pub fn is_empty(&self) -> bool

Is this vector empty (no cells, may have several empty rows).

source

pub fn height(&self) -> usize

How many rows this JaggedVec has.

source

pub const fn empty() -> Self

The empty JaggedVec, identical to JaggedVec::default().

source

pub fn new(ends: Vec<u32>, data: Vec<T>) -> Result<Self, Error>

Create a JaggedVec of ends.len() + 1 rows, values of ends are the end indicies (exclusive) of each row in data.

Note that the last index should be elided. The last row will be the values between the last end in ends and the total size of the data array.

Errors

The ends slice is invalid:

  • An ends[i] > data.len()
  • An ends[i+1] < ends[i]
Example
use datazoo::JaggedVec;

let ends = [0, 0, 3, 4, 7, 9, 10, 10]; // len = 8
let data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 23];
let jagged = JaggedVec::new(ends.to_vec(), data.to_vec()).unwrap();
let iliffe = jagged.into_vecs();
assert_eq!(
    iliffe,
    vec![
        vec![],
        vec![],
        vec![0, 1, 2],
        vec![3],
        vec![4, 5, 6],
        vec![7, 8],
        vec![9],
        vec![],
        vec![11, 23],
    ], // len = 9
);
source

pub fn row(&self, index: usize) -> &[T]

Get slice to row at given index.

Panics

When index > self.height().

source

pub fn get_row(&self, index: usize) -> Option<&[T]>

Get slice to row at given index.

Returns None when index > self.height().

Example
use datazoo::JaggedVec;

let ends = [0, 0, 3, 4, 7, 9, 10, 10];
let data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let jagged = JaggedVec::new(ends.to_vec(), data.to_vec()).unwrap();

assert_eq!(jagged.get_row(4), Some(&[4, 5, 6][..]));
source

pub fn get(&self, direct_index: usize) -> Option<&T>

Get V at exact direct_index ignoring row sizes, acts as if the whole array was a single row.

None when direct_index is out of bound.

Example
use datazoo::JaggedVec;

let ends = [0, 0, 3, 4, 7, 9, 10, 10];
let data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let jagged = JaggedVec::new(ends.to_vec(), data.to_vec()).unwrap();

assert_eq!(jagged.get(4), Some(&4));
source

pub fn into_vecs(self) -> Vec<Vec<T>>

Turn this compact jagged array into a sparse representation.

The returned Vec<Vec<V>> is an Iliffe vector. Iterating over it will be much slower than iterating over JaggedVec, but extending individual rows is much less costly.

source

pub fn rows(&self) -> impl Iterator<Item = &[T]>

Iterate over all the rows in the JaggedVec.

Trait Implementations§

source§

impl<T: Clone> Clone for JaggedVec<T>

source§

fn clone(&self) -> JaggedVec<T>

Returns a copy 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> Debug for JaggedVec<T>

source§

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

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

impl<T> Default for JaggedVec<T>

source§

fn default() -> Self

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

impl<T: PartialEq> PartialEq for JaggedVec<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Eq> Eq for JaggedVec<T>

source§

impl<T> StructuralEq for JaggedVec<T>

source§

impl<T> StructuralPartialEq for JaggedVec<T>

Auto Trait Implementations§

§

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

§

impl<T> Send for JaggedVec<T>where T: Send,

§

impl<T> Sync for JaggedVec<T>where T: Sync,

§

impl<T> Unpin for JaggedVec<T>where T: Unpin,

§

impl<T> UnwindSafe for JaggedVec<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.