Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/matthijs/github_faiss/faiss/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 */
63 void compute_distance_subset (
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
75 struct IndexFlatIP:IndexFlat {
76 explicit IndexFlatIP (idx_t d): IndexFlat (d, METRIC_INNER_PRODUCT) {}
77 IndexFlatIP () {}
78 };
79
80
81 struct IndexFlatL2:IndexFlat {
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
88 struct IndexFlatL2BaseShift: IndexFlatL2 {
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 */
105 struct IndexRefineFlat: Index {
106
107 /// storage for full vectors
108 IndexFlat refine_index;
109
110 /// faster index to pre-select the vectors that should be filtered
111 Index *base_index;
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"
142 struct IndexFlat1D:IndexFlatL2 {
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
faiss::IndexFlatL2BaseShift::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:124
faiss::IndexFlat
Definition: IndexFlat.h:24
faiss::IndexRefineFlat::reset
virtual 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:143
faiss::IndexFlat1D
optimized version for 1D &quot;vectors&quot;
Definition: IndexFlat.h:142
faiss::IndexFlatL2
Definition: IndexFlat.h:81
faiss::IndexFlat::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:54
faiss::IndexFlat::reset
virtual void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:48
faiss::IndexFlat1D::update_permutation
void update_permutation()
Definition: IndexFlat.cpp:283
faiss::IndexFlat::reconstruct
virtual void reconstruct(idx_t key, float *recons) const override
Definition: IndexFlat.cpp:109
faiss::IndexFlat1D::add
virtual void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:293
faiss::IndexFlatIP
Definition: IndexFlat.h:75
faiss::IndexRefineFlat::search
virtual 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:111
faiss::IndexRefineFlat
Definition: IndexFlat.h:105
faiss::IndexRefineFlat::refine_index
IndexFlat refine_index
storage for full vectors
Definition: IndexFlat.h:108
faiss::IndexRefineFlat::own_fields
bool own_fields
should the base index be deallocated?
Definition: IndexFlat.h:112
faiss::Index::d
int d
vector dimension
Definition: Index.h:66
faiss::IndexFlat::range_search
virtual void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
Definition: IndexFlat.cpp:70
faiss::IndexRefineFlat::train
virtual void train(idx_t n, const float *x) override
Definition: IndexFlat.cpp:171
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:64
faiss::IndexFlat::add
virtual void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:41
faiss::IndexFlat1D::search
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
faiss::RangeSearchResult
Definition: AuxIndexStructures.h:35
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:85
faiss::IndexFlatL2BaseShift
Definition: IndexFlat.h:88
faiss::Index
Definition: Index.h:61
faiss::IndexFlat::xb
std::vector< float > xb
database vectors, size ntotal * d
Definition: IndexFlat.h:26
faiss::IndexRefineFlat::k_factor
float k_factor
Definition: IndexFlat.h:116
faiss::IndexFlat1D::reset
virtual void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:300
faiss::IndexFlat1D::perm
std::vector< idx_t > perm
sorted database indices
Definition: IndexFlat.h:145
faiss::MetricType
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.
Definition: Index.h:44
faiss::IndexRefineFlat::add
virtual void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:177