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