Struct index_list::IndexList [−][src]
Implementations
impl<T> IndexList<T>
[src]
pub fn new() -> Self
[src]
Creates a new empty index list.
Example:
use index_list::IndexList; let list = IndexList::<u64>::new();
pub fn capacity(&self) -> usize
[src]
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());
pub fn len(&self) -> usize
[src]
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());
pub fn clear(&mut self)
[src]
Clears the list be removing all elements, making it empty.
Example:
list.clear(); assert!(list.is_empty());
pub fn is_empty(&self) -> bool
[src]
Returns true
when the list is empty.
Example:
let list = IndexList::<u64>::new(); assert!(list.is_empty());
pub fn is_index_used(&self, index: Index) -> bool
[src]
Returns true
if the index is valid.
pub fn first_index(&self) -> Index
[src]
Returns the index of the first element, or None
if the list is empty.
Example:
let index = list.first_index();
pub fn last_index(&self) -> Index
[src]
Returns the index of the last element, or None
if the list is empty.
Example:
let index = list.last_index();
pub fn next_index(&self, index: Index) -> Index
[src]
Returns the index of the next element, after index, or None
when the
end is reached.
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); }
pub fn prev_index(&self, index: Index) -> Index
[src]
Returns the index of the previous element, before index, or None
when
the beginning is reached.
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); }
pub fn move_index(&self, index: Index, steps: i32) -> Index
[src]
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());
pub fn get_first(&self) -> Option<&T>
[src]
Get a reference to the first element data, or None
.
Example:
let data = list.get_first();
pub fn get_last(&self) -> Option<&T>
[src]
Get a reference to the last element data, or None
.
Example:
let data = list.get_last();
pub fn get(&self, index: Index) -> Option<&T>
[src]
Get an immutable reference to the element data at the index, or None
.
Example:
let data = list.get(index);
pub fn get_mut_first(&mut self) -> Option<&mut T>
[src]
Get a mutable reference to the first element data, or None
.
Example:
if let Some(data) = list.get_mut_first() { // Update the data somehow }
pub fn get_mut_last(&mut self) -> Option<&mut T>
[src]
Get a mutable reference to the last element data, or None
.
Example:
if let Some(data) = list.get_mut_last() { // Update the data somehow }
pub fn get_mut(&mut self, index: Index) -> Option<&mut T>
[src]
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 }
pub fn peek_next(&self, index: Index) -> Option<&T>
[src]
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 }
pub fn peek_prev(&self, index: Index) -> Option<&T>
[src]
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 }
pub fn contains(&self, elem: T) -> bool where
T: PartialEq,
[src]
T: PartialEq,
Returns true
if the element is in the list.
Example:
if list.contains(42) { // Find it? } else { // Insert it? }
pub fn index_of(&self, elem: T) -> Index where
T: PartialEq,
[src]
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);
pub fn insert_first(&mut self, elem: T) -> Index
[src]
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);
pub fn insert_last(&mut self, elem: T) -> Index
[src]
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);
pub fn insert_before(&mut self, index: Index, elem: T) -> Index
[src]
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);
pub fn insert_after(&mut self, index: Index, elem: T) -> Index
[src]
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);
pub fn remove_first(&mut self) -> Option<T>
[src]
Remove the first element and return its data.
Example:
let data = list.remove_first();
pub fn remove_last(&mut self) -> Option<T>
[src]
Remove the last element and return its data.
Example:
let data = list.remove_last();
pub fn remove(&mut self, index: Index) -> Option<T>
[src]
Remove the element at the index and return its data.
Example:
let data = list.remove(index);
pub fn iter(&self) -> Iter<'_, T>ⓘ
[src]
Create a new iterator over all the element.
Example:
let total: usize = list.iter().sum(); assert_eq!(total, 720);
pub fn to_vec(&self) -> Vec<&T>
[src]
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();
pub fn from(vec: &mut Vec<T>) -> IndexList<T>
[src]
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);
pub fn trim_safe(&mut self)
[src]
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());
pub fn trim_swap(&mut self)
[src]
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());
pub fn append(&mut self, other: &mut IndexList<T>)
[src]
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);
pub fn prepend(&mut self, other: &mut IndexList<T>)
[src]
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);
pub fn split(&mut self, index: Index) -> IndexList<T>
[src]
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
impl<T: Debug> Debug for IndexList<T>
[src]
impl<T> Default for IndexList<T>
[src]
impl<T> Display for IndexList<T> where
T: Display,
[src]
T: Display,
impl<T> From<T> for IndexList<T>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for IndexList<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for IndexList<T> where
T: Send,
T: Send,
impl<T> Sync for IndexList<T> where
T: Sync,
T: Sync,
impl<T> Unpin for IndexList<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for IndexList<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<!> for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,