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