3 Defines `boost::hana::remove_range` and `boost::hana::remove_range_c`.
5 @copyright Louis Dionne 2013-2016
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_REMOVE_RANGE_HPP
11 #define BOOST_HANA_REMOVE_RANGE_HPP
13 #include <boost/hana/fwd/remove_range.hpp>
15 #include <boost/hana/at.hpp>
16 #include <boost/hana/concept/integral_constant.hpp>
17 #include <boost/hana/concept/sequence.hpp>
18 #include <boost/hana/config.hpp>
19 #include <boost/hana/core/dispatch.hpp>
20 #include <boost/hana/core/make.hpp>
21 #include <boost/hana/integral_constant.hpp>
22 #include <boost/hana/length.hpp>
28 BOOST_HANA_NAMESPACE_BEGIN
30 template <typename Xs, typename From, typename To>
31 constexpr auto remove_range_t::operator()(Xs&& xs, From const& from, To const& to) const {
32 using S = typename hana::tag_of<Xs>::type;
33 using RemoveRange = BOOST_HANA_DISPATCH_IF(remove_range_impl<S>,
34 hana::Sequence<S>::value &&
35 hana::IntegralConstant<From>::value &&
36 hana::IntegralConstant<To>::value
39 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
40 static_assert(hana::Sequence<S>::value,
41 "hana::remove_range(xs, from, to) requires 'xs' to be a Sequence");
43 static_assert(hana::IntegralConstant<From>::value,
44 "hana::remove_range(xs, from, to) requires 'from' to be an IntegralConstant");
46 static_assert(hana::IntegralConstant<To>::value,
47 "hana::remove_range(xs, from, to) requires 'to' to be an IntegralConstant");
50 return RemoveRange::apply(static_cast<Xs&&>(xs), from, to);
54 template <typename S, bool condition>
55 struct remove_range_impl<S, when<condition>> : default_ {
56 template <std::size_t offset, typename Xs, std::size_t ...before, std::size_t ...after>
58 remove_range_helper(Xs&& xs, std::index_sequence<before...>,
59 std::index_sequence<after...>)
62 hana::at_c<before>(static_cast<Xs&&>(xs))...,
63 hana::at_c<offset + after>(static_cast<Xs&&>(xs))...
67 template <typename Xs, typename From, typename To>
68 static constexpr auto apply(Xs&& xs, From const&, To const&) {
69 constexpr std::size_t from = From::value;
70 constexpr std::size_t to = To::value;
71 constexpr std::size_t len = decltype(hana::length(xs))::value;
72 constexpr std::size_t before = from == to ? len : from;
73 constexpr std::size_t after = from == to ? 0 : len - to;
75 static_assert(from <= to,
76 "hana::remove_range(xs, from, to) requires '[from, to)' to be a "
77 "valid interval, meaning that 'from <= to'");
78 static_assert(from == to || from >= 0,
79 "hana::remove_range(xs, from, to) requires 'from' to be non-negative");
80 static_assert(from == to || to <= len,
81 "hana::remove_range(xs, from, to) requires 'to <= length(xs)'");
83 return remove_range_helper<to>(static_cast<Xs&&>(xs),
84 std::make_index_sequence<before>{},
85 std::make_index_sequence<after>{});
89 template <std::size_t from, std::size_t to>
90 struct remove_range_c_t {
91 template <typename Xs>
92 constexpr decltype(auto) operator()(Xs&& xs) const {
93 return hana::remove_range(static_cast<Xs&&>(xs),
98 BOOST_HANA_NAMESPACE_END
100 #endif // !BOOST_HANA_REMOVE_RANGE_HPP