ixa/random/
mod.rs

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