Debian Codegraph @

src/default.rs

raw symbols SCIP
1 use std::cell::RefCell;
2 use std::rc::Rc;
3
4 use {Source, Xorshift128Plus};
5
6 /// An instance of the default source.
7 ///
8 /// The default source is the Xorshift128+ algorithm.
9 #[derive(Clone)]
10 pub struct Default(Rc<RefCell<Xorshift128Plus>>);
11
12 impl Default {
13 /// Create an instance of the default source.
14 ///
15 /// Each thread has its own copy of the default source, and each copy is
16 /// initialized with the same default seed. Therefore, the usage is thread
17 /// safe; however, each thread is responsible for reseeding its source.
18 pub fn new() -> Default {
19 thread_local!(static DEFAULT: Rc<RefCell<Xorshift128Plus>> = {
20 Rc::new(RefCell::new(Xorshift128Plus::new([42, 69])))
21 });
22 Default(DEFAULT.with(|source| source.clone()))
23 }
24
25 /// Seed the source.
26 ///
27 /// At least one bit of the seed should be nonzero.
28 #[inline(always)]
29 pub fn seed(self, seed: [u64; 2]) -> Default {
30 *self.0.borrow_mut() = Xorshift128Plus::new(seed);
31 self
32 }
33 }
34
35 impl Source for Default {
36 #[inline(always)]
37 fn read_u64(&mut self) -> u64 {
38 self.0.borrow_mut().read_u64()
39 }
40 }