From b7de137d9fc83278c128f2d805c598947c76d54e Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 15 Dec 2020 11:40:12 +0100 Subject: [PATCH] Add builder API to facilitate modular construction --- src/lib.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) 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;