10#ifndef XTENSOR_FIXED_HPP
11#define XTENSOR_FIXED_HPP
19#include <xtl/xsequence.hpp>
21#include "../containers/xcontainer.hpp"
22#include "../containers/xstorage.hpp"
23#include "../core/xsemantic.hpp"
24#include "../core/xstrides.hpp"
25#include "../core/xtensor_config.hpp"
31 template <
class T, std::
size_t N>
32 struct sequence_builder<xt::const_array<T, N>>
34 using sequence_type = xt::const_array<T, N>;
35 using value_type =
typename sequence_type::value_type;
36 using size_type =
typename sequence_type::size_type;
38 inline static sequence_type make(size_type , value_type )
40 return sequence_type();
53 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
96 template <
layout_type L, std::size_t I, std::size_t... X>
97 struct calculate_stride;
99 template <std::size_t I, std::size_t Y, std::size_t... X>
102 static constexpr std::ptrdiff_t value = Y
106 template <std::size_t Y, std::size_t... X>
109 static constexpr std::ptrdiff_t value = 1;
112 template <std::size_t I, std::size_t... X>
113 struct calculate_stride_row_major
115 static constexpr std::ptrdiff_t value = at<
sizeof...(X) - I, X...>::value
116 * calculate_stride_row_major<I - 1, X...>::value;
119 template <std::size_t... X>
120 struct calculate_stride_row_major<0, X...>
122 static constexpr std::ptrdiff_t value = 1;
125 template <std::size_t I, std::size_t... X>
128 static constexpr std::ptrdiff_t value = calculate_stride_row_major<
sizeof...(X) - I - 1, X...>::value;
133 template <layout_type L,
size_t I,
class SEQ>
134 struct computed_strides;
137 struct computed_strides<L, I, std::index_sequence<X...>>
139 static constexpr std::ptrdiff_t value = calculate_stride<L, I, X...>::value;
142 template <layout_type L,
size_t I,
class SEQ>
143 constexpr std::ptrdiff_t get_computed_strides(
bool cond)
145 return cond ? 0 : computed_strides<L, I, SEQ>::value;
149 template <
layout_type L,
class R, std::size_t... X, std::size_t... I>
150 constexpr R get_strides_impl(
const xt::fixed_shape<X...>& shape, std::index_sequence<I...>)
154 "Layout not supported for fixed array"
156#if (_MSC_VER >= 1910)
157 using temp_type = std::index_sequence<X...>;
158 return R({workaround::get_computed_strides<L, I, temp_type>(shape[I] == 1)...});
160 return R({shape[I] == 1 ? 0 : calculate_stride<L, I, X...>::value...});
164 template <
class S,
class T, std::size_t... I>
165 constexpr T get_backstrides_impl(
const S& shape,
const T&
strides, std::index_sequence<I...>)
167 return T({(
strides[I] * std::ptrdiff_t(shape[I] - 1))...});
170 template <std::size_t... X>
171 struct fixed_compute_size_impl;
173 template <std::size_t Y, std::size_t... X>
174 struct fixed_compute_size_impl<Y, X...>
176 static constexpr std::size_t value = Y * fixed_compute_size_impl<X...>::value;
179 template <std::
size_t X>
180 struct fixed_compute_size_impl<X>
182 static constexpr std::size_t value = X;
186 struct fixed_compute_size_impl<>
189 static constexpr std::size_t value = 1;
194 struct fixed_compute_size;
196 template <std::size_t... X>
197 struct fixed_compute_size<xt::fixed_shape<X...>>
199 static constexpr std::size_t value = fixed_compute_size_impl<X...>::value;
202 template <
class V, std::size_t... X>
203 struct get_init_type_impl;
205 template <
class V, std::
size_t Y>
206 struct get_init_type_impl<V, Y>
212 struct get_init_type_impl<V>
217 template <
class V, std::size_t Y, std::size_t... X>
218 struct get_init_type_impl<V, Y, X...>
220 using tmp_type =
typename get_init_type_impl<V, X...>::type;
221 using type = tmp_type[Y];
225 template <
layout_type L,
class R, std::size_t... X>
228 return detail::get_strides_impl<L, R>(shape, std::make_index_sequence<
sizeof...(X)>{});
231 template <
class S,
class T>
232 constexpr T get_backstrides(
const S& shape,
const T&
strides)
noexcept
234 return detail::get_backstrides_impl(shape,
strides, std::make_index_sequence<std::tuple_size<T>::value>{});
237 template <
class V,
class S>
240 template <
class V, std::size_t... X>
243 using type =
typename detail::get_init_type_impl<V, X...>::type;
246 template <
class V,
class S>
249 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
252 using shape_type = S;
253 using inner_shape_type =
typename S::cast_type;
254 using strides_type = get_strides_t<inner_shape_type>;
255 using inner_strides_type = strides_type;
256 using backstrides_type = inner_strides_type;
257 using inner_backstrides_type = backstrides_type;
260#if defined(_MSC_VER) && _MSC_VER < 1910 && !defined(_WIN64)
262 using storage_type = std::array<ET, detail::fixed_compute_size<S>::value>;
267 using reference =
typename storage_type::reference;
268 using const_reference =
typename storage_type::const_reference;
269 using size_type =
typename storage_type::size_type;
274 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
295 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
296 class xfixed_container :
public xcontainer<xfixed_container<ET, S, L, SH, Tag>>,
297 public xcontainer_semantic<xfixed_container<ET, S, L, SH, Tag>>
301 using self_type = xfixed_container<ET, S, L, SH, Tag>;
302 using base_type = xcontainer<self_type>;
303 using semantic_base = xcontainer_semantic<self_type>;
305 using storage_type =
typename base_type::storage_type;
306 using value_type =
typename base_type::value_type;
307 using reference =
typename base_type::reference;
308 using const_reference =
typename base_type::const_reference;
309 using pointer =
typename base_type::pointer;
310 using const_pointer =
typename base_type::const_pointer;
311 using shape_type =
typename base_type::shape_type;
312 using inner_shape_type =
typename base_type::inner_shape_type;
313 using strides_type =
typename base_type::strides_type;
314 using backstrides_type =
typename base_type::backstrides_type;
315 using inner_backstrides_type =
typename base_type::inner_backstrides_type;
316 using inner_strides_type =
typename base_type::inner_strides_type;
317 using temporary_type =
typename semantic_base::temporary_type;
318 using expression_tag = Tag;
320 static constexpr std::size_t N = std::tuple_size<shape_type>::value;
321 static constexpr std::size_t rank = N;
323 xfixed_container() =
default;
324 xfixed_container(
const value_type& v);
328 template <
class IX = std::
integral_constant<std::
size_t, N>>
330 requires(IX::value != 0);
332 ~xfixed_container() =
default;
334 xfixed_container(
const xfixed_container&) =
default;
335 xfixed_container& operator=(
const xfixed_container&) =
default;
337 xfixed_container(xfixed_container&&) =
default;
338 xfixed_container& operator=(xfixed_container&&) =
default;
346 template <
class ST = std::array<std::
size_t, N>>
347 static xfixed_container from_shape(ST&& );
349 template <
class ST = std::array<std::
size_t, N>>
351 template <
class ST = shape_type>
353 template <
class ST = shape_type>
356 template <
class ST = std::array<std::
size_t, N>>
360 bool broadcast_shape(ST& s,
bool reuse_cache =
false)
const;
363 bool is_contiguous() const noexcept;
367 storage_type m_storage;
369 XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_shape_type m_shape = S();
370 XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_strides_type m_strides = get_strides<L, inner_strides_type>(S());
371 XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_backstrides_type
372 m_backstrides = get_backstrides(m_shape, m_strides);
374 storage_type& storage_impl() noexcept;
375 const storage_type& storage_impl() const noexcept;
377 XTENSOR_CONSTEXPR_RETURN const inner_shape_type& shape_impl() const noexcept;
378 XTENSOR_CONSTEXPR_RETURN const inner_strides_type& strides_impl() const noexcept;
379 XTENSOR_CONSTEXPR_RETURN const inner_backstrides_type& backstrides_impl() const noexcept;
381 friend class xcontainer<xfixed_container<ET, S, L, SH, Tag>>;
388 template <class EC, class S,
layout_type L,
bool SH, class Tag>
391 template <class EC, class S,
layout_type L,
bool SH, class Tag>
394 using storage_type = std::remove_reference_t<EC>;
395 using reference =
typename storage_type::reference;
396 using const_reference =
typename storage_type::const_reference;
397 using size_type =
typename storage_type::size_type;
398 using shape_type = S;
399 using inner_shape_type =
typename S::cast_type;
400 using strides_type = get_strides_t<inner_shape_type>;
401 using backstrides_type = strides_type;
402 using inner_strides_type = strides_type;
403 using inner_backstrides_type = backstrides_type;
408 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
430 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
432 public xcontainer_semantic<xfixed_adaptor<EC, S, L, SH, Tag>>
436 using container_closure_type = EC;
439 using base_type = xcontainer<self_type>;
440 using semantic_base = xcontainer_semantic<self_type>;
441 using storage_type =
typename base_type::storage_type;
442 using shape_type =
typename base_type::shape_type;
443 using strides_type =
typename base_type::strides_type;
444 using backstrides_type =
typename base_type::backstrides_type;
445 using inner_shape_type =
typename base_type::inner_shape_type;
446 using inner_strides_type =
typename base_type::inner_strides_type;
447 using inner_backstrides_type =
typename base_type::inner_backstrides_type;
448 using temporary_type =
typename semantic_base::temporary_type;
449 using expression_tag = Tag;
451 static constexpr std::size_t N = S::size();
471 template <
class ST = std::array<std::
size_t, N>>
473 template <
class ST = shape_type>
475 template <
class ST = shape_type>
478 template <
class ST = std::array<std::
size_t, N>>
482 bool broadcast_shape(ST& s,
bool reuse_cache =
false)
const;
485 bool is_contiguous()
const noexcept;
489 container_closure_type m_storage;
491 XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_shape_type m_shape = S();
492 XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_strides_type m_strides = get_strides<L, inner_strides_type>(S());
493 XTENSOR_CONSTEXPR_ENHANCED_STATIC inner_backstrides_type
494 m_backstrides = get_backstrides(m_shape, m_strides);
496 storage_type& storage_impl()
noexcept;
497 const storage_type& storage_impl()
const noexcept;
499 XTENSOR_CONSTEXPR_RETURN
const inner_shape_type& shape_impl()
const noexcept;
500 XTENSOR_CONSTEXPR_RETURN
const inner_strides_type& strides_impl()
const noexcept;
501 XTENSOR_CONSTEXPR_RETURN
const inner_backstrides_type& backstrides_impl()
const noexcept;
523 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
524 inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(
const inner_shape_type&
shape,
layout_type l)
528 XTENSOR_ASSERT(
shape.size() == N && std::equal(
shape.begin(),
shape.end(), m_shape.begin()));
529 XTENSOR_ASSERT(L == l);
532 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
533 inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(
const value_type& v)
535 if (this->
size() != 1)
537 XTENSOR_THROW(std::runtime_error,
"wrong shape for scalar assignment (has to be xshape<>).");
551 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
552 inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(
553 const inner_shape_type&
shape,
560 XTENSOR_ASSERT(
shape.size() == N && std::equal(
shape.begin(),
shape.end(), m_shape.begin()));
561 XTENSOR_ASSERT(L == l);
562 std::fill(m_storage.begin(), m_storage.end(), v);
567 template <std::
size_t X>
568 struct check_initializer_list_shape
570 template <
class T,
class S>
571 static bool run(
const T& t,
const S& shape)
573 std::size_t IX = shape.size() - X;
574 bool result = (shape[IX] == t.size());
575 for (std::size_t i = 0; i < shape[IX]; ++i)
577 result = result && check_initializer_list_shape<X - 1>::run(t.begin()[i], shape);
584 struct check_initializer_list_shape<0>
586 template <
class T,
class S>
587 static bool run(
const T& ,
const S& )
594 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
596 inline xfixed_container<ET, S, L, SH, Tag> xfixed_container<ET, S, L, SH, Tag>::from_shape(ST&&
shape)
600 XTENSOR_ASSERT(
shape.size() == N && std::equal(
shape.begin(),
shape.end(), tmp.shape().begin()));
611 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
613 inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(nested_initializer_list_t<value_type, N> t)
614 requires(IX::value != 0)
617 detail::check_initializer_list_shape<N>::run(t, this->
shape()) ==
true,
618 "initializer list shape does not match fixed shape"
621 L == tmp ? nested_copy(m_storage.begin(), t) : nested_copy(this->
template begin<tmp>(), t);
633 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
635 inline xfixed_container<ET, S, L, SH, Tag>::xfixed_container(
const xexpression<E>& e)
637 semantic_base::assign(e);
643 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
645 inline auto xfixed_container<ET, S, L, SH, Tag>::operator=(
const xexpression<E>& e) -> self_type&
647 return semantic_base::operator=(e);
656 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
661 XTENSOR_ASSERT(std::equal(
shape.begin(),
shape.end(), m_shape.begin()) &&
shape.size() == m_shape.size());
668 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
675 std::equal(
shape.begin(),
shape.end(), m_shape.begin()) &&
shape.size() == m_shape.size() && L == l
683 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
689 XTENSOR_ASSERT(std::equal(
shape.begin(),
shape.end(), m_shape.begin()) &&
shape.size() == m_shape.size());
698 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
702 if (!(std::equal(
shape.begin(),
shape.end(), m_shape.begin()) &&
shape.size() == m_shape.size()
705 XTENSOR_THROW(std::runtime_error,
"Trying to reshape xtensor_fixed with different shape or layout.");
710 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
712 inline bool xfixed_container<ET, S, L, SH, Tag>::broadcast_shape(ST& shape,
bool)
const
714 return xt::broadcast_shape(m_shape, shape);
717 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
718 constexpr layout_type xfixed_container<ET, S, L, SH, Tag>::layout() const noexcept
720 return base_type::static_layout;
723 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
724 inline bool xfixed_container<ET, S, L, SH, Tag>::is_contiguous() const noexcept
726 using str_type =
typename inner_strides_type::value_type;
731 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
732 inline auto xfixed_container<ET, S, L, SH, Tag>::storage_impl() noexcept -> storage_type&
737 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
738 inline auto xfixed_container<ET, S, L, SH, Tag>::storage_impl() const noexcept -> const storage_type&
743 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
744 XTENSOR_CONSTEXPR_RETURN
auto xfixed_container<ET, S, L, SH, Tag>::shape_impl() const noexcept
745 -> const inner_shape_type&
750 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
751 XTENSOR_CONSTEXPR_RETURN
auto xfixed_container<ET, S, L, SH, Tag>::strides_impl() const noexcept
752 -> const inner_strides_type&
757 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
758 XTENSOR_CONSTEXPR_RETURN
auto xfixed_container<ET, S, L, SH, Tag>::backstrides_impl() const noexcept
759 -> const inner_backstrides_type&
761 return m_backstrides;
776 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
779 , m_storage(std::move(
data))
787 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
799 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
803 , m_storage(std::forward<D>(
data))
809 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
810 inline auto xfixed_adaptor<EC, S, L, SH, Tag>::operator=(
const xfixed_adaptor& rhs) -> self_type&
812 base_type::operator=(rhs);
813 m_storage = rhs.m_storage;
817 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
818 inline auto xfixed_adaptor<EC, S, L, SH, Tag>::operator=(xfixed_adaptor&& rhs) -> self_type&
820 base_type::operator=(std::move(rhs));
821 m_storage = rhs.m_storage;
825 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
826 inline auto xfixed_adaptor<EC, S, L, SH, Tag>::operator=(
temporary_type&& rhs) -> self_type&
828 m_storage.resize(rhs.storage().size());
829 std::copy(rhs.storage().cbegin(), rhs.storage().cend(), m_storage.begin());
840 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
842 inline auto xfixed_adaptor<EC, S, L, SH, Tag>::operator=(
const xexpression<E>& e) -> self_type&
844 return semantic_base::operator=(e);
853 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
858 XTENSOR_ASSERT(std::equal(
shape.begin(),
shape.end(), m_shape.begin()) &&
shape.size() == m_shape.size());
865 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
872 std::equal(
shape.begin(),
shape.end(), m_shape.begin()) &&
shape.size() == m_shape.size() && L == l
880 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
886 XTENSOR_ASSERT(std::equal(
shape.begin(),
shape.end(), m_shape.begin()) &&
shape.size() == m_shape.size());
895 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
899 if (!(std::equal(
shape.begin(),
shape.end(), m_shape.begin()) &&
shape.size() == m_shape.size()
902 XTENSOR_THROW(std::runtime_error,
"Trying to reshape xtensor_fixed with different shape or layout.");
907 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
909 inline bool xfixed_adaptor<ET, S, L, SH, Tag>::broadcast_shape(ST& shape,
bool)
const
911 return xt::broadcast_shape(m_shape, shape);
914 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
915 inline auto xfixed_adaptor<EC, S, L, SH, Tag>::storage_impl() noexcept -> storage_type&
920 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
921 inline auto xfixed_adaptor<EC, S, L, SH, Tag>::storage_impl() const noexcept -> const storage_type&
926 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
927 constexpr layout_type xfixed_adaptor<EC, S, L, SH, Tag>::layout() const noexcept
929 return base_type::static_layout;
932 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
933 inline bool xfixed_adaptor<EC, S, L, SH, Tag>::is_contiguous() const noexcept
935 using str_type =
typename inner_strides_type::value_type;
940 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
941 XTENSOR_CONSTEXPR_RETURN
auto xfixed_adaptor<EC, S, L, SH, Tag>::shape_impl() const noexcept
942 -> const inner_shape_type&
947 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
948 XTENSOR_CONSTEXPR_RETURN
auto xfixed_adaptor<EC, S, L, SH, Tag>::strides_impl() const noexcept
949 -> const inner_strides_type&
954 template <
class EC,
class S, layout_type L,
bool SH,
class Tag>
955 XTENSOR_CONSTEXPR_RETURN
auto xfixed_adaptor<EC, S, L, SH, Tag>::backstrides_impl() const noexcept
956 -> const inner_backstrides_type&
958 return m_backstrides;
This array class is modeled after std::array but adds optional alignment through a template parameter...
Fixed shape implementation for compile time defined arrays.
size_type size() const noexcept
constexpr const inner_strides_type & strides() const noexcept
Returns the strides of the container.
constexpr const inner_shape_type & shape() const noexcept
Returns the shape of the container.
auto begin() noexcept -> select_iterator< L >
Base class for xexpressions.
Dense multidimensional container adaptor with tensor semantic and fixed dimension.
xfixed_adaptor(storage_type &&data)
Constructs an xfixed_adaptor of the given stl-like container.
const auto & reshape(ST &&shape, layout_type layout=L) const
Note that the xfixed_container cannot be reshaped to a shape different from S.
void resize(ST &&shape, bool force=false) const
Note that the xfixed_adaptor cannot be resized.
Dense multidimensional container with tensor semantic and fixed dimension.
xfixed_container(const inner_shape_type &shape, layout_type l=L)
Create an uninitialized xfixed_container.
xfixed_container(const xexpression< E > &e)
The extended copy constructor.
void resize(ST &&shape, bool force=false) const
Note that the xfixed_container cannot be resized.
void resize(ST &&shape, const strides_type &strides) const
Note that the xfixed_container cannot be resized.
xfixed_container(const inner_shape_type &shape, value_type v, layout_type l=L)
Create an xfixed_container, and initialize with the value of v.
void resize(ST &&shape, layout_type l) const
Note that the xfixed_container cannot be resized.
xfixed_container(nested_initializer_list_t< value_type, N > t)
Allocates an xfixed_container with shape S with values from a C array.
const auto & reshape(ST &&shape, layout_type layout=L) const
Note that the xfixed_container cannot be reshaped to a shape different from S.
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
standard mathematical functions for xexpressions