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>
impl<T: Default> LimitedQueue<T>
sourcepub fn with_capacity(cap: usize) -> LimitedQueue<T>
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
sourcepub fn get(&self, idx: usize) -> Option<&T>
pub fn get(&self, idx: usize) -> Option<&T>
Get the element at position idx,
a.k.a. the position from the start of queue
sourcepub fn pop(&mut self) -> Option<T>
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);sourcepub fn peek(&self) -> Option<&T>
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);sourcepub fn push(&mut self, ele: T) -> Option<T>
pub fn push(&mut self, ele: T) -> Option<T>
Push a new element into queue, removing the oldest element if the queue is full
pub fn is_empty(&self) -> bool
pub fn is_full(&self) -> bool
pub fn len(&self) -> usize
sourcepub fn iter(&self) -> LimitedQueueIterator<'_, T> ⓘ
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);
}Trait Implementations§
source§impl<T: Debug> Debug for LimitedQueue<T>
impl<T: Debug> Debug for LimitedQueue<T>
source§impl<T> Index<usize> for LimitedQueue<T>
impl<T> Index<usize> for LimitedQueue<T>
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> 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