Skip to content

Commit

Permalink
Update Dims
Browse files Browse the repository at this point in the history
  • Loading branch information
chlict committed Apr 9, 2020
1 parent cc892cc commit 603738d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
50 changes: 19 additions & 31 deletions src/includes/Dims.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,22 @@ struct Dims {
static constexpr auto nDims = sizeof...(T);
};

// Sometimes use Dim2, Dim4, Dim5 is more readable than Dims.
#define define_dims(N) \
template <typename ...T> \
struct Dim##N { \
static_assert(sizeof...(T) == N); \
\
static_assert(all_integral_or_constant<T...>, \
"only holding integral or constant numbers"); \
\
using tag = dims_tag; \
\
hana::tuple<T...> dim; \
\
constexpr explicit Dim##N(T... t) : dim(t...) {} \
\
constexpr Dim##N(Dim##N &&other) noexcept : dim(other.dim) {} \
\
constexpr Dim##N(const Dim##N &other) : dim(other.dim) {} \
\
template <typename ...U> \
constexpr Dim##N(hana::tuple<U...> &&other) noexcept : dim(other) {} \
\
template <typename ...U> \
constexpr Dim##N(const hana::tuple<U...> &other) noexcept : dim(other) {} \
\
static constexpr auto nDims = sizeof...(T); \
};

define_dims(2)
define_dims(4)
define_dims(5)
template <typename D0, typename D1>
constexpr auto Dim2(D0 d0, D1 d1) {
static_assert(is_integral_or_constant<D0> && is_integral_or_constant<D1>);
return Dims(d0, d1);
}

template <typename D0, typename D1, typename D2, typename D3>
constexpr auto Dim4(D0 d0, D1 d1, D2 d2, D3 d3) {
static_assert(is_integral_or_constant<D0> && is_integral_or_constant<D1>);
static_assert(is_integral_or_constant<D2> && is_integral_or_constant<D3>);
return Dims(d0, d1, d2, d3);
}

template <typename D0, typename D1, typename D2, typename D3, typename D4>
constexpr auto Dim5(D0 d0, D1 d1, D2 d2, D3 d3, D4 d4) {
static_assert(is_integral_or_constant<D0> && is_integral_or_constant<D1>);
static_assert(is_integral_or_constant<D2> && is_integral_or_constant<D3> && is_integral_or_constant<D4>);
return Dims(d0, d1, d2, d3, d4);
}
17 changes: 16 additions & 1 deletion test/TestDims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TEST(TestDims, Test4) {

TEST(TestDims, Test5) {
auto constexpr dims1 = Dim4(1, 2, 3, 4);
auto constexpr dims2 = Dim4(dims1);
auto constexpr dims2 = Dims(dims1);

static_assert(dims1.dim[0_c] == dims2.dim[0_c]);
static_assert(dims1.dim[1_c] == dims2.dim[1_c]);
Expand Down Expand Up @@ -74,3 +74,18 @@ TEST(TestDims, Test6) {
static_assert(d4.dim[1_c] == 40_c);
}

TEST(TestDims, Test7) {
auto d1 = Dim2(10_c, 20_c);
auto d2 = copy1(d1);
static_assert(d1.dim == d2.dim);

auto d3 = copy2(d1);
static_assert(d1.dim == d3.dim);

auto d4 = add1(d1, d2);
static_assert(d4.dim[0_c] == 20_c);
static_assert(d4.dim[1_c] == 40_c);

auto d5 = Dim2(20_c, 40_c);
static_assert(d4.dim[0_c] == d5.dim[0_c]);
}

0 comments on commit 603738d

Please sign in to comment.