Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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
29 void IndexBinaryFlat::reset() {
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
58 long IndexBinaryFlat::remove_ids(const IDSelector& sel) {
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
faiss::hammings_knn_hc
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
faiss::IndexBinaryFlat::add
void add(idx_t n, const uint8_t *x) override
Definition: IndexBinaryFlat.cpp:24
faiss::hammings_knn_mc
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
faiss::IDSelector
Definition: AuxIndexStructures.h:54
faiss::IndexBinaryFlat::remove_ids
long remove_ids(const IDSelector &sel) override
Definition: IndexBinaryFlat.cpp:58
faiss::IndexBinaryFlat::search
void search(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels) const override
Definition: IndexBinaryFlat.cpp:34
faiss::IndexBinaryFlat::reconstruct
void reconstruct(idx_t key, uint8_t *recons) const override
Definition: IndexBinaryFlat.cpp:78
faiss::IndexBinary::code_size
int code_size
number of bytes per vector ( = d / 8 )
Definition: IndexBinary.h:42
faiss::IndexBinary::idx_t
Index::idx_t idx_t
all indices are this type
Definition: IndexBinary.h:37
faiss::IndexBinaryFlat::use_heap
bool use_heap
Definition: IndexBinaryFlat.h:28
faiss::HeapArray
Definition: Heap.h:349
faiss::IndexBinaryFlat::reset
void reset() override
Removes all elements from the database.
Definition: IndexBinaryFlat.cpp:29
faiss::IndexBinary::ntotal
idx_t ntotal
total nb of indexed vectors
Definition: IndexBinary.h:43
faiss::IndexBinaryFlat::xb
std::vector< uint8_t > xb
database vectors, size ntotal * d / 8
Definition: IndexBinaryFlat.h:23