Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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
30 void IndexBinaryFlat::reset() {
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
59 long IndexBinaryFlat::remove_ids(const IDSelector& sel) {
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
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:518
faiss::IndexBinaryFlat::add
void add(idx_t n, const uint8_t *x) override
Definition: IndexBinaryFlat.cpp:25
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:556
faiss::IDSelector
Definition: AuxIndexStructures.h:54
faiss::IndexBinaryFlat::remove_ids
long remove_ids(const IDSelector &sel) override
Definition: IndexBinaryFlat.cpp:59
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:35
faiss::IndexBinaryFlat::reconstruct
void reconstruct(idx_t key, uint8_t *recons) const override
Definition: IndexBinaryFlat.cpp:79
faiss::IndexBinary::code_size
int code_size
number of bytes per vector ( = d / 8 )
Definition: IndexBinary.h:41
faiss::IndexBinaryFlat::use_heap
bool use_heap
Definition: IndexBinaryFlat.h:29
faiss::HeapArray
Definition: Heap.h:350
faiss::IndexBinaryFlat::reset
void reset() override
Removes all elements from the database.
Definition: IndexBinaryFlat.cpp:30
faiss::IndexBinary::ntotal
idx_t ntotal
total nb of indexed vectors
Definition: IndexBinary.h:42
faiss::IndexBinaryFlat::xb
std::vector< uint8_t > xb
database vectors, size ntotal * d / 8
Definition: IndexBinaryFlat.h:24
faiss::IndexBinary::idx_t
long idx_t
all indices are this type
Definition: IndexBinary.h:38