Skip to content

Commit

Permalink
[feat]Add calculate hamming distance
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuRuoyu01 committed Jan 21, 2025
1 parent 0ec1655 commit 4285154
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions faiss/MetricType.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum MetricType {
METRIC_L1, ///< L1 (aka cityblock)
METRIC_Linf, ///< infinity distance
METRIC_Lp, ///< L_p distance, p is given by a faiss::Index
METRIC_HAMMING, ///< Hamming distance for binary vectors
/// metric_arg

/// some additional metrics defined in scipy.spatial.distance
Expand Down
14 changes: 14 additions & 0 deletions faiss/utils/extra_distances-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <faiss/utils/distances.h>
#include <cmath>
#include <type_traits>
#include <cstddef>
#include <faiss/utils/hamming_distance/common.h>

namespace faiss {

Expand All @@ -23,6 +25,7 @@ struct VectorDistance {
static constexpr bool is_similarity = is_similarity_metric(mt);

inline float operator()(const float* x, const float* y) const;
inline size_t operator()(const uint8_t* x, const uint8_t* y) const;

// heap template to use for this type of metric
using C = typename std::conditional<
Expand All @@ -45,6 +48,17 @@ inline float VectorDistance<METRIC_INNER_PRODUCT>::operator()(
return fvec_inner_product(x, y, d);
}

template<>
inline size_t VectorDistance<METRIC_HAMMING>::operator()(
const uint8_t* x,
const uint8_t* y) const {
int h = 0;
for (size_t i = 0; i < d; i++) {
h += (int)hamdis_tab_ham_bytes[x[i] ^ y[i]];
}
return h;
}

template <>
inline float VectorDistance<METRIC_L1>::operator()(
const float* x,
Expand Down

0 comments on commit 4285154

Please sign in to comment.