immutable_chunkmap::set

Struct Set

source
pub struct Set<K: Ord + Clone, const SIZE: usize>(/* private fields */);
Expand description

This set uses a similar strategy to BTreeSet to ensure cache efficient performance on modern hardware while still providing log(N) get, insert, and remove operations.

§Examples

use alloc::string::String;
use self::immutable_chunkmap::set::SetM;

let m =
   SetM::new()
   .insert(String::from("1")).0
   .insert(String::from("2")).0
   .insert(String::from("3")).0;

assert_eq!(m.contains("1"), true);
assert_eq!(m.contains("2"), true);
assert_eq!(m.contains("3"), true);
assert_eq!(m.contains("4"), false);

for k in &m { println!("{}", k) }

Implementations§

source§

impl<K, const SIZE: usize> Set<K, SIZE>
where K: Ord + Clone,

source

pub fn new() -> Self

Create a new empty set

source

pub fn downgrade(&self) -> WeakSetRef<K, SIZE>

Create a weak reference to this set

source

pub fn strong_count(&self) -> usize

Return the number of strong references to this set (see Arc)

source

pub fn weak_count(&self) -> usize

Return the number of weak references to this set (see Arc)

source

pub fn insert_many<E: IntoIterator<Item = K>>(&self, elts: E) -> Self

This will insert many elements at once, and is potentially a lot faster than inserting one by one, especially if the data is sorted.

#Examples

 use self::immutable_chunkmap::set::SetM;

 let mut v = vec![1, 10, -12, 44, 50];
 v.sort_unstable();

 let m = SetM::new().insert_many(v.iter().map(|k| *k));

 for k in &v {
   assert_eq!(m.contains(k), true)
 }
source

pub fn remove_many<Q, E>(&self, elts: E) -> Self
where Q: Ord, K: Borrow<Q>, E: IntoIterator<Item = Q>,

Remove multiple elements in a single pass. Similar performance to insert_many.

source

pub fn update_many<Q, E, F>(&self, elts: E, f: F) -> Self
where Q: Ord, K: Borrow<Q>, E: IntoIterator<Item = Q>, F: FnMut(Q, Option<&K>) -> Option<K>,

This is just slightly wierd, however if you have a bunch of borrowed forms of members of the set, and you want to look at the real entries and possibly add/update/remove them, then this method is for you.

source

pub fn insert(&self, k: K) -> (Self, bool)

return a new set with k inserted into it. If k already exists in the old set return true, else false. If the element already exists in the set memory will not be allocated.

source

pub fn insert_cow(&mut self, k: K) -> bool

insert k with copy on write semantics. if self is a unique reference to the set, then k will be inserted in place. Otherwise, only the parts of the set necessary to insert k will be copied, and then the copies will be mutated. self will share all the parts that weren’t modfied with any previous clones.

source

pub fn contains<'a, Q>(&'a self, k: &Q) -> bool
where Q: ?Sized + Ord, K: Borrow<Q>,

return true if the set contains k, else false. Runs in log(N) time and constant space. where N is the size of the set.

source

pub fn get<'a, Q>(&'a self, k: &Q) -> Option<&K>
where Q: ?Sized + Ord, K: Borrow<Q>,

return a reference to the item in the set that is equal to the given value, or None if no such value exists.

source

pub fn remove<Q: Sized + Ord>(&self, k: &Q) -> (Self, bool)
where K: Borrow<Q>,

return a new set with k removed. Runs in log(N) time and log(N) space, where N is the size of the set

source

pub fn remove_cow<Q: Sized + Ord>(&mut self, k: &Q) -> bool
where K: Borrow<Q>,

remove k from the set in place with copy on write semantics (see insert_cow). return true if k was in the set.

source

pub fn union(&self, other: &Set<K, SIZE>) -> Self

return the union of 2 sets. Runs in O(log(N) + M) time and space, where N is the largest of the two sets, and M is the number of chunks that intersect, which is roughly proportional to the size of the intersection.

§Examples
use core::iter::FromIterator;
use self::immutable_chunkmap::set::SetM;

let s0 = SetM::from_iter(0..10);
let s1 = SetM::from_iter(5..15);
let s2 = s0.union(&s1);
for i in 0..15 {
    assert!(s2.contains(&i));
}
source

