Faiss
 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  */
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 
72  explicit IndexFlatIP (idx_t d): IndexFlat (d, METRIC_INNER_PRODUCT) {}
73  IndexFlatIP () {}
74 };
75 
76 
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
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  */
102 
103  /// storage for full vectors
105 
106  /// faster index to pre-select the vectors that should be filtered
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"
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
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:133
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:184
bool continuous_update
is the permutation updated continuously?
Definition: IndexFlat.h:137
optimized version for 1D &quot;vectors&quot;
Definition: IndexFlat.h:136
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:42
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:36
void update_permutation()
Definition: IndexFlat.cpp:284
void reconstruct(idx_t key, float *recons) const override
Definition: IndexFlat.cpp:118
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:294
long remove_ids(const IDSelector &sel) override
Definition: IndexFlat.cpp:95
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:219
Index * base_index
faster index to pre-select the vectors that should be filtered
Definition: IndexFlat.h:107
IndexFlat refine_index
storage for full vectors
Definition: IndexFlat.h:104
bool own_fields
should the base index be deallocated?
Definition: IndexFlat.h:108
int d
vector dimension
Definition: Index.h:66
long idx_t
all indices are this type
Definition: Index.h:62
void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
Definition: IndexFlat.cpp:58
void train(idx_t n, const float *x) override
Definition: IndexFlat.cpp:171
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:30
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
void compute_distance_subset(idx_t n, const float *x, idx_t k, float *distances, const idx_t *labels) const
Definition: IndexFlat.cpp:73
std::vector< float > xb
database vectors, size ntotal * d
Definition: IndexFlat.h:23
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:301
std::vector< idx_t > perm
sorted database indices
Definition: IndexFlat.h:139
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:44
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:177