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