Struct algorithm::BitMap

source ·
pub struct BitMap { /* private fields */ }
Expand description

位图类,根据访问的位看是否被占用 解决经典的是否被占用的问题,但是相对占用大小会较大

§Examples

use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_many(&vec![1, 2, 3, 4, 10]);
    assert!(map.contains(&1));
    assert!(!map.contains(&5));
    assert!(map.contains(&10));
    map.add_range(7, 16);
    assert!(!map.contains(&6));
    assert!(map.contains(&7));
    assert!(map.contains(&16));
    assert!(!map.contains(&17));
}

Implementations§

source§

impl BitMap

source

pub fn new(cap: usize) -> Self

source

pub fn capacity(&self) -> usize

source

pub fn len(&self) -> usize

source

pub fn is_empty(&self) -> bool

source

pub fn clear(&mut self)

source

pub fn add(&mut self, val: usize) -> bool

添加新的元素

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add(1);
    assert!(map.contains(&1));
    assert!(map.len() == 1);
}
source

pub fn add_many(&mut self, val: &[usize])

添加许多新的元素

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_many(&vec![1, 2, 3, 4, 10]);
    assert!(map.contains(&1));
    assert!(map.contains(&10));
    assert!(map.len() == 5);
}
source

pub fn add_range(&mut self, start: usize, end: usize)

添加范围内的元素(包含头与结果),批量添加增加效率

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_range(7, 16);
    assert!(!map.contains(&6));
    assert!(map.contains(&7));
    assert!(map.contains(&16));
    assert!(!map.contains(&17));
    assert!(map.len() == 10);
}
source

pub fn remove(&mut self, val: usize) -> bool

删除元素

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_range(7, 16);
    assert!(map.len() == 10);
    assert!(map.contains(&7));
    assert!(map.remove(7));
    assert!(!map.contains(&7));
    assert!(map.len() == 9);
}
source

pub fn remove_many(&mut self, val: &[usize])

删除列表中元素

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_range(7, 16);
    assert!(map.len() == 10);
    assert!(map.contains(&7));
    assert!(map.remove(7));
    assert!(!map.contains(&7));
    assert!(map.len() == 9);
}
source

pub fn remove_range(&mut self, start: usize, end: usize)

删除范围元素(包含头与尾)

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_range(7, 16);
    assert!(map.len() == 10);
    map.remove_range(7, 15);
    assert!(map.len() == 1);
    assert!(map.contains(&16));
}
source

pub fn contains(&self, val: &usize) -> bool

醒看是否包含

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add(7);
    assert!(map.contains(&7));
}
source

pub fn iter(&self) -> Iter<'_>

迭代器,通过遍历进行循环,如果位图的容量非常大,可能效率相当低

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add(7);
    map.add_range(9, 12);
    map.add_many(&vec![20, 100, 300]);
    assert!(map.iter().collect::<Vec<_>>() == vec![7, 9, 10, 11, 12, 20, 100, 300]);
}
source

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

是否保留,通过遍历进行循环,如果位图的容量非常大,可能效率相当低

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_range(9, 16);
    map.retain(|v| v % 2 == 0);
    assert!(map.iter().collect::<Vec<_>>() == vec![10, 12, 14, 16]);
}
source

pub fn contains_sub(&self, other: &BitMap) -> bool

是否为子位图

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_range(9, 16);
    let mut sub_map = BitMap::new(10240);
    sub_map.add_range(9, 12);
    assert!(map.contains_sub(&sub_map));
}
source

pub fn intersect(&self, other: &BitMap) -> BitMap

取两个位图间的交集

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_range(9, 16);
    let mut sub_map = BitMap::new(10240);
    sub_map.add_range(7, 12);
    let map = map.intersect(&sub_map);
    assert!(map.iter().collect::<Vec<_>>() == vec![9, 10, 11, 12]);
}
source

pub fn union(&self, other: &BitMap) -> BitMap

取两个位图间的并集

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_range(9, 16);
    let mut sub_map = BitMap::new(10240);
    sub_map.add_range(7, 12);
    let map = map.union(&sub_map);
    assert!(map.iter().collect::<Vec<_>>() == vec![7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
}
source

pub fn union_xor(&self, other: &BitMap) -> BitMap

取两个位图间的异或并集

§Examples
use algorithm::BitMap;
fn main() {
    let mut map = BitMap::new(10240);
    map.add_range(9, 16);
    let mut sub_map = BitMap::new(10240);
    sub_map.add_range(7, 12);
    let map = map.union_xor(&sub_map);
    assert_eq!(map.iter().collect::<Vec<_>>(), vec![7, 8, 13, 14, 15, 16]);
}

Trait Implementations§

source§

impl BitAnd for &BitMap

source§

type Output = BitMap

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
source§

impl BitOr for &BitMap

source§

type Output = BitMap

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
source§

impl BitXor for &BitMap

source§

type Output = BitMap

The resulting type after applying the ^ operator.
source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
source§

impl Clone for BitMap

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for BitMap

source§

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

Formats the value using the given formatter. Read more
source§

impl Display for BitMap

source§

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

Formats the value using the given formatter. Read more
source§

impl Extend<usize> for BitMap

source§

fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl FromIterator<usize> for BitMap

source§

fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> BitMap

Creates a value from an iterator. Read more
source§

impl PartialEq for BitMap

source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for BitMap

Auto Trait Implementations§

§

impl Freeze for BitMap

§

impl RefUnwindSafe for BitMap

§

impl Send for BitMap

§

impl Sync for BitMap

§

impl Unpin for BitMap

§

impl UnwindSafe for BitMap

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.