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