Faiss
 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 */
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 
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
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:73
void add_with_ids(idx_t n, const float *x, const long *xids) override
void reset() override
removes all elements from the database.
void add(idx_t n, const float *x) override
this will fail. Use add_with_ids
Definition: MetaIndexes.cpp:43
int d
vector dimension
Definition: Index.h:66
long idx_t
all indices are this type
Definition: Index.h:62
std::vector< long > id_map
! whether pointers are deleted in destructo
Definition: MetaIndexes.h:25
void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
Definition: MetaIndexes.cpp:85
void train(idx_t n, const float *x) override
void construct_rev_map()
make the rev_map from scratch
void add(idx_t n, const float *x) override
long remove_ids(const IDSelector &sel) override
remove ids adapted to IndexFlat
void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:56
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
long remove_ids(const IDSelector &sel) override
remove ids adapted to IndexFlat
void reconstruct(idx_t key, float *recons) const override
void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:50
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:64
IndexSplitVectors(idx_t d, bool threaded=false)
sum of dimensions seen so far
bool own_fields
! the sub-index
Definition: MetaIndexes.h:24