remove_range.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*!
  2. @file
  3. Defines `boost::hana::remove_range` and `boost::hana::remove_range_c`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_REMOVE_RANGE_HPP
  9. #define BOOST_HANA_REMOVE_RANGE_HPP
  10. #include <boost/hana/fwd/remove_range.hpp>
  11. #include <boost/hana/at.hpp>
  12. #include <boost/hana/concept/integral_constant.hpp>
  13. #include <boost/hana/concept/sequence.hpp>
  14. #include <boost/hana/config.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/core/make.hpp>
  17. #include <boost/hana/integral_constant.hpp>
  18. #include <boost/hana/length.hpp>
  19. #include <cstddef>
  20. #include <utility>
  21. BOOST_HANA_NAMESPACE_BEGIN
  22. //! @cond
  23. template <typename Xs, typename From, typename To>
  24. constexpr auto remove_range_t::operator()(Xs&& xs, From const& from, To const& to) const {
  25. using S = typename hana::tag_of<Xs>::type;
  26. using RemoveRange = BOOST_HANA_DISPATCH_IF(remove_range_impl<S>,
  27. hana::Sequence<S>::value &&
  28. hana::IntegralConstant<From>::value &&
  29. hana::IntegralConstant<To>::value
  30. );
  31. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  32. static_assert(hana::Sequence<S>::value,
  33. "hana::remove_range(xs, from, to) requires 'xs' to be a Sequence");
  34. static_assert(hana::IntegralConstant<From>::value,
  35. "hana::remove_range(xs, from, to) requires 'from' to be an IntegralConstant");
  36. static_assert(hana::IntegralConstant<To>::value,
  37. "hana::remove_range(xs, from, to) requires 'to' to be an IntegralConstant");
  38. #endif
  39. return RemoveRange::apply(static_cast<Xs&&>(xs), from, to);
  40. }
  41. //! @endcond
  42. template <typename S, bool condition>
  43. struct remove_range_impl<S, when<condition>> : default_ {
  44. template <std::size_t offset, typename Xs, std::size_t ...before, std::size_t ...after>
  45. static constexpr auto
  46. remove_range_helper(Xs&& xs, std::index_sequence<before...>,
  47. std::index_sequence<after...>)
  48. {
  49. return hana::make<S>(
  50. hana::at_c<before>(static_cast<Xs&&>(xs))...,
  51. hana::at_c<offset + after>(static_cast<Xs&&>(xs))...
  52. );
  53. }
  54. template <typename Xs, typename From, typename To>
  55. static constexpr auto apply(Xs&& xs, From const&, To const&) {
  56. constexpr std::size_t from = From::value;
  57. constexpr std::size_t to = To::value;
  58. constexpr std::size_t len = decltype(hana::length(xs))::value;
  59. constexpr std::size_t before = from == to ? len : from;
  60. constexpr std::size_t after = from == to ? 0 : len - to;
  61. static_assert(from <= to,
  62. "hana::remove_range(xs, from, to) requires '[from, to)' to be a "
  63. "valid interval, meaning that 'from <= to'");
  64. static_assert(from == to || from >= 0,
  65. "hana::remove_range(xs, from, to) requires 'from' to be non-negative");
  66. static_assert(from == to || to <= len,
  67. "hana::remove_range(xs, from, to) requires 'to <= length(xs)'");
  68. return remove_range_helper<to>(static_cast<Xs&&>(xs),
  69. std::make_index_sequence<before>{},
  70. std::make_index_sequence<after>{});
  71. }
  72. };
  73. template <std::size_t from, std::size_t to>
  74. struct remove_range_c_t {
  75. template <typename Xs>
  76. constexpr decltype(auto) operator()(Xs&& xs) const {
  77. return hana::remove_range(static_cast<Xs&&>(xs),
  78. hana::size_c<from>,
  79. hana::size_c<to>);
  80. }
  81. };
  82. BOOST_HANA_NAMESPACE_END
  83. #endif // !BOOST_HANA_REMOVE_RANGE_HPP