knncolle
Collection of KNN methods in C++
Loading...
Searching...
No Matches
load_prebuilt.hpp
Go to the documentation of this file.
1#ifndef KNNCOLLE_LOAD_PREBUILT_HPP
2#define KNNCOLLE_LOAD_PREBUILT_HPP
3
4#include <string>
5#include <unordered_map>
6#include <functional>
7#include <fstream>
8#include <memory>
9
10#include "Prebuilt.hpp"
11#include "Bruteforce.hpp"
12#include "Vptree.hpp"
13#include "L2Normalized.hpp"
14
20namespace knncolle {
21
30template<typename Index_, typename Data_, typename Distance_>
31using LoadPrebuiltFunction = std::function<Prebuilt<Index_, Data_, Distance_>* (const std::filesystem::path&)>;
32
44template<typename Index_, typename Data_, typename Distance_>
45inline std::unordered_map<std::string, LoadPrebuiltFunction<Index_, Data_, Distance_> >& load_prebuilt_registry() {
46 static std::unordered_map<std::string, LoadPrebuiltFunction<Index_, Data_, Distance_> > registry;
47 return registry;
48}
49
55class LoadPrebuiltNotFoundError final : public std::runtime_error {
56public:
60 LoadPrebuiltNotFoundError(std::string algorithm, std::filesystem::path path) :
61 std::runtime_error("cannot find a load_prebuilt_registry() function for '" + algorithm + "' at '" + path.string() + "'"),
62 my_algorithm(std::move(algorithm)),
63 my_path(std::move(path))
64 {}
69private:
70 std::string my_algorithm;
71 std::filesystem::path my_path;
72
73public:
77 const std::string& get_algorithm() const {
78 return my_algorithm;
79 }
80
84 const std::filesystem::path& get_path() const {
85 return my_path;
86 }
87};
88
104template<typename Index_, typename Data_, typename Distance_>
105Prebuilt<Index_, Data_, Distance_>* load_prebuilt_raw(const std::filesystem::path& dir) {
106 const auto alg_path = dir / "ALGORITHM";
107 const auto algorithm = quick_load_as_string(alg_path);
108
110 auto it = reg.find(algorithm);
111 if (it == reg.end()) {
112 throw LoadPrebuiltNotFoundError(algorithm, alg_path);
113 }
114
115 return (it->second)(dir);
116}
117
131template<typename Index_, typename Data_, typename Distance_>
132std::unique_ptr<Prebuilt<Index_, Data_, Distance_> > load_prebuilt_unique(const std::filesystem::path& dir) {
133 return std::unique_ptr<Prebuilt<Index_, Data_, Distance_> >(load_prebuilt_raw<Index_, Data_, Distance_>(dir));
134}
135
149template<typename Index_, typename Data_, typename Distance_>
150std::shared_ptr<Prebuilt<Index_, Data_, Distance_> > load_prebuilt_shared(const std::filesystem::path& dir) {
151 return std::shared_ptr<Prebuilt<Index_, Data_, Distance_> >(load_prebuilt_raw<Index_, Data_, Distance_>(dir));
152}
153
165
173inline L2NormalizedPrebuiltTypes load_l2normalized_prebuilt_types(const std::filesystem::path& dir) {
175 quick_load(dir / "NORMALIZED", &(config.normalized), 1);
176 return config;
177}
178
194template<typename Index_, typename Data_, typename Distance_, typename Normalized_>
196 return new L2NormalizedPrebuilt<Index_, Data_, Distance_, Normalized_>(dir);
197}
198
214template<typename Index_, typename Data_, typename Distance_, class DistanceMetric_ = DistanceMetric<Data_, Distance_> >
216 return new BruteforcePrebuilt<Index_, Data_, Distance_, DistanceMetric_>(dir);
217}
218
234template<typename Index_, typename Data_, typename Distance_, class DistanceMetric_ = DistanceMetric<Data_, Distance_> >
236 return new VptreePrebuilt<Index_, Data_, Distance_, DistanceMetric_>(dir);
237}
238
239}
240
241#endif
Implements a brute-force search for nearest neighbors.
Wrapper for L2 normalization prior to search.
Interface for prebuilt nearest-neighbor indices.
Implements a vantage point (VP) tree to search for nearest neighbors.
Exception for unknown search algorithms in load_prebuilt_raw().
Definition load_prebuilt.hpp:55
const std::string & get_algorithm() const
Definition load_prebuilt.hpp:77
const std::filesystem::path & get_path() const
Definition load_prebuilt.hpp:84
Interface for prebuilt nearest-neighbor search indices.
Definition Prebuilt.hpp:29
Collection of KNN algorithms.
Definition Bruteforce.hpp:29
std::function< Prebuilt< Index_, Data_, Distance_ > *(const std::filesystem::path &)> LoadPrebuiltFunction
Definition load_prebuilt.hpp:31
Prebuilt< Index_, Data_, Distance_ > * load_bruteforce_prebuilt(const std::filesystem::path &dir)
Definition load_prebuilt.hpp:215
std::shared_ptr< Prebuilt< Index_, Data_, Distance_ > > load_prebuilt_shared(const std::filesystem::path &dir)
Definition load_prebuilt.hpp:150
void quick_load(const std::filesystem::path &path, Input_ *const contents, const Length_ length)
Definition utils.hpp:57
Prebuilt< Index_, Data_, Distance_ > * load_prebuilt_raw(const std::filesystem::path &dir)
Definition load_prebuilt.hpp:105
Prebuilt< Index_, Data_, Distance_ > * load_l2normalized_prebuilt(const std::filesystem::path &dir)
Definition load_prebuilt.hpp:195
Prebuilt< Index_, Data_, Distance_ > * load_vptree_prebuilt(const std::filesystem::path &dir)
Definition load_prebuilt.hpp:235
L2NormalizedPrebuiltTypes load_l2normalized_prebuilt_types(const std::filesystem::path &dir)
Definition load_prebuilt.hpp:173
std::string quick_load_as_string(const std::filesystem::path &path)
Definition utils.hpp:74
NumericType
Definition NumericType.hpp:18
std::unique_ptr< Prebuilt< Index_, Data_, Distance_ > > load_prebuilt_unique(const std::filesystem::path &dir)
Definition load_prebuilt.hpp:132
std::unordered_map< std::string, LoadPrebuiltFunction< Index_, Data_, Distance_ > > & load_prebuilt_registry()
Definition load_prebuilt.hpp:45
Template type of a saved L2-normalized index.
Definition load_prebuilt.hpp:159
NumericType normalized
Definition load_prebuilt.hpp:163