Faiss
 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  */
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 
73  explicit IndexFlatIP (idx_t d): IndexFlat (d, METRIC_INNER_PRODUCT) {}
74  IndexFlatIP () {}
75 };
76 
77 
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
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  */
103 
104  /// storage for full vectors
106 
107  /// faster index to pre-select the vectors that should be filtered
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"
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
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:134
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:185
bool continuous_update
is the permutation updated continuously?
Definition: IndexFlat.h:138
optimized version for 1D &quot;vectors&quot;
Definition: IndexFlat.h:137
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:43
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:37
void update_permutation()
Definition: IndexFlat.cpp:285
void reconstruct(idx_t key, float *recons) const override
Definition: IndexFlat.cpp:119
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:295
long remove_ids(const IDSelector &sel) override
Definition: IndexFlat.cpp:96
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:220
Index * base_index
faster index to pre-select the vectors that should be filtered
Definition: IndexFlat.h:108
IndexFlat refine_index
storage for full vectors
Definition: IndexFlat.h:105
bool own_fields
should the base index be deallocated?
Definition: IndexFlat.h:109
int d
vector dimension
Definition: Index.h:66
void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
Definition: IndexFlat.cpp:59
void train(idx_t n, const float *x) override
Definition: IndexFlat.cpp:172
long idx_t
all indices are this type
Definition: Index.h:64
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:31
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
void compute_distance_subset(idx_t n, const float *x, idx_t k, float *distances, const idx_t *labels) const
Definition: IndexFlat.cpp:74
std::vector< float > xb
database vectors, size ntotal * d
Definition: IndexFlat.h:24
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:302
std::vector< idx_t > perm
sorted database indices
Definition: IndexFlat.h:140
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:45
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:178