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