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