diff --git a/src/lib.rs b/src/lib.rs index 86f9af7..5576d70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,7 +21,18 @@ impl

Hnsw

where P: Point, { - pub fn new(points: &[P], ef_construction: usize, ef_search: usize) -> (Self, Vec) { + pub fn builder() -> Builder { + Builder::default() + } + + fn new(points: &[P], builder: Builder) -> (Self, Vec) { + let Builder { + ef_search, + ef_construction, + } = builder; + let ef_search = ef_search.unwrap_or(100); + let ef_construction = ef_construction.unwrap_or(100); + if points.is_empty() { return ( Self { @@ -247,6 +258,31 @@ impl Default for Search { } } +#[derive(Default)] +pub struct Builder { + ef_search: Option, + ef_construction: Option, +} + +impl Builder { + pub fn ef_construction(mut self, ef_construction: usize) -> Self { + self.ef_construction = Some(ef_construction); + self + } + + pub fn ef(mut self, ef: usize) -> Self { + self.ef_search = Some(ef); + if self.ef_construction.is_none() { + self.ef_construction = Some(ef); + } + self + } + + pub fn build(self, points: &[P]) -> (Hnsw

, Vec) { + Hnsw::new(points, self) + } +} + impl

Index for Hnsw

{ type Output = P;