ixa/random/
mod.rs

1mod context_ext;
2mod sampling_algorithms;
3
4use std::any::{Any, TypeId};
5use std::cell::RefCell;
6
7pub use context_ext::ContextRandomExt;
8pub use sampling_algorithms::{
9    count_and_sample_single_l_reservoir, sample_multiple_from_known_length,
10    sample_multiple_l_reservoir, sample_single_excluding, sample_single_excluding_iteration,
11    sample_single_excluding_l_reservoir, sample_single_excluding_rejection,
12    sample_single_from_known_length, sample_single_l_reservoir,
13};
14
15pub use crate::define_rng;
16use crate::rand::SeedableRng;
17use crate::{define_data_plugin, HashMap, HashMapExt};
18
19pub trait RngId: Copy + Clone {
20    type RngType: SeedableRng;
21    fn get_name() -> &'static str;
22}
23
24// This is a wrapper that allows for future support for different types of
25// random number generators (anything that implements SeedableRng is valid).
26struct RngHolder {
27    rng: Box<dyn Any>,
28}
29
30struct RngData {
31    base_seed: u64,
32    rng_holders: RefCell<HashMap<TypeId, RngHolder>>,
33}
34
35// Registers a data container which stores:
36// * base_seed: A base seed for all rngs
37// * rng_holders: A map of rngs, keyed by their RngId. Note that this is
38//   stored in a RefCell to allow for mutable borrow without requiring a
39//   mutable borrow of the Context itself.
40define_data_plugin!(
41    RngPlugin,
42    RngData,
43    RngData {
44        base_seed: 0,
45        rng_holders: RefCell::new(HashMap::new()),
46    }
47);