pub fn intersect(&self, other: &Set<K, SIZE>) -> Self

return the intersection of 2 sets. Runs in O(log(N) + M) time and space, where N is the smallest of the two sets, and M is the number of intersecting chunks.

§Examples

use core::iter::FromIterator; use self::immutable_chunkmap::set::SetM;

let s0 = SetM::from_iter(0..100); let s1 = SetM::from_iter(20..50); let s2 = s0.intersect(&s1);

assert!(s2.len() == 30); for i in 0..100 { if i < 20 || i >= 50 { assert!(!s2.contains(&i)); } else { assert!(s2.contains(&i)); } }

source

pub fn diff(&self, other: &Set<K, SIZE>) -> Self
where K: Debug,

Return the difference of two sets. Runs in O(log(N) + M) time and space, where N is the smallest of the two sets, and M is the number of intersecting chunks.

§Examples
use core::iter::FromIterator;
use self::immutable_chunkmap::set::SetM;

let s0 = SetM::from_iter(0..100);
let s1 = SetM::from_iter(0..50);
let s2 = s0.diff(&s1);

assert!(s2.len() == 50);
for i in 0..50 {
    assert!(!s2.contains(&i));
}
for i in 50..100 {
    assert!(s2.contains(&i));
}
source

pub fn len(&self) -> usize

get the number of elements in the map O(1) time and space

source

pub fn range<'a, Q, R>(&'a self, r: R) -> SetIter<'a, R, Q, K, SIZE>
where Q: Ord + ?Sized + 'a, K: 'a + Clone + Ord + Borrow<Q>, R: RangeBounds<Q> + 'a,

return an iterator over the subset of elements in the set that are within the specified range.

The returned iterator runs in O(log(N) + M) time, and constant space. N is the number of elements in the tree, and M is the number of elements you examine.

if lbound >= ubound the returned iterator will be empty

Trait Implementations§

source§

impl<K: Clone + Ord + Clone, const SIZE: usize> Clone for Set<K, SIZE>

source§

fn clone(&self) -> Set<K, SIZE>

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<K, const SIZE: usize> Debug for Set<K, SIZE>
where K: Debug + Ord + Clone,

source§

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

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

impl<K, const SIZE: usize> Default for Set<K, SIZE>
where K: Ord + Clone,

source§

fn default() -> Set<K, SIZE>

Returns the “default value” for a type. Read more
source§

impl<K, const SIZE: usize> FromIterator<K> for Set<K, SIZE>
where K: Ord + Clone,

source§

fn from_iter<T: IntoIterator<Item = K>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<K, const SIZE: usize> Hash for Set<K, SIZE>
where K: Hash + Ord + Clone,

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a, K, const SIZE: usize> IntoIterator for &'a Set<K, SIZE>
where K: 'a + Ord + Clone,

source§

type Item = &'a K

The type of the elements being iterated over.
source§

type IntoIter = SetIter<'a, RangeFull, K, K, SIZE>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, const SIZE: usize> Ord for Set<K, SIZE>
where K: Ord + Clone,

source§

fn cmp(&self, other: &Set<K, SIZE>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
source§

impl<K, const SIZE: usize> PartialEq for Set<K, SIZE>
where K: Ord + Clone,

source§

fn eq(&self, other: &Set<K, SIZE>) -> 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<K, const SIZE: usize> PartialOrd for Set<K, SIZE>
where K: Ord + Clone,

source§

fn partial_cmp(&self, other: &Set<K, SIZE>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<K, const SIZE: usize> Eq for Set<K, SIZE>
where K: Eq + Ord + Clone,

Auto Trait Implementations§

§

impl<K, const SIZE: usize> Freeze for Set<K, SIZE>

§

impl<K, const SIZE: usize> RefUnwindSafe for Set<K, SIZE>
where K: RefUnwindSafe,

§

impl<K, const SIZE: usize> Send for Set<K, SIZE>
where K: Sync + Send,

§

impl<K, const SIZE: usize> Sync for Set<K, SIZE>
where K: Sync + Send,

§

impl<K, const SIZE: usize> Unpin for Set<K, SIZE>

§

impl<K, const SIZE: usize> UnwindSafe for Set<K, SIZE>
where K: RefUnwindSafe,

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<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, 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.