Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/matthijs/github_faiss/faiss/MetaIndexes.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 META_INDEXES_H
13 #define META_INDEXES_H
14 
15 
16 #include <vector>
17 #include <unordered_map>
18 
19 
20 #include "Index.h"
21 
22 
23 namespace faiss {
24 
25 /** Index that translates search results to ids */
26 struct IndexIDMap : Index {
27  Index * index; ///! the sub-index
28  bool own_fields; ///! whether pointers are deleted in destructo
29  std::vector<long> id_map;
30 
31  explicit IndexIDMap (Index *index);
32 
33  /// Same as add_core, but stores xids instead of sequential ids
34  /// @param xids if non-null, ids to store for the vectors (size n)
35  void add_with_ids(idx_t n, const float* x, const long* xids) override;
36 
37  /// this will fail. Use add_with_ids
38  void add(idx_t n, const float* x) override;
39 
40  void search(
41  idx_t n,
42  const float* x,
43  idx_t k,
44  float* distances,
45  idx_t* labels) const override;
46 
47  void train(idx_t n, const float* x) override;
48 
49  void reset() override;
50 
51  /// remove ids adapted to IndexFlat
52  long remove_ids(const IDSelector& sel) override;
53 
54  ~IndexIDMap() override;
55  IndexIDMap () {own_fields=false; index=nullptr; }
56 };
57 
58 /** same as IndexIDMap but also provides an efficient reconstruction
59  implementation via a 2-way index */
61 
62  std::unordered_map<idx_t, idx_t> rev_map;
63 
64  explicit IndexIDMap2 (Index *index);
65 
66  /// make the rev_map from scratch
67  void construct_rev_map ();
68 
69  void add_with_ids(idx_t n, const float* x, const long* xids) override;
70 
71  long remove_ids(const IDSelector& sel) override;
72 
73  void reconstruct (idx_t key, float * recons) const override;
74 
75  ~IndexIDMap2() override {}
76  IndexIDMap2 () {}
77 };
78 
79 
80 /** Index that concatenates the results from several sub-indexes
81  *
82  */
83 struct IndexShards : Index {
84 
85  std::vector<Index*> shard_indexes;
86  bool own_fields; /// should the sub-indexes be deleted along with this?
87  bool threaded;
88  bool successive_ids;
89 
90  /**
91  * @param threaded do we use one thread per sub_index or do
92  * queries sequentially?
93  * @param successive_ids should we shift the returned ids by
94  * the size of each sub-index or return them
95  * as they are?
96  */
97  explicit IndexShards (idx_t d, bool threaded = false,
98  bool successive_ids = true);
99 
100  void add_shard (Index *);
101 
102  // update metric_type and ntotal. Call if you changes something in
103  // the shard indexes.
104  void sync_with_shard_indexes ();
105 
106  Index *at(int i) {return shard_indexes[i]; }
107 
108  /// supported only for sub-indices that implement add_with_ids
109  void add(idx_t n, const float* x) override;
110 
111  void add_with_ids(idx_t n, const float* x, const long* xids) override;
112 
113  void search(
114  idx_t n,
115  const float* x,
116  idx_t k,
117  float* distances,
118  idx_t* labels) const override;
119 
120  void train(idx_t n, const float* x) override;
121 
122  void reset() override;
123 
124  ~IndexShards() override;
125 };
126 
127 /** splits input vectors in segments and assigns each segment to a sub-index
128  * used to distribute a MultiIndexQuantizer
129  */
130 
132  bool own_fields;
133  bool threaded;
134  std::vector<Index*> sub_indexes;
135  idx_t sum_d; /// sum of dimensions seen so far
136 
137  explicit IndexSplitVectors (idx_t d, bool threaded = false);
138 
139  void add_sub_index (Index *);
140  void sync_with_sub_indexes ();
141 
142  void add(idx_t n, const float* x) override;
143 
144  void search(
145  idx_t n,
146  const float* x,
147  idx_t k,
148  float* distances,
149  idx_t* labels) const override;
150 
151  void train(idx_t n, const float* x) override;
152 
153  void reset() override;
154 
155  ~IndexSplitVectors() override;
156 };
157 
158 
159 
160 }
161 
162 
163 #endif
void train(idx_t n, const float *x) override
IndexShards(idx_t d, bool threaded=false, bool successive_ids=true)
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:69
void add_with_ids(idx_t n, const float *x, const long *xids) override
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
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:40
int d
vector dimension
Definition: Index.h:64
std::vector< long > id_map
! whether pointers are deleted in destructo
Definition: MetaIndexes.h:29
void train(idx_t n, const float *x) override
void add(idx_t n, const float *x) override
supported only for sub-indices that implement add_with_ids
long idx_t
all indices are this type
Definition: Index.h:62
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
bool threaded
should the sub-indexes be deleted along with this?
Definition: MetaIndexes.h:87
void reset() override
removes all elements from the database.
void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:53
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
Definition: MetaIndexes.cpp:95
void reconstruct(idx_t key, float *recons) const override
void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:47
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:60
IndexSplitVectors(idx_t d, bool threaded=false)
sum of dimensions seen so far
bool own_fields
! the sub-index
Definition: MetaIndexes.h:28