pub struct SortedList<T> { /* private fields */ }Expand description
An ordered collection that maintains its elements in sorted order.
The SortedList automatically keeps elements sorted upon insertion,
ensuring efficient search operations.
§Type Parameters
T: The type of elements stored in the list. Must implementPartialOrd.
§Examples
use plain_ds::SortedList;
let mut list = SortedList::new();
list.push(3);
list.push(1);
list.push(2);
assert_eq!(list.len(), 3);
assert_eq!(list.to_vec(), vec![1, 2, 3]);Implementations§
Source§impl<T> SortedList<T>
impl<T> SortedList<T>
Sourcepub fn from_slice(slice: &[T]) -> Selfwhere
T: Clone + PartialOrd,
pub fn from_slice(slice: &[T]) -> Selfwhere
T: Clone + PartialOrd,
Creates list from slice.
Efficiency: O(n)
Trait Implementations§
Source§impl<'a, T> List<'a, T> for SortedList<T>where
T: PartialOrd + 'a,
impl<'a, T> List<'a, T> for SortedList<T>where
T: PartialOrd + 'a,
Source§fn head(&self) -> Option<&T>
fn head(&self) -> Option<&T>
Returns the payload value of the first node in the list.
Efficiency: O(1)
Source§fn last(&self) -> Option<&T>
fn last(&self) -> Option<&T>
Returns the payload value of the last node in the list.
Efficiency: O(1)
Source§fn iter(&self) -> impl Iterator<Item = &'a T>
fn iter(&self) -> impl Iterator<Item = &'a T>
Returns an iterator over the immutable items of the list.
Source§fn iter_mut(&mut self) -> impl Iterator<Item = &'a mut T>
fn iter_mut(&mut self) -> impl Iterator<Item = &'a mut T>
Returns an iterator over the mutable items of the list.
Source§fn push(&mut self, payload: T)
fn push(&mut self, payload: T)
Adds a new node to the list according to the sort order.
Efficiency: O(n) at worst
Source§fn pop_back(&mut self) -> Option<T>
fn pop_back(&mut self) -> Option<T>
Removes a node from the end of the list and returns its payload value.
Efficiency: O(n)
Source§fn pop_front(&mut self) -> Option<T>
fn pop_front(&mut self) -> Option<T>
Removes a node from the front of the list and returns its payload value.
Efficiency: O(1)
Source§fn remove(&mut self, index: usize) -> Result<T>
fn remove(&mut self, index: usize) -> Result<T>
Removes a node from the specified location in the list. Error returns, if the index out of bounds.
Efficiency: O(n)
Source§fn find(&self, value: &T) -> Option<usize>where
T: PartialEq<T>,
fn find(&self, value: &T) -> Option<usize>where
T: PartialEq<T>,
Finds the first node whose payload is equal to the given value and returns its index.
Returns None if there is no such node.
Efficiency: O(n) at worst