#ifndef TULIP_TENSOR_VIEW_HPP #define TULIP_TENSOR_VIEW_HPP #include "./expr.hpp" #include "./tensor_fwd.hpp" #include "./tensor_shape.hpp" #include "./tensor_traits.hpp" #include #include #include #include #include namespace tulip { template class tensor_view : public expression_tag, public ops_provider> { static_assert(std::is_arithmetic_v); template friend class tensor; template friend class tensor_view; public: using traits = tensor_traits>; using shape_type = std::conditional_t, static_shape>; using value_type = T; public: template 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v && ...))>> decltype(auto) slice(Offs... offs) { auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...)); if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr; else return inner_view(ptr, std::index_sequence{}); } template 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v && ...))>> decltype(auto) slice(Offs... offs) const { const auto* ptr = m_view + m_shape.calculate_offset(std::make_tuple(offs...)); if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr; else return inner_view(ptr, std::index_sequence{}); } public: template >> decltype(auto) operator[](Off off) { return slice(off); } template >> decltype(auto) operator[](Off off) const { return slice(off); } public: constexpr std::size_t size() const { return size_impl(std::make_index_sequence{}); } auto dimensions() const { return dimensions_impl(std::make_index_sequence{}); } private: template constexpr std::size_t size_impl(std::index_sequence) const { return (dimension() * ... * 1); } template constexpr auto dimensions_impl(std::index_sequence) const { return std::make_tuple(dimension()...); } public: void fill(value_type fill = {}) { for (std::size_t i = 0; i < size(); ++i) { m_view[i] = fill; } } void fill(const value_type* source, std::size_t offset = 0, std::size_t length = dynamic_extent) { offset = std::min(offset, size()); std::memmove(m_view + offset, source, std::min(length, size() - offset) * sizeof(*m_view)); } void fill(const tensor_view& view) { fill(view.m_view, 0, view.size()); } template void fill_random(Dist dist) { static std::random_device s_rd; static std::mt19937 s_mt(s_rd()); for (std::size_t i = 0; i < size(); ++i) { m_view[i] = dist(s_mt); } } public: value_type& coeff(std::size_t index) { return m_view[index]; } const value_type& coeff(std::size_t index) const { return m_view[index]; } value_type* data() { return m_view; } const value_type* data() const { return m_view; } const shape_type& shape() const { return m_shape; } public: // WARN: eval_pack does not perform bounds checks! template auto eval_pack(std::size_t idx) noexcept { using scalar_type = std::remove_cv_t; return simd::detail::load(m_view + idx); } private: template auto inner_view(value_type* view, std::index_sequence) { if constexpr (Off == 0) return tensor_view{ view, m_shape }; else return inner_view(view, std::index_sequence{}); } private: template constexpr std::size_t dimension() const { if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) { return m_shape.dimensions[I]; } else { return traits::dimensions[I]; } } private: template tensor_view(value_type* view, Fwd&& shape) : m_view(view), m_shape(std::forward(shape)) {} private: value_type* m_view; shape_type m_shape; }; } // namespace tulip #endif // TULIP_TENSOR_VIEW_HPP