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  void range_search (idx_t n, const float *x, float radius,
55  RangeSearchResult *result) const override;
56 
57  ~IndexIDMap() override;
58  IndexIDMap () {own_fields=false; index=nullptr; }
59 };
60 
61 /** same as IndexIDMap but also provides an efficient reconstruction
62  implementation via a 2-way index */
64 
65  std::unordered_map<idx_t, idx_t> rev_map;
66 
67  explicit IndexIDMap2 (Index *index);
68 
69  /// make the rev_map from scratch
70  void construct_rev_map ();
71 
72  void add_with_ids(idx_t n, const float* x, const long* xids) override;
73 
74  long remove_ids(const IDSelector& sel) override;
75 
76  void reconstruct (idx_t key, float * recons) const override;
77 
78  ~IndexIDMap2() override {}
79  IndexIDMap2 () {}
80 };
81 
82 
83 /** Index that concatenates the results from several sub-indexes
84  *
85  */
86 struct IndexShards : Index {
87 
88  std::vector<Index*> shard_indexes;
89  bool own_fields; /// should the sub-indexes be deleted along with this?
90  bool threaded;
91  bool successive_ids;
92 
93  /**
94  * @param threaded do we use one thread per sub_index or do
95  * queries sequentially?
96  * @param successive_ids should we shift the returned ids by
97  * the size of each sub-index or return them
98  * as they are?
99  */
100  explicit IndexShards (idx_t d, bool threaded = false,
101  bool successive_ids = true);
102 
103  void add_shard (Index *);
104 
105  // update metric_type and ntotal. Call if you changes something in
106  // the shard indexes.
107  void sync_with_shard_indexes ();
108 
109  Index *at(int i) {return shard_indexes[i]; }
110 
111  /// supported only for sub-indices that implement add_with_ids
112  void add(idx_t n, const float* x) override;
113 
114  void add_with_ids(idx_t n, const float* x, const long* xids) override;
115 
116  void search(
117  idx_t n,
118  const float* x,
119  idx_t k,
120  float* distances,
121  idx_t* labels) const override;
122 
123  void train(idx_t n, const float* x) override;
124 
125  void reset() override;
126 
127  ~IndexShards() override;
128 };
129 
130 /** splits input vectors in segments and assigns each segment to a sub-index
131  * used to distribute a MultiIndexQuantizer
132  */
133 
135  bool own_fields;
136  bool threaded;
137  std::vector<Index*> sub_indexes;
138  idx_t sum_d; /// sum of dimensions seen so far
139 
140  explicit IndexSplitVectors (idx_t d, bool threaded = false);
141 
142  void add_sub_index (Index *);
143  void sync_with_sub_indexes ();
144 
145  void add(idx_t n, const float* x) override;
146 
147  void search(
148  idx_t n,
149  const float* x,
150  idx_t k,
151  float* distances,
152  idx_t* labels) const override;
153 
154  void train(idx_t n, const float* x) override;
155 
156  void reset() override;
157 
158  ~IndexSplitVectors() override;
159 };
160 
161 
162 
163 }
164 
165 
166 #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 range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
Definition: MetaIndexes.cpp:80
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:90
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
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