Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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