Struct heapless::sorted_linked_list::SortedLinkedList

source ·
pub struct SortedLinkedList<T, Idx, K, const N: usize>{ /* private fields */ }
Expand description

The linked list.

Implementations§

source§

impl<T, K, const N: usize> SortedLinkedList<T, LinkedIndexU8, K, N>

source

pub const fn new_u8() -> Self

Create a new linked list.

source§

impl<T, K, const N: usize> SortedLinkedList<T, LinkedIndexU16, K, N>

source

pub const fn new_u16() -> Self

Create a new linked list.

source§

impl<T, K, const N: usize> SortedLinkedList<T, LinkedIndexUsize, K, N>

source

pub const fn new_usize() -> Self

Create a new linked list.

source§

impl<T, Idx, K, const N: usize> SortedLinkedList<T, Idx, K, N>
where T: Ord, Idx: SortedLinkedListIndex, K: Kind,

source

pub unsafe fn push_unchecked(&mut self, value: T)

Pushes a value onto the list without checking if the list is full.

Complexity is worst-case O(N).

§Safety

Assumes that the list is not full.

source

pub fn push(&mut self, value: T) -> Result<(), T>

Pushes an element to the linked list and sorts it into place.

Complexity is worst-case O(N).

§Example
use heapless::sorted_linked_list::{SortedLinkedList, Max};
let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();

// The largest value will always be first
ll.push(1).unwrap();
assert_eq!(ll.peek(), Some(&1));

ll.push(2).unwrap();
assert_eq!(ll.peek(), Some(&2));

ll.push(3).unwrap();
assert_eq!(ll.peek(), Some(&3));

// This will not fit in the queue.
assert_eq!(ll.push(4), Err(4));
source

pub fn iter(&self) -> Iter<'_, T, Idx, K, N>

Get an iterator over the sorted list.

§Example
use heapless::sorted_linked_list::{SortedLinkedList, Max};
let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();

ll.push(1).unwrap();
ll.push(2).unwrap();

let mut iter = ll.iter();

assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), None);
source

pub fn find_mut<F>(&mut self, f: F) -> Option<FindMut<'_, T, Idx, K, N>>
where F: FnMut(&T) -> bool,

Find an element in the list that can be changed and resorted.

§Example
use heapless::sorted_linked_list::{SortedLinkedList, Max};
let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();

ll.push(1).unwrap();
ll.push(2).unwrap();
ll.push(3).unwrap();

// Find a value and update it
let mut find = ll.find_mut(|v| *v == 2).unwrap();
*find += 1000;
find.finish();

assert_eq!(ll.pop(), Ok(1002));
assert_eq!(ll.pop(), Ok(3));
assert_eq!(ll.pop(), Ok(1));
assert_eq!(ll.pop(), Err(()));
source

pub fn peek(&self) -> Option<&T>

Peek at the first element.

§Example
use heapless::sorted_linked_list::{SortedLinkedList, Max, Min};
let mut ll_max: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();

// The largest value will always be first
ll_max.push(1).unwrap();
assert_eq!(ll_max.peek(), Some(&1));
ll_max.push(2).unwrap();
assert_eq!(ll_max.peek(), Some(&2));
ll_max.push(3).unwrap();
assert_eq!(ll_max.peek(), Some(&3));

let mut ll_min: SortedLinkedList<_, _, Min, 3> = SortedLinkedList::new_usize();

// The Smallest value will always be first
ll_min.push(3).unwrap();
assert_eq!(ll_min.peek(), Some(&3));
ll_min.push(2).unwrap();
assert_eq!(ll_min.peek(), Some(&2));
ll_min.push(1).unwrap();
assert_eq!(ll_min.peek(), Some(&1));
source

pub unsafe fn pop_unchecked(&mut self) -> T

Pop an element from the list without checking so the list is not empty.

§Safety

Assumes that the list is not empty.

source

pub fn pop(&mut self) -> Result<T, ()>

Pops the first element in the list.

Complexity is worst-case O(1).

§Example
use heapless::sorted_linked_list::{SortedLinkedList, Max};
let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();

ll.push(1).unwrap();
ll.push(2).unwrap();

assert_eq!(ll.pop(), Ok(2));
assert_eq!(ll.pop(), Ok(1));
assert_eq!(ll.pop(), Err(()));
source

pub fn is_full(&self) -> bool

Checks if the linked list is full.

§Example
use heapless::sorted_linked_list::{SortedLinkedList, Max};
let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();

assert_eq!(ll.is_full(), false);

ll.push(1).unwrap();
assert_eq!(ll.is_full(), false);
ll.push(2).unwrap();
assert_eq!(ll.is_full(), false);
ll.push(3).unwrap();
assert_eq!(ll.is_full(), true);
source

pub fn is_empty(&self) -> bool

Checks if the linked list is empty.

§Example
use heapless::sorted_linked_list::{SortedLinkedList, Max};
let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();

assert_eq!(ll.is_empty(), true);

ll.push(1).unwrap();
assert_eq!(ll.is_empty(), false);

Trait Implementations§

source§

impl<T, Idx, K, const N: usize> Debug for SortedLinkedList<T, Idx, K, N>
where T: Ord + Debug, Idx: SortedLinkedListIndex, K: Kind,

source§

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

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

impl<T, Idx, K, const N: usize> Drop for SortedLinkedList<T, Idx, K, N>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, Idx, K, const N: usize> Freeze for SortedLinkedList<T, Idx, K, N>
where Idx: Freeze, T: Freeze,

§

impl<T, Idx, K, const N: usize> RefUnwindSafe for SortedLinkedList<T, Idx, K, N>

§

impl<T, Idx, K, const N: usize> Send for SortedLinkedList<T, Idx, K, N>
where Idx: Send, K: Send, T: Send,

§

impl<T, Idx, K, const N: usize> Sync for SortedLinkedList<T, Idx, K, N>
where Idx: Sync, K: Sync, T: Sync,

§

impl<T, Idx, K, const N: usize> Unpin for SortedLinkedList<T, Idx, K, N>
where Idx: Unpin, K: Unpin, T: Unpin,

§

impl<T, Idx, K, const N: usize> UnwindSafe for SortedLinkedList<T, Idx, K, N>
where Idx: UnwindSafe, K: UnwindSafe, T: UnwindSafe,

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

§

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

§

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.