Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/IndexBinaryFlat.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 "IndexBinaryFlat.h"
11 
12 #include <cstring>
13 #include "hamming.h"
14 #include "utils.h"
15 #include "Heap.h"
16 #include "FaissAssert.h"
17 #include "AuxIndexStructures.h"
18 
19 namespace faiss {
20 
21 IndexBinaryFlat::IndexBinaryFlat(idx_t d)
22  : IndexBinary(d) {}
23 
24 void IndexBinaryFlat::add(idx_t n, const uint8_t *x) {
25  xb.insert(xb.end(), x, x + n * code_size);
26  ntotal += n;
27 }
28 
30  xb.clear();
31  ntotal = 0;
32 }
33 
34 void IndexBinaryFlat::search(idx_t n, const uint8_t *x, idx_t k,
35  int32_t *distances, idx_t *labels) const {
36  const idx_t block_size = query_batch_size;
37  for (idx_t s = 0; s < n; s += block_size) {
38  idx_t nn = block_size;
39  if (s + block_size > n) {
40  nn = n - s;
41  }
42 
43  if (use_heap) {
44  // We see the distances and labels as heaps.
45  int_maxheap_array_t res = {
46  size_t(nn), size_t(k), labels + s * k, distances + s * k
47  };
48 
49  hammings_knn_hc(&res, x + s * code_size, xb.data(), ntotal, code_size,
50  /* ordered = */ true);
51  } else {
52  hammings_knn_mc(x + s * code_size, xb.data(), nn, ntotal, k, code_size,
53  distances + s * k, labels + s * k);
54  }
55  }
56 }
57 
59  idx_t j = 0;
60  for (idx_t i = 0; i < ntotal; i++) {
61  if (sel.is_member(i)) {
62  // should be removed
63  } else {
64  if (i > j) {
65  memmove(&xb[code_size * j], &xb[code_size * i], sizeof(xb[0]) * code_size);
66  }
67  j++;
68  }
69  }
70  long nremove = ntotal - j;
71  if (nremove > 0) {
72  ntotal = j;
73  xb.resize(ntotal * code_size);
74  }
75  return nremove;
76 }
77 
78 void IndexBinaryFlat::reconstruct(idx_t key, uint8_t *recons) const {
79  memcpy(recons, &(xb[code_size * key]), sizeof(*recons) * code_size);
80 }
81 
82 
83 } // namespace faiss
void hammings_knn_hc(int_maxheap_array_t *ha, const uint8_t *a, const uint8_t *b, size_t nb, size_t ncodes, int order)
Definition: hamming.cpp:517
void add(idx_t n, const uint8_t *x) override
void hammings_knn_mc(const uint8_t *a, const uint8_t *b, size_t na, size_t nb, size_t k, size_t ncodes, int32_t *distances, long *labels)
Definition: hamming.cpp:555
long remove_ids(const IDSelector &sel) override
void search(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels) const override
void reconstruct(idx_t key, uint8_t *recons) const override
int code_size
number of bytes per vector ( = d / 8 )
Definition: IndexBinary.h:42
Index::idx_t idx_t
all indices are this type
Definition: IndexBinary.h:37
void reset() override
Removes all elements from the database.
idx_t ntotal
total nb of indexed vectors
Definition: IndexBinary.h:43
std::vector< uint8_t > xb
database vectors, size ntotal * d / 8