feat(math): introduce best shape trait
This commit is contained in:
@@ -12,6 +12,16 @@ namespace cenet {
|
||||
namespace math {
|
||||
namespace ops {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T, typename = void>
|
||||
struct has_shape : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct has_shape<T, std::void_t<decltype(std::declval<T>().shape())>> : std::true_type {};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename Op>
|
||||
struct generic_binary_op {
|
||||
template <typename L, typename R>
|
||||
@@ -26,8 +36,9 @@ struct generic_binary_op {
|
||||
template <typename L, typename R>
|
||||
static constexpr auto shape(const L& lhs, const R& rhs) {
|
||||
if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands");
|
||||
// TODO: introduce "best shape" function/trait
|
||||
if constexpr (detail::is_static_shape_v<decltype(lhs.shape())>) {
|
||||
using best_shape = cenet::math::detail::best_shape<decltype(shape_wrapper(lhs)), decltype(shape_wrapper(rhs))>;
|
||||
static_assert(best_shape::valid, "uncompatible operands");
|
||||
if constexpr (best_shape::left) {
|
||||
return lhs.shape();
|
||||
} else {
|
||||
return rhs.shape();
|
||||
@@ -44,6 +55,15 @@ struct generic_binary_op {
|
||||
return Op{}(lhs.coeff(index), rhs.coeff(index));
|
||||
}
|
||||
}
|
||||
private:
|
||||
template <typename T>
|
||||
static constexpr auto shape_wrapper(T&& fwd) {
|
||||
if constexpr (detail::has_shape<T>::value) {
|
||||
return std::forward<T>(fwd).shape();
|
||||
} else {
|
||||
return std::forward<T>(fwd);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct plus : generic_binary_op<std::plus<>> {};
|
||||
|
||||
@@ -40,6 +40,8 @@ struct static_shape : shape<Dims...> {
|
||||
constexpr auto get() const {
|
||||
return get_impl<I, Dims...>::value;
|
||||
}
|
||||
|
||||
static constexpr auto score = sizeof...(Dims);
|
||||
private:
|
||||
template <typename T>
|
||||
struct compatible_impl : std::false_type {};
|
||||
@@ -105,6 +107,16 @@ private:
|
||||
struct get_impl<0, First, Rest...> {
|
||||
static constexpr auto value = First;
|
||||
};
|
||||
|
||||
template <std::size_t Score, std::size_t...>
|
||||
struct score_impl {
|
||||
static constexpr auto value = Score;
|
||||
};
|
||||
|
||||
template <std::size_t Score, std::size_t First, std::size_t... Rest>
|
||||
struct score_impl<Score, First, Rest...> : score_impl<Score + ((First != dynamic_extent) ? 1 : 0), Rest...> {};
|
||||
public:
|
||||
static constexpr auto score = score_impl<0, Dims...>::value;
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
@@ -144,6 +156,35 @@ struct is_shape_equivalent<L<LFirst, LRest...>, R<RFirst, RRest...>>
|
||||
template <template <std::size_t...> class L, template <std::size_t...> class R>
|
||||
struct is_shape_equivalent<L<>, R<>> : std::true_type {};
|
||||
|
||||
template <typename L, typename R, typename = void>
|
||||
struct best_shape {};
|
||||
|
||||
template <template <std::size_t...> class T, std::size_t... Dims, typename O>
|
||||
struct best_shape<T<Dims...>, O> {
|
||||
using type = T<Dims...>;
|
||||
|
||||
static constexpr bool valid = true;
|
||||
static constexpr bool left = true;
|
||||
};
|
||||
|
||||
template <template <std::size_t...> class T, std::size_t... Dims, typename O>
|
||||
struct best_shape<O, T<Dims...>> {
|
||||
using type = T<Dims...>;
|
||||
|
||||
static constexpr bool valid = true;
|
||||
static constexpr bool left = false;
|
||||
};
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
std::size_t... LDims,
|
||||
template <std::size_t...> class R,
|
||||
std::size_t... RDims>
|
||||
struct best_shape<L<LDims...>, R<RDims...>> {
|
||||
static constexpr bool valid = sizeof...(LDims) == sizeof...(RDims);
|
||||
static constexpr bool left = L<RDims...>::score >= R<LDims...>::score;
|
||||
using type = std::enable_if_t<valid, std::conditional_t<left, L<LDims...>, R<RDims...>>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
|
||||
Reference in New Issue
Block a user