|
|
use std::path::PathBuf; |
|
|
|
|
|
use rand::{SeedableRng, rngs::StdRng, seq::IndexedRandom}; |
|
|
use rustc_hash::FxHashMap; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ModulePicker { |
|
|
depths: Vec<usize>, |
|
|
modules_by_depth: FxHashMap<usize, Vec<PathBuf>>, |
|
|
rng: parking_lot::Mutex<StdRng>, |
|
|
} |
|
|
|
|
|
impl ModulePicker { |
|
|
|
|
|
pub fn new(mut modules: Vec<(PathBuf, usize)>) -> Self { |
|
|
let rng = StdRng::seed_from_u64(42); |
|
|
|
|
|
|
|
|
modules.sort(); |
|
|
|
|
|
let mut modules_by_depth: FxHashMap<_, Vec<_>> = FxHashMap::default(); |
|
|
for (module, depth) in modules { |
|
|
modules_by_depth.entry(depth).or_default().push(module); |
|
|
} |
|
|
let mut depths: Vec<_> = modules_by_depth.keys().copied().collect(); |
|
|
|
|
|
depths.sort(); |
|
|
|
|
|
Self { |
|
|
depths, |
|
|
modules_by_depth, |
|
|
rng: parking_lot::Mutex::new(rng), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn pick(&self) -> &PathBuf { |
|
|
let mut rng = self.rng.lock(); |
|
|
|
|
|
let depth = self.depths.choose(&mut *rng).unwrap(); |
|
|
self.modules_by_depth[depth].choose(&mut *rng).unwrap() |
|
|
} |
|
|
} |
|
|
|