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§
Implementations§
Source§impl<K, KS> CachedKeySet<K, KS>
impl<K, KS> CachedKeySet<K, KS>
Source§impl<K, PI: GetIterator> CachedKeySet<K, DynamicKeySet<PI>>
impl<K, PI: GetIterator> CachedKeySet<K, DynamicKeySet<PI>>
Sourcepub fn dynamic(keys: PI, clone_threshold: usize) -> Self
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);Sourcepub fn dynamic_with_len(keys: PI, len: usize, clone_threshold: usize) -> Self
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>>
impl<'k, K: Sync> CachedKeySet<K, SliceSourceWithRefs<'k, K>>
Sourcepub fn slice(keys: &'k [K], clone_threshold: usize) -> Self
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>
impl<K, KS> Default for CachedKeySet<K, KS>
Source§impl<K: Clone + Sync + Send, KS: KeySet<K>> KeySet<K> for CachedKeySet<K, KS>
impl<K: Clone + Sync + Send, KS: KeySet<K>> KeySet<K> for CachedKeySet<K, KS>
Source§fn has_par_for_each_key(&self) -> bool
fn has_par_for_each_key(&self) -> bool
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)
fn for_each_key<F, P>(&self, f: F, retained_hint: P)
f for each key in the set, using single thread. Read moreSource§fn par_for_each_key<F, P>(&self, f: F, retained_hint: P)
fn par_for_each_key<F, P>(&self, f: F, retained_hint: P)
for_each_key.Source§fn map_each_key<R, M, P>(&self, map: M, retained_hint: P) -> Vec<R>
fn map_each_key<R, M, P>(&self, map: M, retained_hint: P) -> Vec<R>
map for each key in the set, and returns outputs of these calls. Uses single thread. Read moreSource§fn par_map_each_key<R, M, P>(&self, map: M, retained_hint: P) -> Vec<R>
fn par_map_each_key<R, M, P>(&self, map: M, retained_hint: P) -> Vec<R>
map_each_key.Source§fn retain_keys<F, P, R>(
&mut self,
filter: F,
retained_earlier: P,
remove_count: R,
)
fn retain_keys<F, P, R>( &mut self, filter: F, retained_earlier: P, remove_count: R, )
Source§fn par_retain_keys<F, P, R>(
&mut self,
filter: F,
retained_earlier: P,
remove_count: R,
)
fn par_retain_keys<F, P, R>( &mut self, filter: F, retained_earlier: P, remove_count: R, )
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,
)
fn retain_keys_with_indices<IF, F, P, R>( &mut self, index_filter: IF, filter: F, retained_earlier: P, remove_count: R, )
self keys pointed by the index_filter
(or filter if self does not support index_filter)
and remove the rest.
Uses single thread. Read moreSource§fn par_retain_keys_with_indices<IF, F, P, R>(
&mut self,
index_filter: IF,
filter: F,
retained_earlier: P,
remove_count: R,
)
fn par_retain_keys_with_indices<IF, F, P, R>( &mut self, index_filter: IF, filter: F, retained_earlier: P, remove_count: R, )
retain_keys_with_indices.Source§fn into_vec<P>(self, retained_hint: P) -> Vec<K>
fn into_vec<P>(self, retained_hint: P) -> Vec<K>
self into the vector of retained keys. Read moreSource§fn par_into_vec<P>(self, retained_hint: P) -> Vec<K>
fn par_into_vec<P>(self, retained_hint: P) -> Vec<K>
into_vec.Source§fn maybe_par_map_each_key<R, M, P>(
&self,
map: M,
retained_hint: P,
use_mt: bool,
) -> Vec<R>
fn maybe_par_map_each_key<R, M, P>( &self, map: M, retained_hint: P, use_mt: bool, ) -> Vec<R>
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,
)
fn maybe_par_retain_keys<F, P, R>( &mut self, filter: F, retained_earlier: P, remove_count: R, use_mt: bool, )
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,
)
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, )
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>where
KS: RefUnwindSafe,
K: RefUnwindSafe,
impl<K, KS> Send for CachedKeySet<K, KS>
impl<K, KS> Sync for CachedKeySet<K, KS>
impl<K, KS> Unpin for CachedKeySet<K, KS>
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> 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
Source§impl<T, U> CastableInto<U> for Twhere
U: CastableFrom<T>,
impl<T, U> CastableInto<U> for Twhere
U: CastableFrom<T>,
Source§impl<T> DowncastableFrom<T> for T
impl<T> DowncastableFrom<T> for T
Source§fn downcast_from(value: T) -> T
fn downcast_from(value: T) -> T
Source§impl<T, U> DowncastableInto<U> for Twhere
U: DowncastableFrom<T>,
impl<T, U> DowncastableInto<U> for Twhere
U: DowncastableFrom<T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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