Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
AuxIndexStructures_c.cpp
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 // Copyright 2004-present Facebook. All Rights Reserved.
10 // -*- c++ -*-
11
12 #include "AuxIndexStructures_c.h"
13 #include "AuxIndexStructures.h"
14 #include "macros_impl.h"
15 #include <iostream>
16
17 extern "C" {
18
19 using faiss::BufferList;
20 using faiss::IDSelector;
21 using faiss::IDSelectorBatch;
22 using faiss::IDSelectorRange;
23 using faiss::RangeSearchResult;
24 using faiss::RangeSearchPartialResult;
25
26 DEFINE_GETTER(RangeSearchResult, size_t, nq)
27
28 int faiss_RangeSearchResult_new(FaissRangeSearchResult** p_rsr, idx_t nq) {
29 try {
30 *p_rsr = reinterpret_cast<FaissRangeSearchResult*>(
31 new RangeSearchResult(nq));
32 return 0;
33 } CATCH_AND_HANDLE
34 }
35
36 int faiss_RangeSearchResult_new_with(FaissRangeSearchResult** p_rsr, idx_t nq, int alloc_lims) {
37 try {
38 *p_rsr = reinterpret_cast<FaissRangeSearchResult*>(
39 new RangeSearchResult(nq, static_cast<bool>(alloc_lims)));
40 return 0;
41 } CATCH_AND_HANDLE
42 }
43
44 /// called when lims contains the nb of elements result entries
45 /// for each query
46 int faiss_RangeSearchResult_do_allocation(FaissRangeSearchResult* rsr) {
47 try {
48 reinterpret_cast<RangeSearchResult*>(rsr)->do_allocation();
49 return 0;
50 } CATCH_AND_HANDLE
51 }
52
53 DEFINE_DESTRUCTOR(RangeSearchResult)
54
55 /// getter for buffer_size
56 DEFINE_GETTER(RangeSearchResult, size_t, buffer_size)
57
58 /// getter for lims: size (nq + 1)
59 void faiss_RangeSearchResult_lims(FaissRangeSearchResult* rsr, size_t** lims) {
60 *lims = reinterpret_cast<RangeSearchResult*>(rsr)->lims;
61 }
62
63 /// getter for labels and respective distances (not sorted):
64 /// result for query i is labels[lims[i]:lims[i+1]]
65 void faiss_RangeSearchResult_labels(FaissRangeSearchResult* rsr, idx_t** labels, float** distances) {
66 auto sr = reinterpret_cast<RangeSearchResult*>(rsr);
67 *labels = sr->labels;
68 *distances = sr->distances;
69 }
70
71 DEFINE_DESTRUCTOR(IDSelector)
72
73 int faiss_IDSelector_is_member(const FaissIDSelector* sel, idx_t id) {
74 return reinterpret_cast<const IDSelector*>(sel)->is_member(id);
75 }
76
77 DEFINE_DESTRUCTOR(IDSelectorRange)
78
79 DEFINE_GETTER(IDSelectorRange, idx_t, imin)
80 DEFINE_GETTER(IDSelectorRange, idx_t, imax)
81
82 int faiss_IDSelectorRange_new(FaissIDSelectorRange** p_sel, idx_t imin, idx_t imax) {
83 try {
84 *p_sel = reinterpret_cast<FaissIDSelectorRange*>(
85 new IDSelectorRange(imin, imax)
86 );
87 return 0;
88 } CATCH_AND_HANDLE
89 }
90
91 DEFINE_GETTER(IDSelectorBatch, int, nbits)
92 DEFINE_GETTER(IDSelectorBatch, idx_t, mask)
93
94 int faiss_IDSelectorBatch_new(FaissIDSelectorBatch** p_sel, long n, const idx_t* indices) {
95 try {
96 *p_sel = reinterpret_cast<FaissIDSelectorBatch*>(
97 new IDSelectorBatch(n, indices)
98 );
99 return 0;
100 } CATCH_AND_HANDLE
101 }
102
103 // Below are structures used only by Index implementations
104
105 DEFINE_DESTRUCTOR(BufferList)
106
107 DEFINE_GETTER(BufferList, size_t, buffer_size)
108 DEFINE_GETTER(BufferList, size_t, wp)
109
110 int faiss_BufferList_append_buffer(FaissBufferList* bl) {
111 try {
112 reinterpret_cast<BufferList*>(bl)->append_buffer();
113 return 0;
114 } CATCH_AND_HANDLE
115 }
116
117 int faiss_BufferList_new(FaissBufferList** p_bl, size_t buffer_size) {
118 try {
119 *p_bl = reinterpret_cast<FaissBufferList*>(
120 new BufferList(buffer_size)
121 );
122 return 0;
123 } CATCH_AND_HANDLE
124 }
125
126 int faiss_BufferList_add(FaissBufferList* bl, idx_t id, float dis) {
127 try {
128 reinterpret_cast<BufferList*>(bl)->add(id, dis);
129 return 0;
130 } CATCH_AND_HANDLE
131 }
132
133 /// copy elemnts ofs:ofs+n-1 seen as linear data in the buffers to
134 /// tables dest_ids, dest_dis
135 int faiss_BufferList_copy_range(
136 FaissBufferList* bl, size_t ofs, size_t n, idx_t *dest_ids, float *dest_dis) {
137 try {
138 reinterpret_cast<BufferList*>(bl)->copy_range(ofs, n, dest_ids, dest_dis);
139 return 0;
140 } CATCH_AND_HANDLE
141 }
142
143 DEFINE_GETTER_PERMISSIVE(RangeSearchPartialResult, FaissRangeSearchResult*, res)
144
145 int faiss_RangeSearchPartialResult_new(
146 FaissRangeSearchPartialResult** p_res, FaissRangeSearchResult* res_in) {
147 try {
148 *p_res = reinterpret_cast<FaissRangeSearchPartialResult*>(
149 new RangeSearchPartialResult(
150 reinterpret_cast<RangeSearchResult*>(res_in))
151 );
152 return 0;
153 } CATCH_AND_HANDLE
154 }
155
156 int faiss_RangeSearchPartialResult_finalize(
157 FaissRangeSearchPartialResult* res) {
158 try {
159 reinterpret_cast<RangeSearchPartialResult*>(res)->finalize();
160 return 0;
161 } CATCH_AND_HANDLE
162 }
163
164 /// called by range_search before do_allocation
165 int faiss_RangeSearchPartialResult_set_lims(
166 FaissRangeSearchPartialResult* res) {
167 try {
168 reinterpret_cast<RangeSearchPartialResult*>(res)->set_lims();
169 return 0;
170 } CATCH_AND_HANDLE
171 }
172
173 /// called by range_search after do_allocation
174 int faiss_RangeSearchPartialResult_set_result(
175 FaissRangeSearchPartialResult* res, int incremental) {
176 try {
177 reinterpret_cast<RangeSearchPartialResult*>(res)->set_result(
178 static_cast<bool>(incremental));
179 return 0;
180 } CATCH_AND_HANDLE
181 }
182
183 DEFINE_GETTER_SUBCLASS(QueryResult, RangeSearchPartialResult, idx_t, qno)
184 DEFINE_GETTER_SUBCLASS(QueryResult, RangeSearchPartialResult, size_t, nres)
185 DEFINE_GETTER_SUBCLASS_PERMISSIVE(QueryResult, RangeSearchPartialResult, FaissRangeSearchPartialResult*, pres)
186
187 int faiss_RangeSearchPartialResult_new_result(
188 FaissRangeSearchPartialResult* res, idx_t qno, FaissQueryResult** qr) {
189
190 try {
191 RangeSearchPartialResult::QueryResult* q =
192 &reinterpret_cast<RangeSearchPartialResult*>(res)->new_result(qno);
193 if (qr) {
194 *qr = reinterpret_cast<FaissQueryResult*>(q);
195 }
196 return 0;
197 } CATCH_AND_HANDLE
198 }
199
200 int faiss_QueryResult_add(FaissQueryResult* qr, float dis, idx_t id) {
201 try {
202 reinterpret_cast<RangeSearchPartialResult::QueryResult*>(qr)->add(dis, id);
203 return 0;
204 } CATCH_AND_HANDLE
205 }
206
207 }
faiss::IDSelectorBatch
Definition: AuxIndexStructures.h:78
faiss::IDSelector
Definition: AuxIndexStructures.h:54
faiss::IDSelectorRange
Definition: AuxIndexStructures.h:63
faiss::RangeSearchPartialResult
the entries in the buffers are split per query
Definition: AuxIndexStructures.h:141
faiss::RangeSearchResult
Definition: AuxIndexStructures.h:31
faiss::BufferList
Definition: AuxIndexStructures.h:99
faiss::RangeSearchResult::labels
idx_t * labels
result for query i is labels[lims[i]:lims[i+1]]
Definition: AuxIndexStructures.h:37