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
:
- Consider using
SortedList::new_min()
if you want smallest items sorted first. - Consider using
SortedList::new_max()
if you want largest items sorted first.
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>
impl<T> SortedList<T>
Sourcepub const fn new_min() -> Self
pub const fn new_min() -> Self
Create a new (empty) sorted list, sorted LEAST FIRST
- Consider using
SortedList::new_max()
if you want largest items sorted first. - Consider using
SortedList::new_with_cmp()
if you want to provide your own sorting implementation.
If two items are considered of equal value, new values will be placed AFTER old values.
Sourcepub fn from_stack_min(stack: Stack<T>) -> Self
pub fn from_stack_min(stack: Stack<T>) -> Self
Create a new sorted list, consuming the stack, sorted LEAST FIRST
Sourcepub const fn new_max() -> Self
pub const fn new_max() -> Self
Create a new (empty) sorted list, sorted GREATEST FIRST
- Consider using
SortedList::new_min()
if you want smallest items sorted first. - Consider using
SortedList::new_with_cmp()
if you want to provide your own sorting implementation.
If two items are considered of equal value, new values will be placed AFTER old values.
Sourcepub fn from_stack_max(stack: Stack<T>) -> Self
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>
impl<T: Linked<Links<T>>> SortedList<T>
Sourcepub const fn new_with_cmp(f: fn(_: &T, _: &T) -> Ordering) -> Self
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
:
- Consider using
SortedList::new_min()
if you want smallest items sorted first. - Consider using
SortedList::new_max()
if you want largest items sorted first.
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.
Sourcepub fn from_stack_with_cmp(
stack: Stack<T>,
f: fn(_: &T, _: &T) -> Ordering,
) -> Self
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
Sourcepub fn pop_front(&mut self) -> Option<T::Handle>
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.
Sourcepub fn insert(&mut self, element: T::Handle)
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.
Sourcepub fn iter(&self) -> SortedListIter<'_, T> ⓘ
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>
impl<T> Debug for SortedList<T>
Source§impl<T: Linked<Links<T>>> Extend<<T as Linked<Links<T>>>::Handle> for SortedList<T>
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)
fn extend<I: IntoIterator<Item = T::Handle>>(&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
)