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