Debian Codegraph @

c_api/IndexFlat_c.cpp

raw
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 #include "IndexFlat_c.h"
11 #include <faiss/IndexFlat.h>
12 #include <faiss/IndexRefine.h>
13 #include "macros_impl.h"
14
15 extern "C" {
16
17 using faiss::Index;
18 using faiss::IndexFlat;
19 using faiss::IndexFlat1D;
20 using faiss::IndexFlatIP;
21 using faiss::IndexFlatL2;
22 using faiss::IndexRefineFlat;
23
24 DEFINE_DESTRUCTOR(IndexFlat)
25 DEFINE_INDEX_DOWNCAST(IndexFlat)
26
27 int faiss_IndexFlat_new(FaissIndexFlat** p_index) {
28 try {
29 *p_index = reinterpret_cast<FaissIndexFlat*>(new IndexFlat());
30 return 0;
31 }
32 CATCH_AND_HANDLE
33 }
34
35 int faiss_IndexFlat_new_with(
36 FaissIndexFlat** p_index,
37 idx_t d,
38 FaissMetricType metric) {
39 try {
40 IndexFlat* index =
41 new IndexFlat(d, static_cast<faiss::MetricType>(metric));
42 *p_index = reinterpret_cast<FaissIndexFlat*>(index);
43 return 0;
44 }
45 CATCH_AND_HANDLE
46 }
47
48 void faiss_IndexFlat_xb(FaissIndexFlat* index, float** p_xb, size_t* p_size) {
49 IndexFlat* indexf = reinterpret_cast<IndexFlat*>(index);
50 *p_xb = indexf->get_xb();
51 if (p_size) {
52 *p_size = indexf->codes.size() / sizeof(float);
53 }
54 }
55
56 int faiss_IndexFlat_compute_distance_subset(
57 FaissIndex* index,
58 idx_t n,
59 const float* x,
60 idx_t k,
61 float* distances,
62 const idx_t* labels) {
63 try {
64 reinterpret_cast<IndexFlat*>(index)->compute_distance_subset(
65 n, x, k, distances, labels);
66 return 0;
67 }
68 CATCH_AND_HANDLE
69 }
70
71 DEFINE_DESTRUCTOR(IndexFlatIP)
72 DEFINE_INDEX_DOWNCAST(IndexFlatIP)
73
74 int faiss_IndexFlatIP_new(FaissIndexFlatIP** p_index) {
75 try {
76 IndexFlatIP* index = new IndexFlatIP();
77 *p_index = reinterpret_cast<FaissIndexFlatIP*>(index);
78 return 0;
79 }
80 CATCH_AND_HANDLE
81 }
82
83 int faiss_IndexFlatIP_new_with(FaissIndexFlatIP** p_index, idx_t d) {
84 try {
85 IndexFlatIP* index = new IndexFlatIP(d);
86 *p_index = reinterpret_cast<FaissIndexFlatIP*>(index);
87 return 0;
88 }
89 CATCH_AND_HANDLE
90 }
91
92 DEFINE_DESTRUCTOR(IndexFlatL2)
93 DEFINE_INDEX_DOWNCAST(IndexFlatL2)
94
95 int faiss_IndexFlatL2_new(FaissIndexFlatL2** p_index) {
96 try {
97 IndexFlatL2* index = new IndexFlatL2();
98 *p_index = reinterpret_cast<FaissIndexFlatL2*>(index);
99 return 0;
100 }
101 CATCH_AND_HANDLE
102 }
103
104 int faiss_IndexFlatL2_new_with(FaissIndexFlatL2** p_index, idx_t d) {
105 try {
106 IndexFlatL2* index = new IndexFlatL2(d);
107 *p_index = reinterpret_cast<FaissIndexFlatL2*>(index);
108 return 0;
109 }
110 CATCH_AND_HANDLE
111 }
112
113 int faiss_IndexRefineFlat_new(
114 FaissIndexRefineFlat** p_index,
115 FaissIndex* base_index) {
116 try {
117 IndexRefineFlat* index = new IndexRefineFlat(
118 reinterpret_cast<faiss::Index*>(base_index));
119 *p_index = reinterpret_cast<FaissIndexRefineFlat*>(index);
120 return 0;
121 }
122 CATCH_AND_HANDLE
123 }
124
125 DEFINE_DESTRUCTOR(IndexRefineFlat)
126 DEFINE_INDEX_DOWNCAST(IndexRefineFlat)
127
128 DEFINE_GETTER(IndexRefineFlat, int, own_fields)
129 DEFINE_SETTER(IndexRefineFlat, int, own_fields)
130
131 DEFINE_GETTER(IndexRefineFlat, float, k_factor)
132 DEFINE_SETTER(IndexRefineFlat, float, k_factor)
133
134 DEFINE_DESTRUCTOR(IndexFlat1D)
135 DEFINE_INDEX_DOWNCAST(IndexFlat1D)
136
137 int faiss_IndexFlat1D_new(FaissIndexFlat1D** p_index) {
138 try {
139 IndexFlat1D* index = new IndexFlat1D();
140 *p_index = reinterpret_cast<FaissIndexFlat1D*>(index);
141 return 0;
142 }
143 CATCH_AND_HANDLE
144 }
145
146 int faiss_IndexFlat1D_new_with(
147 FaissIndexFlat1D** p_index,
148 int continuous_update) {
149 try {
150 IndexFlat1D* index =
151 new IndexFlat1D(static_cast<bool>(continuous_update));
152 *p_index = reinterpret_cast<FaissIndexFlat1D*>(index);
153 return 0;
154 }
155 CATCH_AND_HANDLE
156 }
157
158 int faiss_IndexFlat1D_update_permutation(FaissIndexFlat1D* index) {
159 try {
160 reinterpret_cast<IndexFlat1D*>(index)->update_permutation();
161 return 0;
162 }
163 CATCH_AND_HANDLE
164 }
165 }