set_difference.h 2.7 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_SET_DIFFERENCE_H
  9. #define _LIBCPP___ALGORITHM_SET_DIFFERENCE_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/comp_ref_type.h>
  12. #include <__algorithm/copy.h>
  13. #include <__algorithm/iterator_operations.h>
  14. #include <__config>
  15. #include <__functional/identity.h>
  16. #include <__functional/invoke.h>
  17. #include <__iterator/iterator_traits.h>
  18. #include <__type_traits/remove_cvref.h>
  19. #include <__utility/move.h>
  20. #include <__utility/pair.h>
  21. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  22. # pragma GCC system_header
  23. #endif
  24. _LIBCPP_PUSH_MACROS
  25. #include <__undef_macros>
  26. _LIBCPP_BEGIN_NAMESPACE_STD
  27. template <class _AlgPolicy, class _Comp, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
  28. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<__remove_cvref_t<_InIter1>, __remove_cvref_t<_OutIter> >
  29. __set_difference(
  30. _InIter1&& __first1, _Sent1&& __last1, _InIter2&& __first2, _Sent2&& __last2, _OutIter&& __result, _Comp&& __comp) {
  31. while (__first1 != __last1 && __first2 != __last2) {
  32. if (__comp(*__first1, *__first2)) {
  33. *__result = *__first1;
  34. ++__first1;
  35. ++__result;
  36. } else if (__comp(*__first2, *__first1)) {
  37. ++__first2;
  38. } else {
  39. ++__first1;
  40. ++__first2;
  41. }
  42. }
  43. return std::__copy<_AlgPolicy>(std::move(__first1), std::move(__last1), std::move(__result));
  44. }
  45. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
  46. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_difference(
  47. _InputIterator1 __first1,
  48. _InputIterator1 __last1,
  49. _InputIterator2 __first2,
  50. _InputIterator2 __last2,
  51. _OutputIterator __result,
  52. _Compare __comp) {
  53. return std::__set_difference<_ClassicAlgPolicy, __comp_ref_type<_Compare> >(
  54. __first1, __last1, __first2, __last2, __result, __comp)
  55. .second;
  56. }
  57. template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
  58. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_difference(
  59. _InputIterator1 __first1,
  60. _InputIterator1 __last1,
  61. _InputIterator2 __first2,
  62. _InputIterator2 __last2,
  63. _OutputIterator __result) {
  64. return std::__set_difference<_ClassicAlgPolicy>(__first1, __last1, __first2, __last2, __result, __less<>()).second;
  65. }
  66. _LIBCPP_END_NAMESPACE_STD
  67. _LIBCPP_POP_MACROS
  68. #endif // _LIBCPP___ALGORITHM_SET_DIFFERENCE_H