/root/doris/contrib/faiss/faiss/IndexIDMap.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 <faiss/Index.h> |
11 | | #include <faiss/IndexBinary.h> |
12 | | #include <faiss/impl/IDSelector.h> |
13 | | |
14 | | #include <unordered_map> |
15 | | #include <vector> |
16 | | |
17 | | namespace faiss { |
18 | | |
19 | | /** Index that translates search results to ids */ |
20 | | template <typename IndexT> |
21 | | struct IndexIDMapTemplate : IndexT { |
22 | | using component_t = typename IndexT::component_t; |
23 | | using distance_t = typename IndexT::distance_t; |
24 | | |
25 | | IndexT* index = nullptr; ///! the sub-index |
26 | | bool own_fields = false; ///! whether pointers are deleted in destructo |
27 | | std::vector<idx_t> id_map; |
28 | | |
29 | | explicit IndexIDMapTemplate(IndexT* index); |
30 | | |
31 | | /// @param xids if non-null, ids to store for the vectors (size n) |
32 | | void add_with_ids(idx_t n, const component_t* x, const idx_t* xids) |
33 | | override; |
34 | | |
35 | | /// this will fail. Use add_with_ids |
36 | | void add(idx_t n, const component_t* x) override; |
37 | | |
38 | | void search( |
39 | | idx_t n, |
40 | | const component_t* x, |
41 | | idx_t k, |
42 | | distance_t* distances, |
43 | | idx_t* labels, |
44 | | const SearchParameters* params = nullptr) const override; |
45 | | |
46 | | void train(idx_t n, const component_t* x) override; |
47 | | |
48 | | void reset() override; |
49 | | |
50 | | /// remove ids adapted to IndexFlat |
51 | | size_t remove_ids(const IDSelector& sel) override; |
52 | | |
53 | | void range_search( |
54 | | idx_t n, |
55 | | const component_t* x, |
56 | | distance_t radius, |
57 | | RangeSearchResult* result, |
58 | | const SearchParameters* params = nullptr) const override; |
59 | | |
60 | | void merge_from(IndexT& otherIndex, idx_t add_id = 0) override; |
61 | | void check_compatible_for_merge(const IndexT& otherIndex) const override; |
62 | | |
63 | | size_t sa_code_size() const override; |
64 | | void add_sa_codes(idx_t n, const uint8_t* x, const idx_t* xids) override; |
65 | | |
66 | | ~IndexIDMapTemplate() override; |
67 | 0 | IndexIDMapTemplate() { |
68 | 0 | own_fields = false; |
69 | 0 | index = nullptr; |
70 | 0 | } Unexecuted instantiation: _ZN5faiss18IndexIDMapTemplateINS_5IndexEEC2Ev Unexecuted instantiation: _ZN5faiss18IndexIDMapTemplateINS_11IndexBinaryEEC2Ev |
71 | | }; |
72 | | |
73 | | using IndexIDMap = IndexIDMapTemplate<Index>; |
74 | | using IndexBinaryIDMap = IndexIDMapTemplate<IndexBinary>; |
75 | | |
76 | | /** same as IndexIDMap but also provides an efficient reconstruction |
77 | | * implementation via a 2-way index */ |
78 | | template <typename IndexT> |
79 | | struct IndexIDMap2Template : IndexIDMapTemplate<IndexT> { |
80 | | using component_t = typename IndexT::component_t; |
81 | | using distance_t = typename IndexT::distance_t; |
82 | | |
83 | | std::unordered_map<idx_t, idx_t> rev_map; |
84 | | |
85 | | explicit IndexIDMap2Template(IndexT* index); |
86 | | |
87 | | /// make the rev_map from scratch |
88 | | void construct_rev_map(); |
89 | | |
90 | | void add_with_ids(idx_t n, const component_t* x, const idx_t* xids) |
91 | | override; |
92 | | |
93 | | size_t remove_ids(const IDSelector& sel) override; |
94 | | |
95 | | void reconstruct(idx_t key, component_t* recons) const override; |
96 | | |
97 | | /// check that the rev_map and the id_map are in sync |
98 | | void check_consistency() const; |
99 | | |
100 | | void merge_from(IndexT& otherIndex, idx_t add_id = 0) override; |
101 | | |
102 | 0 | ~IndexIDMap2Template() override {} Unexecuted instantiation: _ZN5faiss19IndexIDMap2TemplateINS_5IndexEED2Ev Unexecuted instantiation: _ZN5faiss19IndexIDMap2TemplateINS_11IndexBinaryEED2Ev |
103 | 0 | IndexIDMap2Template() {} Unexecuted instantiation: _ZN5faiss19IndexIDMap2TemplateINS_5IndexEEC2Ev Unexecuted instantiation: _ZN5faiss19IndexIDMap2TemplateINS_11IndexBinaryEEC2Ev |
104 | | }; |
105 | | |
106 | | using IndexIDMap2 = IndexIDMap2Template<Index>; |
107 | | using IndexBinaryIDMap2 = IndexIDMap2Template<IndexBinary>; |
108 | | |
109 | | // IDSelector that translates the ids using an IDMap |
110 | | struct IDSelectorTranslated : IDSelector { |
111 | | const std::vector<int64_t>& id_map; |
112 | | const IDSelector* sel; |
113 | | |
114 | | IDSelectorTranslated( |
115 | | const std::vector<int64_t>& id_map, |
116 | | const IDSelector* sel) |
117 | 0 | : id_map(id_map), sel(sel) {} |
118 | | |
119 | | IDSelectorTranslated(IndexBinaryIDMap& index_idmap, const IDSelector* sel) |
120 | 0 | : id_map(index_idmap.id_map), sel(sel) {} |
121 | | |
122 | | IDSelectorTranslated(IndexIDMap& index_idmap, const IDSelector* sel) |
123 | 0 | : id_map(index_idmap.id_map), sel(sel) {} |
124 | | |
125 | 0 | bool is_member(idx_t id) const override { |
126 | 0 | return sel->is_member(id_map[id]); |
127 | 0 | } |
128 | | }; |
129 | | |
130 | | } // namespace faiss |