Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / hana / unique.hpp
1 /*!
2 @file
3 Defines `boost::hana::unique`.
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_UNIQUE_HPP
11 #define BOOST_HANA_UNIQUE_HPP
12
13 #include <boost/hana/fwd/unique.hpp>
14
15 #include <boost/hana/concept/sequence.hpp>
16 #include <boost/hana/config.hpp>
17 #include <boost/hana/core/dispatch.hpp>
18 #include <boost/hana/detail/nested_by.hpp> // required by fwd decl
19 #include <boost/hana/equal.hpp>
20 #include <boost/hana/front.hpp>
21 #include <boost/hana/group.hpp>
22 #include <boost/hana/transform.hpp>
23
24
25 BOOST_HANA_NAMESPACE_BEGIN
26     //! @cond
27     template <typename Xs>
28     constexpr auto unique_t::operator()(Xs&& xs) const {
29         using S = typename hana::tag_of<Xs>::type;
30         using Unique = BOOST_HANA_DISPATCH_IF(unique_impl<S>,
31             hana::Sequence<S>::value
32         );
33
34     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
35         static_assert(hana::Sequence<S>::value,
36         "hana::unique(xs) requires 'xs' to be a Sequence");
37     #endif
38
39         return Unique::apply(static_cast<Xs&&>(xs));
40     }
41
42     template <typename Xs, typename Predicate>
43     constexpr auto unique_t::operator()(Xs&& xs, Predicate&& predicate) const {
44         using S = typename hana::tag_of<Xs>::type;
45         using Unique = BOOST_HANA_DISPATCH_IF(unique_impl<S>,
46             hana::Sequence<S>::value
47         );
48
49     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
50         static_assert(hana::Sequence<S>::value,
51         "hana::unique(xs, predicate) requires 'xs' to be a Sequence");
52     #endif
53
54         return Unique::apply(static_cast<Xs&&>(xs),
55                              static_cast<Predicate&&>(predicate));
56     }
57     //! @endcond
58
59     template <typename S, bool condition>
60     struct unique_impl<S, when<condition>> : default_ {
61         template <typename Xs, typename Pred>
62         static constexpr auto apply(Xs&& xs, Pred&& pred) {
63             return hana::transform(
64                 hana::group(static_cast<Xs&&>(xs), static_cast<Pred&&>(pred)),
65                 hana::front
66             );
67         }
68
69         template <typename Xs>
70         static constexpr auto apply(Xs&& xs)
71         { return unique_impl::apply(static_cast<Xs&&>(xs), hana::equal); }
72     };
73 BOOST_HANA_NAMESPACE_END
74
75 #endif // !BOOST_HANA_UNIQUE_HPP