pub struct CircularBuffer<T> { /* private fields */ }
Expand description
循环的圆结构 如果数据满了之后将自动在结尾后续添加,并保持最大个数
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
circular.push_back(1);
circular.push_back(2);
circular.push_back(3);
assert_eq!(circular.len(), 2);
assert_eq!(circular[0], 2);
assert_eq!(circular[1], 3);
}
Implementations§
Source§impl<T> CircularBuffer<T>
impl<T> CircularBuffer<T>
pub fn new(cap: usize) -> Self
Sourcepub fn is_inited(&self) -> bool
pub fn is_inited(&self) -> bool
是否已经填满过所有的元素
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
circular.push_back(1);
assert_eq!(circular.is_inited(), false);
circular.push_back(2);
assert_eq!(circular.is_inited(), true);
circular.pop_back();
assert_eq!(circular.is_inited(), true);
}
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
是否元素已满
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
circular.push_back(1);
assert_eq!(circular.is_full(), false);
circular.push_back(2);
assert_eq!(circular.is_full(), true);
circular.pop_back();
assert_eq!(circular.is_full(), false);
}
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
是否元素为空
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
assert_eq!(circular.is_empty(), true);
circular.push_back(1);
assert_eq!(circular.is_empty(), false);
}
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
返回元素长度
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
assert_eq!(circular.is_empty(), true);
circular.push_back(1);
circular.push_back(2);
assert_eq!(circular.len(), 2);
circular.push_front(1);
assert_eq!(circular.len(), 2);
circular.pop_front();
assert_eq!(circular.len(), 1);
}
Sourcepub fn push_back(&mut self, val: T)
pub fn push_back(&mut self, val: T)
在元素末尾添加元素
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
assert_eq!(circular.is_empty(), true);
circular.push_back(1);
circular.push_back(2);
assert_eq!(circular[0], 1);
}
Sourcepub fn push_front(&mut self, val: T)
pub fn push_front(&mut self, val: T)
在元素前面添加元素
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
assert_eq!(circular.is_empty(), true);
circular.push_front(1);
circular.push_front(2);
assert_eq!(circular[0], 2);
}
Sourcepub fn pop_front(&mut self)
pub fn pop_front(&mut self)
将最前面的元素弹出
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
assert_eq!(circular.is_empty(), true);
circular.push_front(1);
circular.push_front(2);
assert_eq!(circular[0], 2);
circular.pop_front();
assert_eq!(circular[0], 1);
}
Sourcepub fn pop_back(&mut self)
pub fn pop_back(&mut self)
将最后面的元素弹出
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
assert_eq!(circular.is_empty(), true);
circular.push_back(1);
circular.push_back(2);
assert_eq!(circular[0], 1);
circular.pop_back();
assert_eq!(circular[0], 1);
}
Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
迭代器
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
assert_eq!(circular.is_empty(), true);
circular.push_back(1);
circular.push_back(2);
let val: Vec<i32> = circular.iter().map(|s| *s).collect();
assert_eq!(val, vec![1, 2]);
let val: Vec<i32> = circular.iter().rev().map(|s| *s).collect();
assert_eq!(val, vec![2, 1]);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
迭代更改器
§Examples
use algorithm::CircularBuffer;
fn main() {
let mut circular = CircularBuffer::new(2);
assert_eq!(circular.is_empty(), true);
circular.push_back(1);
circular.push_back(2);
let val: Vec<i32> = circular.iter_mut().map(|v| { *v *= 2; *v }).collect();
assert_eq!(val, vec![2, 4]);
let val: Vec<i32> = circular.iter_mut().rev().map(|v| { *v *= 2; *v }).collect();
assert_eq!(val, vec![8, 4]);
}
Trait Implementations§
Source§impl<T: Clone> Clone for CircularBuffer<T>
impl<T: Clone> Clone for CircularBuffer<T>
Source§impl<T> Index<usize> for CircularBuffer<T>
impl<T> Index<usize> for CircularBuffer<T>
Source§impl<T> IndexMut<usize> for CircularBuffer<T>
impl<T> IndexMut<usize> for CircularBuffer<T>
Source§impl<T> PartialEq for CircularBuffer<T>where
T: Eq,
impl<T> PartialEq for CircularBuffer<T>where
T: Eq,
impl<T> Eq for CircularBuffer<T>where
T: Eq,
Auto Trait Implementations§
impl<T> Freeze for CircularBuffer<T>
impl<T> RefUnwindSafe for CircularBuffer<T>where
T: RefUnwindSafe,
impl<T> Send for CircularBuffer<T>where
T: Send,
impl<T> Sync for CircularBuffer<T>where
T: Sync,
impl<T> Unpin for CircularBuffer<T>where
T: Unpin,
impl<T> UnwindSafe for CircularBuffer<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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.