@@ -130,6 +130,48 @@ an_index.save(path_prefix);
130130auto reloaded = knncolle::load_prebuilt(path_prefix);
131131```
132132
133+ For custom Annoy-related types and distances, users can define their own saving and loading methods.
134+ For example, if we had a custom distance:
135+
136+ ``` cpp
137+ // Mock up a custom distance.
138+ class MyCustomDistance : public Annoy ::Euclidean {};
139+
140+ // Define a function to save information about this custom distance during .save() calls.
141+ knncolle_annoy::custom_save_for_annoy_data<MyCustomDistance >() = [ ] (const std::string& prefix) -> void {
142+ const auto path = prefix + "_ custom";
143+ // write something about MyCustomDistance at 'path'.
144+ };
145+
146+ // Make a new loading function that uses the saved custom information.
147+ reg[ knncolle_annoy::save_name] = [ ] (const std::string& prefix) -> Prebuilt<int, double, double>* {
148+ auto config = knncolle_annoy::scan_prebuilt_save_config(prefix);
149+
150+ if (config.index != knncolle_annoy::get_numeric_type<int>()) {
151+ throw std::runtime_error("unexpected type for the Annoy index");
152+ }
153+ if (config.data != knncolle_annoy::get_numeric_type<float >()) {
154+ throw std::runtime_error("unexpected type for the Annoy data");
155+ }
156+
157+ if (config.distance == "euclidean") {
158+ return knncolle_annoy::load_annoy_prebuilt<int, double, double, Annoy::Euclidean>();
159+ } else if (config.distance == "manhattan") {
160+ return knncolle_annoy::load_annoy_prebuilt<int, double, double, Annoy::Manhattan>();
161+ } else {
162+ const auto info = prefix + "_custom";
163+ // read something about the custom distance.
164+ return knncolle_annoy::load_annoy_prebuilt<int, double, double, MyCustomDistance>();
165+ }
166+ };
167+
168+ std::string custom_prefix = " anno/location/custom_" ;
169+ knncolle_annoy::AnnoyBuilder<int , double , double , MyCustomDistance> custom_builder;
170+ auto custom_index = custom_builder.build_unique(mat);
171+ custom_index.save(custom_prefix);
172+ auto custom_reloaded = knncolle::load_prebuilt(custom_prefix);
173+ ```
174+
133175## Building projects
134176
135177### CMake with ` FetchContent `
0 commit comments