pub struct SortedLinkedList<T, Idx, K, const N: usize>where
Idx: SortedLinkedListIndex,{ /* private fields */ }
Expand description
The linked list.
Implementations§
source§impl<T, K, const N: usize> SortedLinkedList<T, LinkedIndexU8, K, N>
impl<T, K, const N: usize> SortedLinkedList<T, LinkedIndexU8, K, N>
source§impl<T, K, const N: usize> SortedLinkedList<T, LinkedIndexU16, K, N>
impl<T, K, const N: usize> SortedLinkedList<T, LinkedIndexU16, K, N>
source§impl<T, K, const N: usize> SortedLinkedList<T, LinkedIndexUsize, K, N>
impl<T, K, const N: usize> SortedLinkedList<T, LinkedIndexUsize, K, N>
source§impl<T, Idx, K, const N: usize> SortedLinkedList<T, Idx, K, N>
impl<T, Idx, K, const N: usize> SortedLinkedList<T, Idx, K, N>
sourcepub unsafe fn push_unchecked(&mut self, value: T)
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.
sourcepub fn push(&mut self, value: T) -> Result<(), T>
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));
sourcepub fn iter(&self) -> Iter<'_, T, Idx, K, N> ⓘ
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);
sourcepub fn find_mut<F>(&mut self, f: F) -> Option<FindMut<'_, T, Idx, K, N>>
pub fn find_mut<F>(&mut self, f: F) -> Option<FindMut<'_, T, Idx, K, N>>
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(()));
sourcepub fn peek(&self) -> Option<&T>
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));
sourcepub unsafe fn pop_unchecked(&mut self) -> T
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.
sourcepub fn pop(&mut self) -> Result<T, ()>
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(()));
sourcepub fn is_full(&self) -> bool
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);
sourcepub fn is_empty(&self) -> bool
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>
impl<T, Idx, K, const N: usize> Debug for SortedLinkedList<T, Idx, K, N>
source§impl<T, Idx, K, const N: usize> Drop for SortedLinkedList<T, Idx, K, N>where
Idx: SortedLinkedListIndex,
impl<T, Idx, K, const N: usize> Drop for SortedLinkedList<T, Idx, K, N>where
Idx: SortedLinkedListIndex,
Auto Trait Implementations§
impl<T, Idx, K, const N: usize> Freeze for SortedLinkedList<T, Idx, K, N>
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>
impl<T, Idx, K, const N: usize> Sync for SortedLinkedList<T, Idx, K, N>
impl<T, Idx, K, const N: usize> Unpin for SortedLinkedList<T, Idx, K, N>
impl<T, Idx, K, const N: usize> UnwindSafe for SortedLinkedList<T, Idx, K, N>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more