is_disjoint.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*!
  2. @file
  3. Defines `boost::hana::is_disjoint`.
  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_IS_DISJOINT_HPP
  9. #define BOOST_HANA_IS_DISJOINT_HPP
  10. #include <boost/hana/fwd/is_disjoint.hpp>
  11. #include <boost/hana/concept/searchable.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/contains.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/none_of.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. //! @cond
  18. template <typename Xs, typename Ys>
  19. constexpr auto is_disjoint_t::operator()(Xs&& xs, Ys&& ys) const {
  20. using S1 = typename hana::tag_of<Xs>::type;
  21. using S2 = typename hana::tag_of<Ys>::type;
  22. using IsDisjoint = BOOST_HANA_DISPATCH_IF(
  23. decltype(is_disjoint_impl<S1, S2>{}),
  24. hana::Searchable<S1>::value &&
  25. hana::Searchable<S2>::value
  26. );
  27. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  28. static_assert(hana::Searchable<S1>::value,
  29. "hana::is_disjoint(xs, ys) requires 'xs' to be Searchable");
  30. static_assert(hana::Searchable<S2>::value,
  31. "hana::is_disjoint(xs, ys) requires 'ys' to be Searchable");
  32. #endif
  33. return IsDisjoint::apply(static_cast<Xs&&>(xs), static_cast<Ys&&>(ys));
  34. }
  35. //! @endcond
  36. namespace detail {
  37. template <typename Ys>
  38. struct in_by_reference {
  39. Ys const& ys;
  40. template <typename X>
  41. constexpr auto operator()(X const& x) const
  42. { return hana::contains(ys, x); }
  43. };
  44. }
  45. template <typename S1, typename S2, bool condition>
  46. struct is_disjoint_impl<S1, S2, when<condition>> : default_ {
  47. template <typename Xs, typename Ys>
  48. static constexpr auto apply(Xs const& xs, Ys const& ys) {
  49. return hana::none_of(xs, detail::in_by_reference<Ys>{ys});
  50. }
  51. };
  52. BOOST_HANA_NAMESPACE_END
  53. #endif // !BOOST_HANA_IS_DISJOINT_HPP