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>
impl<T> IndexList<T>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty index list.
Example:
use index_list::IndexList;
let list = IndexList::<u64>::new();
sourcepub fn with_capacity(capacity: usize) -> Self
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);
sourcepub fn capacity(&self) -> usize
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());
sourcepub fn len(&self) -> usize
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());
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the list be removing all elements, making it empty.
Example:
list.clear();
assert!(list.is_empty());
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
when the list is empty.
Example:
let list = IndexList::<u64>::new();
assert!(list.is_empty());
sourcepub fn is_index_used(&self, index: ListIndex) -> bool
pub fn is_index_used(&self, index: ListIndex) -> bool
Returns true
if the index is valid.
sourcepub fn first_index(&self) -> ListIndex
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();
sourcepub fn last_index(&self) -> ListIndex
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();
sourcepub fn next_index(&self, index: ListIndex) -> ListIndex
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);
}
sourcepub fn prev_index(&self, index: ListIndex) -> ListIndex
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);
}
sourcepub fn move_index(&self, index: ListIndex, steps: i32) -> ListIndex
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());
sourcepub fn get_first(&self) -> Option<&T>
pub fn get_first(&self) -> Option<&T>
Get a reference to the first element data, or None
.
Example:
let data = list.get_first();
sourcepub fn get_last(&self) -> Option<&T>
pub fn get_last(&self) -> Option<&T>
Get a reference to the last element data, or None
.
Example:
let data = list.get_last();
sourcepub fn get(&self, index: ListIndex) -> Option<&T>
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);
sourcepub fn get_mut_first(&mut self) -> Option<&mut T>
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;
}
sourcepub fn get_mut_last(&mut self) -> Option<&mut T>
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;
}
sourcepub fn get_mut(&mut self, index: ListIndex) -> Option<&mut T>
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;
}
sourcepub fn swap_index(&mut self, this: ListIndex, that: ListIndex)
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());
sourcepub fn peek_next(&self, index: ListIndex) -> Option<&T>
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
}
sourcepub fn peek_prev(&self, index: ListIndex) -> Option<&T>
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
}
sourcepub fn contains(&self, elem: T) -> boolwhere
T: PartialEq,
pub fn contains(&self, elem: T) -> boolwhere
T: PartialEq,
Returns true
if the element is in the list.
Example:
if list.contains(42) {
// Find it?
} else {
// Insert it?
}
sourcepub fn index_of(&self, elem: T) -> ListIndexwhere
T: PartialEq,
pub fn index_of(&self, elem: T) -> ListIndexwhere
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);
sourcepub fn insert_first(&mut self, elem: T) -> ListIndex
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);
sourcepub fn insert_last(&mut self, elem: T) -> ListIndex
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);
sourcepub fn insert_before(&mut self, index: ListIndex, elem: T) -> ListIndex
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);
sourcepub fn insert_after(&mut self, index: ListIndex, elem: T) -> ListIndex
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);
sourcepub fn remove_first(&mut self) -> Option<T>
pub fn remove_first(&mut self) -> Option<T>
Remove the first element and return its data.
Example:
let data = list.remove_first();
sourcepub fn remove_last(&mut self) -> Option<T>
pub fn remove_last(&mut self) -> Option<T>
Remove the last element and return its data.
Example:
let data = list.remove_last();
sourcepub fn remove(&mut self, index: ListIndex) -> Option<T>
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);
sourcepub fn iter(&self) -> ListIter<'_, T> ⓘ
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);
sourcepub fn drain_iter(&mut self) -> ListDrainIter<'_, T> ⓘ
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"]);
sourcepub fn to_vec(&self) -> Vec<&T>
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();
sourcepub fn from(vec: &mut Vec<T>) -> IndexList<T>
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);
sourcepub fn trim_safe(&mut self)
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());
sourcepub fn trim_swap(&mut self)
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());
sourcepub fn append(&mut self, other: &mut IndexList<T>)
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);
sourcepub fn prepend(&mut self, other: &mut IndexList<T>)
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);
sourcepub fn split(&mut self, index: ListIndex) -> IndexList<T>
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> Extend<T> for IndexList<T>
impl<T> Extend<T> for IndexList<T>
source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)