1#ifndef ROUTING_KIT_VECTOR_IO_H
2#define ROUTING_KIT_VECTOR_IO_H
16void save_vector(
const std::string&file_name,
const std::vector<T>&vec){
17 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
18 std::ofstream out(file_name, std::ios::binary);
20 throw std::runtime_error(
"Can not open \""+file_name+
"\" for writing.");
21 out.write(
reinterpret_cast<const char*
>(&vec[0]), vec.size()*
sizeof(T));
26 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
27 std::ifstream in(file_name, std::ios::binary);
29 throw std::runtime_error(
"Can not open \""+file_name+
"\" for reading.");
30 in.seekg(0, std::ios::end);
31 unsigned long long file_size = in.tellg();
32 if(file_size %
sizeof(T) != 0)
33 throw std::runtime_error(
"File \""+file_name+
"\" can not be a vector of the requested type because it's size is no multiple of the element type's size.");
34 in.seekg(0, std::ios::beg);
35 std::vector<T>vec(file_size /
sizeof(T));
36 in.read(
reinterpret_cast<char*
>(&vec[0]), file_size);
54void save_value(
const std::string&file_name,
const T&val){
61 auto v = load_vector<T>(file_name);
63 throw std::runtime_error(file_name+
" is empty");
65 throw std::runtime_error(file_name+
" contains more than one element");
72 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
73 out.write((
const char*)&val,
sizeof(T));
75 throw std::runtime_error(
"Could not write value to file");
80 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
82 in.read((
char*)&val,
sizeof(T));
84 throw std::runtime_error(
"Could not read value to file");
90 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
91 out.write((
const char*)&v[0],
sizeof(T)*v.size());
93 throw std::runtime_error(
"Could not write vector data");
98std::vector<T>
read_vector(std::istream&in,
unsigned long long size){
99 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
100 std::vector<T>v(size);
101 in.read((
char*)&v[0],
sizeof(T)*size);
103 throw std::runtime_error(
"Could not read vector data");
111void write_value(
const std::function<
void(
const char*,
unsigned long long)>&out,
const T&val){
112 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
113 out((
const char*)&val,
sizeof(T));
117T
read_value(
const std::function<
void(
char*,
unsigned long long)>&in){
118 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
120 in((
char*)&val,
sizeof(T));
125void write_vector(
const std::function<
void(
const char*,
unsigned long long)>&out,
const std::vector<T>&v){
126 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
127 out((
const char*)&v[0],
sizeof(T)*v.size());
131std::vector<T>
read_vector(
const std::function<
void(
char*,
unsigned long long)>&in,
unsigned long long size){
132 static_assert(std::is_pod<T>::value,
"Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
133 std::vector<T>v(size);
134 in((
char*)&v[0],
sizeof(T)*size);
144 in((
char*)v.
data(), ((size+511)/512)*64);
150 out((
const char*)v.
data(), ((v.
size()+511)/512)*64);
157 std::ofstream out(file_name, std::ios::binary);
159 throw std::runtime_error(
"Can not open \""+file_name+
"\" for writing.");
165 std::ifstream in(file_name, std::ios::binary);
167 throw std::runtime_error(
"Can not open \""+file_name+
"\" for reading.");
169 in.seekg(0, std::ios_base::end);
170 unsigned long long file_size = in.tellg();
171 in.seekg(0, std::ios_base::beg);
void write_bit_vector(const std::function< void(const char *, unsigned long long)> &out, const BitVector &v)
void write_value(std::ostream &out, const T &val)
void save_vector(const std::string &file_name, const std::vector< T > &vec)
void save_vector< std::string >(const std::string &file_name, const std::vector< std::string > &vec)
BitVector load_bit_vector(const std::string &file_name)
void save_bit_vector(const std::string &file_name, const BitVector &vec)
T read_value(std::istream &in)
T load_value(const std::string &file_name)
std::vector< T > read_vector(std::istream &in, unsigned long long size)
std::vector< T > load_vector(const std::string &file_name)
void open_file_for_loading(const std::string &file_name, const F &f)
void write_vector(std::ostream &out, const std::vector< T > &v)
std::vector< std::string > load_vector< std::string >(const std::string &file_name)
BitVector read_bit_vector(const std::function< void(char *, unsigned long long)> &in, unsigned long long size)
void save_value(const std::string &file_name, const T &val)
void open_file_for_saving(const std::string &file_name, const F &f)