#ifndef TULIP_TENSOR_SHAPE_HPP #define TULIP_TENSOR_SHAPE_HPP #include "./tensor_fwd.hpp" #include #include #include namespace tulip { template struct shape {}; template struct static_shape; template struct dynamic_shape; template struct static_shape : shape { static_shape() = default; template = sizeof...(Dims))>> explicit static_shape(const static_shape& oth) {} template static constexpr bool compatible() { return ((Dims == OthDims) && ...); } template static constexpr bool compatible() { return compatible_impl::value; } template constexpr auto get() const { return get_impl::value; } template static constexpr auto sget() { return get_impl::value; } static constexpr auto rank = sizeof...(Dims); static constexpr auto score = sizeof...(Dims); private: template struct compatible_impl : std::false_type {}; template struct compatible_impl> : std::conditional_t<((Dims == OthDims) && ...), std::true_type, std::false_type> {}; template struct get_impl : get_impl {}; template struct get_impl<0, First, Rest...> { static constexpr auto value = First; }; private: template struct size_impl { static constexpr auto value = S; }; template struct size_impl : size_impl {}; public: static constexpr auto size = size_impl<1, Dims...>::value; template std::size_t calculate_offset(const std::tuple& offs) const { constexpr std::size_t off_count = sizeof...(Offs); static_assert(off_count > 0); static_assert(Start + off_count <= sizeof...(Dims)); return calculate_offset_impl(offs) * trailing_product(); } private: template std::size_t calculate_offset_impl(const std::tuple& offs) const { if constexpr (I == 0) { return std::get<0>(offs); } else { return (calculate_offset_impl(offs) * get()) + std::get(offs); } } template std::size_t trailing_product() const { if constexpr (I == sizeof...(Dims)) { return 1; } else { return get() * trailing_product(); } } }; template struct dynamic_shape : shape { dynamic_shape() = default; template && ...)>> explicit dynamic_shape(RDims... rtDimensions) { store_impl<0>(rtDimensions...); } template = sizeof...(Dims))>> explicit dynamic_shape(const dynamic_shape& oth) { constexpr std::size_t ext_offset = ExtRank - sizeof...(Dims); for (std::size_t i = 0; i < sizeof...(Dims); ++i) { dimensions[i] = oth.dimensions[ext_offset + i]; } } template static constexpr bool compatible() { return ((Dims == dynamic_extent || Dims == OthDims) && ...); } template static constexpr bool compatible() { return compatible_impl::value; } template constexpr auto get() const { return get_impl::value == dynamic_extent ? dimensions[I] : get_impl::value; } template static constexpr auto sget() { return get_impl::value; } std::size_t dimensions[sizeof...(Dims)]{}; private: template struct compatible_impl : std::false_type {}; template struct compatible_impl> : std::conditional_t<((Dims == dynamic_extent || Dims == OthDims) && ...), std::true_type, std::false_type> {}; template