refactor(tulip): move cenet/math/ to tulip/

This commit is contained in:
2026-11-22 12:54:57 +01:00
parent 14292b1d97
commit dea973ddd6
19 changed files with 157 additions and 167 deletions
+143
View File
@@ -0,0 +1,143 @@
#ifndef TULIP_TENSOR_TRAITS_HPP
#define TULIP_TENSOR_TRAITS_HPP
#include "./tensor_fwd.hpp"
#include "./tensor_shape.hpp"
#include "./type_traits.hpp"
#include <type_traits>
namespace tulip {
template <typename T>
struct tensor_storage {};
template <typename T, std::size_t... Dims>
struct tensor_storage<tensor<T, Dims...>> {
static_assert(std::is_arithmetic_v<T>);
using value_type = T;
using storage_type = T*;
};
template <typename T, std::size_t... Dims>
struct tensor_storage<tensor<inline_storage<T>, Dims...>> {
static_assert(std::is_arithmetic_v<T>);
using value_type = T;
using storage_type = T[(Dims * ... * 1)];
};
template <typename T>
struct is_storage_inline : std::false_type {};
template <typename T>
struct is_storage_inline<inline_storage<T>> : std::true_type {};
template <typename T>
static constexpr auto is_storage_inline_v = is_storage_inline<T>::value;
template <typename T>
struct detach_storage {
using type = T;
};
template <typename T>
struct detach_storage<inline_storage<T>> {
using type = T;
};
template <typename T>
using detach_storage_t = typename detach_storage<T>::type;
template <typename T>
struct is_tensor : std::false_type {};
template <typename T, std::size_t... Dims>
struct is_tensor<tensor<T, Dims...>> : std::true_type {};
template <typename T>
static constexpr auto is_tensor_v = is_tensor<T>::value;
template <typename T>
struct is_tensor_dynamic : std::false_type {};
template <typename T>
struct is_tensor_dynamic<tensor<T>> : std::false_type {};
template <typename T, std::size_t First, std::size_t... Rest>
struct is_tensor_dynamic<tensor<T, First, Rest...>> : is_tensor_dynamic<tensor<T, Rest...>> {};
template <typename T, std::size_t... Rest>
struct is_tensor_dynamic<tensor<T, dynamic_extent, Rest...>> : std::true_type {};
template <typename T>
static constexpr auto is_tensor_dynamic_v = is_tensor_dynamic<T>::value;
namespace detail {
template <typename>
struct tensor_with_storage {};
template <>
struct tensor_with_storage<heap_storage_tag> {
template <typename T, std::size_t... Dims>
using type = tensor<detach_storage_t<T>, Dims...>;
};
template <>
struct tensor_with_storage<inline_storage_tag> {
template <typename T, std::size_t... Dims>
using type = tensor<inline_storage<detach_storage_t<T>>, Dims...>;
};
template <typename T, typename Shape>
struct tensor_from_shape {};
template <typename T, std::size_t... Dims>
struct tensor_from_shape<T, static_shape<Dims...>> {
using type = tensor<T, Dims...>;
};
template <typename T, std::size_t... Dims>
struct tensor_from_shape<T, dynamic_shape<Dims...>> {
using type = tensor<T, Dims...>;
};
} // namespace detail
template <typename T>
struct tensor_traits {};
template <typename T, std::size_t... Dims>
struct tensor_traits<tensor<T, Dims...>> {
using tensor_type = tensor<T, Dims...>;
using value_type = typename tensor_storage<tensor_type>::value_type;
using storage_type = typename tensor_storage<tensor_type>::storage_type;
static constexpr auto rank = sizeof...(Dims);
static constexpr auto dynamic = is_tensor_dynamic_v<tensor_type>;
static constexpr auto inline_storage = is_storage_inline_v<T>;
static_assert(!inline_storage || !dynamic, "Tensor storage set to inline with dynamic extents!");
static constexpr std::enable_if_t<(sizeof...(Dims) > 0), std::size_t[sizeof...(Dims)]> dimensions = { Dims... };
static constexpr std::size_t size = dynamic ? dynamic_extent : (Dims * ... * 1);
template <typename ST>
using with_storage_t = typename detail::tensor_with_storage<ST>::template type<T, Dims...>;
};
template <typename T, typename Shape>
constexpr decltype(auto) tensor_from_shape(const Shape& shape) {
typename detail::tensor_from_shape<T, remove_cvref_t<Shape>>::type tensor;
if constexpr (!detail::is_static_shape_v<Shape>) {
tensor.set_dimensions(shape);
}
return tensor;
}
} // namespace tulip
#endif // TULIP_TENSOR_TRAITS_HPP