knncolle_annoy
Annoy nearest neighbors in knncolle
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef KNNCOLLE_ANNOY_UTILS_HPP
2#define KNNCOLLE_ANNOY_UTILS_HPP
3
4#include <fstream>
5#include <string>
6#include <cstdint>
7#include <cstddef>
8#include <stdexcept>
9#include <type_traits>
10
11#include "knncolle/knncolle.hpp"
12
18namespace knncolle_annoy {
19
24inline static const char* save_name = "knncolle_annoy::Annoy";
25
29template<typename Input_>
30using I = std::remove_cv_t<std::remove_reference_t<Input_> >;
31
32// Doing some SFINAE nonsense.
33template<typename AnnoyDistance_, typename Other_ = int>
34struct has_name {
35 static constexpr bool value = false;
36};
37
38template<typename AnnoyDistance_>
39struct has_name<AnnoyDistance_, decltype(AnnoyDistance_::name(), 0)> {
40 static constexpr bool value = std::is_same<decltype(AnnoyDistance_::name()), const char*>::value;
41};
51template<typename AnnoyDistance_>
52const char* get_distance_name() {
53 if constexpr(!has_name<AnnoyDistance_>::value) {
54 return "unknown";
55 } else {
56 return AnnoyDistance_::name();
57 }
58}
59
64enum class NumericType : char {
65 UINT8_T, INT8_T,
66 UINT16_T, INT16_T,
67 UINT32_T, INT32_T,
68 UINT64_T, INT64_T,
69 UNSIGNED_CHAR, SIGNED_CHAR, CHAR,
70 UNSIGNED_SHORT, SHORT,
71 UNSIGNED_INT, INT,
72 UNSIGNED_LONG, LONG,
73 UNSIGNED_LONG_LONG, LONG_LONG,
74 SIZE_T, PTRDIFF_T,
75 FLOAT, DOUBLE,
76 UNKNOWN
77};
78
83template<typename Type_>
85#ifdef UINT8_MAX
86 if constexpr(std::is_same<Type_, std::uint8_t>::value) {
87 return NumericType::UINT8_T;
88 }
89#endif
90#ifdef INT8_MAX
91 if constexpr(std::is_same<Type_, std::int8_t>::value) {
92 return NumericType::INT8_T;
93 }
94#endif
95
96#ifdef UINT16_MAX
97 if constexpr(std::is_same<Type_, std::uint16_t>::value) {
98 return NumericType::UINT16_T;
99 }
100#endif
101#ifdef INT16_MAX
102 if constexpr(std::is_same<Type_, std::int16_t>::value) {
103 return NumericType::INT16_T;
104 }
105#endif
106
107#ifdef UINT32_MAX
108 if constexpr(std::is_same<Type_, std::uint32_t>::value) {
109 return NumericType::UINT32_T;
110 }
111#endif
112#ifdef INT32_MAX
113 if constexpr(std::is_same<Type_, std::int32_t>::value) {
114 return NumericType::INT32_T;
115 }
116#endif
117
118#ifdef UINT64_MAX
119 if constexpr(std::is_same<Type_, std::uint64_t>::value) {
120 return NumericType::UINT64_T;
121 }
122#endif
123#ifdef INT64_MAX
124 if constexpr(std::is_same<Type_, std::int64_t>::value) {
125 return NumericType::INT64_T;
126 }
127#endif
128
129 if constexpr(std::is_same<Type_, unsigned char>::value) {
130 return NumericType::UNSIGNED_CHAR;
131 }
132 if constexpr(std::is_same<Type_, signed char>::value) {
133 return NumericType::SIGNED_CHAR;
134 }
135 if constexpr(std::is_same<Type_, char>::value) {
136 return NumericType::CHAR;
137 }
138
139 if constexpr(std::is_same<Type_, unsigned short>::value) {
140 return NumericType::UNSIGNED_SHORT;
141 }
142 if constexpr(std::is_same<Type_, short>::value) {
143 return NumericType::SHORT;
144 }
145
146 if constexpr(std::is_same<Type_, unsigned int>::value) {
147 return NumericType::UNSIGNED_INT;
148 }
149 if constexpr(std::is_same<Type_, int>::value) {
150 return NumericType::INT;
151 }
152
153 if constexpr(std::is_same<Type_, unsigned long>::value) {
154 return NumericType::UNSIGNED_LONG;
155 }
156 if constexpr(std::is_same<Type_, long>::value) {
157 return NumericType::LONG;
158 }
159
160 if constexpr(std::is_same<Type_, unsigned long long>::value) {
161 return NumericType::UNSIGNED_LONG_LONG;
162 }
163 if constexpr(std::is_same<Type_, long long>::value) {
164 return NumericType::LONG_LONG;
165 }
166
167 if constexpr(std::is_same<Type_, std::size_t>::value) {
168 return NumericType::SIZE_T;
169 }
170 if constexpr(std::is_same<Type_, std::ptrdiff_t>::value) {
171 return NumericType::PTRDIFF_T;
172 }
173
174 if constexpr(std::is_same<Type_, float>::value) {
175 return NumericType::FLOAT;
176 }
177 if constexpr(std::is_same<Type_, double>::value) {
178 return NumericType::DOUBLE;
179 }
180
181 return NumericType::UNKNOWN;
182}
183
205
213inline PrebuiltSaveConfig scan_prebuilt_save_config(const std::string& prefix) {
214 NumericType types[2];
215 knncolle::quick_load(prefix + "types", types, 2);
216
217 PrebuiltSaveConfig config;
218 config.index = types[0];
219 config.data = types[1];
220
221 const std::string distpath = prefix + "distance";
222 std::ifstream input(distpath);
223 config.distance.insert(config.distance.end(), (std::istreambuf_iterator<char>(input)), (std::istreambuf_iterator<char>()) );
224
225 return config;
226}
227
228}
229
230#endif
Approximate nearest neighbor search with Annoy.
Definition Annoy.hpp:23
const char * get_distance_name()
Definition utils.hpp:52
PrebuiltSaveConfig scan_prebuilt_save_config(const std::string &prefix)
Definition utils.hpp:213
NumericType get_numeric_type()
Definition utils.hpp:84
NumericType
Definition utils.hpp:64
void quick_load(const std::string &path, Input_ *const contents, const Length_ length)
Configuration of a saved Annoy index.
Definition utils.hpp:189
NumericType index
Definition utils.hpp:193
std::string distance
Definition utils.hpp:203
NumericType data
Definition utils.hpp:198