Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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