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 * 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 */
56 void compute_distance_subset (
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
73 struct IndexFlatIP:IndexFlat {
74 explicit IndexFlatIP (idx_t d): IndexFlat (d, METRIC_INNER_PRODUCT) {}
75 IndexFlatIP () {}
76 };
77
78
79 struct IndexFlatL2:IndexFlat {
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
86 struct IndexFlatL2BaseShift: IndexFlatL2 {
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 */
103 struct IndexRefineFlat: Index {
104
105 /// storage for full vectors
106 IndexFlat refine_index;
107
108 /// faster index to pre-select the vectors that should be filtered
109 Index *base_index;
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"
138 struct IndexFlat1D:IndexFlatL2 {
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
faiss::IndexFlatL2BaseShift::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:133
faiss::IndexFlat
Definition: IndexFlat.h:23
faiss::IndexRefineFlat::reset
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:139
faiss::IndexFlat1D
optimized version for 1D &quot;vectors&quot;
Definition: IndexFlat.h:138
faiss::IDSelector
Definition: AuxIndexStructures.h:52
faiss::IndexFlatL2
Definition: IndexFlat.h:79
faiss::IndexFlat::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:42
faiss::IndexFlat::reset
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:36
faiss::IndexFlat1D::update_permutation
void update_permutation()
Definition: IndexFlat.cpp:284
faiss::IndexFlat::reconstruct
void reconstruct(idx_t key, float *recons) const override
Definition: IndexFlat.cpp:118
faiss::IndexFlat1D::add
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:294
faiss::IndexFlat::remove_ids
long remove_ids(const IDSelector &sel) override
Definition: IndexFlat.cpp:95
faiss::IndexFlatIP
Definition: IndexFlat.h:73
faiss::IndexRefineFlat::search
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:109
faiss::IndexRefineFlat
Definition: IndexFlat.h:103
faiss::IndexRefineFlat::refine_index
IndexFlat refine_index
storage for full vectors
Definition: IndexFlat.h:106
faiss::IndexRefineFlat::own_fields
bool own_fields
should the base index be deallocated?
Definition: IndexFlat.h:110
faiss::Index::d
int d
vector dimension
Definition: Index.h:64
faiss::IndexFlat::range_search
void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
Definition: IndexFlat.cpp:58
faiss::IndexRefineFlat::train
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:62
faiss::IndexFlat::add
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:30
faiss::IndexFlat1D::search
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
faiss::RangeSearchResult
Definition: AuxIndexStructures.h:29
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:73
faiss::IndexFlatL2BaseShift
Definition: IndexFlat.h:86
faiss::Index
Definition: Index.h:60
faiss::IndexFlat::xb
std::vector< float > xb
database vectors, size ntotal * d
Definition: IndexFlat.h:25
faiss::IndexRefineFlat::k_factor
float k_factor
Definition: IndexFlat.h:114
faiss::IndexFlat1D::reset
void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:301
faiss::IndexFlat1D::perm
std::vector< idx_t > perm
sorted database indices
Definition: IndexFlat.h:141
faiss::MetricType
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.
Definition: Index.h:43
faiss::IndexRefineFlat::add
void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:177