slice.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*!
  2. @file
  3. Defines `boost::hana::slice` and `boost::hana::slice_c`.
  4. Copyright Louis Dionne 2013-2022
  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_SLICE_HPP
  9. #define BOOST_HANA_SLICE_HPP
  10. #include <boost/hana/fwd/slice.hpp>
  11. #include <boost/hana/at.hpp>
  12. #include <boost/hana/concept/foldable.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/range.hpp>
  18. #include <boost/hana/unpack.hpp>
  19. #include <cstddef>
  20. #include <utility>
  21. namespace boost { namespace hana {
  22. //! @cond
  23. template <typename Xs, typename Indices>
  24. constexpr auto slice_t::operator()(Xs&& xs, Indices&& indices) const {
  25. using S = typename hana::tag_of<Xs>::type;
  26. using Slice = BOOST_HANA_DISPATCH_IF(slice_impl<S>,
  27. hana::Sequence<S>::value &&
  28. hana::Foldable<Indices>::value
  29. );
  30. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  31. static_assert(hana::Sequence<S>::value,
  32. "hana::slice(xs, indices) requires 'xs' to be a Sequence");
  33. static_assert(hana::Foldable<Indices>::value,
  34. "hana::slice(xs, indices) requires 'indices' to be Foldable");
  35. #endif
  36. return Slice::apply(static_cast<Xs&&>(xs), static_cast<Indices&&>(indices));
  37. }
  38. //! @endcond
  39. namespace detail {
  40. template <typename Xs>
  41. struct take_arbitrary {
  42. Xs& xs;
  43. using S = typename hana::tag_of<Xs>::type;
  44. template <typename ...N>
  45. constexpr auto operator()(N const& ...) const {
  46. return hana::make<S>(hana::at_c<N::value>(xs)...);
  47. }
  48. };
  49. }
  50. template <typename S, bool condition>
  51. struct slice_impl<S, when<condition>> : default_ {
  52. template <std::size_t from, typename Xs, std::size_t ...i>
  53. static constexpr auto from_offset(Xs&& xs, std::index_sequence<i...>) {
  54. return hana::make<S>(hana::at_c<from + i>(static_cast<Xs&&>(xs))...);
  55. }
  56. template <typename Xs, typename T, T from, T to>
  57. static constexpr auto apply(Xs&& xs, hana::range<T, from, to> const&) {
  58. return slice_impl::from_offset<from>(
  59. static_cast<Xs&&>(xs), std::make_index_sequence<to - from>{}
  60. );
  61. }
  62. //! @todo
  63. //! Since we have the right to specify the same index more than once,
  64. //! we can't move from the elements of the source sequence even if it
  65. //! is a temporary object: we could end up double-moving. Perhaps it
  66. //! would be possible to determine the indices from which we can move
  67. //! without incurring a too large compile-time penalty?
  68. template <typename Xs, typename Indices>
  69. static constexpr auto apply(Xs const& xs, Indices const& indices) {
  70. return hana::unpack(indices, detail::take_arbitrary<Xs const>{xs});
  71. }
  72. };
  73. template <std::size_t from, std::size_t to>
  74. struct slice_c_t {
  75. template <typename Xs>
  76. constexpr auto operator()(Xs&& xs) const {
  77. return hana::slice(static_cast<Xs&&>(xs),
  78. hana::range_c<std::size_t, from, to>);
  79. }
  80. };
  81. }} // end namespace boost::hana
  82. #endif // !BOOST_HANA_SLICE_HPP