| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- /***************************************************************************
- * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
- * Copyright (c) QuantStack *
- * *
- * Distributed under the terms of the BSD 3-Clause License. *
- * *
- * The full license is in the file LICENSE, distributed with this software. *
- ****************************************************************************/
- #ifndef XTENSOR_VIEW_UTILS_HPP
- #define XTENSOR_VIEW_UTILS_HPP
- #include <array>
- #include "xlayout.hpp"
- #include "xslice.hpp"
- #include "xtensor_forward.hpp"
- namespace xt
- {
- /********************************
- * helper functions declaration *
- ********************************/
- // number of integral types in the specified sequence of types
- template <class... S>
- constexpr std::size_t integral_count();
- // number of integral types in the specified sequence of types before specified index
- template <class... S>
- constexpr std::size_t integral_count_before(std::size_t i);
- // index in the specified sequence of types of the ith non-integral type
- template <class... S>
- constexpr std::size_t integral_skip(std::size_t i);
- // number of newaxis types in the specified sequence of types
- template <class... S>
- constexpr std::size_t newaxis_count();
- // number of newaxis types in the specified sequence of types before specified index
- template <class... S>
- constexpr std::size_t newaxis_count_before(std::size_t i);
- // index in the specified sequence of types of the ith non-newaxis type
- template <class... S>
- constexpr std::size_t newaxis_skip(std::size_t i);
- template <class S, class It>
- inline disable_xslice<S, std::size_t> get_slice_value(const S& s, It&) noexcept
- {
- return static_cast<std::size_t>(s);
- }
- template <class S, class It>
- inline auto get_slice_value(const xslice<S>& slice, It& it) noexcept
- {
- return slice.derived_cast()(typename S::size_type(*it));
- }
- /***********************
- * view_temporary_type *
- ***********************/
- namespace detail
- {
- template <class T, class S, layout_type L, class... SL>
- struct view_temporary_type_impl
- {
- using type = xt::xarray<T, L>;
- };
- template <class T, class I, std::size_t N, layout_type L, class... SL>
- struct view_temporary_type_impl<T, std::array<I, N>, L, SL...>
- {
- using type = xt::xtensor<T, N + newaxis_count<SL...>() - integral_count<SL...>(), L>;
- };
- }
- template <class E, class... SL>
- struct view_temporary_type
- {
- using type = typename detail::view_temporary_type_impl<
- std::decay_t<typename E::value_type>,
- typename E::shape_type,
- E::static_layout,
- SL...>::type;
- };
- template <class E, class... SL>
- using view_temporary_type_t = typename view_temporary_type<E, SL...>::type;
- /************************
- * count integral types *
- ************************/
- namespace detail
- {
- template <class T, class... S>
- struct integral_count_impl
- {
- static constexpr std::size_t count(std::size_t i) noexcept
- {
- return i
- ? (integral_count_impl<S...>::count(i - 1)
- + (xtl::is_integral<std::remove_reference_t<T>>::value ? 1 : 0))
- : 0;
- }
- };
- template <>
- struct integral_count_impl<void>
- {
- static constexpr std::size_t count(std::size_t /*i*/) noexcept
- {
- return 0;
- }
- };
- }
- template <class... S>
- constexpr std::size_t integral_count()
- {
- return detail::integral_count_impl<S..., void>::count(sizeof...(S));
- }
- template <class... S>
- constexpr std::size_t integral_count_before(std::size_t i)
- {
- return detail::integral_count_impl<S..., void>::count(i);
- }
- /***********************
- * count newaxis types *
- ***********************/
- namespace detail
- {
- template <class T>
- struct is_newaxis : std::false_type
- {
- };
- template <class T>
- struct is_newaxis<xnewaxis<T>> : public std::true_type
- {
- };
- template <class T, class... S>
- struct newaxis_count_impl
- {
- static constexpr std::size_t count(std::size_t i) noexcept
- {
- return i
- ? (newaxis_count_impl<S...>::count(i - 1)
- + (is_newaxis<std::remove_reference_t<T>>::value ? 1 : 0))
- : 0;
- }
- };
- template <>
- struct newaxis_count_impl<void>
- {
- static constexpr std::size_t count(std::size_t /*i*/) noexcept
- {
- return 0;
- }
- };
- }
- template <class... S>
- constexpr std::size_t newaxis_count()
- {
- return detail::newaxis_count_impl<S..., void>::count(sizeof...(S));
- }
- template <class... S>
- constexpr std::size_t newaxis_count_before(std::size_t i)
- {
- return detail::newaxis_count_impl<S..., void>::count(i);
- }
- /**********************************
- * index of ith non-integral type *
- **********************************/
- namespace detail
- {
- template <class T, class... S>
- struct integral_skip_impl
- {
- static constexpr std::size_t count(std::size_t i) noexcept
- {
- return i == 0 ? count_impl() : count_impl(i);
- }
- private:
- static constexpr std::size_t count_impl(std::size_t i) noexcept
- {
- return 1
- + (xtl::is_integral<std::remove_reference_t<T>>::value
- ? integral_skip_impl<S...>::count(i)
- : integral_skip_impl<S...>::count(i - 1));
- }
- static constexpr std::size_t count_impl() noexcept
- {
- return xtl::is_integral<std::remove_reference_t<T>>::value
- ? 1 + integral_skip_impl<S...>::count(0)
- : 0;
- }
- };
- template <>
- struct integral_skip_impl<void>
- {
- static constexpr std::size_t count(std::size_t i) noexcept
- {
- return i;
- }
- };
- }
- template <class... S>
- constexpr std::size_t integral_skip(std::size_t i)
- {
- return detail::integral_skip_impl<S..., void>::count(i);
- }
- /*********************************
- * index of ith non-newaxis type *
- *********************************/
- namespace detail
- {
- template <class T, class... S>
- struct newaxis_skip_impl
- {
- static constexpr std::size_t count(std::size_t i) noexcept
- {
- return i == 0 ? count_impl() : count_impl(i);
- }
- private:
- static constexpr std::size_t count_impl(std::size_t i) noexcept
- {
- return 1
- + (is_newaxis<std::remove_reference_t<T>>::value
- ? newaxis_skip_impl<S...>::count(i)
- : newaxis_skip_impl<S...>::count(i - 1));
- }
- static constexpr std::size_t count_impl() noexcept
- {
- return is_newaxis<std::remove_reference_t<T>>::value ? 1 + newaxis_skip_impl<S...>::count(0)
- : 0;
- }
- };
- template <>
- struct newaxis_skip_impl<void>
- {
- static constexpr std::size_t count(std::size_t i) noexcept
- {
- return i;
- }
- };
- }
- template <class... S>
- constexpr std::size_t newaxis_skip(std::size_t i)
- {
- return detail::newaxis_skip_impl<S..., void>::count(i);
- }
- }
- #endif
|