Skip to content

Commit

Permalink
deduplicate type traits
Browse files Browse the repository at this point in the history
  • Loading branch information
niermann999 committed Nov 16, 2024
1 parent 86cda05 commit 7c69c1e
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 814 deletions.
99 changes: 97 additions & 2 deletions common/include/algebra/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,101 @@ template <class M>
using block_getter_t = typename block_getter<M>::type;
/// @}

/// @}

} // namespace algebra::trait

/// Default type trait specializations
/// @{
#define ALGEBRA_PLUGINS_DEFINE_TYPE_TRAITS(A) \
\
namespace trait { \
\
template <typename T, auto N> \
struct index<A::vector_type<T, N>> { \
using type = algebra::A::size_type; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct index<A::matrix_type<T, ROWS, COLS>> { \
using type = algebra::A::size_type; \
}; \
\
template <typename T, auto N> \
struct dimensions<A::vector_type<T, N>> { \
\
using size_type = index_t<A::vector_type<T, N>>; \
\
static constexpr size_type dim{1}; \
static constexpr size_type rows{N}; \
static constexpr size_type columns{1}; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct dimensions<A::matrix_type<T, ROWS, COLS>> { \
\
using size_type = index_t<A::matrix_type<T, ROWS, COLS>>; \
\
static constexpr size_type dim{2}; \
static constexpr size_type rows{ROWS}; \
static constexpr size_type columns{COLS}; \
}; \
\
template <typename T, auto N> \
struct value<A::vector_type<T, N>> { \
using type = T; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct value<A::matrix_type<T, ROWS, COLS>> { \
using type = T; \
}; \
\
template <typename T, auto N> \
struct vector<A::vector_type<T, N>> { \
\
template <typename other_T, auto other_N> \
using other_type = A::vector_type<other_T, other_N>; \
\
using type = other_type<T, N>; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct vector<A::matrix_type<T, ROWS, COLS>> { \
\
template <typename other_T, auto other_N> \
using other_type = A::vector_type<other_T, other_N>; \
\
using type = other_type<T, ROWS>; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct matrix<A::matrix_type<T, ROWS, COLS>> { \
template <typename other_T, auto other_ROWS, auto other_COLS> \
using other_type = A::matrix_type<other_T, other_ROWS, other_COLS>; \
\
using type = A::matrix_type<T, ROWS, COLS>; \
}; \
\
template <typename T, int N> \
struct matrix<A::vector_type<T, N>> { \
template <typename other_T, int other_ROWS, int other_COLS> \
using other_type = A::matrix_type<other_T, other_ROWS, other_COLS>; \
\
using type = other_type<T, N, 1>; \
}; \
\
template <typename T, auto N> \
struct element_getter<A::vector_type<T, N>> { \
using type = A::element_getter; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct element_getter<A::matrix_type<T, ROWS, COLS>> { \
using type = A::element_getter; \
}; \
\
template <typename T, auto ROWS, auto COLS> \
struct block_getter<A::matrix_type<T, ROWS, COLS>> { \
using type = A::block_getter; \
}; \
\
} // namespace algebra::trait
119 changes: 6 additions & 113 deletions storage/array/include/algebra/storage/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,120 +44,13 @@ using vector2 = storage_type<T, 2>;
template <typename T>
using point2 = vector2<T>;

} // namespace array

namespace trait {

/// Type trait specializations
/// @{

/// Index
/// @{
template <typename T, std::size_t N>
struct index<std::array<T, N>> {
using type = algebra::array::size_type;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct index<std::array<std::array<T, ROWS>, COLS>> {
using type = algebra::array::size_type;
};
/// @}

/// Dimension
/// @{
template <typename T, std::size_t N>
struct dimensions<std::array<T, N>> {

using size_type = index_t<std::array<T, N>>;

static constexpr size_type dim{1};
static constexpr size_type rows{N};
static constexpr size_type columns{1};
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct dimensions<std::array<std::array<T, ROWS>, COLS>> {

using size_type = index_t<std::array<std::array<T, ROWS>, COLS>>;

static constexpr size_type dim{2};
static constexpr size_type rows{ROWS};
static constexpr size_type columns{COLS};
};
/// @}

/// Value
/// @{
template <typename T, std::size_t N>
struct value<std::array<T, N>> {
using type = T;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct value<std::array<std::array<T, ROWS>, COLS>> {
using type = T;
};
/// @}
/// Element Getter
using element_getter = cmath::storage::element_getter;
/// Block Getter
using block_getter = cmath::storage::block_getter;

/// Vector
/// @{
template <typename T, std::size_t N>
struct vector<std::array<T, N>> {

template <typename other_T, std::size_t other_N>
using other_type = std::array<other_T, other_N>;

using type = other_type<T, N>;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct vector<std::array<std::array<T, ROWS>, COLS>> {

template <typename other_T, std::size_t other_N>
using other_type = std::array<other_T, other_N>;

using type = other_type<T, ROWS>;
};
/// @}

/// Matrix
/// @{
template <typename T, std::size_t ROWS, std::size_t COLS>
struct matrix<std::array<std::array<T, ROWS>, COLS>> {
template <typename other_T, std::size_t other_ROWS, std::size_t other_COLS>
using other_type = std::array<std::array<other_T, other_ROWS>, other_COLS>;

using type = std::array<std::array<T, ROWS>, COLS>;
};

template <typename T, int N>
struct matrix<std::array<T, N>> {
template <typename other_T, int other_ROWS, int other_COLS>
using other_type = std::array<std::array<other_T, other_ROWS>, other_COLS>;

using type = other_type<T, N, 1>;
};
/// @}

/// Elemet/Block Getter
/// @{
template <typename T, std::size_t N>
struct element_getter<std::array<T, N>> {
using type = cmath::storage::element_getter;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct element_getter<std::array<std::array<T, ROWS>, COLS>> {
using type = cmath::storage::element_getter;
};

template <typename T, std::size_t ROWS, std::size_t COLS>
struct block_getter<std::array<std::array<T, ROWS>, COLS>> {
using type = cmath::storage::block_getter;
};
/// @}
} // namespace array

} // namespace trait
ALGEBRA_PLUGINS_DEFINE_TYPE_TRAITS(array)

} // namespace algebra
Loading

0 comments on commit 7c69c1e

Please sign in to comment.