slice.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*!
  2. @file
  3. Forward declares `boost::hana::slice` and `boost::hana::slice_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_FWD_SLICE_HPP
  9. #define BOOST_HANA_FWD_SLICE_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. #include <cstddef>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. //! Extract the elements of a `Sequence` at the given indices.
  15. //! @ingroup group-Sequence
  16. //!
  17. //! Given an arbitrary sequence of `indices`, `slice` returns a new
  18. //! sequence of the elements of the original sequence that appear at
  19. //! those indices. In other words,
  20. //! @code
  21. //! slice([x1, ..., xn], [i1, ..., ik]) == [x_i1, ..., x_ik]
  22. //! @endcode
  23. //!
  24. //! The indices do not have to be ordered or contiguous in any particular
  25. //! way, but they must not be out of the bounds of the sequence. It is
  26. //! also possible to specify the same index multiple times, in which case
  27. //! the element at this index will be repeatedly included in the resulting
  28. //! sequence.
  29. //!
  30. //!
  31. //! @param xs
  32. //! The sequence from which a subsequence is extracted.
  33. //!
  34. //! @param indices
  35. //! A compile-time `Foldable` containing non-negative `IntegralConstant`s
  36. //! representing the indices. The indices are 0-based, and they must all
  37. //! be in bounds of the `xs` sequence. Note that any `Foldable` will
  38. //! really do (no need for an `Iterable`, for example); the linearization
  39. //! of the `indices` is used to determine the order of the elements
  40. //! included in the slice.
  41. //!
  42. //!
  43. //! Example
  44. //! -------
  45. //! @include example/slice.cpp
  46. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  47. constexpr auto slice = [](auto&& xs, auto&& indices) {
  48. return tag-dispatched;
  49. };
  50. #else
  51. template <typename S, typename = void>
  52. struct slice_impl : slice_impl<S, when<true>> { };
  53. struct slice_t {
  54. template <typename Xs, typename Indices>
  55. constexpr auto operator()(Xs&& xs, Indices&& indices) const;
  56. };
  57. constexpr slice_t slice{};
  58. #endif
  59. //! Shorthand to `slice` a contiguous range of elements.
  60. //! @ingroup group-Sequence
  61. //!
  62. //! `slice_c` is simply a shorthand to slice a contiguous range of
  63. //! elements. In particular, `slice_c<from, to>(xs)` is equivalent to
  64. //! `slice(xs, range_c<std::size_t, from, to>)`, which simply slices
  65. //! all the elements of `xs` contained in the half-open interval
  66. //! delimited by `[from, to)`. Like for `slice`, the indices used with
  67. //! `slice_c` are 0-based and they must be in the bounds of the sequence
  68. //! being sliced.
  69. //!
  70. //!
  71. //! @tparam from
  72. //! The index of the first element in the slice.
  73. //!
  74. //! @tparam to
  75. //! One-past the index of the last element in the slice. It must hold
  76. //! that `from <= to`.
  77. //!
  78. //!
  79. //! Example
  80. //! -------
  81. //! @include example/slice_c.cpp
  82. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  83. template <std::size_t from, std::size_t to>
  84. constexpr auto slice_c = [](auto&& xs) {
  85. return hana::slice(forwarded(xs), hana::range_c<std::size_t, from, to>);
  86. };
  87. #else
  88. template <std::size_t from, std::size_t to>
  89. struct slice_c_t;
  90. template <std::size_t from, std::size_t to>
  91. constexpr slice_c_t<from, to> slice_c{};
  92. #endif
  93. BOOST_HANA_NAMESPACE_END
  94. #endif // !BOOST_HANA_FWD_SLICE_HPP