Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/IndexBinary.cpp
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 #include "IndexBinary.h"
11 #include "FaissAssert.h"
12 
13 #include <cstring>
14 
15 namespace faiss {
16 
17 IndexBinary::~IndexBinary() {}
18 
19 void IndexBinary::train(idx_t, const uint8_t *) {
20  // Does nothing by default.
21 }
22 
23 void IndexBinary::range_search(idx_t, const uint8_t *, int,
24  RangeSearchResult *) const {
25  FAISS_THROW_MSG("range search not implemented");
26 }
27 
28 void IndexBinary::assign(idx_t n, const uint8_t *x, idx_t *labels, idx_t k) {
29  int *distances = new int[n * k];
30  ScopeDeleter<int> del(distances);
31  search(n, x, k, distances, labels);
32 }
33 
34 void IndexBinary::add_with_ids(idx_t, const uint8_t *, const long *) {
35  FAISS_THROW_MSG("add_with_ids not implemented for this type of index");
36 }
37 
39  FAISS_THROW_MSG("remove_ids not implemented for this type of index");
40  return -1;
41 }
42 
43 void IndexBinary::reconstruct(idx_t, uint8_t *) const {
44  FAISS_THROW_MSG("reconstruct not implemented for this type of index");
45 }
46 
47 void IndexBinary::reconstruct_n(idx_t i0, idx_t ni, uint8_t *recons) const {
48  for (idx_t i = 0; i < ni; i++) {
49  reconstruct(i0 + i, recons + i * d);
50  }
51 }
52 
53 void IndexBinary::search_and_reconstruct(idx_t n, const uint8_t *x, idx_t k,
54  int32_t *distances, idx_t *labels,
55  uint8_t *recons) const {
56  search(n, x, k, distances, labels);
57  for (idx_t i = 0; i < n; ++i) {
58  for (idx_t j = 0; j < k; ++j) {
59  idx_t ij = i * k + j;
60  idx_t key = labels[ij];
61  uint8_t *reconstructed = recons + ij * d;
62  if (key < 0) {
63  // Fill with NaNs
64  memset(reconstructed, -1, sizeof(*reconstructed) * d);
65  } else {
66  reconstruct(key, reconstructed);
67  }
68  }
69  }
70 }
71 
72 void IndexBinary::display() const {
73  printf("Index: %s -> %ld elements\n", typeid (*this).name(), ntotal);
74 }
75 
76 
77 } // namespace faiss
virtual void search(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels) const =0
virtual long remove_ids(const IDSelector &sel)
Definition: IndexBinary.cpp:38
virtual void train(idx_t n, const uint8_t *x)
Definition: IndexBinary.cpp:19
Index::idx_t idx_t
all indices are this type
Definition: IndexBinary.h:37
int d
vector dimension
Definition: IndexBinary.h:41
void display() const
Definition: IndexBinary.cpp:72
virtual void reconstruct(idx_t key, uint8_t *recons) const
Definition: IndexBinary.cpp:43
void assign(idx_t n, const uint8_t *x, idx_t *labels, idx_t k=1)
Definition: IndexBinary.cpp:28
virtual void range_search(idx_t n, const uint8_t *x, int radius, RangeSearchResult *result) const
Definition: IndexBinary.cpp:23
virtual void add_with_ids(idx_t n, const uint8_t *x, const long *xids)
Definition: IndexBinary.cpp:34
virtual void search_and_reconstruct(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels, uint8_t *recons) const
Definition: IndexBinary.cpp:53
virtual void reconstruct_n(idx_t i0, idx_t ni, uint8_t *recons) const
Definition: IndexBinary.cpp:47
idx_t ntotal
total nb of indexed vectors
Definition: IndexBinary.h:43