/root/doris/contrib/faiss/faiss/IndexFlatCodes.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and 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 | | #pragma once |
9 | | |
10 | | #include <vector> |
11 | | |
12 | | #include <faiss/Index.h> |
13 | | #include <faiss/impl/DistanceComputer.h> |
14 | | #include <faiss/impl/maybe_owned_vector.h> |
15 | | |
16 | | namespace faiss { |
17 | | |
18 | | struct CodePacker; |
19 | | |
20 | | /** Index that encodes all vectors as fixed-size codes (size code_size). Storage |
21 | | * is in the codes vector */ |
22 | | struct IndexFlatCodes : Index { |
23 | | size_t code_size; |
24 | | |
25 | | /// encoded dataset, size ntotal * code_size |
26 | | MaybeOwnedVector<uint8_t> codes; |
27 | | |
28 | | IndexFlatCodes(); |
29 | | |
30 | | IndexFlatCodes(size_t code_size, idx_t d, MetricType metric = METRIC_L2); |
31 | | |
32 | | /// default add uses sa_encode |
33 | | void add(idx_t n, const float* x) override; |
34 | | |
35 | | void reset() override; |
36 | | |
37 | | void reconstruct_n(idx_t i0, idx_t ni, float* recons) const override; |
38 | | |
39 | | void reconstruct(idx_t key, float* recons) const override; |
40 | | |
41 | | size_t sa_code_size() const override; |
42 | | |
43 | | /** remove some ids. NB that because of the structure of the |
44 | | * index, the semantics of this operation are |
45 | | * different from the usual ones: the new ids are shifted */ |
46 | | size_t remove_ids(const IDSelector& sel) override; |
47 | | |
48 | | /** a FlatCodesDistanceComputer offers a distance_to_code method |
49 | | * |
50 | | * The default implementation explicitly decodes the vector with sa_decode. |
51 | | */ |
52 | | virtual FlatCodesDistanceComputer* get_FlatCodesDistanceComputer() const; |
53 | | |
54 | 18.2k | DistanceComputer* get_distance_computer() const override { |
55 | 18.2k | return get_FlatCodesDistanceComputer(); |
56 | 18.2k | } |
57 | | |
58 | | /** Search implemented by decoding */ |
59 | | void search( |
60 | | idx_t n, |
61 | | const float* x, |
62 | | idx_t k, |
63 | | float* distances, |
64 | | idx_t* labels, |
65 | | const SearchParameters* params = nullptr) const override; |
66 | | |
67 | | void range_search( |
68 | | idx_t n, |
69 | | const float* x, |
70 | | float radius, |
71 | | RangeSearchResult* result, |
72 | | const SearchParameters* params = nullptr) const override; |
73 | | |
74 | | // returns a new instance of a CodePacker |
75 | | CodePacker* get_CodePacker() const; |
76 | | |
77 | | void check_compatible_for_merge(const Index& otherIndex) const override; |
78 | | |
79 | | virtual void merge_from(Index& otherIndex, idx_t add_id = 0) override; |
80 | | |
81 | | virtual void add_sa_codes(idx_t n, const uint8_t* x, const idx_t* xids) |
82 | | override; |
83 | | |
84 | | // permute_entries. perm of size ntotal maps new to old positions |
85 | | void permute_entries(const idx_t* perm); |
86 | | }; |
87 | | |
88 | | } // namespace faiss |