Add vector serialization
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "util/vector_view.hpp"
|
||||
|
||||
#include "storage/io.hpp"
|
||||
#include "storage/tar.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
@@ -86,6 +87,34 @@ inline void write(storage::io::FileWriter &writer, const stxxl::vector<T> &vec)
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T> void read(tar::FileReader &reader, const std::string& name, std::vector<T> &data)
|
||||
{
|
||||
const auto count = reader.ReadElementCount64(name);
|
||||
data.resize(count);
|
||||
reader.ReadInto(name, data.data(), count);
|
||||
}
|
||||
|
||||
template <typename T> void write(tar::FileWriter &writer, const std::string& name, const std::vector<T> &data)
|
||||
{
|
||||
const auto count = data.size();
|
||||
writer.WriteElementCount64(name, count);
|
||||
writer.WriteFrom(name, data.data(), count);
|
||||
}
|
||||
|
||||
template <typename T> void read(tar::FileReader &reader, const std::string& name, util::vector_view<T> &data)
|
||||
{
|
||||
const auto count = reader.ReadElementCount64(name);
|
||||
BOOST_ASSERT(data.size() == count);
|
||||
reader.ReadInto(name, data.data(), count);
|
||||
}
|
||||
|
||||
template <typename T> void write(tar::FileWriter &writer, const std::string& name, const util::vector_view<T> &data)
|
||||
{
|
||||
const auto count = data.size();
|
||||
writer.WriteElementCount64(name, count);
|
||||
writer.WriteFrom(name, data.data(), count);
|
||||
}
|
||||
|
||||
template <typename T> void read(io::FileReader &reader, std::vector<T> &data)
|
||||
{
|
||||
const auto count = reader.ReadElementCount64();
|
||||
|
||||
+20
-18
@@ -16,11 +16,13 @@ namespace osrm
|
||||
{
|
||||
namespace storage
|
||||
{
|
||||
namespace tar
|
||||
{
|
||||
|
||||
class TarFileReader
|
||||
class FileReader
|
||||
{
|
||||
public:
|
||||
TarFileReader(const boost::filesystem::path &path) : path(path)
|
||||
FileReader(const boost::filesystem::path &path) : path(path)
|
||||
{
|
||||
auto ret = mtar_open(&handle, path.c_str(), "r");
|
||||
if (ret != MTAR_ESUCCESS)
|
||||
@@ -29,11 +31,16 @@ class TarFileReader
|
||||
}
|
||||
}
|
||||
|
||||
~TarFileReader()
|
||||
~FileReader()
|
||||
{
|
||||
mtar_close(&handle);
|
||||
}
|
||||
|
||||
std::uint64_t ReadElementCount64(const std::string &name)
|
||||
{
|
||||
return ReadOne<std::uint64_t>(name + "_count");
|
||||
}
|
||||
|
||||
template <typename T> T ReadOne(const std::string &name)
|
||||
{
|
||||
T tmp;
|
||||
@@ -41,12 +48,6 @@ class TarFileReader
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ReadInto(const std::string &name, std::vector<T> &data)
|
||||
{
|
||||
ReadInto(name, data.data(), data.size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ReadInto(const std::string &name, T *data, const std::size_t number_of_entries)
|
||||
{
|
||||
@@ -113,10 +114,10 @@ class TarFileReader
|
||||
mtar_t handle;
|
||||
};
|
||||
|
||||
class TarFileWriter
|
||||
class FileWriter
|
||||
{
|
||||
public:
|
||||
TarFileWriter(const boost::filesystem::path &path) : path(path)
|
||||
FileWriter(const boost::filesystem::path &path) : path(path)
|
||||
{
|
||||
auto ret = mtar_open(&handle, path.c_str(), "w");
|
||||
if (ret != MTAR_ESUCCESS)
|
||||
@@ -124,23 +125,22 @@ class TarFileWriter
|
||||
WriteFingerprint();
|
||||
}
|
||||
|
||||
~TarFileWriter()
|
||||
~FileWriter()
|
||||
{
|
||||
mtar_finalize(&handle);
|
||||
mtar_close(&handle);
|
||||
}
|
||||
|
||||
void WriteElementCount64(const std::string &name, const std::uint64_t count)
|
||||
{
|
||||
WriteOne(name + "_count", count);
|
||||
}
|
||||
|
||||
template <typename T> void WriteOne(const std::string &name, const T &data)
|
||||
{
|
||||
WriteFrom(name, &data, 1);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteFrom(const std::string &name, const std::vector<T> &data)
|
||||
{
|
||||
WriteFrom(name, data.data(), data.size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteFrom(const std::string &name, const T *data, const std::size_t number_of_entries)
|
||||
{
|
||||
@@ -167,6 +167,8 @@ class TarFileWriter
|
||||
boost::filesystem::path path;
|
||||
mtar_t handle;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user