rust_dsa

Struct VList

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

A dynamic array implementation backed by a VList.

§Example

use rust_dsa::VList;

// First, we create a new list.
let mut list = VList::new();

// Then we can push values.
list.push(4);
list.push(1);
list.push(3);

// We can get values.
assert_eq!(list.get(0), Some(&4));
assert_eq!(list.get(1), Some(&1));
assert_eq!(list.last(), Some(&3));

// And pop from them off.
assert_eq!(list.pop(), Some(3));
assert_eq!(list.pop(), Some(1));
assert_eq!(list.pop(), Some(4));

assert!(list.is_empty());

// We can also crate lists from arrays.
let list_a = VList::from(['v', 'l', 'i', 's', 't']);

// And iterators.
let list_b: VList<_> = "vlist".chars().collect();

// We can iterate over a list.
for (a, b) in std::iter::zip(list_a, list_b) {
    assert_eq!(a, b);
}

Implementations§

Source§

impl<T> VList<T>

Source

pub fn new() -> VList<T>

Creates an empty list.

Source

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

Pushes a value onto the end of the list.

§Example
use rust_dsa::VList;

let mut list = VList::new();

list.push(5);

assert_eq!(list.last(), Some(&5));
Source

pub fn pop(&mut self) -> Option<T>

Removes and returns the value at the end of the list, or None if the list is empty.

§Example
use rust_dsa::VList;

let mut list = VList::new();

list.push(5);

assert_eq!(list.pop(), Some(5));
assert_eq!(list.pop(), None);
Source

pub fn last(&mut self) -> Option<&T>

Returns a reference to value at the end of the list, or None if the list is empty.

§Example
use rust_dsa::VList;

let mut list = VList::from([5]);

assert_eq!(list.last(), Some(&5));

list.pop();

assert_eq!(list.last(), None);
Source

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

Returns a reference to the value at position index if one exists.

§Example
use rust_dsa::VList;

let list = VList::from(['a', 'b', 'c', 'd']);

assert_eq!(list.get(0), Some(&'a'));
assert_eq!(list.get(1), Some(&'b'));
assert_eq!(list.get(2), Some(&'c'));
assert_eq!(list.get(3), Some(&'d'));
assert_eq!(list.get(4), None);
Source

pub fn len(&self) -> usize

Returns the length of the list.

§Example
use rust_dsa::VList;

let full: VList<_> = (0..10).collect();
assert_eq!(full.len(), 10);

let empty: VList<i32> = VList::new();
assert_eq!(empty.len(), 0);
Source

pub fn is_empty(&self) -> bool

Returns true if the list is empty.

§Example
use rust_dsa::VList;

let mut list = VList::from([5, 6, 4, 2, 8]);

assert!(!list.is_empty());

list.clear();

assert!(list.is_empty());
Source

pub fn clear(&mut self)

Clears the list.

§Example
use rust_dsa::VList;

let mut list = VList::from([5, 6, 4, 2, 8]);

assert!(!list.is_empty());

list.clear();

assert!(list.is_empty());

Trait Implementations§

Source§

impl<T: Clone> Clone for VList<T>

Source§

fn clone(&self) -> VList<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 for VList<T>
where T: Debug,

Source§

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

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

impl<T> Default for VList<T>

Source§

fn default() -> VList<T>

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

impl<T, const N: usize> From<[T; N]> for VList<T>

Source§

fn from(array: [T; N]) -> VList<T>

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for VList<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VList<T>

§Example
use rust_dsa::VList;

let mut ints: VList<_> = (0..100_000).collect();

for i in (0..100_000).rev() {
    assert_eq!(ints.get(i), Some(&i));
    assert_eq!(ints.pop(), Some(i));
}
Source§

impl<'a, T> IntoIterator for &'a VList<T>

Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for VList<T>

Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

type Item = T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

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

Source§

fn eq(&self, other: &VList<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: Eq> Eq for VList<T>

Auto Trait Implementations§

§

impl<T> Freeze for VList<T>

§

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

§

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

§

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

§

impl<T> Unpin for VList<T>

§

impl<T> UnwindSafe for VList<T>

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.