Add API to set the seed for the RNG
This commit is contained in:
parent
4b3cb28820
commit
df8388a8a1
15
src/lib.rs
15
src/lib.rs
|
@ -32,6 +32,11 @@ where
|
||||||
let ef_search = builder.ef_search.unwrap_or(100);
|
let ef_search = builder.ef_search.unwrap_or(100);
|
||||||
let ef_construction = builder.ef_construction.unwrap_or(100);
|
let ef_construction = builder.ef_construction.unwrap_or(100);
|
||||||
let ml = builder.ml.unwrap_or_else(|| (M as f32).ln());
|
let ml = builder.ml.unwrap_or_else(|| (M as f32).ln());
|
||||||
|
let mut rng = match builder.seed {
|
||||||
|
Some(seed) => SmallRng::seed_from_u64(seed),
|
||||||
|
None => SmallRng::from_entropy(),
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg(feature = "indicatif")]
|
#[cfg(feature = "indicatif")]
|
||||||
let progress = builder.progress;
|
let progress = builder.progress;
|
||||||
#[cfg(feature = "indicatif")]
|
#[cfg(feature = "indicatif")]
|
||||||
|
@ -57,7 +62,6 @@ where
|
||||||
// progresses, while preserving randomness in each point's layer and insertion order.
|
// progresses, while preserving randomness in each point's layer and insertion order.
|
||||||
|
|
||||||
assert!(points.len() < u32::MAX as usize);
|
assert!(points.len() < u32::MAX as usize);
|
||||||
let mut rng = SmallRng::from_entropy();
|
|
||||||
let mut nodes = (0..points.len())
|
let mut nodes = (0..points.len())
|
||||||
.map(|i| (LayerId::random(ml, &mut rng), i))
|
.map(|i| (LayerId::random(ml, &mut rng), i))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
@ -402,6 +406,7 @@ pub struct Builder {
|
||||||
ef_search: Option<usize>,
|
ef_search: Option<usize>,
|
||||||
ef_construction: Option<usize>,
|
ef_construction: Option<usize>,
|
||||||
ml: Option<f32>,
|
ml: Option<f32>,
|
||||||
|
seed: Option<u64>,
|
||||||
#[cfg(feature = "indicatif")]
|
#[cfg(feature = "indicatif")]
|
||||||
progress: Option<ProgressBar>,
|
progress: Option<ProgressBar>,
|
||||||
}
|
}
|
||||||
|
@ -433,6 +438,14 @@ impl Builder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the seed value for the random number generator used to generate a layer for each point
|
||||||
|
///
|
||||||
|
/// If this value is left unset, a seed is generated from entropy (via `getrandom()`).
|
||||||
|
pub fn seed(mut self, seed: u64) -> Self {
|
||||||
|
self.seed = Some(seed);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// A `ProgressBar` to track `Hnsw` construction progress
|
/// A `ProgressBar` to track `Hnsw` construction progress
|
||||||
#[cfg(feature = "indicatif")]
|
#[cfg(feature = "indicatif")]
|
||||||
pub fn progress(mut self, bar: ProgressBar) -> Self {
|
pub fn progress(mut self, bar: ProgressBar) -> Self {
|
||||||
|
|
Loading…
Reference in New Issue