Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:06

0001 /*!
0002 @file
0003 Defines `boost::hana::take_back`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_TAKE_BACK_HPP
0011 #define BOOST_HANA_TAKE_BACK_HPP
0012 
0013 #include <boost/hana/fwd/take_back.hpp>
0014 
0015 #include <boost/hana/at.hpp>
0016 #include <boost/hana/concept/integral_constant.hpp>
0017 #include <boost/hana/concept/sequence.hpp>
0018 #include <boost/hana/config.hpp>
0019 #include <boost/hana/core/dispatch.hpp>
0020 #include <boost/hana/core/make.hpp>
0021 #include <boost/hana/integral_constant.hpp>
0022 #include <boost/hana/length.hpp>
0023 
0024 #include <cstddef>
0025 #include <utility>
0026 
0027 
0028 namespace boost { namespace hana {
0029     //! @cond
0030     template <typename Xs, typename N>
0031     constexpr auto take_back_t::operator()(Xs&& xs, N const& n) const {
0032         using S = typename hana::tag_of<Xs>::type;
0033         using TakeBack = BOOST_HANA_DISPATCH_IF(take_back_impl<S>,
0034             hana::Sequence<S>::value &&
0035             hana::IntegralConstant<N>::value
0036         );
0037 
0038 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0039         static_assert(hana::Sequence<S>::value,
0040         "hana::take_back(xs, n) requires 'xs' to be a Sequence");
0041 
0042         static_assert(hana::IntegralConstant<N>::value,
0043         "hana::take_back(xs, n) requires 'n' to be an IntegralConstant");
0044 #endif
0045 
0046         return TakeBack::apply(static_cast<Xs&&>(xs), n);
0047     }
0048     //! @endcond
0049 
0050     template <typename S, bool condition>
0051     struct take_back_impl<S, when<condition>> : default_ {
0052         template <std::size_t start, typename Xs, std::size_t ...n>
0053         static constexpr auto take_back_helper(Xs&& xs, std::index_sequence<n...>) {
0054             return hana::make<S>(hana::at_c<start + n>(static_cast<Xs&&>(xs))...);
0055         }
0056 
0057         template <typename Xs, typename N>
0058         static constexpr auto apply(Xs&& xs, N const&) {
0059             constexpr std::size_t n = N::value;
0060             constexpr std::size_t len = decltype(hana::length(xs))::value;
0061             constexpr std::size_t start = n < len ? len - n : 0;
0062             return take_back_helper<start>(static_cast<Xs&&>(xs),
0063                         std::make_index_sequence<(n < len ? n : len)>{});
0064         }
0065     };
0066 
0067     template <std::size_t n>
0068     struct take_back_c_t {
0069         template <typename Xs>
0070         constexpr auto operator()(Xs&& xs) const {
0071             return hana::take_back(static_cast<Xs&&>(xs), hana::size_c<n>);
0072         }
0073     };
0074 }} // end namespace boost::hana
0075 
0076 #endif // !BOOST_HANA_TAKE_BACK_HPP