knncolle
Collection of KNN methods in C++
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef KNNCOLLE_UTILS_HPP
2#define KNNCOLLE_UTILS_HPP
3
4#include <fstream>
5#include <string>
6#include <cstddef>
7#include <type_traits>
8#include <filesystem>
9
10#include "sanisizer/sanisizer.hpp"
11
17namespace knncolle {
18
32template<typename Input_, typename Length_>
33void quick_save(const std::filesystem::path& path, const Input_* const contents, const Length_ length) {
34 std::ofstream output(path, std::ofstream::binary);
35 if (!output) {
36 throw std::runtime_error("failed to open a binary file at '" + path.string() + "'");
37 }
38
39 output.exceptions(std::ofstream::failbit | std::ofstream::badbit);
40 output.write(reinterpret_cast<const char*>(contents), sanisizer::product<std::streamsize>(sizeof(Input_), sanisizer::attest_gez(length)));
41}
42
56template<typename Input_, typename Length_>
57void quick_load(const std::filesystem::path& path, Input_* const contents, const Length_ length) {
58 std::ifstream input(path, std::ifstream::binary);
59 if (!input) {
60 throw std::runtime_error("failed to open a binary file at '" + path.string() + "'");
61 }
62
63 input.exceptions(std::ifstream::failbit | std::ifstream::badbit);
64 input.read(reinterpret_cast<char*>(contents), sanisizer::product<std::streamsize>(sizeof(Input_), sanisizer::attest_gez(length)));
65}
66
74inline std::string quick_load_as_string(const std::filesystem::path& path) {
75 std::ifstream input(path, std::ifstream::binary);
76 if (!input) {
77 throw std::runtime_error("failed to open a binary file at '" + path.string() + "'");
78 }
79
80 input.exceptions(std::ifstream::failbit | std::ifstream::badbit);
81 return std::string( (std::istreambuf_iterator<char>(input)), (std::istreambuf_iterator<char>()) );
82}
83
87template<typename Input_>
88using I = std::remove_cv_t<std::remove_reference_t<Input_> >;
93}
94
95#endif
Collection of KNN algorithms.
Definition Bruteforce.hpp:29
void quick_load(const std::filesystem::path &path, Input_ *const contents, const Length_ length)
Definition utils.hpp:57
std::string quick_load_as_string(const std::filesystem::path &path)
Definition utils.hpp:74
void quick_save(const std::filesystem::path &path, const Input_ *const contents, const Length_ length)
Definition utils.hpp:33