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>
impl<T> 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
use limited_queue::LimitedQueue;
let mut q = LimitedQueue::with_capacity(2);
q.push(1);
assert_eq!(q.get(100), 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
Sourcepub fn len(&self) -> usize
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);
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);
}
Source§impl<T: Default> LimitedQueue<T>
Optionally provide pop
method for
types that implements Default
trait
impl<T: Default> LimitedQueue<T>
Optionally provide pop
method for
types that implements Default
trait