unique.hpp 2.3 KB

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