algorithm

Struct FixedVec

Source
pub struct FixedVec<T> { /* private fields */ }
Expand description

指定位置的序列, 每个位置上指向上向的位置, 相当于模拟指针 可以根据坐标位置获取相应数据, 亦可以操作上级及下级位置

§Examples

use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_head(1);
    val.insert_head(2);
    assert_eq!(val.len(), 2);
    assert_eq!(val.head(), Some(&2));
    assert_eq!(val.tail(), Some(&1));
    assert_eq!(val.insert_head(3), None);
}

Implementations§

Source§

impl<T> FixedVec<T>

Source

pub fn new(capacity: usize) -> Self

Source

pub fn with_memory(capacity: usize, reserve: usize) -> Self

Source

pub fn capacity(&self) -> usize

获取容量

Source

pub fn len(&self) -> usize

返回长度

Source

pub fn is_empty(&self) -> bool

Source

pub fn is_full(&self) -> bool

Source

pub fn clear(&mut self)

清除数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_head(1);
    assert_eq!(val.len(), 1);
    val.clear();
    assert_eq!(val.len(), 0);
}
Source

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

Source

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

Source

pub fn head_idx(&self) -> usize

获取头部的坐标位置

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_tail(1);
    assert_eq!(val.head_idx(), 0);
}
Source

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

获取头部首位数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_tail(1);
    assert_eq!(val.head(), Some(&1));
}
Source

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

获取头部首位可变数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_tail(1);
    assert_eq!(val.head_mut(), Some(&mut 1));
}
Source

pub fn tail_idx(&self) -> usize

获取尾部的坐标位置

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_tail(1);
    assert_eq!(val.tail_idx(), 0);
}
Source

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

获取尾部首位数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_head(1);
    assert_eq!(val.tail(), Some(&1));
}
Source

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

获取尾部首位可变数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_head(1);
    assert_eq!(val.tail_mut(), Some(&mut 1));
}
Source

pub fn insert_head(&mut self, data: T) -> Option<(usize, &mut T)>

从头部插入新的数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    assert_eq!(val.insert_head(1), Some((0, &mut 1)));
    assert_eq!(val.insert_head(2), Some((1, &mut 2)));
    assert_eq!(val.insert_head(3), None);
}
Source

pub fn insert_tail(&mut self, data: T) -> Option<(usize, &mut T)>

从头部插入新的数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    assert_eq!(val.insert_tail(1), Some((0, &mut 1)));
    assert_eq!(val.insert_tail(2), Some((1, &mut 2)));
    assert_eq!(val.insert_tail(3), None);
}
Source

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

从头部弹出数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_tail(1);
    val.insert_tail(2);
    assert_eq!(val.pop_head(), Some(1));
}
Source

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

从尾部弹出数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_head(1);
    val.insert_head(2);
    assert_eq!(val.pop_tail(), Some(1));
}
Source

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

删除指定位置数据

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(2);
    val.insert_head(1);
    val.insert_head(2);
    assert_eq!(val.remove(1), Some(2));
    assert_eq!(val.len(), 1);
    assert_eq!(val.tail_idx(), 0);
}
Source

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

迭代器

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(5);
    val.insert_head(1);
    val.insert_head(2);
    val.insert_head(3);
    assert_eq!(val.iter().map(|(_, v)| *v).collect::<Vec<_>>(), vec![3, 2, 1]);
}
Source

pub fn iter_mut(&mut self) -> FixedVecIterMut<'_, T>

迭代器

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(5);
    val.insert_head(1);
    val.insert_head(2);
    val.insert_head(3);
    let _ = val.iter_mut().map(|(_, v)| *v = *v * 2).collect::<Vec<_>>();
    assert_eq!(val.iter().map(|(_, v)| *v).collect::<Vec<_>>(), vec![6, 4, 2]);
}
Source

pub fn resize(&mut self, capacity: usize)

重置设置大小

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(5);
    val.insert_head(1);
    val.insert_head(2);
    val.resize(3);
    assert_eq!(val.len(), 2);
    assert_eq!(val.head_idx(), 0);
    assert_eq!(val.tail_idx(), 1);
    assert_eq!(val.tail(), Some(&1));
}
Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&T) -> bool,

Source

pub fn retain_mut<F>(&mut self, f: F)
where F: FnMut(&mut T) -> bool,

Source

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

将指定位置挪到最前面

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(5);
    val.insert_head(1);
    val.insert_head(2);
    val.insert_head(3);
    assert_eq!(val.get(1), Some(&2));
    assert_eq!(val.head(), Some(&3));
    val.move_head(1);
    assert_eq!(val.head(), Some(&2));
    assert_eq!(val.get(1), Some(&2));
    assert_eq!(val.tail(), Some(&1));
}
Source

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

将指定位置挪到最后面

§Examples
use algorithm::FixedVec;
fn main() {
    let mut val = FixedVec::new(5);
    val.insert_tail(1);
    val.insert_tail(2);
    val.insert_tail(3);
    assert_eq!(val.get(1), Some(&2));
    assert_eq!(val.tail(), Some(&3));
    val.move_tail(1);
    assert_eq!(val.tail(), Some(&2));
    assert_eq!(val.get(1), Some(&2));
    assert_eq!(val.head(), Some(&1));
}

Trait Implementations§

Source§

impl<T: Debug> Debug for FixedVec<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for FixedVec<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for FixedVec<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.