Skip to main content

CachedKeySet

Enum CachedKeySet 

Source
pub enum CachedKeySet<K, KS> {
    Dynamic(KS, usize),
    Cached(Vec<K>),
}
Expand description

Implementation of KeySet that initially stores another KeySet (which is usually succinct but slow, such as DynamicKeySet), but when number of keys drops below given threshold, the remaining keys are cached (cloned into the vector), and later only the cache is used.

Variants§

§

Dynamic(KS, usize)

§

Cached(Vec<K>)

Implementations§

Source§

impl<K, KS> CachedKeySet<K, KS>

Source

pub fn new(key_set: KS, clone_threshold: usize) -> Self

Constructs cached key_set. The keys are cloned and cached as soon as their number drops below clone_threshold.

Source§

impl<K, PI: GetIterator> CachedKeySet<K, DynamicKeySet<PI>>

Source

pub fn dynamic(keys: PI, clone_threshold: usize) -> Self

Constructs cached DynamicKeySet that obtains the keys by keys that returns iterator over keys. The keys are cloned and cached as soon as their number drops below clone_threshold.

If the number of keys is known, it is more efficient to call CachedKeySet::dynamic_with_len instead.

§Example
use {ph::fmph::keyset::{KeySet, CachedKeySet}, rayon::iter::{ParallelIterator, IntoParallelIterator}};
// Constructing a dynamic key set consisting of the squares of the integers from 1 to 100,
// part of which will be cached by the first call of any of the retain methods:
let ks = CachedKeySet::dynamic(|| (1..=100).map(|v| v*v), usize::MAX);
assert_eq!(ks.keys_len(), 100);
// Same as above but using multiple threads to generate keys:
let ks = CachedKeySet::dynamic((|| (1..=100).map(|v| v*v), || (1..=100).into_par_iter().map(|v| v*v)), usize::MAX);
assert_eq!(ks.keys_len(), 100);
Source

pub fn dynamic_with_len(keys: PI, len: usize, clone_threshold: usize) -> Self

Constructs cached DynamicKeySet that obtains the keys by keys that returns iterator over exactly len keys. The keys are cloned and cached as soon as their number drops below clone_threshold.

§Example
use {ph::fmph::keyset::{KeySet, CachedKeySet}, rayon::iter::{ParallelIterator, IntoParallelIterator}};
// Constructing a dynamic key set consisting of the squares of the integers from 1 to 100,
// part of which will be cached by the first call of any of the retain methods:
let ks = CachedKeySet::dynamic_with_len(|| (1..=100).map(|v| v*v), 100, usize::MAX);
assert_eq!(ks.keys_len(), 100);
// Same as above but using multiple threads to generate keys:
let ks = CachedKeySet::dynamic_with_len((|| (1..=100).map(|v| v*v), || (1..=100).into_par_iter().map(|v| v*v)), 100, usize::MAX);
assert_eq!(ks.keys_len(), 100);
Source§

impl<'k, K: Sync> CachedKeySet<K, SliceSourceWithRefs<'k, K>>

Source

