Skip to content

Commit

Permalink
Added size() requirement for SoA types
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce Ferenczi <[email protected]>
  • Loading branch information
5had3z committed Aug 14, 2024
1 parent 9d3afcd commit 2a22915
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 8 additions & 7 deletions include/soa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
namespace cvt {

/**
* @brief Concept that checks if a struct is a Struct-of-Arrays type if it has the original struct_type defined and
* operator[index] to gather data from SoA format to a struct.
* @brief Concept that checks if a struct is a Struct-of-Arrays type if:
* - it has the original struct_type defined
* - operator[index] to gather data from SoA format to a struct
* - size() returns the number of entries in the SoA
*/
template<typename T>
concept IsSoAType = requires(T x) {
typename T::struct_type;
{ x[std::size_t{}] } -> std::same_as<typename T::struct_type>;
{ x.size() } -> std::same_as<std::size_t>;
};

namespace detail {
Expand Down Expand Up @@ -146,13 +149,11 @@ template<IsSoAType SoA> [[nodiscard]] auto SoAtoAoS(const SoA &soa) noexcept ->
std::vector<typename SoA::struct_type> aos{};

// Ensure SoA is all equally sized
std::vector<std::size_t> sizes;
boost::pfr::for_each_field(soa, [&](auto &field) { sizes.push_back(field.size()); });
assert(std::all_of(sizes.begin(), sizes.end(), [sz = sizes.front()](std::size_t s) { return s == sz; }));
aos.resize(sizes.front());
boost::pfr::for_each_field(soa, [&](auto &field) { assert(field.size() == soa.size()); });
aos.resize(soa.size());

// Copy data element-by-element
for (std::size_t idx = 0; idx < sizes.front(); ++idx) { aos[idx] = soa[idx]; }
for (std::size_t idx = 0; idx < soa.size(); ++idx) { aos[idx] = soa[idx]; }
return aos;
}

Expand Down
2 changes: 2 additions & 0 deletions test/test_soa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct ASoA
std::vector<double> c;

auto operator[](std::size_t index) const noexcept -> struct_type { return cvt::gatherStructAtIndex(*this, index); }
auto size() const noexcept -> std::size_t { return a.size(); }
};

/**
Expand All @@ -40,6 +41,7 @@ struct ASoA2
std::vector<double> c;

auto operator[](std::size_t index) const noexcept -> struct_type { return cvt::gatherStructAtIndex(*this, index); }
auto size() const noexcept -> std::size_t { return a.size(); }
};

TEST(SoATransforms, SameOrder)
Expand Down

0 comments on commit 2a22915

Please sign in to comment.