Struct index_list::IndexList

source ·
pub struct IndexList<T> { /* private fields */ }
Expand description

Doubly-linked list implemented in safe Rust.

Implementations§

source§

impl<T> IndexList<T>

source

pub fn new() -> Self

Creates a new empty index list.

Example:

use index_list::IndexList;

let list = IndexList::<u64>::new();
source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty IndexList with at least the specified capacity.

Example:

use index_list::IndexList;
let list = IndexList::<u64>::with_capacity(233);
source

pub fn capacity(&self) -> usize

Returns the current capacity of the list.

This value is always greater than or equal to the length.

Example:

let cap = list.capacity();
assert!(cap >= list.len());
source

pub fn len(&self) -> usize

Returns the number of valid elements in the list.

This value is always less than or equal to the capacity.

Example:

let first = list.remove_first();
assert!(list.len() < list.capacity());
source

pub fn clear(&mut self)

Clears the list be removing all elements, making it empty.

Example:

list.clear();
assert!(list.is_empty());
source

pub fn is_empty(&self) -> bool

Returns true when the list is empty.

Example:

let list = IndexList::<u64>::new();
assert!(list.is_empty());
source

pub fn is_index_used(&self, index: ListIndex) -> bool

Returns true if the index is valid.

source

pub fn first_index(&self) -> ListIndex

Returns the index of the first element, or None if the list is empty.

Example:

let index = list.first_index();
source

pub fn last_index(&self) -> ListIndex

Returns the index of the last element, or None if the list is empty.

Example:

let index = list.last_index();
source

pub fn next_index(&self, index: ListIndex) -> ListIndex

Returns the index of the next element, after index, or None when the end is reached.

If index is None then the first index in the list is returned.

NOTE that indexes are likely not sequential.

Example:

let mut index = list.first_index();
while index.is_some() {
    // Do something
    index = list.next_index(index);
}
source

pub fn prev_index(&self, index: ListIndex) -> ListIndex

Returns the index of the previous element, before index, or None when the beginning is reached.

If index is None then the last index in the list is returned.

NOTE that indexes are likely not sequential.

Example:

let mut index = list.last_index();
while index.is_some() {
    // Do something
    index = list.prev_index(index);
}
source

pub fn move_index(&self, index: ListIndex, steps: i32) -> ListIndex

Move to an index steps number of elements away. Positive numbers will move in the next direction, while negative number in the prev direction.

Returns the index steps elements away, or None when the end is reached.

NOTE that indexes are likely not sequential.

Example:

let mut index = list.first_index();
index = list.move_index(index, 3);
// Do something with the 4:th element
index = list.move_index(index, -2);
// Do something with the 2:nd element
index = list.move_index(index, -2);
assert!(index.is_none());
source

pub fn get_first(&self) -> Option<&T>

Get a reference to the first element data, or None.

Example:

let data = list.get_first();
source

pub fn get_last(&self) -> Option<&T>

Get a reference to the last element data, or None.

Example:

let data = list.get_last();
source

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

Get an immutable reference to the element data at the index, or None.

Example:

let data = list.get(index);
source

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

Get a mutable reference to the first element data, or None.

Example:

if let Some(data) = list.get_mut_first() {
    // Update the data somehow
    *data = 0;
}
source

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

Get a mutable reference to the last element data, or None.

Example:

if let Some(data) = list.get_mut_last() {
    // Update the data somehow
    *data *= 2;
}
source

pub fn get_mut(&mut self, index: ListIndex) -> Option<&mut T>

Get a mutable reference to the element data at the index, or None.

Example:

if let Some(data) = list.get_mut(index) {
    // Update the data somehow
    *data += 1;
}
source

pub fn swap_index(&mut self, this: ListIndex, that: ListIndex)

Swap the element data between two indexes.

Both indexes must be valid.

Example:

list.swap_index(list.first_index(), list.last_index());
source

pub fn peek_next(&self, index: ListIndex) -> Option<&T>

Peek at next element data, after the index, if any.

Returns None if there is no next index in the list.

Example:

if let Some(data) = list.peek_next(index) {
    // Consider the next data
}
source

pub fn peek_prev(&self, index: ListIndex) -> Option<&T>

Peek at previous element data, before the index, if any.

Returns None if there is no previous index in the list.

Example:

if let Some(data) = list.peek_prev(index) {
    // Consider the previous data
}
source

pub fn contains(&self, elem: T) -> bool
where T: PartialEq,

Returns true if the element is in the list.

Example:

if list.contains(42) {
    // Find it?
} else {
    // Insert it?
}
source

pub fn index_of(&self, elem: T) -> ListIndex
where T: PartialEq,

Returns the index of the element containg the data.

