All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/matthijs/github_faiss/faiss/IndexFlat.h
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the CC-by-NC license found in the
* LICENSE file in the root directory of this source tree.
*/
// Copyright 2004-present Facebook. All Rights Reserved
// -*- c++ -*-
#ifndef INDEX_FLAT_H
#define INDEX_FLAT_H
#include <vector>
#include "Index.h"
namespace faiss {
/** Index that stores the full vectors and performs exhaustive search */
struct IndexFlat: Index {
/// database vectors, size ntotal * d
std::vector<float> xb;
explicit IndexFlat (idx_t d, MetricType metric = METRIC_INNER_PRODUCT);
virtual void set_typename() override;
virtual void add (idx_t n, const float *x) override;
virtual void reset() override;
virtual void search (
idx_t n,
const float *x,
idx_t k,
float *distances,
idx_t *labels) const override;
virtual void range_search (
idx_t n,
const float *x,
float radius,
RangeSearchResult *result) const override;
virtual void reconstruct (idx_t key, float * recons)
const override;
/** compute distance with a subset of vectors
*
* @param x query vectors, size n * d
* @param labels indices of the vectors that should be compared
* for each query vector, size n * k
* @param distances
* corresponding output distances, size n * k
*/
void compute_distance_subset (
idx_t n,
const float *x,
idx_t k,
float *distances,
const idx_t *labels) const;
IndexFlat () {}
};
struct IndexFlatIP:IndexFlat {
explicit IndexFlatIP (idx_t d): IndexFlat (d, METRIC_INNER_PRODUCT) {}
IndexFlatIP () {}
};
struct IndexFlatL2:IndexFlat {
explicit IndexFlatL2 (idx_t d): IndexFlat (d, METRIC_L2) {}
IndexFlatL2 () {}
};
// same as an IndexFlatL2 but a value is subtracted from each distance
struct IndexFlatL2BaseShift: IndexFlatL2 {
std::vector<float> shift;
IndexFlatL2BaseShift (idx_t d, size_t nshift, const float *shift);
virtual void search (
idx_t n,
const float *x,
idx_t k,
float *distances,
idx_t *labels) const override;
};
/** Index that queries in a base_index (a fast one) and refines the
* results with an exact search, hopefully improving the results.
*/
struct IndexRefineFlat: Index {
/// storage for full vectors
IndexFlat refine_index;
/// faster index to pre-select the vectors that should be filtered
Index *base_index;
bool own_fields; ///< should the base index be deallocated?
/// factor between k requested in search and the k requested from
/// the base_index (should be >= 1)
float k_factor;
explicit IndexRefineFlat (Index *base_index);
IndexRefineFlat ();
virtual void train (idx_t n, const float *x) override;
virtual void add (idx_t n, const float *x) override;
virtual void reset() override;
virtual void search (
idx_t n,
const float *x,
idx_t k,
float *distances,
idx_t *labels) const override;
virtual void set_typename () override;
virtual ~IndexRefineFlat ();
};
/// optimized version for 1D "vectors"
struct IndexFlat1D:IndexFlatL2 {
bool continuous_update; ///< is the permutation updated continuously?
std::vector<idx_t> perm; ///< sorted database indices
explicit IndexFlat1D (bool continuous_update=true);
/// if not continuous_update, call this between the last add and
/// the first search
void update_permutation ();
virtual void add (idx_t n, const float *x) override;
virtual void reset() override;
/// Warn: the distances returned are L1 not L2
virtual void search (
idx_t n,
const float *x,
idx_t k,
float *distances,
idx_t *labels) const override;
};
}
#endif
faiss::IndexFlatL2BaseShift::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:124
faiss::IndexFlat
Definition: IndexFlat.h:24
faiss::IndexRefineFlat::reset
virtual void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:184
faiss::IndexFlat1D::continuous_update
bool continuous_update
is the permutation updated continuously?
Definition: IndexFlat.h:143
faiss::IndexFlat1D
optimized version for 1D &quot;vectors&quot;
Definition: IndexFlat.h:142
faiss::IndexFlatL2
Definition: IndexFlat.h:81
faiss::IndexFlat::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:54
faiss::IndexFlat::reset
virtual void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:48
faiss::IndexFlat1D::update_permutation
void update_permutation()
Definition: IndexFlat.cpp:283
faiss::IndexFlat::reconstruct
virtual void reconstruct(idx_t key, float *recons) const override
Definition: IndexFlat.cpp:109
faiss::IndexFlat1D::add
virtual void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:293
faiss::IndexFlatIP
Definition: IndexFlat.h:75
faiss::IndexRefineFlat::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexFlat.cpp:219
faiss::IndexRefineFlat::base_index
Index * base_index
faster index to pre-select the vectors that should be filtered
Definition: IndexFlat.h:111
faiss::IndexRefineFlat
Definition: IndexFlat.h:105
faiss::IndexRefineFlat::refine_index
IndexFlat refine_index
storage for full vectors
Definition: IndexFlat.h:108
faiss::IndexRefineFlat::own_fields
bool own_fields
should the base index be deallocated?
Definition: IndexFlat.h:112
faiss::Index::d
int d
vector dimension
Definition: Index.h:66
faiss::IndexFlat::range_search
virtual void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
Definition: IndexFlat.cpp:70
faiss::IndexRefineFlat::train
virtual void train(idx_t n, const float *x) override
Definition: IndexFlat.cpp:171
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:64
faiss::IndexFlat::add
virtual void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:41
faiss::IndexFlat1D::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Warn: the distances returned are L1 not L2.
Definition: IndexFlat.cpp:306
faiss::RangeSearchResult
Definition: AuxIndexStructures.h:35
faiss::IndexFlat::compute_distance_subset
void compute_distance_subset(idx_t n, const float *x, idx_t k, float *distances, const idx_t *labels) const
Definition: IndexFlat.cpp:85
faiss::IndexFlatL2BaseShift
Definition: IndexFlat.h:88
faiss::Index
Definition: Index.h:61
faiss::IndexFlat::xb
std::vector< float > xb
database vectors, size ntotal * d
Definition: IndexFlat.h:26
faiss::IndexRefineFlat::k_factor
float k_factor
Definition: IndexFlat.h:116
faiss::IndexFlat1D::reset
virtual void reset() override
removes all elements from the database.
Definition: IndexFlat.cpp:300
faiss::IndexFlat1D::perm
std::vector< idx_t > perm
sorted database indices
Definition: IndexFlat.h:145
faiss::MetricType
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.
Definition: Index.h:44
faiss::IndexRefineFlat::add
virtual void add(idx_t n, const float *x) override
Definition: IndexFlat.cpp:177