Debian Codegraph @

c_api/IndexIVFFlat_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 "IndexIVFFlat_c.h"
11 #include <faiss/IndexIVFFlat.h>
12 #include "Clustering_c.h"
13 #include "Index_c.h"
14 #include "macros_impl.h"
15
16 using faiss::Index;
17 using faiss::IndexIVFFlat;
18 using faiss::MetricType;
19
20 DEFINE_DESTRUCTOR(IndexIVFFlat)
21 DEFINE_INDEX_DOWNCAST(IndexIVFFlat)
22
23 /// number of possible key values
24 DEFINE_GETTER(IndexIVFFlat, size_t, nlist)
25 /// number of probes at query time
26 DEFINE_GETTER(IndexIVFFlat, size_t, nprobe)
27 DEFINE_SETTER(IndexIVFFlat, size_t, nprobe)
28
29 /// quantizer that maps vectors to inverted lists
30 DEFINE_GETTER_PERMISSIVE(IndexIVFFlat, FaissIndex*, quantizer)
31
32 /**
33 * = 0: use the quantizer as index in a kmeans training
34 * = 1: just pass on the training set to the train() of the quantizer
35 * = 2: kmeans training on a flat index + add the centroids to the quantizer
36 */
37 DEFINE_GETTER(IndexIVFFlat, char, quantizer_trains_alone)
38
39 /// whether object owns the quantizer
40 DEFINE_GETTER(IndexIVFFlat, int, own_fields)
41 DEFINE_SETTER(IndexIVFFlat, int, own_fields)
42
43 int faiss_IndexIVFFlat_new(FaissIndexIVFFlat** p_index) {
44 try {
45 *p_index = reinterpret_cast<FaissIndexIVFFlat*>(new IndexIVFFlat());
46 }
47 CATCH_AND_HANDLE
48 }
49
50 int faiss_IndexIVFFlat_new_with(
51 FaissIndexIVFFlat** p_index,
52 FaissIndex* quantizer,
53 size_t d,
54 size_t nlist) {
55 try {
56 auto q = reinterpret_cast<Index*>(quantizer);
57 *p_index = reinterpret_cast<FaissIndexIVFFlat*>(
58 new IndexIVFFlat(q, d, nlist));
59 }
60 CATCH_AND_HANDLE
61 }
62
63 int faiss_IndexIVFFlat_new_with_metric(
64 FaissIndexIVFFlat** p_index,
65 FaissIndex* quantizer,
66 size_t d,
67 size_t nlist,
68 FaissMetricType metric) {
69 try {
70 auto q = reinterpret_cast<Index*>(quantizer);
71 auto m = static_cast<MetricType>(metric);
72 *p_index = reinterpret_cast<FaissIndexIVFFlat*>(
73 new IndexIVFFlat(q, d, nlist, m));
74 }
75 CATCH_AND_HANDLE
76 }
77
78 int faiss_IndexIVFFlat_add_core(
79 FaissIndexIVFFlat* index,
80 idx_t n,
81 const float* x,
82 const idx_t* xids,
83 const int64_t* precomputed_idx) {
84 try {
85 reinterpret_cast<IndexIVFFlat*>(index)->add_core(
86 n, x, xids, precomputed_idx);
87 }
88 CATCH_AND_HANDLE
89 }
90
91 int faiss_IndexIVFFlat_update_vectors(
92 FaissIndexIVFFlat* index,
93 int nv,
94 idx_t* idx,
95 const float* v) {
96 try {
97 reinterpret_cast<IndexIVFFlat*>(index)->update_vectors(nv, idx, v);
98 }
99 CATCH_AND_HANDLE
100 }