pub fn slice(keys: &'k [K], clone_threshold: usize) -> Self

Constructs cached SliceSourceWithRefs that wraps given keys. The keys are cloned and cached as soon as their number drops below clone_threshold. After cloning, the keys are placed in a continuous memory area which is friendly to the CPU cache.

Trait Implementations§

Source§

impl<K, KS> Default for CachedKeySet<K, KS>

Source§

fn default() -> Self

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

impl<K: Clone + Sync + Send, KS: KeySet<K>> KeySet<K> for CachedKeySet<K, KS>

Source§

fn keys_len(&self) -> usize

Returns number of retained keys. Guarantee to be very fast.
Source§

fn has_par_for_each_key(&self) -> bool

Returns true only if Self::par_for_each_key can use multiple threads.
Source§

fn for_each_key<F, P>(&self, f: F, retained_hint: P)
where F: FnMut(&K), P: FnMut(&K) -> bool,

Call f for each key in the set, using single thread. Read more
Source§

fn par_for_each_key<F, P>(&self, f: F, retained_hint: P)
where F: Fn(&K) + Sync + Send, P: Fn(&K) -> bool + Sync + Send,

Multi-threaded version of for_each_key.
Source§

fn map_each_key<R, M, P>(&self, map: M, retained_hint: P) -> Vec<R>
where M: FnMut(&K) -> R, P: FnMut(&K) -> bool,

Calls map for each key in the set, and returns outputs of these calls. Uses single thread. Read more
Source§

fn par_map_each_key<R, M, P>(&self, map: M, retained_hint: P) -> Vec<R>
where M: Fn(&K) -> R + Sync + Send, R: Send, P: Fn(&K) -> bool + Sync + Send,

Multi-threaded version of map_each_key.
Source§

fn retain_keys<F, P, R>( &mut self, filter: F, retained_earlier: P, remove_count: R, )
where F: FnMut(&K) -> bool, P: FnMut(&K) -> bool, R: FnMut() -> usize,

Retains in self keys pointed by the filter and remove the rest, using single thread. Read more
Source§

fn par_retain_keys<F, P, R>( &mut self, filter: F, retained_earlier: P, remove_count: R, )
where F: Fn(&K) -> bool + Sync + Send, P: Fn(&K) -> bool + Sync + Send, R: Fn() -> usize,

Multi-threaded version of retain_keys.
Source§

fn retain_keys_with_indices<IF, F, P, R>( &mut self, index_filter: IF, filter: F, retained_earlier: P, remove_count: R, )
where IF: FnMut(usize) -> bool, F: FnMut(&K) -> bool, P: FnMut(&K) -> bool, R: FnMut() -> usize,

Retains in self keys pointed by the index_filter (or filter if self does not support index_filter) and remove the rest. Uses single thread. Read more
Source§

fn par_retain_keys_with_indices<IF, F, P, R>( &mut self, index_filter: IF, filter: F, retained_earlier: P, remove_count: R, )
where IF: Fn(usize) -> bool + Sync + Send, F: Fn(&K) -> bool + Sync + Send, P: Fn(&K) -> bool + Sync + Send, R: Fn() -> usize,

Multi-threaded version of retain_keys_with_indices.
Source§

fn into_vec<P>(self, retained_hint: P) -> Vec<K>
where P: FnMut(&K) -> bool, K: Clone, Self: Sized,

Convert self into the vector of retained keys. Read more
Source§

fn par_into_vec<P>(self, retained_hint: P) -> Vec<K>
where P: Fn(&K) -> bool + Sync + Send, Self: Sized, K: Clone + Send,

Multi-threaded version of into_vec.
Source§

fn maybe_par_map_each_key<R, M, P>( &self, map: M, retained_hint: P, use_mt: bool, ) -> Vec<R>
where M: Fn(&K) -> R + Sync + Send, R: Send, P: Fn(&K) -> bool + Sync + Send,

Calls either map_each_key (if use_mt is false) or par_map_each_key (if use_mt is true).
Source§

fn maybe_par_retain_keys<F, P, R>( &mut self, filter: F, retained_earlier: P, remove_count: R, use_mt: bool, )
where F: Fn(&K) -> bool + Sync + Send, P: Fn(&K) -> bool + Sync + Send, R: Fn() -> usize,

Calls either retain_keys (if use_mt is false) or par_retain_keys (if use_mt is true).
Source§

fn maybe_par_retain_keys_with_indices<IF, F, P, R>( &mut self, index_filter: IF, filter: F, retained_earlier: P, remove_count: R, use_mt: bool, )
where IF: Fn(usize) -> bool + Sync + Send, F: Fn(&K) -> bool + Sync + Send, P: Fn(&K) -> bool + Sync + Send, R: Fn() -> usize,

Calls either retain_keys_with_indices (if use_mt is false) or par_retain_keys_with_indices (if use_mt is true).

Auto Trait Implementations§

§

impl<K, KS> Freeze for CachedKeySet<K, KS>
where KS: Freeze,

§

impl<K, KS> RefUnwindSafe for CachedKeySet<K, KS>

§

impl<K, KS> Send for CachedKeySet<K, KS>
where KS: Send, K: Send,

§

impl<K, KS> Sync for CachedKeySet<K, KS>
where KS: Sync, K: Sync,

§

impl<K, KS> Unpin for CachedKeySet<K, KS>
where KS: Unpin, K: Unpin,

§

impl<K, KS> UnsafeUnpin for CachedKeySet<K, KS>
where KS: UnsafeUnpin,

§

impl<K, KS> UnwindSafe for CachedKeySet<K, KS>
where KS: UnwindSafe, K: 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> CastableFrom<T> for T

Source§

fn cast_from(value: T) -> T

Call Self as W
Source§

impl<T, U> CastableInto<U> for T
where U: CastableFrom<T>,

Source§

fn cast(self) -> U

Call W::cast_from(self)
Source§

impl<T> DowncastableFrom<T> for T

Source§

fn downcast_from(value: T) -> T

Truncate the current UnsignedInt to a possibly smaller size
Source§

impl<T, U> DowncastableInto<U> for T
where U: DowncastableFrom<T>,

Source§

fn downcast(self) -> U

Call W::downcast_from(self)
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Splat<T> for T

Source§

fn splat(value: T) -> T

Source§

impl<T> To<T> for T

Source§

fn to(self) -> T

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.
Source§

impl<T> UpcastableFrom<T> for T

Source§

fn upcast_from(value: T) -> T

Extend the current UnsignedInt to a possibly bigger size.
Source§

impl<T, U> UpcastableInto<U> for T
where U: UpcastableFrom<T>,

Source§

fn upcast(self) -> U

Call W::upcast_from(self)
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V