Fix vector<bool> serialization for tar files and add unit tests

This commit is contained in:
Patrick Niklaus
2018-03-16 14:59:05 +00:00
parent cb31f9ec29
commit aaf39162a8
11 changed files with 210 additions and 108 deletions
+2 -3
View File
@@ -67,10 +67,9 @@ inline void writeGraph(const boost::filesystem::path &path,
util::serialization::write(writer, "/ch/contracted_graph", graph);
writer.WriteElementCount64("/ch/edge_filter", edge_filter.size());
auto id = 0;
for (const auto &filter : edge_filter)
for (const auto index : util::irange<std::size_t>(0, edge_filter.size()))
{
storage::serialization::write(writer, "/ch/edge_filter/" + std::to_string(id++), filter);
storage::serialization::write(writer, "/ch/edge_filter/" + std::to_string(index), edge_filter[index]);
}
writer.WriteElementCount64("/ch/connectivity_checksum", 1);
+7 -7
View File
@@ -139,7 +139,7 @@ template <typename T> void write(io::FileWriter &writer, const std::vector<T> &d
template <typename T> void read(io::FileReader &reader, util::vector_view<T> &data)
{
const auto count = reader.ReadElementCount64();
BOOST_ASSERT(data.size() == count);
data.resize(count);
reader.ReadInto(data.data(), count);
}
@@ -174,7 +174,7 @@ inline void unpackBits(T &data, std::size_t index, std::size_t count, unsigned c
template <typename VectorT> void readBoolVector(io::FileReader &reader, VectorT &data)
{
const auto count = reader.ReadElementCount64();
BOOST_ASSERT(data.size() == count);
data.resize(count);
std::uint64_t index = 0;
for (std::uint64_t next = CHAR_BIT; next < count; index = next, next += CHAR_BIT)
{
@@ -191,7 +191,7 @@ template <typename VectorT> void writeBoolVector(io::FileWriter &writer, const V
std::uint64_t index = 0;
for (std::uint64_t next = CHAR_BIT; next < count; index = next, next += CHAR_BIT)
{
writer.WriteOne<unsigned char>(packBits(data, CHAR_BIT * index, CHAR_BIT));
writer.WriteOne<unsigned char>(packBits(data, index, CHAR_BIT));
}
if (count > index)
writer.WriteOne<unsigned char>(packBits(data, index, count - index));
@@ -201,10 +201,10 @@ template <typename VectorT>
void readBoolVector(tar::FileReader &reader, const std::string &name, VectorT &data)
{
const auto count = reader.ReadElementCount64(name);
BOOST_ASSERT(data.size() == count);
data.resize(count);
std::uint64_t index = 0;
const auto decode = [&data, &index, count](const char block) {
const auto decode = [&](const unsigned char block) {
auto read_size = std::min<std::size_t>(count - index, CHAR_BIT);
unpackBits(data, index, read_size, block);
index += CHAR_BIT;
@@ -222,12 +222,12 @@ void writeBoolVector(tar::FileWriter &writer, const std::string &name, const Vec
const auto encode = [&]() {
auto write_size = std::min<std::size_t>(count - index, CHAR_BIT);
auto packed = packBits(data, CHAR_BIT * index, write_size);
auto packed = packBits(data, index, write_size);
index += CHAR_BIT;
return packed;
};
std::uint64_t number_of_blocks = std::ceil(count / CHAR_BIT);
std::uint64_t number_of_blocks = std::ceil((double)count / CHAR_BIT);
writer.WriteStreaming<unsigned char>(
name, boost::make_function_input_iterator(encode, boost::infinite()), number_of_blocks);
}
+21 -1
View File
@@ -108,6 +108,15 @@ template <typename DataT> class vector_view
std::size_t size() const { return m_size; }
void resize(const size_t size) {
if (size > m_size)
{
throw util::exception("Trying to resize a view to a larger size.");
}
m_size = size;
}
bool empty() const { return 0 == size(); }
DataT &operator[](const unsigned index)
@@ -185,7 +194,18 @@ template <> class vector_view<bool>
return m_ptr[bucket] & (1u << offset);
}
void reset(unsigned *, std::size_t size) { m_size = size; }
void reset(unsigned * ptr, std::size_t size) {
m_ptr = ptr;
m_size = size;
}
void resize(const size_t size) {
if (size > m_size)
{
throw util::exception("Trying to resize a view to a larger size.");
}
m_size = size;
}
std::size_t size() const { return m_size; }