10#ifndef XTENSOR_UTILS_HPP
11#define XTENSOR_UTILS_HPP
16#include <initializer_list>
24#include <xtl/xfunctional.hpp>
25#include <xtl/xmeta_utils.hpp>
26#include <xtl/xsequence.hpp>
27#include <xtl/xtype_traits.hpp>
29#include "../core/xtensor_config.hpp"
46 template <std::size_t I,
class... Args>
47 constexpr decltype(
auto) argument(Args&&... args)
noexcept;
49 template <
class R,
class F,
class... S>
50 R apply(std::size_t index, F&& func,
const std::tuple<S...>& s)
noexcept(
noexcept(func(std::get<0>(s))));
52 template <
class T,
class S>
53 void nested_copy(T&& iter,
const S& s);
55 template <
class T,
class S>
56 void nested_copy(T&& iter, std::initializer_list<S> s);
59 bool resize_container(C& c,
typename C::size_type size);
61 template <
class T, std::
size_t N>
62 bool resize_container(std::array<T, N>& a,
typename std::array<T, N>::size_type size);
64 template <std::size_t... I>
67 template <std::size_t... I>
70 template <
class X,
class C>
73 template <
class X,
class C>
76 std::size_t normalize_axis(std::size_t dim, std::ptrdiff_t axis);
87 using void_t =
typename make_void<T...>::type;
101 template <
class T,
class R>
102 using disable_integral_t = std::enable_if_t<!xtl::is_integral<T>::value, R>;
118 template <
template <
class...>
class TT,
class T>
123 template <
template <
class...>
class TT,
class... Ts>
137 template <
class C,
class R,
class... Args>
140 typedef R type(Args...);
143 template <
class C,
class R,
class... Args>
146 typedef R type(Args...);
158 template <
class F,
size_t... I,
class... Ts>
159 void for_each(F&& f, std::tuple<Ts...>& t, std::index_sequence<I...>)
noexcept(
160 (
noexcept(f(std::get<I>(t))) && ...)
163 (f(std::get<I>(t)), ...);
166 template <
class F,
size_t... I,
class... Ts>
167 void for_each(F&& f,
const std::tuple<Ts...>& t, std::index_sequence<I...>)
noexcept(
168 (
noexcept(f(std::get<I>(t))) && ...)
171 (f(std::get<I>(t)), ...);
175 template <
class F,
class... Ts>
176 inline void for_each(F&& f, std::tuple<Ts...>& t)
noexcept(
177 noexcept(detail::for_each(std::forward<F>(f), t, std::make_index_sequence<
sizeof...(Ts)>{}))
180 detail::for_each(std::forward<F>(f), t, std::make_index_sequence<
sizeof...(Ts)>{});
183 template <
class F,
class... Ts>
184 inline void for_each(F&& f,
const std::tuple<Ts...>& t)
noexcept(
185 noexcept(detail::for_each(std::forward<F>(f), t, std::make_index_sequence<
sizeof...(Ts)>{}))
188 detail::for_each(std::forward<F>(f), t, std::make_index_sequence<
sizeof...(Ts)>{});
199 template <
class F,
class R,
class... T,
size_t... I>
200 R accumulate_impl(F&& f, R init,
const std::tuple<T...>& t, std::index_sequence<I...> )
noexcept(
201 (
noexcept(f(init, std::get<I>(t))) && ...)
205 auto wrapper = [&](
const auto& i,
const auto& j)
209 (wrapper(res, std::get<I>(t)), ...);
214 template <
class F,
class R,
class... T>
215 inline R
accumulate(F&& f, R init,
const std::tuple<T...>& t)
noexcept(
216 noexcept(detail::accumulate_impl(std::forward<F>(f), init, t, std::make_index_sequence<
sizeof...(T)>{}))
219 return detail::accumulate_impl(std::forward<F>(f), init, t, std::make_index_sequence<
sizeof...(T)>{});
230 template <std::
size_t I>
233 template <
class Arg,
class... Args>
234 static constexpr decltype(
auto) get(Arg&& , Args&&... args)
noexcept
236 return getter<I - 1>::get(std::forward<Args>(args)...);
243 template <
class Arg,
class... Args>
244 static constexpr Arg&& get(Arg&&
arg, Args&&... ) noexcept
246 return std::forward<Arg>(
arg);
251 template <std::size_t I,
class... Args>
252 constexpr decltype(
auto) argument(Args&&... args)
noexcept
254 static_assert(I <
sizeof...(Args),
"I should be lesser than sizeof...(Args)");
255 return detail::getter<I>::get(std::forward<Args>(args)...);
262 template <
class R,
class F,
class... S>
264 apply(std::size_t index, F&& func,
const std::tuple<S...>& s)
noexcept(
noexcept(func(std::get<0>(s))))
266 XTENSOR_ASSERT(
sizeof...(S) > index);
268 [&](
const S&... args) -> R
270 auto f_impl = [&](
auto&& self,
auto&& i,
auto&& h,
auto&&... t) -> R
274 return static_cast<R
>(func(h));
276 if constexpr (
sizeof...(t) > 0)
278 return self(self, std::size_t{i + 1}, t...);
282 return f_impl(f_impl, std::size_t{0}, args...);
292 template <
class T, std::
size_t I>
304 template <
class T, std::
size_t I>
305 using nested_initializer_list_t =
typename nested_initializer_list<T, I>::type;
311 template <
class T,
class S>
312 inline void nested_copy(T&& iter,
const S& s)
317 template <
class T,
class S>
318 inline void nested_copy(T&& iter, std::initializer_list<S> s)
320 for (
auto it = s.begin(); it != s.end(); ++it)
322 nested_copy(std::forward<T>(iter), *it);
330 inline bool resize_container(C& c,
typename C::size_type size)
336 template <
class T, std::
size_t N>
337 inline bool resize_container(std::array<T, N>& ,
typename std::array<T, N>::size_type size)
342 template <std::size_t... I>
343 inline bool resize_container(xt::fixed_shape<I...>&, std::size_t size)
345 return sizeof...(I) == size;
353 inline std::size_t normalize_axis(std::size_t dim, std::ptrdiff_t axis)
355 return axis < 0 ? static_cast<std::size_t>(
static_cast<std::ptrdiff_t
>(dim) + axis)
356 : static_cast<std::size_t>(axis);
359 template <
class E,
class C>
360 inline std::enable_if_t<
361 !xtl::is_integral<std::decay_t<C>>::value && xtl::is_signed<typename std::decay_t<C>::value_type>::value,
362 rebind_container_t<std::size_t, std::decay_t<C>>>
363 normalize_axis(E& expr, C&& axes)
365 rebind_container_t<std::size_t, std::decay_t<C>> res;
366 resize_container(res, axes.size());
368 for (std::size_t i = 0; i < axes.size(); ++i)
370 res[i] = normalize_axis(expr.dimension(), axes[i]);
373 XTENSOR_ASSERT(std::all_of(
378 return ax_el < expr.dimension();
385 template <
class C,
class E>
386 inline std::enable_if_t<
387 !xtl::is_integral<std::decay_t<C>>::value && std::is_unsigned<typename std::decay_t<C>::value_type>::value,
389 normalize_axis(E& expr, C&& axes)
391 static_cast<void>(expr);
392 XTENSOR_ASSERT(std::all_of(
397 return ax_el < expr.dimension();
400 return std::forward<C>(axes);
403 template <
class R,
class E,
class C>
404 inline auto forward_normalize(E& expr, C&& axes)
405 -> std::enable_if_t<xtl::is_signed<std::decay_t<
decltype(*std::begin(axes))>>::value, R>
408 xt::resize_container(res, std::size(axes));
409 auto dim = expr.dimension();
416 return normalize_axis(dim, ax_el);
420 XTENSOR_ASSERT(std::all_of(
425 return ax_el < expr.dimension();
432 template <
class R,
class E,
class C>
433 inline auto forward_normalize(E& expr, C&& axes) -> std::enable_if_t<
434 !xtl::is_signed<std::decay_t<
decltype(*std::begin(axes))>>::value && !std::is_same<R, std::decay_t<C>>::value,
437 static_cast<void>(expr);
440 xt::resize_container(res, std::size(axes));
441 std::copy(std::begin(axes), std::end(axes), std::begin(res));
442 XTENSOR_ASSERT(std::all_of(
447 return ax_el < expr.dimension();
453 template <
class R,
class E,
class C>
454 inline auto forward_normalize(E& expr, C&& axes) -> std::enable_if_t<
455 !xtl::is_signed<std::decay_t<
decltype(*std::begin(axes))>>::value && std::is_same<R, std::decay_t<C>>::value,
458 static_cast<void>(expr);
459 XTENSOR_ASSERT(std::all_of(
464 return ax_el < expr.dimension();
467 return std::move(axes);
474 template <
class T,
class =
void_t<>>
483 using type =
typename T::value_type;
487 using get_value_type_t =
typename get_value_type<T>::type;
495 template <std::size_t I,
template <
typename... Args>
class T, typename... Args>
496 decltype(auto) get(T<Args...>&& v)
498 return std::get<I>(
static_cast<std::tuple<Args...
>&&>(v));
501 template <std::size_t I,
template <
typename... Args>
class T, typename... Args>
502 decltype(auto) get(T<Args...>& v)
504 return std::get<I>(
static_cast<std::tuple<Args...
>&>(v));
507 template <std::size_t I,
template <
typename... Args>
class T, typename... Args>
508 decltype(auto) get(const T<Args...>& v)
510 return std::get<I>(
static_cast<const std::tuple<Args...
>&>(v));
519 template <
class T, std::size_t N, std::size_t... I>
520 constexpr std::array<std::remove_cv_t<T>, N> to_array_impl(T (&a)[N], std::index_sequence<I...>)
526 template <
class T, std::
size_t N>
527 constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N])
529 return detail::to_array_impl(a, std::make_index_sequence<N>{});
536 template <
class T,
class =
void>
547 std::is_same<typename std::remove_cv<typename xcontainer_inner_types<T>::storage_type>::type, invalid_type>>
555 template <
class E,
class =
void>
568 template <
class E,
class =
void>
578 template <
class E,
class =
void>
595 template <
class E,
class =
void>
604 decltype(*std::declval<const E>(), std::declval<const E>() == std::declval<const E>(), std::declval<const E>() != std::declval<const E>(), ++(*std::declval<E*>()), (*std::declval<E*>())++, std::true_type())>>
609 template <
typename E>
616 template <
bool condition,
class T>
628 inline auto operator()(U&& u)
const
630 return static_cast<T
>(std::forward<U>(u));
642 template <
bool condition,
class T,
class U>
652 namespace alloc_tracking
654 inline bool& enabled()
665 inline void disable()
677 template <
class T,
class A, alloc_tracking::policy P>
678 struct tracking_allocator :
private A
681 using value_type =
typename A::value_type;
682 using reference = value_type&;
683 using const_reference =
const value_type&;
684 using pointer =
typename std::allocator_traits<A>::pointer;
685 using const_pointer =
typename std::allocator_traits<A>::const_pointer;
686 using size_type =
typename std::allocator_traits<A>::size_type;
687 using difference_type =
typename std::allocator_traits<A>::difference_type;
689 tracking_allocator() =
default;
691 T* allocate(std::size_t n)
693 if (alloc_tracking::enabled())
695 if (P == alloc_tracking::print)
697 std::cout <<
"xtensor allocating: " << n <<
"" << std::endl;
699 else if (P == alloc_tracking::assert)
703 "xtensor allocation of " + std::to_string(n) +
" elements detected"
707 return base_type::allocate(n);
710 using base_type::deallocate;
713#if ((defined(__cplusplus) && __cplusplus < 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG < 202002L))
714 using base_type::construct;
715 using base_type::destroy;
721 using traits = std::allocator_traits<A>;
722 using other = tracking_allocator<U, typename traits::template rebind_alloc<U>, P>;
726 template <
class T,
class AT, alloc_tracking::policy PT,
class U,
class AU, alloc_tracking::policy PU>
729 return std::is_same<AT, AU>::value;
732 template <
class T,
class AT, alloc_tracking::policy PT,
class U,
class AU, alloc_tracking::policy PU>
733 inline bool operator!=(
const tracking_allocator<T, AT, PT>& a,
const tracking_allocator<U, AU, PU>& b)
742 template <
class E1,
class E2,
class =
void>
747 template <
class E1,
class E2>
748 struct has_assign_to<E1, E2, void_t<decltype(std::declval<const E2&>().assign_to(std::declval<E1&>()))>>
753 template <
class E1,
class E2>
760 template <
class T,
class Enable =
void>
766 struct has_memory_address<T, void_t<decltype(std::addressof(*std::declval<T>().begin()))>> : std::true_type
770 template <
typename T>
772 template <
typename T>
779 const uintptr_t m_first = 0;
780 const uintptr_t m_last = 0;
782 explicit memory_range() =
default;
785 explicit memory_range(T* first, T* last)
786 : m_first(
reinterpret_cast<uintptr_t
>(last < first ? last : first))
787 , m_last(
reinterpret_cast<uintptr_t
>(last < first ? first : last))
792 bool overlaps(T* first, T* last)
const
796 return reinterpret_cast<uintptr_t
>(first) <= m_last
797 &&
reinterpret_cast<uintptr_t
>(last) >= m_first;
801 return reinterpret_cast<uintptr_t
>(last) <= m_last
802 &&
reinterpret_cast<uintptr_t
>(first) >= m_first;
807 template <
class E,
class Enable =
void>
810 static bool check_overlap(
const E&,
const memory_range&)
819 static bool check_overlap(
const E& expr,
const memory_range& dst_range)
821 if (expr.size() == 0)
827 return dst_range.overlaps(std::addressof(*expr.begin()), std::addressof(*expr.rbegin()));
832 struct overlapping_memory_checker_base
836 explicit overlapping_memory_checker_base() =
default;
838 explicit overlapping_memory_checker_base(
memory_range dst_memory_range)
839 : m_dst_range(std::move(dst_memory_range))
844 bool check_overlap(
const E& expr)
const
846 if (!m_dst_range.m_first || !m_dst_range.m_last)
852 return overlapping_memory_checker_traits<E>::check_overlap(expr, m_dst_range);
857 template <
class Dst,
class Enable =
void>
858 struct overlapping_memory_checker : overlapping_memory_checker_base
860 explicit overlapping_memory_checker(
const Dst&)
861 : overlapping_memory_checker_base()
867 struct overlapping_memory_checker<Dst, std::enable_if_t<has_memory_address<Dst>::value>>
868 : overlapping_memory_checker_base
870 explicit overlapping_memory_checker(
const Dst& aDst)
871 : overlapping_memory_checker_base(
874 if (aDst.size() == 0)
880 return memory_range(std::addressof(*aDst.begin()), std::addressof(*aDst.rbegin()));
889 auto make_overlapping_memory_checker(
const Dst& a_dst)
898 template <
class X,
template <
class,
class>
class C,
class T,
class A>
901 using traits = std::allocator_traits<A>;
902 using allocator =
typename traits::template rebind_alloc<X>;
903 using type = C<X, allocator>;
907#ifdef __cpp_template_template_args
908 template <
class X,
class T, std::
size_t N>
911 using type = std::array<X, N>;
914 template <
class X,
template <
class, std::
size_t>
class C,
class T, std::size_t N>
917 using type = C<X, N>;
931 template <std::size_t... I>
937 using type = std::array<std::ptrdiff_t,
sizeof...(I)>;
940 template <
class CP,
class O,
class A>
943 template <
class CP,
class O,
class A>
949 using type = std::vector<
950 typename xbuffer_adaptor<CP, O, A>::value_type,
951 typename xbuffer_adaptor<CP, O, A>::allocator_type>;
956 using get_strides_t =
typename get_strides_type<C>::type;
965 using storage_type = std::decay_t<ST>;
966 using type = std::conditional_t<
967 std::is_const<std::remove_reference_t<ST>>::value,
968 typename storage_type::const_reference,
969 typename storage_type::reference>;
973 using inner_reference_t =
typename inner_reference<ST>::type;
979 template <
class E,
typename =
void>
982 static constexpr std::size_t value = SIZE_MAX;
986 struct get_rank<E, decltype((void) E::rank, void())>
988 static constexpr std::size_t value = E::rank;
998 using type = std::integral_constant<bool, get_rank<std::decay_t<E>>::value != SIZE_MAX>;
1008 template <
class E,
size_t N>
1011 using type = std::integral_constant<bool, get_rank<std::decay_t<E>>::value == N>;
1014 template <
class E,
size_t N>
Fixed shape implementation for compile time defined arrays.
auto arg(E &&e) noexcept
Calculates the phase angle (in radians) elementwise for the complex numbers in e.
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
standard mathematical functions for xexpressions
auto accumulate(F &&f, E &&e, EVS evaluation_strategy=EVS())
Accumulate and flatten array NOTE This function is not lazy!
auto conditional_cast(U &&u)
Perform a type cast when a condition is true.