cordyceps

Struct SortedList

Source
pub struct SortedList<T: Linked<Links<T>>> { /* private fields */ }
Expand description

A sorted singly linked list

This behaves similar to Stack, in that it is a singly linked list, however items are stored in an ordered fashion. This means that insertion is an O(n) operation, and retrieval of the first item is an O(1) operation.

It allows for a user selected ordering operation. If your type T implements Ord:

If your type T does NOT implement Ord, or you want to use a custom sorting anyway, consider using SortedList::new_with_cmp()

In order to be part of a SortedList, a type T must implement the Linked trait for sorted_list::Links<T>, which is an alias for stack::Links<T>. This means that you can link the same element into either structure, but you can’t have something that’s linked into a SortedList and a Stack at the same time (without wrapper structs that have separate sets of links, left as an exercise for the reader).

Pushing elements into a SortedList takes ownership of those elements through an owning Handle type. Dropping a SortedList drops all elements currently linked into the stack.

Implementations§

Source§

impl<T> SortedList<T>
where T: Linked<Links<T>> + Ord,

Source

pub const fn new_min() -> Self

Create a new (empty) sorted list, sorted LEAST FIRST

If two items are considered of equal value, new values will be placed AFTER old values.

Source

pub fn from_stack_min(stack: Stack<T>) -> Self

Create a new sorted list, consuming the stack, sorted LEAST FIRST

Source

pub const fn new_max() -> Self

Create a new (empty) sorted list, sorted GREATEST FIRST

If two items are considered of equal value, new values will be placed AFTER old values.

Source

pub fn from_stack_max(stack: Stack<T>) -> Self

Create a new sorted list, consuming the stack, sorted GREATEST FIRST

Source§

impl<T: Linked<Links<T>>> SortedList<T>

Source

pub const fn new_with_cmp(f: fn(_: &T, _: &T) -> Ordering) -> Self

Create a new (empty) sorted list with the given ordering function

If your type T implements Ord:

If T contained an i32, and you wanted the SMALLEST items at the front, then you could provide a function something like:

let f: fn(&i32, &i32) -> core::cmp::Ordering = |lhs, rhs| {
    lhs.cmp(rhs)
};

SortedList takes a function (and not just Ord) so you can use it on types that don’t have a general way of ordering them, allowing you to select a specific metric within the sorting function.

If two items are considered of equal value, new values will be placed AFTER old values.

Source

pub fn from_stack_with_cmp( stack: Stack<T>, f: fn(_: &T, _: &T) -> Ordering, ) -> Self

Create a new sorted list, consuming the stack, using the provided ordering function

Source

pub fn pop_front(&mut self) -> Option<T::Handle>

Pop the front-most item from the list, returning it by ownership (if it exists)

Note that “front” here refers to the sorted ordering. If this list was created with SortedList::new_min, the SMALLEST item will be popped. If this was created with SortedList::new_max, the LARGEST item will be popped.

This is an O(1) operation.

Source

pub fn insert(&mut self, element: T::Handle)

Insert a single item into the list, in its sorted order position

Note that if the inserted item is Equal to another item in the list, the newest item is always sorted AFTER the existing item.

This is an O(n) operation.

Source

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

Iterate through the items of the list, in sorted order

Trait Implementations§

Source§

impl<T> Debug for SortedList<T>
where T: Linked<Links<T>>,

Source§

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

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

impl<T: Linked<Links<T>>> Drop for SortedList<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Linked<Links<T>>> Extend<<T as Linked<Links<T>>>::Handle> for SortedList<T>

Source§

fn extend<I: IntoIterator<Item = T::Handle>>(&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

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