Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexImpl to IdMapIndex #61

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/index/id_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ use std::mem;
use std::os::raw::c_int;
use std::ptr;

use super::IndexImpl;

/// Wrapper for implementing arbitrary ID mapping to an index.
///
/// See the [module level documentation] for more information.
Expand Down Expand Up @@ -364,6 +366,21 @@ where
}
}

impl IndexImpl {
pub fn into_id_map(self) -> Result<IdMap<IndexImpl>> {
ava57r marked this conversation as resolved.
Show resolved Hide resolved
unsafe {
let new_inner = faiss_IndexIDMap_cast(self.inner_ptr());
if new_inner.is_null() {
Err(Error::BadCast)
} else {
mem::forget(self);
let index_inner = faiss_IndexIDMap_sub_index(new_inner);
Ok(IdMap { inner: new_inner, index_inner, phantom: PhantomData })
}
}
}
}

#[cfg(test)]
mod tests {
use super::IdMap;
Expand Down Expand Up @@ -471,4 +488,12 @@ mod tests {
let flat_index: FlatIndexImpl = id_index.try_into_inner().unwrap();
assert_eq!(flat_index.d(), 4);
}

#[test]
fn index_impl_to_id_map() {
let index = index_factory(4, "IDMap,Flat", MetricType::L2).unwrap();
let id_map = index.into_id_map().unwrap();

assert_eq!(id_map.d(), 4);
}
}