Skip to main content

faiss_next/
parameter.rs

1use std::ffi::CString;
2use std::ptr;
3
4use faiss_next_sys::{self, FaissParameterSpace};
5
6use crate::error::{check_return_code, Result};
7use crate::index::Index;
8
9pub struct ParameterSpace {
10    ptr: *mut FaissParameterSpace,
11}
12
13impl ParameterSpace {
14    pub fn new() -> Result<Self> {
15        unsafe {
16            let mut ptr: *mut FaissParameterSpace = ptr::null_mut();
17            check_return_code(faiss_next_sys::faiss_ParameterSpace_new(&mut ptr))?;
18            Ok(Self { ptr })
19        }
20    }
21
22    pub fn n_combinations(&self) -> usize {
23        unsafe { faiss_next_sys::faiss_ParameterSpace_n_combinations(self.ptr) }
24    }
25
26    pub fn combination_name(&self, cno: usize) -> Result<String> {
27        let mut name_buf = vec![0i8; 256];
28        check_return_code(unsafe {
29            faiss_next_sys::faiss_ParameterSpace_combination_name(
30                self.ptr,
31                cno,
32                name_buf.as_mut_ptr(),
33                name_buf.len(),
34            )
35        })?;
36        let name = unsafe {
37            std::ffi::CStr::from_ptr(name_buf.as_ptr())
38                .to_string_lossy()
39                .into_owned()
40        };
41        Ok(name)
42    }
43
44    pub fn set_index_parameters<I: Index>(
45        &self,
46        index: &mut I,
47        parameter_string: &str,
48    ) -> Result<()> {
49        let c_param = CString::new(parameter_string)?;
50        check_return_code(unsafe {
51            faiss_next_sys::faiss_ParameterSpace_set_index_parameters(
52                self.ptr,
53                index.inner_ptr(),
54                c_param.as_ptr(),
55            )
56        })
57    }
58
59    pub fn set_index_parameters_cno<I: Index>(&self, index: &mut I, cno: usize) -> Result<()> {
60        check_return_code(unsafe {
61            faiss_next_sys::faiss_ParameterSpace_set_index_parameters_cno(
62                self.ptr,
63                index.inner_ptr(),
64                cno,
65            )
66        })
67    }
68
69    pub fn set_index_parameter<I: Index>(
70        &self,
71        index: &mut I,
72        name: &str,
73        value: f64,
74    ) -> Result<()> {
75        let c_name = CString::new(name)?;
76        check_return_code(unsafe {
77            faiss_next_sys::faiss_ParameterSpace_set_index_parameter(
78                self.ptr,
79                index.inner_ptr(),
80                c_name.as_ptr(),
81                value,
82            )
83        })
84    }
85
86    pub fn display(&self) {
87        unsafe { faiss_next_sys::faiss_ParameterSpace_display(self.ptr) }
88    }
89
90    pub fn add_range(&mut self, name: &str) -> Result<()> {
91        let c_name = CString::new(name)?;
92        unsafe {
93            let mut range_ptr: *mut faiss_next_sys::FaissParameterRange = ptr::null_mut();
94            check_return_code(faiss_next_sys::faiss_ParameterSpace_add_range(
95                self.ptr,
96                c_name.as_ptr(),
97                &mut range_ptr,
98            ))
99        }
100    }
101}
102
103impl Default for ParameterSpace {
104    fn default() -> Self {
105        Self::new().expect("Failed to create ParameterSpace")
106    }
107}
108
109impl Drop for ParameterSpace {
110    fn drop(&mut self) {
111        if !self.ptr.is_null() {
112            unsafe {
113                faiss_next_sys::faiss_ParameterSpace_free(self.ptr);
114            }
115        }
116    }
117}