remove_at.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*!
  2. @file
  3. Defines `boost::hana::remove_at` and `boost::hana::remove_at_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_AT_HPP
  9. #define BOOST_HANA_REMOVE_AT_HPP
  10. #include <boost/hana/fwd/remove_at.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 N>
  24. constexpr auto remove_at_t::operator()(Xs&& xs, N const& n) const {
  25. using S = typename hana::tag_of<Xs>::type;
  26. using RemoveAt = BOOST_HANA_DISPATCH_IF(remove_at_impl<S>,
  27. hana::Sequence<S>::value &&
  28. hana::IntegralConstant<N>::value
  29. );
  30. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  31. static_assert(hana::Sequence<S>::value,
  32. "hana::remove_at(xs, n) requires 'xs' to be a Sequence");
  33. static_assert(hana::IntegralConstant<N>::value,
  34. "hana::remove_at(xs, n) requires 'n' to be an IntegralConstant");
  35. #endif
  36. static_assert(N::value >= 0,
  37. "hana::remove_at(xs, n) requires 'n' to be non-negative");
  38. return RemoveAt::apply(static_cast<Xs&&>(xs), n);
  39. }
  40. //! @endcond
  41. template <typename S, bool condition>
  42. struct remove_at_impl<S, when<condition>> : default_ {
  43. template <typename Xs, std::size_t ...before, std::size_t ...after>
  44. static constexpr auto
  45. remove_at_helper(Xs&& xs, std::index_sequence<before...>,
  46. std::index_sequence<after...>)
  47. {
  48. return hana::make<S>(
  49. hana::at_c<before>(static_cast<Xs&&>(xs))...,
  50. hana::at_c<after + sizeof...(before) + 1>(static_cast<Xs&&>(xs))...
  51. );
  52. }
  53. template <typename Xs, typename N>
  54. static constexpr auto apply(Xs&& xs, N const&) {
  55. constexpr std::size_t n = N::value;
  56. constexpr std::size_t len = decltype(hana::length(xs))::value;
  57. static_assert(n < len,
  58. "hana::remove_at(xs, n) requires 'n' to be in the bounds of the sequence");
  59. return remove_at_helper(static_cast<Xs&&>(xs),
  60. std::make_index_sequence<n>{},
  61. std::make_index_sequence<len - n - 1>{});
  62. }
  63. };
  64. template <std::size_t n>
  65. struct remove_at_c_t {
  66. template <typename Xs>
  67. constexpr decltype(auto) operator()(Xs&& xs) const
  68. { return hana::remove_at(static_cast<Xs&&>(xs), hana::size_c<n>); }
  69. };
  70. BOOST_HANA_NAMESPACE_END
  71. #endif // !BOOST_HANA_REMOVE_AT_HPP