Skip to content

Commit

Permalink
Fixed Hamming Distance by allowing b1x8 vectors to have differing dim…
Browse files Browse the repository at this point in the history
…ensions
  • Loading branch information
Julius Brummack committed May 19, 2024
1 parent 6d9ca21 commit 022508a
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,15 @@ pub enum MetricFunction {
F64Metric(std::boxed::Box<dyn Fn(*const f64, *const f64) -> Distance + Send + Sync>),
}

impl MetricFunction {
fn is_variable_length(&self) -> bool {
match self {
B1X8Metric => true,
_ => false,
}
}
}

/// Approximate Nearest Neighbors search index for dense vectors.
///
/// The `Index` struct provides an abstraction over a dense vector space, allowing
Expand Down Expand Up @@ -547,6 +556,9 @@ impl Clone for ffi::IndexOptions {
/// in an index. It supports generic operations on vectors of different types,
/// allowing for the addition, retrieval, and search of vectors within an index.
pub trait VectorType {
fn allow_variable_length() -> bool {
false
}
/// Adds a vector to the index under the specified key.
///
/// # Parameters
Expand Down Expand Up @@ -906,6 +918,9 @@ impl VectorType for f16 {
}

impl VectorType for b1x8 {
fn allow_variable_length() -> bool {
true
}
fn search(index: &Index, query: &[Self], count: usize) -> Result<ffi::Matches, cxx::Exception> {
index.inner.search_b1x8(b1x8::to_u8s(query), count)
}
Expand Down Expand Up @@ -979,6 +994,14 @@ impl VectorType for b1x8 {
fn wrap_cxx_exception<T>(error: Result<T, cxx::Exception>) -> Result<T, USearchError> {
error.map_err(|cxx_error| USearchError::CxxException(cxx_error))
}
use std::any::type_name;
fn type_of<T>(_: T) -> &'static str {
type_name::<T>()
}
fn allow_different_dimensions<T>(type_check: T) -> bool {
let checktype: &[b1x8] = &[b1x8(0b00001111)];
type_of(type_check) == type_of(checktype)
}

impl Index {
pub fn new(options: &ffi::IndexOptions) -> Result<Self, USearchError> {
Expand Down Expand Up @@ -1083,13 +1106,15 @@ impl Index {
/// * `vector` - A slice containing the vector data.
pub fn add<T: VectorType>(self: &Index, key: Key, vector: &[T]) -> Result<(), USearchError> {
if vector.len() == self.dimensions() {
wrap_cxx_exception(T::add(self, key, vector))
} else {
Err(USearchError::WrongDimensionSize(
vector.len(),
self.dimensions(),
))
return wrap_cxx_exception(T::add(self, key, vector));
}
if allow_different_dimensions(vector) {
return wrap_cxx_exception(T::add(self, key, vector));
}
Err(USearchError::WrongDimensionSize(
vector.len(),
self.dimensions(),
))
}

/// Extracts one or more vectors matching the specified key.
Expand Down

0 comments on commit 022508a

Please sign in to comment.