Struct limited_queue::LimitedQueue

source ·
pub struct LimitedQueue<T> { /* private fields */ }
Expand description

A circular queue that overrides the oldest data if trying to push data when queue is full.

All operations are of O(1) complexity, except the constructor with O(Vec::with_capacity).

§Example

let mut q = limited_queue::LimitedQueue::with_capacity(5);
// push_ret: [None x 5, 0, 1, ..., 4]
let push_ret = [[None; 5], core::array::from_fn(|i| Some(i))].concat();
for (i, pr) in (0..10).zip(push_ret) {
    assert_eq!(q.push(i), pr);
}
for (n, element) in q.iter().enumerate() {
    assert_eq!(element.clone(), q[n]); // 5, 6, 7, 8, 9
    assert_eq!(element.clone(), n + 5); // 5, 6, 7, 8, 9
}

§Error

For indexing, no bound check will occur, so please check the size of the queue with len method before subscription.

Implementations§

source§

impl<T: Default> LimitedQueue<T>

source

pub fn with_capacity(cap: usize) -> LimitedQueue<T>

Vec-like constructor

use limited_queue::LimitedQueue;

let mut q = LimitedQueue::with_capacity(2);

assert_eq!(q.push(1), None);
assert_eq!(q.push(2), None);

// first element popped since the capacity is 2
assert_eq!(q.push(3), Some(1));

assert_eq!(q.peek(), Some(&2));
assert_eq!(q.pop(), Some(2));
assert_eq!(q.peek(), Some(&3));
assert_eq!(q.pop(), Some(3));

@param cap Capacity (limit size) of the queue

source

pub fn get(&self, idx: usize) -> Option<&T>

Get the element at position idx, a.k.a. the position from the start of queue

source

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

Pop the first element from queue

use limited_queue::LimitedQueue;

let mut q = LimitedQueue::with_capacity(1);

q.push(1234);
assert_eq!(q.pop(), Some(1234));
assert_eq!(q.pop(), None);
source

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

Peek the oldest element at the front of the queue

use limited_queue::LimitedQueue;

let mut q = LimitedQueue::with_capacity(1);

q.push(1234);
assert_eq!(q.peek(), Some(&1234));
assert_eq!(q.pop(), Some(1234));
assert_eq!(q.peek(), None);
source

pub fn push(&mut self, ele: T) -> Option<T>

Push a new element into queue, removing the oldest element if the queue is full

source

pub fn is_empty(&self) -> bool

source

pub fn is_full(&self) -> bool

source

pub fn len(&self) -> usize

source

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

To traverse all the elements in LimitedQueue, for example:

use limited_queue::LimitedQueue;

let mut q = LimitedQueue::with_capacity(5);
for i in 0..10 {
    q.push(i);
}
for (&n, element) in q.iter().zip(5usize..=9) {
    // will be 5, 6, 7, 8, 9 since 0 ~ 4
    // are popped because the queue is full
    assert_eq!(element.clone(), n);
}
source

pub fn clear(&mut self)

O(1) method to (lazily) clear all the elements

Trait Implementations§

source§

impl<T: Debug> Debug for LimitedQueue<T>

source§

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

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

impl<T> Index<usize> for LimitedQueue<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, idx: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T> IndexMut<usize> for LimitedQueue<T>

source§

fn index_mut(&mut self, idx: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for LimitedQueue<T>

§

impl<T> RefUnwindSafe for LimitedQueue<T>
where T: RefUnwindSafe,

§

impl<T> Send for LimitedQueue<T>
where T: Send,

§

impl<T> Sync for LimitedQueue<T>
where T: Sync,

§

impl<T> Unpin for LimitedQueue<T>
where T: Unpin,

§

impl<T> UnwindSafe for LimitedQueue<T>
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>,

§

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.