Skip to main content

SortedList

Struct SortedList 

Source
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 implement PartialOrd.

§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>

Source

pub fn new() -> Self

Creates empty ordered list.

Source

pub fn from_slice(slice: &[T]) -> Self
where T: Clone + PartialOrd,

Creates list from slice.

Efficiency: O(n)

Source

pub fn to_vec(&self) -> Vec<T>
where T: Clone,

Collect list values into a vector.

Efficiency: O(n)

Trait Implementations§

Source§

impl<'a, T> List<'a, T> for SortedList<T>
where T: PartialOrd + 'a,

Source§

fn len(&self) -> usize

Returns list size.

Efficiency: O(1)

Source§

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>

Returns the payload value of the last node in the list.

Efficiency: O(1)

Source§

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>

Returns an iterator over the mutable items of the list.

Source§

fn into_iter(self) -> impl Iterator<Item = T>

Returns an iterator that consumes the list.

Source§

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>

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>

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>

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>,

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

Source§

fn is_empty(&self) -> bool

Checks if the list is empty. Read more
Source§

fn get(&self, index: usize) -> Result<&'a T>

Returns a list item by index, or error if index out of bounds. Read more
Source§

fn get_mut(&mut self, index: usize) -> Result<&'a mut T>

Returns a mutable list item by index, or error if index out of bounds. Read more
Source§

fn clear(&mut self)

Removes all items from the list. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SortedList<T>

§

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

§

impl<T> !Send for SortedList<T>

§

impl<T> !Sync for SortedList<T>

§

impl<T> Unpin for SortedList<T>

§

impl<T> UnsafeUnpin for SortedList<T>

§

impl<T> UnwindSafe for SortedList<T>
where T: RefUnwindSafe,

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<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, 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.