If there is more than one element with the same data, the one with the lowest index will always be returned.

Example:

let index = list.index_of(2);
source

pub fn insert_first(&mut self, elem: T) -> ListIndex

Insert a new element at the beginning.

It is usually not necessary to keep the index, as the element data can always be found again by walking the list.

Example:

let index = list.insert_first(42);
source

pub fn insert_last(&mut self, elem: T) -> ListIndex

Insert a new element at the end.

It is typically not necessary to store the index, as the data will be there when walking the list.

Example:

let index = list.insert_last(42);
source

pub fn insert_before(&mut self, index: ListIndex, elem: T) -> ListIndex

Insert a new element before the index.

If the index is None then the new element will be inserted first.

Example:

index = list.insert_before(index, 42);
source

pub fn insert_after(&mut self, index: ListIndex, elem: T) -> ListIndex

Insert a new element after the index.

If the index is None then the new element will be inserted last.

Example:

index = list.insert_after(index, 42);
source

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

Remove the first element and return its data.

Example:

let data = list.remove_first();
source

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

Remove the last element and return its data.

Example:

let data = list.remove_last();
source

pub fn remove(&mut self, index: ListIndex) -> Option<T>

Remove the element at the index and return its data.

Example:

let data = list.remove(index);
source

pub fn iter(&self) -> ListIter<'_, T>

Create a new iterator over all the elements.

Example:

let total: usize = list.iter().sum();
assert_eq!(total, 720);
source

pub fn drain_iter(&mut self) -> ListDrainIter<'_, T>

Create a draining iterator over all the elements.

This iterator will remove the elements as it is iterating over them.

Example:

let items: Vec<&str> = list.drain_iter().collect();
assert_eq!(list.len(), 0);
assert_eq!(items, vec!["A", "B", "C"]);
source

pub fn to_vec(&self) -> Vec<&T>

Create a vector for all elements.

Returns a new vector with immutable reference to the elements data.

Example:

let vector: Vec<&u64> = list.to_vec();
source

pub fn from(vec: &mut Vec<T>) -> IndexList<T>

Insert all the elements from the vector, which will be drained.

Example:

let mut the_numbers = vec![4, 8, 15, 16, 23, 42];
let list = IndexList::from(&mut the_numbers);
assert_eq!(the_numbers.len(), 0);
assert_eq!(list.len(), 6);
source

pub fn trim_safe(&mut self)

Remove any unused indexes at the end by truncating.

If the unused indexes don’t appear at the end, then nothing happens.

No valid indexes are changed.

Example:

list.remove_last();
assert!(list.len() < list.capacity());
list.trim_safe();
assert_eq!(list.len(), list.capacity());
source

pub fn trim_swap(&mut self)

Remove all unused elements by swapping indexes and then truncating.

This will reduce the capacity of the list, but only if there are any unused elements. Length and capacity will be equal after the call.

NOTE that this call may invalidate some indexes.

While it is possible to tell if an index has become invalid, because only indexes at or above the new capacity limit has been moved, it is not recommended to rely on that fact or test for it.

Example:

list.remove_first();
assert!(list.len() < list.capacity());
list.trim_swap();
assert_eq!(list.len(), list.capacity());
source

pub fn append(&mut self, other: &mut IndexList<T>)

Add the elements of the other list at the end.

The other list will be empty after the call as all its elements have been moved to this list.

Example:

let sum_both = list.len() + other.len();
list.append(&mut other);
assert!(other.is_empty());
assert_eq!(list.len(), sum_both);
source

pub fn prepend(&mut self, other: &mut IndexList<T>)

Add the elements of the other list at the beginning.

The other list will be empty after the call as all its elements have been moved to this list.

Example:

let sum_both = list.len() + other.len();
list.prepend(&mut other);
assert!(other.is_empty());
assert_eq!(list.len(), sum_both);
source

pub fn split(&mut self, index: ListIndex) -> IndexList<T>

Split the list by moving the elements from the index to a new list.

The original list will no longer contain the elements data that was moved to the other list.

Example:

let total = list.len();
let other = list.split(index);
assert!(list.len() < total);
assert_eq!(list.len() + other.len(), total);

Trait Implementations§

source§

impl<T: Debug> Debug for IndexList<T>

source§

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

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

impl<T> Default for IndexList<T>

source§

fn default() -> Self

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

impl<T> Display for IndexList<T>
where T: Display,

source§

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

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

impl<T> Extend<T> for IndexList<T>

source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T> From<T> for IndexList<T>

source§

fn from(elem: T) -> IndexList<T>

Converts to this type from the input type.
source§

impl<T> FromIterator<T> for IndexList<T>

source§

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

Creates a value from an iterator. Read more
source§

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

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = ListIter<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

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> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.