|
7 | 7 | #include <cstddef> |
8 | 8 | #include <stdexcept> |
9 | 9 | #include <type_traits> |
| 10 | +#include <functional> |
10 | 11 |
|
11 | 12 | #include "knncolle/knncolle.hpp" |
12 | 13 |
|
@@ -47,6 +48,8 @@ struct has_name<AnnoyDistance_, decltype(AnnoyDistance_::name(), 0)> { |
47 | 48 | * @tparam AnnoyDistance_ An **Annoy**-compatible class to compute the distance between vectors, as used in `AnnoyBuilder()`. |
48 | 49 | * @return Name of the distance metric, e.g., `"euclidean"`, `"manhattan"`. |
49 | 50 | * This is taken from `AnnoyDistance_::name()` if such a method exists, otherwise `"unknown"` is returned. |
| 51 | + * |
| 52 | + * For unknown distances, consider using `customize_save_for_annoy_types()` to add more information to the on-disk representation during a `knncolle::Prebuilt::save()` call. |
50 | 53 | */ |
51 | 54 | template<typename AnnoyDistance_> |
52 | 55 | const char* get_distance_name() { |
@@ -79,6 +82,8 @@ enum class NumericType : char { |
79 | 82 | /** |
80 | 83 | * @tparam Type_ Some integer or floating-point type, typically used as `AnnoyIndex_` or `AnnoyData_` in `AnnoyBuilder()`. |
81 | 84 | * @return Identity of the numeric type. |
| 85 | + * |
| 86 | + * For unknown types, consider using `customize_save_for_annoy_types()` to add more information to the on-disk representation during a `knncolle::Prebuilt::save()` call. |
82 | 87 | */ |
83 | 88 | template<typename Type_> |
84 | 89 | NumericType get_numeric_type() { |
@@ -217,14 +222,29 @@ inline PrebuiltSaveConfig scan_prebuilt_save_config(const std::string& prefix) { |
217 | 222 | PrebuiltSaveConfig config; |
218 | 223 | config.index = types[0]; |
219 | 224 | 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>()) ); |
| 225 | + config.distance = knncolle::quick_load_as_string(prefix + "distance"); |
224 | 226 |
|
225 | 227 | return config; |
226 | 228 | } |
227 | 229 |
|
| 230 | +/** |
| 231 | + * Define a customized saving function to preserve type information from the Annoy index in `knncolle::Prebuilt::save()`. |
| 232 | + * Users can provide their own function here, to handle types that are unknown to `get_numeric_type()` or `get_distance_name()`. |
| 233 | + * Any modifications to this function are not thread-safe and should be done in a serial section. |
| 234 | + * |
| 235 | + * @tparam AnnoyDistance_ An **Annoy**-compatible class to compute the distance between vectors. |
| 236 | + * @tparam AnnoyIndex_ Integer type for the observation indices in the Annoy index. |
| 237 | + * @tparam AnnoyData_ Floating-point type for data in the Annoy index. |
| 238 | + * |
| 239 | + * @return A global function for saving information about `AnnoyDistance_`, `AnnoyIndex_` and `AnnoyData_`. |
| 240 | + * If set, this will be called by the `knncolle::Prebuilt::save()` method for the Annoy `Prebuilt` subclass. |
| 241 | + */ |
| 242 | +template<class AnnoyDistance_, typename AnnoyIndex_, typename AnnoyData_> |
| 243 | +std::function<void(const std::string&)>& customize_save_for_annoy_types() { |
| 244 | + static std::function<void(const std::string&)> fun; |
| 245 | + return fun; |
| 246 | +} |
| 247 | + |
228 | 248 | } |
229 | 249 |
|
230 | 250 | #endif |
0 commit comments