| //! Miscellaneous utilities. |
| use std::time::{Duration, Instant}; |
| /// Randomly shuffles a slice. |
| pub(crate) fn shuffle<T>(v: &mut [T]) { |
| static RNG: Cell<Wrapping<u32>> = const { Cell::new(Wrapping(1_406_868_647)) }; |
| let _ = RNG.try_with(|rng| { |
| // This is the 32-bit variant of Xorshift. |
| // Source: https://en.wikipedia.org/wiki/Xorshift |
| // This is a fast alternative to `let j = x % n`. |
| // Source: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ |
| let j = ((x as u64).wrapping_mul(n as u64) >> 32) as u32 as usize; |
| /// Sleeps until the deadline, or forever if the deadline isn't specified. |
| pub(crate) fn sleep_until(deadline: Option<Instant>) { |
| None => thread::sleep(Duration::from_secs(1000)), |
| let now = Instant::now(); |