includes.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ALGORITHM_INCLUDES_H
  9. #define _LIBCPP___ALGORITHM_INCLUDES_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/comp_ref_type.h>
  12. #include <__config>
  13. #include <__functional/identity.h>
  14. #include <__functional/invoke.h>
  15. #include <__iterator/iterator_traits.h>
  16. #include <__type_traits/is_callable.h>
  17. #include <__utility/move.h>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_PUSH_MACROS
  22. #include <__undef_macros>
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Comp, class _Proj1, class _Proj2>
  25. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __includes(
  26. _Iter1 __first1,
  27. _Sent1 __last1,
  28. _Iter2 __first2,
  29. _Sent2 __last2,
  30. _Comp&& __comp,
  31. _Proj1&& __proj1,
  32. _Proj2&& __proj2) {
  33. for (; __first2 != __last2; ++__first1) {
  34. if (__first1 == __last1 ||
  35. std::__invoke(__comp, std::__invoke(__proj2, *__first2), std::__invoke(__proj1, *__first1)))
  36. return false;
  37. if (!std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
  38. ++__first2;
  39. }
  40. return true;
  41. }
  42. template <class _InputIterator1, class _InputIterator2, class _Compare>
  43. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  44. includes(_InputIterator1 __first1,
  45. _InputIterator1 __last1,
  46. _InputIterator2 __first2,
  47. _InputIterator2 __last2,
  48. _Compare __comp) {
  49. static_assert(
  50. __is_callable<_Compare, decltype(*__first1), decltype(*__first2)>::value, "Comparator has to be callable");
  51. return std::__includes(
  52. std::move(__first1),
  53. std::move(__last1),
  54. std::move(__first2),
  55. std::move(__last2),
  56. static_cast<__comp_ref_type<_Compare> >(__comp),
  57. __identity(),
  58. __identity());
  59. }
  60. template <class _InputIterator1, class _InputIterator2>
  61. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  62. includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
  63. return std::includes(std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __less<>());
  64. }
  65. _LIBCPP_END_NAMESPACE_STD
  66. _LIBCPP_POP_MACROS
  67. #endif // _LIBCPP___ALGORITHM_INCLUDES_H