Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/MetaIndexes.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 META_INDEXES_H
11 #define META_INDEXES_H
12
13 #include <vector>
14 #include <unordered_map>
15 #include "Index.h"
16 #include "IndexShards.h"
17 #include "IndexReplicas.h"
18
19 namespace faiss {
20
21 /** Index that translates search results to ids */
22 struct IndexIDMap : Index {
23 Index * index; ///! the sub-index
24 bool own_fields; ///! whether pointers are deleted in destructo
25 std::vector<long> id_map;
26
27 explicit IndexIDMap (Index *index);
28
29 /// Same as add_core, but stores xids instead of sequential ids
30 /// @param xids if non-null, ids to store for the vectors (size n)
31 void add_with_ids(idx_t n, const float* x, const long* xids) override;
32
33 /// this will fail. Use add_with_ids
34 void add(idx_t n, const float* x) override;
35
36 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 void train(idx_t n, const float* x) override;
44
45 void reset() override;
46
47 /// remove ids adapted to IndexFlat
48 long remove_ids(const IDSelector& sel) override;
49
50 void range_search (idx_t n, const float *x, float radius,
51 RangeSearchResult *result) const override;
52
53 ~IndexIDMap() override;
54 IndexIDMap () {own_fields=false; index=nullptr; }
55 };
56
57 /** same as IndexIDMap but also provides an efficient reconstruction
58 implementation via a 2-way index */
59 struct IndexIDMap2 : IndexIDMap {
60
61 std::unordered_map<idx_t, idx_t> rev_map;
62
63 explicit IndexIDMap2 (Index *index);
64
65 /// make the rev_map from scratch
66 void construct_rev_map ();
67
68 void add_with_ids(idx_t n, const float* x, const long* xids) override;
69
70 long remove_ids(const IDSelector& sel) override;
71
72 void reconstruct (idx_t key, float * recons) const override;
73
74 ~IndexIDMap2() override {}
75 IndexIDMap2 () {}
76 };
77
78 /** splits input vectors in segments and assigns each segment to a sub-index
79 * used to distribute a MultiIndexQuantizer
80 */
81
82 struct IndexSplitVectors: Index {
83 bool own_fields;
84 bool threaded;
85 std::vector<Index*> sub_indexes;
86 idx_t sum_d; /// sum of dimensions seen so far
87
88 explicit IndexSplitVectors (idx_t d, bool threaded = false);
89
90 void add_sub_index (Index *);
91 void sync_with_sub_indexes ();
92
93 void add(idx_t n, const float* x) override;
94
95 void search(
96 idx_t n,
97 const float* x,
98 idx_t k,
99 float* distances,
100 idx_t* labels) const override;
101
102 void train(idx_t n, const float* x) override;
103
104 void reset() override;
105
106 ~IndexSplitVectors() override;
107 };
108
109
110 } // namespace faiss
111
112
113 #endif
faiss::IndexIDMap2
Definition: MetaIndexes.h:59
faiss::IndexIDMap::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:73
faiss::IDSelector
Definition: AuxIndexStructures.h:54
faiss::IndexIDMap2::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:148
faiss::IndexIDMap
Definition: MetaIndexes.h:22
faiss::IndexSplitVectors::reset
void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:303
faiss::IndexIDMap::add
void add(idx_t n, const float *x) override
this will fail. Use add_with_ids
Definition: MetaIndexes.cpp:43
faiss::IndexSplitVectors
Definition: MetaIndexes.h:82
faiss::Index::d
int d
vector dimension
Definition: Index.h:66
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::IndexIDMap::id_map
std::vector< long > id_map
! whether pointers are deleted in destructo
Definition: MetaIndexes.h:25
faiss::IndexIDMap::range_search
void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
Definition: MetaIndexes.cpp:85
faiss::IndexSplitVectors::train
void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:299
faiss::IndexIDMap2::construct_rev_map
void construct_rev_map()
make the rev_map from scratch
Definition: MetaIndexes.cpp:157
faiss::IndexSplitVectors::add
void add(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:218
faiss::IndexIDMap2::remove_ids
long remove_ids(const IDSelector &sel) override
remove ids adapted to IndexFlat
Definition: MetaIndexes.cpp:166
faiss::RangeSearchResult
Definition: AuxIndexStructures.h:31
faiss::IndexIDMap::reset
void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:56
faiss::IndexSplitVectors::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:224
faiss::IndexIDMap::remove_ids
long remove_ids(const IDSelector &sel) override
remove ids adapted to IndexFlat
Definition: MetaIndexes.cpp:112
faiss::IndexIDMap2::reconstruct
void reconstruct(idx_t key, float *recons) const override
Definition: MetaIndexes.cpp:174
faiss::IndexIDMap::train
void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:50
faiss::Index
Definition: Index.h:61
faiss::IndexIDMap::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:64
faiss::IndexSplitVectors::IndexSplitVectors
IndexSplitVectors(idx_t d, bool threaded=false)
sum of dimensions seen so far
Definition: MetaIndexes.cpp:188
faiss::IndexIDMap::own_fields
bool own_fields
! the sub-index
Definition: MetaIndexes.h:24