Skip to content

Commit 558e085

Browse files
committed
Renamed function and class for loadinng the saved Annoy types.
Also moved the source code out of utils as it is only required for loading. Also fixed the header to avoid infinitely recursive includes.
1 parent cace2a4 commit 558e085

3 files changed

Lines changed: 44 additions & 44 deletions

File tree

include/knncolle_annoy/load_annoy_prebuilt.hpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define KNNCOLLE_ANNOY_LOAD_PREBUILT_HPP
33

44
#include "utils.hpp"
5-
#include "knncolle_annoy.hpp"
5+
#include "Annoy.hpp"
66

77
#include <cstddef>
88
#include <stdexcept>
@@ -17,11 +17,52 @@
1717

1818
namespace knncolle_annoy {
1919

20+
/**
21+
* @brief Template types of a saved Annoy index.
22+
*
23+
* Instances are typically created by `load_annoy_prebuilt_types()`.
24+
*/
25+
struct AnnoyPrebuiltTypes {
26+
/**
27+
* Type of the index, i.e., `AnnoyIndex_` in `AnnoyBuilder()`.
28+
*/
29+
NumericType index;
30+
31+
/**
32+
* Type of the data, i.e., `AnnoyData_` in `AnnoyBuilder()`.
33+
*/
34+
NumericType data;
35+
36+
/**
37+
* Name of the distance metric, i.e., `AnnoyDistance_` in `AnnoyBuilder()`.
38+
*/
39+
std::string distance;
40+
};
41+
42+
/**
43+
* @param prefix Prefix of the file paths in which a prebuilt Annoy index was saved.
44+
* An Annoy index is typically saved by calling the `knncolle::Prebuilt::save()` method of the Annoy subclass instance.
45+
*
46+
* @return Template types of the saved instance of a `knncolle::Prebuilt` Annoy subclass.
47+
* This is typically used to choose template parameters for `load_annoy_prebuilt()`.
48+
*/
49+
inline AnnoyPrebuiltTypes load_annoy_prebuilt_types(const std::string& prefix) {
50+
NumericType types[2];
51+
knncolle::quick_load(prefix + "types", types, 2);
52+
53+
AnnoyPrebuiltTypes config;
54+
config.index = types[0];
55+
config.data = types[1];
56+
config.distance = knncolle::quick_load_as_string(prefix + "distance");
57+
58+
return config;
59+
}
60+
2061
/**
2162
* Helper function to define a `knncolle::LoadPrebuiltFunction` for Annoy in `knncolle::load_prebuilt()`.
2263
*
2364
* In an Annoy-specific `knncolle::LoadPrebuiltFunction`,
24-
* users should first call `scan_prebuilt_save_config()` to figure out the saved index's `AnnoyDistance_`, `AnnoyIndex` and `AnnoyData_`.
65+
* users should first call `load_annoy_prebuilt_types()` to figure out the saved index's `AnnoyDistance_`, `AnnoyIndex` and `AnnoyData_`.
2566
* Then, they can call `load_annoy_prebuilt()` with the specified types to return a pointer to a `knncolle::Prebuilt` object.
2667
* This can be registered in `load_prebuilt_registry()` with the key in `knncolle_annoy::save_name`.
2768
*

include/knncolle_annoy/utils.hpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -186,47 +186,6 @@ NumericType get_numeric_type() {
186186
return NumericType::UNKNOWN;
187187
}
188188

189-
/**
190-
* @brief Configuration of a saved Annoy index.
191-
*
192-
* Instances are typically created by `scan_prebuilt_save_config()`.
193-
*/
194-
struct PrebuiltSaveConfig {
195-
/**
196-
* Type of the index, i.e., `AnnoyIndex_` in `AnnoyBuilder()`.
197-
*/
198-
NumericType index;
199-
200-
/**
201-
* Type of the index, i.e., `AnnoyData_` in `AnnoyBuilder()`.
202-
*/
203-
NumericType data;
204-
205-
/**
206-
* Name of the distance metric, i.e., `AnnoyDistance_` in `AnnoyBuilder()`.
207-
*/
208-
std::string distance;
209-
};
210-
211-
/**
212-
* @param prefix Prefix of the file paths in which a prebuilt Annoy index was saved.
213-
* An Annoy index is typically saved by calling the `knncolle::Prebuilt::save()` method of the Annoy subclass instance.
214-
*
215-
* @return Configuration of the saved instance of a `knncolle::Prebuilt` Annoy subclass.
216-
* This is typically used to choose template parameters for `load_annoy_prebuilt()`.
217-
*/
218-
inline PrebuiltSaveConfig scan_prebuilt_save_config(const std::string& prefix) {
219-
NumericType types[2];
220-
knncolle::quick_load(prefix + "types", types, 2);
221-
222-
PrebuiltSaveConfig config;
223-
config.index = types[0];
224-
config.data = types[1];
225-
config.distance = knncolle::quick_load_as_string(prefix + "distance");
226-
227-
return config;
228-
}
229-
230189
/**
231190
* Define a customized saving function to preserve type information from the Annoy index in `knncolle::Prebuilt::save()`.
232191
* Users can provide their own function here, to handle types that are unknown to `get_numeric_type()` or `get_distance_name()`.

tests/src/load_annoy_prebuilt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AnnoyLoadPrebuiltTest : public TestCore, public ::testing::Test {
2323

2424
auto& reg = knncolle::load_prebuilt_registry<int, double, double>();
2525
reg[knncolle_annoy::save_name] = [](const std::string& prefix) -> knncolle::Prebuilt<int, double, double>* {
26-
auto scanned = knncolle_annoy::scan_prebuilt_save_config(prefix);
26+
auto scanned = knncolle_annoy::load_annoy_prebuilt_types(prefix);
2727
if (std::strcmp(scanned.distance.c_str(), "euclidean") == 0) {
2828
return knncolle_annoy::load_annoy_prebuilt<int, double, double, Annoy::Euclidean>(prefix);
2929
}

0 commit comments

Comments
 (0)