Skip to content

Commit

Permalink
fix nvec serialization (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling authored Jun 2, 2023
1 parent b1db434 commit fa29a08
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
45 changes: 30 additions & 15 deletions include/cista/containers/nvec.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ struct basic_nvec {
using reference = std::add_lvalue_reference<value_type>;

const_bucket(basic_nvec const* map, index_value_type const i)
: map_{map}, i_{to_idx(i)} {}
: i_{to_idx(i)}, map_{map} {}

template <typename T = std::decay_t<data_value_type>,
typename = std::enable_if_t<std::is_same_v<T, char>>>
Expand Down Expand Up @@ -236,13 +236,13 @@ struct basic_nvec {
private:
std::size_t bucket_begin_idx() const { return to_idx(map_->index_[0][i_]); }
std::size_t bucket_end_idx() const {
return to_idx(map_->index_[0][i_ + 1]);
return to_idx(map_->index_[0][i_ + 1U]);
}
bool is_inside_bucket(std::size_t const i) const {
return bucket_begin_idx() + i < bucket_end_idx();
}

std::size_t i_;
size_type i_;
basic_nvec const* map_;
};

Expand All @@ -254,6 +254,11 @@ struct basic_nvec {

template <typename Container>
void emplace_back(Container&& bucket) {
if (index_[0].size() == 0U) {
for (auto& i : index_) {
i.push_back(0U);
}
}
add<N - 1>(bucket);
}

Expand All @@ -275,31 +280,41 @@ struct basic_nvec {
constexpr auto const I = sizeof...(Indices);
static_assert(I == N - 1);
verify(to_idx(first) < index_[I].size(), "nvec::at: index out of range");
return get_bucket<bucket>(index_[I][first], rest...);
return get_bucket(index_[I][first], rest...);
}

template <typename... Indices>
const_bucket at(Key const first, Indices... rest) const {
constexpr auto const I = sizeof...(Indices);
static_assert(I == N - 1);
verify(to_idx(first) < index_[I].size(), "nvec::at: index out of range");
return get_bucket<const_bucket, Indices...>(index_[I][first], rest...);
return get_bucket(index_[I][first], rest...);
}

auto cista_members() noexcept { return std::tie(index_, data_); }

protected:
template <typename BucketType, typename... Rest>
BucketType get_bucket(index_value_type const bucket_start,
index_value_type const i, Rest... rest) {
return get_bucket<BucketType, Rest...>(
index_[sizeof...(Rest)][bucket_start + i], rest...);
template <typename... Rest>
bucket get_bucket(index_value_type const bucket_start,
index_value_type const i, Rest... rest) {
return get_bucket<Rest...>(index_[sizeof...(Rest)][bucket_start + i],
rest...);
}

bucket get_bucket(index_value_type const bucket_start,
index_value_type const i) {
return bucket{this, bucket_start + i};
}

template <typename... Rest>
const_bucket get_bucket(index_value_type const bucket_start,
index_value_type const i, Rest... rest) const {
return get_bucket<Rest...>(index_[sizeof...(Rest)][bucket_start + i],
rest...);
}

template <typename BucketType>
BucketType get_bucket(index_value_type const bucket_start,
index_value_type const i) {
return BucketType{this, bucket_start + i};
const_bucket get_bucket(index_value_type const bucket_start,
index_value_type const i) const {
return const_bucket{this, bucket_start + i};
}

template <std::size_t L, typename Container>
Expand Down
7 changes: 4 additions & 3 deletions include/cista/reflection/to_tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ auto to_ptrs(T&& t) {

template <typename T>
inline constexpr auto to_tuple_works_v =
detail::has_cista_members_v<T> || (std::is_aggregate_v<T> &&
detail::has_cista_members_v<T> ||
(std::is_aggregate_v<T> &&
#if !defined(_MSC_VER) || defined(NDEBUG)
std::is_standard_layout_v<T> &&
std::is_standard_layout_v<T> &&
#endif
!std::is_polymorphic_v<T>);
!std::is_polymorphic_v<T> && !std::is_union_v<T>);

template <typename T,
std::enable_if_t<detail::has_cista_members_v<T> && std::is_const_v<T>,
Expand Down
2 changes: 1 addition & 1 deletion include/cista/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ void recurse(Ctx& c, T* el, Fn&& fn) {
using Type = decay_t<T>;
if constexpr (is_indexed_v<Type>) {
fn(static_cast<typename T::value_type*>(el));
} else if constexpr (std::is_aggregate_v<Type> && !std::is_union_v<Type>) {
} else if constexpr (to_tuple_works_v<Type>) {
for_each_ptr_field(*el, [&](auto& f) { fn(f); });
} else if constexpr (is_mode_enabled(Ctx::MODE, mode::_PHASE_II) &&
std::is_pointer_v<Type>) {
Expand Down
36 changes: 25 additions & 11 deletions test/nvec_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,38 @@
#include "cista.h"
#else
#include "cista/containers/nvec.h"
#include "cista/serialization.h"
#include "cista/targets/buf.h"
#endif

TEST_CASE("nvec test") {
constexpr auto const kMode =
cista::mode::WITH_INTEGRITY | cista::mode::WITH_VERSION;

struct transfer {
bool operator==(transfer const& o) const { return o.i_ == i_; }
int i_;
};
cista::raw::nvec<std::uint32_t, transfer, 2> v;
v.emplace_back(std::vector<std::vector<transfer>>{
{transfer{1}, transfer{2}}, {transfer{3}, transfer{4}, transfer{5}}});
v.emplace_back(std::vector<std::vector<transfer>>{
{transfer{6}, transfer{7}},
{transfer{8}, transfer{9}, transfer{10}},
{transfer{11}}});
v.emplace_back(std::vector<std::vector<transfer>>{
{transfer{12}, transfer{13}},
{transfer{14}, transfer{15}, transfer{16}},
{transfer{17}}});

cista::byte_buf buf;
{
cista::offset::nvec<std::uint32_t, transfer, 2> v;
v.emplace_back(std::vector<std::vector<transfer>>{
{transfer{1}, transfer{2}}, {transfer{3}, transfer{4}, transfer{5}}});
v.emplace_back(std::vector<std::vector<transfer>>{
{transfer{6}, transfer{7}},
{transfer{8}, transfer{9}, transfer{10}},
{transfer{11}}});
v.emplace_back(std::vector<std::vector<transfer>>{
{transfer{12}, transfer{13}},
{transfer{14}, transfer{15}, transfer{16}},
{transfer{17}}});
buf = cista::serialize<kMode>(v);
}

auto const& v =
*cista::deserialize<cista::raw::nvec<std::uint32_t, transfer, 2>, kMode>(
buf);

auto const all = std::vector<transfer>{
transfer{1}, transfer{2}, transfer{3}, transfer{4}, transfer{5},
Expand Down
1 change: 1 addition & 0 deletions test/union_derive_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TEST_CASE("union_test") {
int32_t a_;
float b_;
};
static_assert(!cista::to_tuple_works_v<union_type>);

cista::byte_buf buf;

Expand Down

0 comments on commit fa29a08

Please sign in to comment.