Last active
April 11, 2018 23:18
-
-
Save Enet4/a5ef2fc181c61a5813dea4d2fd5ee45c to your computer and use it in GitHub Desktop.
index.rs in faiss-rs
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
#[derive(Debug)] | |
pub struct IndexImpl { | |
inner: *mut FaissIndex, | |
} | |
unsafe impl Send for IndexImpl {} | |
unsafe impl Sync for IndexImpl {} | |
impl Drop for IndexImpl { | |
fn drop(&mut self) { | |
unsafe { | |
faiss_Index_free(self.inner); | |
} | |
} | |
} | |
impl Index for IndexImpl { | |
fn is_trained(&self) -> bool { | |
unsafe { faiss_Index_is_trained(self.inner) != 0 } | |
} | |
fn add(&mut self, x: &[f32]) -> Result<()> { | |
unsafe { | |
let n = x.len() / self.d() as usize; | |
faiss_try!(faiss_Index_add(self.inner, n as i64, x.as_ptr())); | |
Ok(()) | |
} | |
} | |
fn search(&mut self, query: &[f32], k: usize) -> Result<SearchResult> { | |
unsafe { | |
let nq = query.len() / self.d() as usize; | |
let mut distances = vec![0_f32; k * nq]; | |
let mut labels = vec![0 as Idx; k * nq]; | |
faiss_try!(faiss_Index_search( | |
self.inner, | |
nq as idx_t, | |
query.as_ptr(), | |
k as idx_t, | |
distances.as_mut_ptr(), | |
labels.as_mut_ptr() | |
)); | |
Ok(SearchResult { distances, labels }) | |
} | |
} | |
// ... and many more | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment