Struct 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.

If you need boundary check, please use get method.

Implementations§

Source§

impl<T> 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

use limited_queue::LimitedQueue;

let mut q = LimitedQueue::with_capacity(2);
q.push(1);
assert_eq!(q.get(100), 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

Get queue length

use limited_queue::LimitedQueue;

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

q.push(1234);
assert_eq!(q.len(), 1);
q.push(1234);
assert_eq!(q.len(), 2);
q.push(1234);
assert_eq!(q.len(), 3);
q.push(1234);
assert_eq!(q.len(), 3);
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

use limited_queue::LimitedQueue;

let mut q = LimitedQueue::with_capacity(5);
for i in 0..10 {
    q.push(i);
}
q.clear();
assert_eq!(q.peek(), None);
assert_eq!(q.is_empty(), true);
Source§

impl<T: Default> LimitedQueue<T>

Optionally provide pop method for types that implements Default trait

Source

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

Pop the first element from queue, will replace the element in queue with the Default value of the element

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);

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>

Source§

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

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.