| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
* |
| 4 |
* This source code is licensed under the MIT license found in the |
| 5 |
* LICENSE file in the root directory of this source tree. |
| 6 |
*/ |
| 7 |
|
| 8 |
// -*- c -*- |
| 9 |
|
| 10 |
#ifndef FAISS_AUX_INDEX_STRUCTURES_C_H |
| 11 |
#define FAISS_AUX_INDEX_STRUCTURES_C_H |
| 12 |
|
| 13 |
#include "../Index_c.h" |
| 14 |
#include "../faiss_c.h" |
| 15 |
|
| 16 |
#ifdef __cplusplus |
| 17 |
extern "C" { |
| 18 |
#endif |
| 19 |
|
| 20 |
FAISS_DECLARE_CLASS(RangeSearchResult) |
| 21 |
|
| 22 |
FAISS_DECLARE_GETTER(RangeSearchResult, size_t, nq) |
| 23 |
|
| 24 |
int faiss_RangeSearchResult_new(FaissRangeSearchResult** p_rsr, idx_t nq); |
| 25 |
|
| 26 |
int faiss_RangeSearchResult_new_with( |
| 27 |
FaissRangeSearchResult** p_rsr, |
| 28 |
idx_t nq, |
| 29 |
int alloc_lims); |
| 30 |
|
| 31 |
/// called when lims contains the nb of elements result entries |
| 32 |
/// for each query |
| 33 |
int faiss_RangeSearchResult_do_allocation(FaissRangeSearchResult* rsr); |
| 34 |
|
| 35 |
FAISS_DECLARE_DESTRUCTOR(RangeSearchResult) |
| 36 |
|
| 37 |
/// getter for buffer_size |
| 38 |
FAISS_DECLARE_GETTER(RangeSearchResult, size_t, buffer_size) |
| 39 |
|
| 40 |
/// getter for lims: size (nq + 1) |
| 41 |
void faiss_RangeSearchResult_lims(FaissRangeSearchResult* rsr, size_t** lims); |
| 42 |
|
| 43 |
/// getter for labels and respective distances (not sorted): |
| 44 |
/// result for query i is labels[lims[i]:lims[i+1]] |
| 45 |
void faiss_RangeSearchResult_labels( |
| 46 |
FaissRangeSearchResult* rsr, |
| 47 |
idx_t** labels, |
| 48 |
float** distances); |
| 49 |
|
| 50 |
/** Encapsulates a set of ids to remove. */ |
| 51 |
FAISS_DECLARE_CLASS(IDSelector) |
| 52 |
FAISS_DECLARE_DESTRUCTOR(IDSelector) |
| 53 |
|
| 54 |
int faiss_IDSelector_is_member(const FaissIDSelector* sel, idx_t id); |
| 55 |
|
| 56 |
/** remove ids between [imni, imax) */ |
| 57 |
FAISS_DECLARE_CLASS(IDSelectorRange) |
| 58 |
FAISS_DECLARE_DESTRUCTOR(IDSelectorRange) |
| 59 |
|
| 60 |
FAISS_DECLARE_GETTER(IDSelectorRange, idx_t, imin) |
| 61 |
FAISS_DECLARE_GETTER(IDSelectorRange, idx_t, imax) |
| 62 |
|
| 63 |
int faiss_IDSelectorRange_new( |
| 64 |
FaissIDSelectorRange** p_sel, |
| 65 |
idx_t imin, |
| 66 |
idx_t imax); |
| 67 |
|
| 68 |
/** Remove ids from a set. Repetitions of ids in the indices set |
| 69 |
* passed to the constructor does not hurt performance. The hash |
| 70 |
* function used for the bloom filter and GCC's implementation of |
| 71 |
* unordered_set are just the least significant bits of the id. This |
| 72 |
* works fine for random ids or ids in sequences but will produce many |
| 73 |
* hash collisions if lsb's are always the same */ |
| 74 |
FAISS_DECLARE_CLASS(IDSelectorBatch) |
| 75 |
|
| 76 |
FAISS_DECLARE_GETTER(IDSelectorBatch, int, nbits) |
| 77 |
FAISS_DECLARE_GETTER(IDSelectorBatch, idx_t, mask) |
| 78 |
|
| 79 |
int faiss_IDSelectorBatch_new( |
| 80 |
FaissIDSelectorBatch** p_sel, |
| 81 |
size_t n, |
| 82 |
const idx_t* indices); |
| 83 |
|
| 84 |
FAISS_DECLARE_CLASS(IDSelectorBitmap) |
| 85 |
FAISS_DECLARE_DESTRUCTOR(IDSelectorBitmap) |
| 86 |
|
| 87 |
FAISS_DECLARE_GETTER(IDSelectorBitmap, size_t, n) |
| 88 |
FAISS_DECLARE_GETTER(IDSelectorBitmap, const uint8_t*, bitmap) |
| 89 |
|
| 90 |
int faiss_IDSelectorBitmap_new( |
| 91 |
FaissIDSelectorBitmap** p_sel, |
| 92 |
size_t n, |
| 93 |
const uint8_t* bitmap); |
| 94 |
|
| 95 |
FAISS_DECLARE_CLASS(IDSelectorNot) |
| 96 |
int faiss_IDSelectorNot_new( |
| 97 |
FaissIDSelectorNot** p_sel, |
| 98 |
const FaissIDSelector* sel); |
| 99 |
|
| 100 |
FAISS_DECLARE_CLASS(IDSelectorAnd) |
| 101 |
int faiss_IDSelectorAnd_new( |
| 102 |
FaissIDSelectorAnd** p_sel, |
| 103 |
const FaissIDSelector* lhs_sel, |
| 104 |
const FaissIDSelector* rhs_sel); |
| 105 |
|
| 106 |
FAISS_DECLARE_CLASS(IDSelectorOr) |
| 107 |
int faiss_IDSelectorOr_new( |
| 108 |
FaissIDSelectorOr** p_sel, |
| 109 |
const FaissIDSelector* lhs_sel, |
| 110 |
const FaissIDSelector* rhs_sel); |
| 111 |
|
| 112 |
FAISS_DECLARE_CLASS(IDSelectorXOr) |
| 113 |
int faiss_IDSelectorXOr_new( |
| 114 |
FaissIDSelectorXOr** p_sel, |
| 115 |
const FaissIDSelector* lhs_sel, |
| 116 |
const FaissIDSelector* rhs_sel); |
| 117 |
|
| 118 |
// Below are structures used only by Index implementations |
| 119 |
|
| 120 |
/** List of temporary buffers used to store results before they are |
| 121 |
* copied to the RangeSearchResult object. */ |
| 122 |
FAISS_DECLARE_CLASS(BufferList) |
| 123 |
FAISS_DECLARE_DESTRUCTOR(BufferList) |
| 124 |
|
| 125 |
FAISS_DECLARE_GETTER(BufferList, size_t, buffer_size) |
| 126 |
FAISS_DECLARE_GETTER(BufferList, size_t, wp) |
| 127 |
|
| 128 |
typedef struct FaissBuffer { |
| 129 |
idx_t* ids; |
| 130 |
float* dis; |
| 131 |
} FaissBuffer; |
| 132 |
|
| 133 |
int faiss_BufferList_append_buffer(FaissBufferList* bl); |
| 134 |
|
| 135 |
int faiss_BufferList_new(FaissBufferList** p_bl, size_t buffer_size); |
| 136 |
|
| 137 |
int faiss_BufferList_add(FaissBufferList* bl, idx_t id, float dis); |
| 138 |
|
| 139 |
/// copy elements ofs:ofs+n-1 seen as linear data in the buffers to |
| 140 |
/// tables dest_ids, dest_dis |
| 141 |
int faiss_BufferList_copy_range( |
| 142 |
FaissBufferList* bl, |
| 143 |
size_t ofs, |
| 144 |
size_t n, |
| 145 |
idx_t* dest_ids, |
| 146 |
float* dest_dis); |
| 147 |
|
| 148 |
/// the entries in the buffers are split per query |
| 149 |
FAISS_DECLARE_CLASS(RangeSearchPartialResult) |
| 150 |
|
| 151 |
/// result structure for a single query |
| 152 |
FAISS_DECLARE_CLASS(RangeQueryResult) |
| 153 |
FAISS_DECLARE_GETTER(RangeQueryResult, idx_t, qno) |
| 154 |
FAISS_DECLARE_GETTER(RangeQueryResult, size_t, nres) |
| 155 |
FAISS_DECLARE_GETTER(RangeQueryResult, FaissRangeSearchPartialResult*, pres) |
| 156 |
|
| 157 |
int faiss_RangeQueryResult_add(FaissRangeQueryResult* qr, float dis, idx_t id); |
| 158 |
|
| 159 |
FAISS_DECLARE_GETTER(RangeSearchPartialResult, FaissRangeSearchResult*, res) |
| 160 |
|
| 161 |
int faiss_RangeSearchPartialResult_new( |
| 162 |
FaissRangeSearchPartialResult** p_res, |
| 163 |
FaissRangeSearchResult* res_in); |
| 164 |
|
| 165 |
int faiss_RangeSearchPartialResult_finalize(FaissRangeSearchPartialResult* res); |
| 166 |
|
| 167 |
/// called by range_search before do_allocation |
| 168 |
int faiss_RangeSearchPartialResult_set_lims(FaissRangeSearchPartialResult* res); |
| 169 |
|
| 170 |
int faiss_RangeSearchPartialResult_new_result( |
| 171 |
FaissRangeSearchPartialResult* res, |
| 172 |
idx_t qno, |
| 173 |
FaissRangeQueryResult** qr); |
| 174 |
|
| 175 |
FAISS_DECLARE_CLASS(DistanceComputer) |
| 176 |
/// called before computing distances |
| 177 |
int faiss_DistanceComputer_set_query(FaissDistanceComputer* dc, const float* x); |
| 178 |
|
| 179 |
/** |
| 180 |
* Compute distance of vector i to current query. |
| 181 |
* This function corresponds to the function call operator: |
| 182 |
* DistanceComputer::operator() |
| 183 |
*/ |
| 184 |
int faiss_DistanceComputer_vector_to_query_dis( |
| 185 |
FaissDistanceComputer* dc, |
| 186 |
idx_t i, |
| 187 |
float* qd); |
| 188 |
/// compute distance between two stored vectors |
| 189 |
int faiss_DistanceComputer_symmetric_dis( |
| 190 |
FaissDistanceComputer* dc, |
| 191 |
idx_t i, |
| 192 |
idx_t j, |
| 193 |
float* vd); |
| 194 |
|
| 195 |
FAISS_DECLARE_DESTRUCTOR(DistanceComputer) |
| 196 |
|
| 197 |
#ifdef __cplusplus |
| 198 |
} |
| 199 |
#endif |
| 200 |
|
| 201 |
#endif |