-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewrite error-based reranker
Signed-off-by: usamoi <usamoi@outlook.com>
- Loading branch information
Showing
4 changed files
with
45 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use base::always_equal::AlwaysEqual; | ||
use base::distance::Distance; | ||
use base::search::RerankerPop; | ||
use std::cmp::Reverse; | ||
use std::collections::BinaryHeap; | ||
|
||
pub struct ErrorFlatReranker<T, R> { | ||
rerank: R, | ||
heap: BinaryHeap<(Reverse<Distance>, AlwaysEqual<u32>)>, | ||
cache: BinaryHeap<(Reverse<Distance>, AlwaysEqual<u32>, AlwaysEqual<T>)>, | ||
} | ||
|
||
impl<T, R> ErrorFlatReranker<T, R> { | ||
pub fn new(heap: Vec<(Reverse<Distance>, AlwaysEqual<u32>)>, rerank: R) -> Self | ||
where | ||
R: Fn(u32) -> (Distance, T), | ||
{ | ||
Self { | ||
rerank, | ||
heap: heap.into(), | ||
cache: BinaryHeap::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<T, R> RerankerPop<T> for ErrorFlatReranker<T, R> | ||
where | ||
R: Fn(u32) -> (Distance, T), | ||
{ | ||
fn pop(&mut self) -> Option<(Distance, u32, T)> { | ||
while !self.heap.is_empty() | ||
&& self.heap.peek().map(|x| x.0) > self.cache.peek().map(|x| x.0) | ||
{ | ||
let (_, AlwaysEqual(u)) = self.heap.pop().unwrap(); | ||
let (dis_u, pay_u) = (self.rerank)(u); | ||
self.cache | ||
.push((Reverse(dis_u), AlwaysEqual(u), AlwaysEqual(pay_u))); | ||
} | ||
let (Reverse(dist), AlwaysEqual(u), AlwaysEqual(pay_u)) = self.cache.pop()?; | ||
Some((dist, u, pay_u)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
pub mod error_based; | ||
pub mod error; | ||
pub mod quantization; | ||
pub mod quantizer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters