DynamicDeque

Struct DynamicDeque 

Source
pub struct DynamicDeque<T, const STACK_SIZE: usize> { /* private fields */ }
Expand description

A dynamic deque that can store elements in a stack or a heap. First, elements are pushed to the stack. If the stack is full, elements are pushed to the heap. When popping elements, the stack is popped first. If the stack is empty and the heap is not, the heap is popped and the elements are pushed to the stack. Example:

use sans_io_runtime::collections::DynamicDeque;
let mut deque: DynamicDeque<i32, 5> = DynamicDeque::default();
deque.push_back_stack(1).unwrap();
deque.push_back_stack(2).unwrap();
deque.push_back_stack(3).unwrap();
deque.push_back_stack(4).unwrap();
deque.push_back_stack(5).unwrap();
deque.push_back(6); // Should overflow to heap
assert_eq!(deque.len(), 6);

Implementations§

Source§

impl<T, const STATIC_SIZE: usize> DynamicDeque<T, STATIC_SIZE>

Source

pub fn from<const SIZE: usize>(prepare: [T; SIZE]) -> Self

Creates a new instance of DynamicDeque from an array of elements.

§Arguments
  • prepare - An array of elements to initialize the deque with.
§Returns

A new instance of DynamicDeque.

Source

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

Pushes an element to the stack of the deque.

§Arguments
  • value - The value to be pushed.
§Returns
  • Ok(()) - If the element was successfully pushed.
  • Err(value) - If the element could not be pushed due to overflow.
Source

pub fn push_back(&mut self, value: T)

Pushes an element to the stack or the heap of the deque.

§Arguments
  • value - The value to be pushed.
Source

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

Pops the element from the front of the deque.

§Returns

The popped element, or None if the deque is empty.

Source

pub fn is_empty(&self) -> bool

Checks if the deque is empty.

§Returns

true if the deque is empty, false otherwise.

Source

pub fn len(&self) -> usize

Returns the number of elements in the deque.

§Returns

The number of elements in the deque.

Source

pub fn pop2(&mut self, e: T) -> T

This is useful in task where we usually doing output in queue We need push to back then immediate pop from front

Trait Implementations§

Source§

impl<T: Debug, const STACK_SIZE: usize> Debug for DynamicDeque<T, STACK_SIZE>

Source§

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

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

impl<T, const STACK_SIZE: usize> Default for DynamicDeque<T, STACK_SIZE>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, const STACK_SIZE: usize> Freeze for DynamicDeque<T, STACK_SIZE>
where T: Freeze,

§

impl<T, const STACK_SIZE: usize> RefUnwindSafe for DynamicDeque<T, STACK_SIZE>
where T: RefUnwindSafe,

§

impl<T, const STACK_SIZE: usize> Send for DynamicDeque<T, STACK_SIZE>
where T: Send,

§

impl<T, const STACK_SIZE: usize> Sync for DynamicDeque<T, STACK_SIZE>
where T: Sync,

§

impl<T, const STACK_SIZE: usize> Unpin for DynamicDeque<T, STACK_SIZE>
where T: Unpin,

§

impl<T, const STACK_SIZE: usize> UnwindSafe for DynamicDeque<T, STACK_SIZE>
where 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>,

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.