refactor(math): move calculate_offset to shapes
This commit is contained in:
@@ -63,6 +63,33 @@ private:
|
||||
struct get_impl<0, First, Rest...> {
|
||||
static constexpr auto value = First;
|
||||
};
|
||||
private:
|
||||
template <std::size_t S, std::size_t...>
|
||||
struct size_impl {
|
||||
static constexpr auto value = S;
|
||||
};
|
||||
|
||||
template <std::size_t S, std::size_t First, std::size_t... Rest>
|
||||
struct size_impl<S, First, Rest...> : size_impl<First * S, Rest...> {};
|
||||
public:
|
||||
static constexpr auto size = size_impl<1, Dims...>::value;
|
||||
|
||||
template <std::size_t Start = 0, typename... Offs>
|
||||
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
|
||||
static_assert(sizeof...(Offs) > 0);
|
||||
static_assert(sizeof...(Offs) <= sizeof...(Dims));
|
||||
|
||||
return calculate_offset_impl<Start, sizeof...(Dims) - sizeof...(Offs)>(offs);
|
||||
}
|
||||
private:
|
||||
template <std::size_t I, std::size_t Start, typename... Offs>
|
||||
std::size_t calculate_offset_impl(const std::tuple<Offs...>& offs) const {
|
||||
if constexpr (I == 0) {
|
||||
return std::get<0>(offs);
|
||||
} else {
|
||||
return (calculate_offset_impl<I - 1, Start>(offs) * get<Start + I>()) + std::get<I>(offs);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
@@ -141,6 +168,23 @@ private:
|
||||
public:
|
||||
static constexpr auto rank = sizeof...(Dims);
|
||||
static constexpr auto score = score_impl<0, Dims...>::value;
|
||||
|
||||
template <std::size_t Start = 0, typename... Offs>
|
||||
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
|
||||
static_assert(sizeof...(Offs) > 0);
|
||||
static_assert(sizeof...(Offs) <= sizeof...(Dims));
|
||||
|
||||
return calculate_offset_impl<Start, sizeof...(Dims) - sizeof...(Offs)>(offs);
|
||||
}
|
||||
private:
|
||||
template <std::size_t I, std::size_t Start, typename... Offs>
|
||||
std::size_t calculate_offset_impl(const std::tuple<Offs...>& offs) const {
|
||||
if constexpr (I == 0) {
|
||||
return std::get<0>(offs);
|
||||
} else {
|
||||
return (calculate_offset_impl<I - 1, Start>(offs) * get<Start + I>()) + std::get<I>(offs);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
@@ -33,7 +33,7 @@ public:
|
||||
typename = std::enable_if_t<(
|
||||
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
|
||||
decltype(auto) slice(Offs... offs) {
|
||||
auto* ptr = m_view + calculate_offset(std::make_tuple(offs...));
|
||||
auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...));
|
||||
if constexpr (sizeof...(Offs) == sizeof...(Dims))
|
||||
return *ptr;
|
||||
else
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
typename = std::enable_if_t<(
|
||||
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
|
||||
decltype(auto) slice(Offs... offs) const {
|
||||
const auto* ptr = m_view + calculate_offset(std::make_tuple(offs...));
|
||||
const auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...));
|
||||
if constexpr (sizeof...(Offs) == sizeof...(Dims))
|
||||
return *ptr;
|
||||
else
|
||||
@@ -60,19 +60,6 @@ public:
|
||||
decltype(auto) operator[](Off off) const {
|
||||
return slice(off);
|
||||
}
|
||||
private:
|
||||
template <typename... Offs>
|
||||
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
|
||||
return calculate_offset_impl<sizeof...(Offs) - 1, sizeof...(Dims) - sizeof...(Offs)>(offs);
|
||||
}
|
||||
|
||||
template <std::size_t I, std::size_t Start, typename... Offs>
|
||||
std::size_t calculate_offset_impl(const std::tuple<Offs...>& offs) const {
|
||||
if constexpr (I == 0)
|
||||
return std::get<I>(offs) * dimension<Start + I>();
|
||||
else
|
||||
return (calculate_offset_impl<I - 1, Start>(offs) + std::get<I>(offs)) * dimension<Start + I>();
|
||||
}
|
||||
public:
|
||||
constexpr std::size_t size() const { return size_impl(std::make_index_sequence<sizeof...(Dims)>{}); }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user