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
9#include "sanisizer/sanisizer.hpp"
10
16namespace knncolle {
17
31template<typename Input_, typename Length_>
32void quick_save(const std::string& path, const Input_* const contents, const Length_ length) {
33 std::ofstream output(path, std::ofstream::binary);
34 if (!output) {
35 throw std::runtime_error("failed to open a binary file at '" + path + "'");
36 }
37
38 output.exceptions(std::ofstream::failbit | std::ofstream::badbit);
39 output.write(reinterpret_cast<const char*>(contents), sanisizer::product<std::streamsize>(sizeof(Input_), sanisizer::attest_gez(length)));
40}
41
55template<typename Input_, typename Length_>
56void quick_load(const std::string& path, Input_* const contents, const Length_ length) {
57 std::ifstream input(path, std::ifstream::binary);
58 if (!input) {
59 throw std::runtime_error("failed to open a binary file at '" + path + "'");
60 }
61
62 input.exceptions(std::ifstream::failbit | std::ifstream::badbit);
63 input.read(reinterpret_cast<char*>(contents), sanisizer::product<std::streamsize>(sizeof(Input_), sanisizer::attest_gez(length)));
64}
65
73inline std::string quick_load_as_string(const std::string& path) {
74 std::ifstream input(path, std::ifstream::binary);
75 if (!input) {
76 throw std::runtime_error("failed to open a binary file at '" + path + "'");
77 }
78
79 input.exceptions(std::ifstream::failbit | std::ifstream::badbit);
80 return std::string( (std::istreambuf_iterator<char>(input)), (std::istreambuf_iterator<char>()) );
81}
82
86template<typename Input_>
87using I = std::remove_cv_t<std::remove_reference_t<Input_> >;
92}
93
94#endif
Collection of KNN algorithms.
Definition Bruteforce.hpp:28
std::string quick_load_as_string(const std::string &path)
Definition utils.hpp:73
void quick_load(const std::string &path, Input_ *const contents, const Length_ length)
Definition utils.hpp:56
void quick_save(const std::string &path, const Input_ *const contents, const Length_ length)
Definition utils.hpp:32