Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/tmp/faiss/IndexFlat.h
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 // -*- c++ -*-
10
11 #ifndef INDEX_FLAT_H
12 #define INDEX_FLAT_H
13
14 #include <vector>
15
16 #include "Index.h"
17
18
19 namespace faiss {
20
21 /** Index that stores the full vectors and performs exhaustive search */
22 struct IndexFlat: Index {
23 /// database vectors, size ntotal * d
24 std::vector<float> xb;
25
26 explicit IndexFlat (idx_t d, MetricType metric = METRIC_L2);
27
28 void add(idx_t n, const float* x) override;
29
30 void reset() override;
31
32 void search(
33 idx_t n,
34 const float* x,
35 idx_t k,
36 float* distances,
37 idx_t* labels) const override;
38
39 void range_search(
40 idx_t n,
41 const float* x,
42 float radius,
43 RangeSearchResult* result) const override;
44
45 void reconstruct(idx_t key, float* recons) const override;
46
47 /** compute distance with a subset of vectors
48 *
49 * @param x query vectors, size n * d
50 * @param labels indices of the vectors that should be compared
51 * for each query vector, size n * k
52 * @param distances
53 * corresponding output distances, size n * k
54 */
55 void compute_distance_subset (
56 idx_t n,
57 const float *x,
58 idx_t k,
59 float *distances,
60 const idx_t *labels) const;
61
62 /** remove some ids. NB that Because of the structure of the
63 * indexing structre, the semantics of this operation are
64 * different from the usual ones: the new ids are shifted */
65 long remove_ids(const IDSelector& sel) override;
66
67 IndexFlat () {}
68 };
69
70
71
72 struct IndexFlatIP:IndexFlat {
73 explicit IndexFlatIP (idx_t d): IndexFlat (d, METRIC_INNER_PRODUCT) {}
74 IndexFlatIP () {}
75 };
76
77
78 struct IndexFlatL2:IndexFlat {
79 explicit IndexFlatL2 (idx_t d): IndexFlat (d, METRIC_L2) {}
80 IndexFlatL2 () {}
81 };
82
83
84 // same as an IndexFlatL2 but a value is subtracted from each distance
85 struct IndexFlatL2BaseShift: IndexFlatL2 {
86 std::vector<float> shift;
87
88 IndexFlatL2BaseShift (idx_t d, size_t nshift, const float *shift);
89
90 void search(
91 idx_t n,
92 const float* x,
93 idx_t k,
94 float* distances,
95 idx_t* labels) const override;
96 };
97
98
99 /** Index that queries in a base_index (a fast one) and refines the
100 * results with an exact search, hopefully improving the results.
101 */
102 struct IndexRefineFlat: Index {
103
104 /// storage for full vectors
105 IndexFlat refine_index;
106
107 /// faster index to pre-select the vectors that should be filtered
108 Index *base_index;
109 bool own_fields; ///< should the base index be deallocated?
110
111 /// factor between k requested in search and the k requested from
112 /// the base_index (should be >= 1)
113 float k_factor;
114
115 explicit IndexRefineFlat (Index *base_index);
116
117 IndexRefineFlat ();
118
119 void train(idx_t n, const float* x) override;
120
121 void add(idx_t n, const float* x) override;
122
123 void reset() override;
124
125 void search(
126 idx_t n,
127 const float* x,
128 idx_t k,
129 float* distances,
130 idx_t* labels) const override;
131
132 ~IndexRefineFlat() override;
133 };
134
135
136 /// optimized version for 1D "vectors"
137 struct IndexFlat1D:IndexFlatL2 {
138 bool continuous_update; ///< is the permutation updated continuously?
139
140 std::vector<idx_t> perm; ///< sorted database indices
141
142 explicit IndexFlat1D (bool continuous_update=true);
143
144 /// if not continuous_update, call this between the last add and
145 /// the first search
146 void update_permutation ();
147
148 void add(idx_t n, const float* x) override;
149
150 void reset() override;
151
152 /// Warn: the distances returned are L1 not L2
153 void search(
154 idx_t n,
155 const float* x,
156 idx_t k,
157 float* distances,
158 idx_t* labels) const override;
159 };
160
161
162 }
163
164 #endif
faiss::IndexFlatL2BaseShift::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:134
faiss::IndexFlat
Definition: IndexFlat.h:22
faiss::IndexRefineFlat::reset
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:185
faiss::IndexFlat1D::continuous_update
bool continuous_update
is the permutation updated continuously?
Definition: IndexFlat.h:138
faiss::IndexFlat1D
optimized version for 1D &quot;vectors&quot;
Definition: IndexFlat.h:137
faiss::IDSelector
Definition: AuxIndexStructures.h:54
faiss::IndexFlatL2
Definition: IndexFlat.h:78
faiss::IndexFlat::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:43
faiss::IndexFlat::reset
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:37
faiss::IndexFlat1D::update_permutation
void update_permutation()
Definition: IndexFlat.cpp:285
faiss::IndexFlat::reconstruct
void reconstruct(idx_t key, float *recons) const override
Definition: IndexFlat.cpp:119
faiss::IndexFlat1D::add
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:295
faiss::IndexFlat::remove_ids
long remove_ids(const IDSelector &sel) override
Definition: IndexFlat.cpp:96
faiss::IndexFlatIP
Definition: IndexFlat.h:72
faiss::IndexRefineFlat::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:220
faiss::IndexRefineFlat::base_index
Index * base_index
faster index to pre-select the vectors that should be filtered
Definition: IndexFlat.h:108
faiss::IndexRefineFlat
Definition: IndexFlat.h:102
faiss::IndexRefineFlat::refine_index
IndexFlat refine_index
storage for full vectors
Definition: IndexFlat.h:105
faiss::IndexRefineFlat::own_fields
bool own_fields
should the base index be deallocated?
Definition: IndexFlat.h:109
faiss::Index::d
int d
vector dimension
Definition: Index.h:66
faiss::IndexFlat::range_search
void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
Definition: IndexFlat.cpp:59
faiss::IndexRefineFlat::train
void train(idx_t n, const float *x) override
Definition: IndexFlat.cpp:172
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:64
faiss::IndexFlat::add
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:31
faiss::IndexFlat1D::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Warn: the distances returned are L1 not L2.
Definition: IndexFlat.cpp:308
faiss::RangeSearchResult
Definition: AuxIndexStructures.h:31
faiss::IndexFlat::compute_distance_subset
void compute_distance_subset(idx_t n, const float *x, idx_t k, float *distances, const idx_t *labels) const
Definition: IndexFlat.cpp:74
faiss::IndexFlatL2BaseShift
Definition: IndexFlat.h:85
faiss::Index
Definition: Index.h:62
faiss::IndexFlat::xb
std::vector< float > xb
database vectors, size ntotal * d
Definition: IndexFlat.h:24
faiss::IndexRefineFlat::k_factor
float k_factor
Definition: IndexFlat.h:113
faiss::IndexFlat1D::reset
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:302
faiss::IndexFlat1D::perm
std::vector< idx_t > perm
sorted database indices
Definition: IndexFlat.h:140
faiss::MetricType
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:45
faiss::IndexRefineFlat::add
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:178