xtensor
Loading...
Searching...
No Matches
xutils.hpp
1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3 * Copyright (c) QuantStack *
4 * *
5 * Distributed under the terms of the BSD 3-Clause License. *
6 * *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9
10#ifndef XTENSOR_UTILS_HPP
11#define XTENSOR_UTILS_HPP
12
13#include <algorithm>
14#include <array>
15#include <cstddef>
16#include <initializer_list>
17#include <iostream>
18#include <iterator>
19#include <memory>
20#include <tuple>
21#include <type_traits>
22#include <utility>
23#include <vector>
24
25#include <xtl/xfunctional.hpp>
26#include <xtl/xmeta_utils.hpp>
27#include <xtl/xsequence.hpp>
28#include <xtl/xtype_traits.hpp>
29
30#include "../core/xtensor_config.hpp"
31
32namespace xt
33{
34 /****************
35 * declarations *
36 ****************/
37
38 template <class T>
39 struct remove_class;
40
41 /*template <class F, class... T>
42 void for_each(F&& f, std::tuple<T...>& t) noexcept(implementation_dependent);*/
43
44 /*template <class F, class R, class... T>
45 R accumulate(F&& f, R init, const std::tuple<T...>& t) noexcept(implementation_dependent);*/
46
47 template <std::size_t I, class... Args>
48 constexpr decltype(auto) argument(Args&&... args) noexcept;
49
50 template <class R, class F, class... S>
51 R apply(std::size_t index, F&& func, const std::tuple<S...>& s) noexcept(noexcept(func(std::get<0>(s))));
52
53 template <class T, class S>
54 void nested_copy(T&& iter, const S& s);
55
56 template <class T, class S>
57 void nested_copy(T&& iter, std::initializer_list<S> s);
58
59 template <class C>
60 bool resize_container(C& c, typename C::size_type size);
61
62 template <class T, std::size_t N>
63 bool resize_container(std::array<T, N>& a, typename std::array<T, N>::size_type size);
64
65 template <std::size_t... I>
66 class fixed_shape;
67
68 template <std::size_t... I>
69 bool resize_container(fixed_shape<I...>& a, std::size_t size);
70
71 template <class X, class C>
73
74 template <class X, class C>
75 using rebind_container_t = typename rebind_container<X, C>::type;
76
77 std::size_t normalize_axis(std::size_t dim, std::ptrdiff_t axis);
78
79 // gcc 4.9 is affected by C++14 defect CGW 1558
80 // see http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558
81 template <class... T>
82 struct make_void
83 {
84 using type = void;
85 };
86
87 template <class... T>
88 using void_t = typename make_void<T...>::type;
89
90 // This is used for non existent types (e.g. storage for some expressions
91 // like generators)
93 {
94 };
95
96 template <class... T>
98 {
99 using type = invalid_type;
100 };
101
102 template <class T, class R>
103 using disable_integral_t = std::enable_if_t<!xtl::is_integral<T>::value, R>;
104
105 /***************************************
106 * is_specialization_of implementation *
107 ***************************************/
108
109 template <template <class...> class TT, class T>
110 struct is_specialization_of : std::false_type
111 {
112 };
113
114 template <template <class...> class TT, class... Ts>
115 struct is_specialization_of<TT, TT<Ts...>> : std::true_type
116 {
117 };
118
119 /*******************************
120 * remove_class implementation *
121 *******************************/
122
123 template <class T>
125 {
126 };
127
128 template <class C, class R, class... Args>
129 struct remove_class<R (C::*)(Args...)>
130 {
131 typedef R type(Args...);
132 };
133
134 template <class C, class R, class... Args>
135 struct remove_class<R (C::*)(Args...) const>
136 {
137 typedef R type(Args...);
138 };
139
140 template <class T>
141 using remove_class_t = typename remove_class<T>::type;
142
143 /***************************
144 * for_each implementation *
145 ***************************/
146
147 namespace detail
148 {
149 template <class F, size_t... I, class... Ts>
150 void for_each(F&& f, std::tuple<Ts...>& t, std::index_sequence<I...>) noexcept(
151 (noexcept(f(std::get<I>(t))) && ...)
152 )
153 {
154 (f(std::get<I>(t)), ...);
155 }
156
157 template <class F, size_t... I, class... Ts>
158 void for_each(F&& f, const std::tuple<Ts...>& t, std::index_sequence<I...>) noexcept(
159 (noexcept(f(std::get<I>(t))) && ...)
160 )
161 {
162 (f(std::get<I>(t)), ...);
163 }
164 }
165
166 template <class F, class... Ts>
167 inline void for_each(F&& f, std::tuple<Ts...>& t) noexcept(
168 noexcept(detail::for_each(std::forward<F>(f), t, std::make_index_sequence<sizeof...(Ts)>{}))
169 )
170 {
171 detail::for_each(std::forward<F>(f), t, std::make_index_sequence<sizeof...(Ts)>{});
172 }
173
174 template <class F, class... Ts>
175 inline void for_each(F&& f, const std::tuple<Ts...>& t) noexcept(
176 noexcept(detail::for_each(std::forward<F>(f), t, std::make_index_sequence<sizeof...(Ts)>{}))
177 )
178 {
179 detail::for_each(std::forward<F>(f), t, std::make_index_sequence<sizeof...(Ts)>{});
180 }
181
182 /*****************************
183 * accumulate implementation *
184 *****************************/
185
187
188 namespace detail
189 {
190 template <class F, class R, class... T, size_t... I>
191 R accumulate_impl(F&& f, R init, const std::tuple<T...>& t, std::index_sequence<I...> /*I*/) noexcept(
192 (noexcept(f(init, std::get<I>(t))) && ...)
193 )
194 {
195 R res = init;
196 ((res = f(res, std::get<I>(t))), ...);
197 return res;
198 }
199 }
200
201 template <class F, class R, class... T>
202 inline R accumulate(F&& f, R init, const std::tuple<T...>& t) noexcept(
203 noexcept(detail::accumulate_impl(std::forward<F>(f), init, t, std::make_index_sequence<sizeof...(T)>{}))
204 )
205 {
206 return detail::accumulate_impl(std::forward<F>(f), init, t, std::make_index_sequence<sizeof...(T)>{});
207 }
208
210
211 /***************************
212 * argument implementation *
213 ***************************/
214
215 namespace detail
216 {
217 template <std::size_t I>
218 struct getter
219 {
220 template <class Arg, class... Args>
221 static constexpr decltype(auto) get(Arg&& /*arg*/, Args&&... args) noexcept
222 {
223 return getter<I - 1>::get(std::forward<Args>(args)...);
224 }
225 };
226
227 template <>
228 struct getter<0>
229 {
230 template <class Arg, class... Args>
231 static constexpr Arg&& get(Arg&& arg, Args&&... /*args*/) noexcept
232 {
233 return std::forward<Arg>(arg);
234 }
235 };
236 }
237
238 template <std::size_t I, class... Args>
239 constexpr decltype(auto) argument(Args&&... args) noexcept
240 {
241 static_assert(I < sizeof...(Args), "I should be lesser than sizeof...(Args)");
242 return detail::getter<I>::get(std::forward<Args>(args)...);
243 }
244
245 /************************
246 * apply implementation *
247 ************************/
248
249 template <class R, class F, class... S>
250 inline R
251 apply(std::size_t index, F&& func, const std::tuple<S...>& s) noexcept(noexcept(func(std::get<0>(s))))
252 {
253 XTENSOR_ASSERT(sizeof...(S) > index);
254 return std::apply(
255 [&](const S&... args) -> R
256 {
257 auto f_impl = [&](auto&& self, auto&& i, auto&& h, auto&&... t) -> R
258 {
259 if (i == index)
260 {
261 return static_cast<R>(func(h));
262 }
263 if constexpr (sizeof...(t) > 0)
264 {
265 return self(self, std::size_t{i + 1}, t...);
266 }
267 return R{};
268 };
269 return f_impl(f_impl, std::size_t{0}, args...);
270 },
271 s
272 );
273 }
274
275 /***************************
276 * nested_initializer_list *
277 ***************************/
278
279 template <class T, std::size_t I>
281 {
282 using type = std::initializer_list<typename nested_initializer_list<T, I - 1>::type>;
283 };
284
285 template <class T>
287 {
288 using type = T;
289 };
290
291 template <class T, std::size_t I>
292 using nested_initializer_list_t = typename nested_initializer_list<T, I>::type;
293
294 /******************************
295 * nested_copy implementation *
296 ******************************/
297
298 template <class T, class S>
299 inline void nested_copy(T&& iter, const S& s)
300 {
301 *iter++ = s;
302 }
303
304 template <class T, class S>
305 inline void nested_copy(T&& iter, std::initializer_list<S> s)
306 {
307 for (auto it = s.begin(); it != s.end(); ++it)
308 {
309 nested_copy(std::forward<T>(iter), *it);
310 }
311 }
312
313 /***********************************
314 * resize_container implementation *
315 ***********************************/
316 template <class C>
317 inline bool resize_container(C& c, typename C::size_type size)
318 {
319 c.resize(size);
320 return true;
321 }
322
323 template <class T, std::size_t N>
324 inline bool resize_container(std::array<T, N>& /*a*/, typename std::array<T, N>::size_type size)
325 {
326 return size == N;
327 }
328
329 template <std::size_t... I>
330 inline bool resize_container(xt::fixed_shape<I...>&, std::size_t size)
331 {
332 return sizeof...(I) == size;
333 }
334
335 /*********************************
336 * normalize_axis implementation *
337 *********************************/
338
339 // scalar normalize axis
340 inline std::size_t normalize_axis(std::size_t dim, std::ptrdiff_t axis)
341 {
342 return axis < 0 ? static_cast<std::size_t>(static_cast<std::ptrdiff_t>(dim) + axis)
343 : static_cast<std::size_t>(axis);
344 }
345
346 template <class E, class C>
347 inline std::enable_if_t<
348 !xtl::is_integral<std::decay_t<C>>::value && xtl::is_signed<typename std::decay_t<C>::value_type>::value,
349 rebind_container_t<std::size_t, std::decay_t<C>>>
350 normalize_axis(E& expr, C&& axes)
351 {
352 rebind_container_t<std::size_t, std::decay_t<C>> res;
353 resize_container(res, axes.size());
354
355 for (std::size_t i = 0; i < axes.size(); ++i)
356 {
357 res[i] = normalize_axis(expr.dimension(), axes[i]);
358 }
359
360 XTENSOR_ASSERT(std::all_of(
361 res.begin(),
362 res.end(),
363 [&expr](auto ax_el)
364 {
365 return ax_el < expr.dimension();
366 }
367 ));
368
369 return res;
370 }
371
372 template <class C, class E>
373 inline std::enable_if_t<
374 !xtl::is_integral<std::decay_t<C>>::value && std::is_unsigned<typename std::decay_t<C>::value_type>::value,
375 C&&>
376 normalize_axis(E& expr, C&& axes)
377 {
378 static_cast<void>(expr);
379 XTENSOR_ASSERT(std::all_of(
380 axes.begin(),
381 axes.end(),
382 [&expr](auto ax_el)
383 {
384 return ax_el < expr.dimension();
385 }
386 ));
387 return std::forward<C>(axes);
388 }
389
390 template <class R, class E, class C>
391 inline auto forward_normalize(E& expr, C&& axes)
392 -> std::enable_if_t<xtl::is_signed<std::decay_t<decltype(*std::begin(axes))>>::value, R>
393 {
394 R res;
395 xt::resize_container(res, std::size(axes));
396 auto dim = expr.dimension();
397 std::transform(
398 std::begin(axes),
399 std::end(axes),
400 std::begin(res),
401 [&dim](auto ax_el)
402 {
403 return normalize_axis(dim, ax_el);
404 }
405 );
406
407 XTENSOR_ASSERT(std::all_of(
408 res.begin(),
409 res.end(),
410 [&expr](auto ax_el)
411 {
412 return ax_el < expr.dimension();
413 }
414 ));
415
416 return res;
417 }
418
419 template <class R, class E, class C>
420 inline auto forward_normalize(E& expr, C&& axes) -> std::enable_if_t<
421 !xtl::is_signed<std::decay_t<decltype(*std::begin(axes))>>::value && !std::is_same<R, std::decay_t<C>>::value,
422 R>
423 {
424 static_cast<void>(expr);
425
426 R res;
427 xt::resize_container(res, std::size(axes));
428 std::copy(std::begin(axes), std::end(axes), std::begin(res));
429 XTENSOR_ASSERT(std::all_of(
430 res.begin(),
431 res.end(),
432 [&expr](auto ax_el)
433 {
434 return ax_el < expr.dimension();
435 }
436 ));
437 return res;
438 }
439
440 template <class R, class E, class C>
441 inline auto forward_normalize(E& expr, C&& axes) -> std::enable_if_t<
442 !xtl::is_signed<std::decay_t<decltype(*std::begin(axes))>>::value && std::is_same<R, std::decay_t<C>>::value,
443 R&&>
444 {
445 static_cast<void>(expr);
446 XTENSOR_ASSERT(std::all_of(
447 std::begin(axes),
448 std::end(axes),
449 [&expr](auto ax_el)
450 {
451 return ax_el < expr.dimension();
452 }
453 ));
454 return std::move(axes);
455 }
456
457 /******************
458 * get_value_type *
459 ******************/
460
461 template <class T>
463 {
464 using type = T;
465 };
466
467 template <class T>
468 requires requires { typename T::value_type; }
470 {
471 using type = typename T::value_type;
472 };
473
474 template <class T>
475 using get_value_type_t = typename get_value_type<T>::type;
476
477 /**********************
478 * get implementation *
479 **********************/
480
481 // When subclassing from std::tuple not all compilers are able to correctly instantiate get
482 // See here: https://stackoverflow.com/a/37188019/2528668
483 template <std::size_t I, template <typename... Args> class T, typename... Args>
484 decltype(auto) get(T<Args...>&& v)
485 {
486 return std::get<I>(static_cast<std::tuple<Args...>&&>(v));
487 }
488
489 template <std::size_t I, template <typename... Args> class T, typename... Args>
490 decltype(auto) get(T<Args...>& v)
491 {
492 return std::get<I>(static_cast<std::tuple<Args...>&>(v));
493 }
494
495 template <std::size_t I, template <typename... Args> class T, typename... Args>
496 decltype(auto) get(const T<Args...>& v)
497 {
498 return std::get<I>(static_cast<const std::tuple<Args...>&>(v));
499 }
500
501 /**************************
502 * to_array implementation *
503 ***************************/
504
505 namespace detail
506 {
507 template <class T, std::size_t N, std::size_t... I>
508 constexpr std::array<std::remove_cv_t<T>, N> to_array_impl(T (&a)[N], std::index_sequence<I...>)
509 {
510 return {{a[I]...}};
511 }
512 }
513
514 template <class T, std::size_t N>
515 constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N])
516 {
517 return detail::to_array_impl(a, std::make_index_sequence<N>{});
518 }
519
520 /***********************************
521 * container_expression / data_interface_expression / strided_expression / iterable_expression *
522 ************************************************************************************************/
523
524 template <class T>
526
527 template <class T>
528 concept container_expression = requires {
530 requires !std::is_same_v<typename std::remove_cv<typename xcontainer_inner_types<T>::storage_type>::type, invalid_type>;
531 };
532
533 template <class T>
534 using get_storage_type_t = typename xcontainer_inner_types<T>::storage_type;
535
536 template <class E>
537 concept data_interface_expression = requires { std::declval<E>().data(); };
538
539 template <class E>
540 concept strided_expression = requires { std::declval<E>().strides(); };
541
542 template <class E>
543 concept iterable_expression = requires { std::declval<E>().begin(); };
544
545 /*************************
546 * conditional type cast *
547 *************************/
548
549 template <bool condition, class T>
551
552 template <class T>
553 struct conditional_cast_functor<false, T> : public xtl::identity
554 {
555 };
556
557 template <class T>
559 {
560 template <class U>
561 inline auto operator()(U&& u) const
562 {
563 return static_cast<T>(std::forward<U>(u));
564 }
565 };
566
575 template <bool condition, class T, class U>
576 inline auto conditional_cast(U&& u)
577 {
578 return conditional_cast_functor<condition, T>()(std::forward<U>(u));
579 }
580
581 /**********************
582 * tracking allocator *
583 **********************/
584
585 namespace alloc_tracking
586 {
587 inline bool& enabled()
588 {
589 static bool enabled;
590 return enabled;
591 }
592
593 inline void enable()
594 {
595 enabled() = true;
596 }
597
598 inline void disable()
599 {
600 enabled() = false;
601 }
602
603 enum policy
604 {
605 print,
606 assert
607 };
608 }
609
610 template <class T, class A, alloc_tracking::policy P>
611 struct tracking_allocator : private A
612 {
613 using base_type = A;
614 using value_type = typename A::value_type;
615 using reference = value_type&;
616 using const_reference = const value_type&;
617 using pointer = typename std::allocator_traits<A>::pointer;
618 using const_pointer = typename std::allocator_traits<A>::const_pointer;
619 using size_type = typename std::allocator_traits<A>::size_type;
620 using difference_type = typename std::allocator_traits<A>::difference_type;
621
622 tracking_allocator() = default;
623
624 T* allocate(std::size_t n)
625 {
626 if (alloc_tracking::enabled())
627 {
628 if (P == alloc_tracking::print)
629 {
630 std::cout << "xtensor allocating: " << n << "" << std::endl;
631 }
632 else if (P == alloc_tracking::assert)
633 {
634 XTENSOR_THROW(
635 std::runtime_error,
636 "xtensor allocation of " + std::to_string(n) + " elements detected"
637 );
638 }
639 }
640 return base_type::allocate(n);
641 }
642
643 using base_type::deallocate;
644
645// Construct and destroy are removed in --std=c++-20
646#if ((defined(__cplusplus) && __cplusplus < 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG < 202002L))
647 using base_type::construct;
648 using base_type::destroy;
649#endif
650
651 template <class U>
652 struct rebind
653 {
654 using traits = std::allocator_traits<A>;
655 using other = tracking_allocator<U, typename traits::template rebind_alloc<U>, P>;
656 };
657 };
658
659 template <class T, class AT, alloc_tracking::policy PT, class U, class AU, alloc_tracking::policy PU>
660 inline bool operator==(const tracking_allocator<T, AT, PT>&, const tracking_allocator<U, AU, PU>&)
661 {
662 return std::is_same<AT, AU>::value;
663 }
664
665 template <class T, class AT, alloc_tracking::policy PT, class U, class AU, alloc_tracking::policy PU>
666 inline bool operator!=(const tracking_allocator<T, AT, PT>& a, const tracking_allocator<U, AU, PU>& b)
667 {
668 return !(a == b);
669 }
670
671 /*****************
672 * assignable_to *
673 ******************/
674
675 template <class E1, class E2>
676 concept assignable_to_expression = requires { std::declval<const E2&>().assign_to(std::declval<E1&>()); };
677
678 /*************************************
679 * overlapping_memory_checker_traits *
680 *************************************/
681
682 template <class T>
683 concept addressable_to_expression = requires { std::addressof(*std::declval<T>().begin()); };
684
685 struct memory_range
686 {
687 // Checking pointer overlap is more correct in integer values,
688 // for more explanation check https://devblogs.microsoft.com/oldnewthing/20170927-00/?p=97095
689 const uintptr_t m_first = 0;
690 const uintptr_t m_last = 0;
691
692 explicit memory_range() = default;
693
694 template <class T>
695 explicit memory_range(T* first, T* last)
696 : m_first(reinterpret_cast<uintptr_t>(last < first ? last : first))
697 , m_last(reinterpret_cast<uintptr_t>(last < first ? first : last))
698 {
699 }
700
701 template <class T>
702 bool overlaps(T* first, T* last) const
703 {
704 if (first <= last)
705 {
706 return reinterpret_cast<uintptr_t>(first) <= m_last
707 && reinterpret_cast<uintptr_t>(last) >= m_first;
708 }
709 else
710 {
711 return reinterpret_cast<uintptr_t>(last) <= m_last
712 && reinterpret_cast<uintptr_t>(first) >= m_first;
713 }
714 }
715 };
716
717 template <class E, class Enable = void>
719 {
720 static bool check_overlap(const E&, const memory_range&)
721 {
722 return true;
723 }
724 };
725
726 template <class E>
727 struct overlapping_memory_checker_traits<E, std::enable_if_t<addressable_to_expression<E>>>
728 {
729 static bool check_overlap(const E& expr, const memory_range& dst_range)
730 {
731 if (expr.size() == 0)
732 {
733 return false;
734 }
735 else
736 {
737 return dst_range.overlaps(std::addressof(*expr.begin()), std::addressof(*expr.rbegin()));
738 }
739 }
740 };
741
742 struct overlapping_memory_checker_base
743 {
744 memory_range m_dst_range;
745
746 explicit overlapping_memory_checker_base() = default;
747
748 explicit overlapping_memory_checker_base(memory_range dst_memory_range)
749 : m_dst_range(std::move(dst_memory_range))
750 {
751 }
752
753 template <class E>
754 bool check_overlap(const E& expr) const
755 {
756 if (!m_dst_range.m_first || !m_dst_range.m_last)
757 {
758 return false;
759 }
760 else
761 {
762 return overlapping_memory_checker_traits<E>::check_overlap(expr, m_dst_range);
763 }
764 }
765 };
766
767 template <class Dst, class Enable = void>
768 struct overlapping_memory_checker : overlapping_memory_checker_base
769 {
770 explicit overlapping_memory_checker(const Dst&)
771 : overlapping_memory_checker_base()
772 {
773 }
774 };
775
776 template <class Dst>
777 struct overlapping_memory_checker<Dst, std::enable_if_t<addressable_to_expression<Dst>>>
778 : overlapping_memory_checker_base
779 {
780 explicit overlapping_memory_checker(const Dst& aDst)
781 : overlapping_memory_checker_base(
782 [&]()
783 {
784 if (aDst.size() == 0)
785 {
786 return memory_range();
787 }
788 else
789 {
790 return memory_range(std::addressof(*aDst.begin()), std::addressof(*aDst.rbegin()));
791 }
792 }()
793 )
794 {
795 }
796 };
797
798 template <class Dst>
799 auto make_overlapping_memory_checker(const Dst& a_dst)
800 {
802 }
803
804 /********************
805 * rebind_container *
806 ********************/
807
808 template <class X, template <class, class> class C, class T, class A>
809 struct rebind_container<X, C<T, A>>
810 {
811 using traits = std::allocator_traits<A>;
812 using allocator = typename traits::template rebind_alloc<X>;
813 using type = C<X, allocator>;
814 };
815
816// Workaround for rebind_container problems when C++17 feature is enabled
817#ifdef __cpp_template_template_args
818 template <class X, class T, std::size_t N>
819 struct rebind_container<X, std::array<T, N>>
820 {
821 using type = std::array<X, N>;
822 };
823#else
824 template <class X, template <class, std::size_t> class C, class T, std::size_t N>
825 struct rebind_container<X, C<T, N>>
826 {
827 using type = C<X, N>;
828 };
829#endif
830
831 /***************
832 * get_strides *
833 ***************/
834
835 template <class CP, class O, class A>
836 class xbuffer_adaptor;
837
838 namespace detail
839 {
840 template <class>
841 inline constexpr bool is_fixed_shape_v = false;
842
843 template <std::size_t... I>
844 inline constexpr bool is_fixed_shape_v<fixed_shape<I...>> = true;
845
846 template <class>
847 inline constexpr bool is_xbuffer_adaptor_v = false;
848
849 template <class CP, class O, class A>
850 inline constexpr bool is_xbuffer_adaptor_v<xbuffer_adaptor<CP, O, A>> = true;
851
852 template <class S>
853 concept fixed_shape_type = is_fixed_shape_v<S>;
854
855 template <class S>
856 concept xbuffer_adaptor_type = is_xbuffer_adaptor_v<S>;
857 }
858
859 // Defers strides-type mapping so callers can use it inside std::conditional_t<cond, A, B>::type
860 // without evaluating both branches (see xshared_expression in xexpression.hpp).
861 template <class S>
863 {
864 using type = typename rebind_container<std::ptrdiff_t, S>::type;
865 };
866
867 template <detail::fixed_shape_type S>
869 {
870 // TODO we could compute the strides statically here.
871 // But we'll need full constexpr support to have a
872 // homogenous ``compute_strides`` method
873 using type = std::array<std::ptrdiff_t, S::size()>;
874 };
875
876 template <detail::xbuffer_adaptor_type S>
877 struct get_strides_type<S>
878 {
879 // In bindings this mapping is called by reshape_view with an inner shape of type
880 // xbuffer_adaptor.
881 // Since we cannot create a buffer adaptor holding data, we map it to an std::vector.
882 using type = std::vector<typename S::value_type, typename S::allocator_type>;
883 };
884
885 template <class S>
886 using get_strides_t = typename get_strides_type<S>::type;
887
888 /*******************
889 * inner_reference *
890 *******************/
891
892 template <class ST>
893 using inner_reference_t = std::conditional_t<
894 std::is_const<std::remove_reference_t<ST>>::value,
895 typename std::decay_t<ST>::const_reference,
896 typename std::decay_t<ST>::reference>;
897
898 /************
899 * get_rank *
900 ************/
901
902 // Define the requirement
903 template <typename T>
904 concept HasRank = requires {
905 T::rank; // Checks if T::rank exists as a type nested member
906 };
907
908 template <class E>
909 constexpr std::size_t has_rank()
910 {
911 return HasRank<E>;
912 }
913
914 template <class E>
915 constexpr std::size_t get_rank()
916 {
917 if constexpr (HasRank<std::decay_t<E>>)
918 {
919 return std::decay_t<E>::rank;
920 }
921 return SIZE_MAX;
922 }
923
924 template <class E>
925 constexpr std::size_t has_fixed_rank()
926 {
927 return get_rank<E>() != SIZE_MAX;
928 }
929}
930
931#endif
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.
Definition xcomplex.hpp:221
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.
Definition xutils.hpp:576