set_difference.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_BEGIN_NAMESPACE_STD
  25. template <class _AlgPolicy, class _Comp, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
  26. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<__remove_cvref_t<_InIter1>, __remove_cvref_t<_OutIter> >
  27. __set_difference(
  28. _InIter1&& __first1, _Sent1&& __last1, _InIter2&& __first2, _Sent2&& __last2, _OutIter&& __result, _Comp&& __comp) {
  29. while (__first1 != __last1 && __first2 != __last2) {
  30. if (__comp(*__first1, *__first2)) {
  31. *__result = *__first1;
  32. ++__first1;
  33. ++__result;
  34. } else if (__comp(*__first2, *__first1)) {
  35. ++__first2;
  36. } else {
  37. ++__first1;
  38. ++__first2;
  39. }
  40. }
  41. return std::__copy<_AlgPolicy>(std::move(__first1), std::move(__last1), std::move(__result));
  42. }
  43. template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
  44. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_difference(
  45. _InputIterator1 __first1,
  46. _InputIterator1 __last1,
  47. _InputIterator2 __first2,
  48. _InputIterator2 __last2,
  49. _OutputIterator __result,
  50. _Compare __comp) {
  51. return std::__set_difference<_ClassicAlgPolicy, __comp_ref_type<_Compare> >(
  52. __first1, __last1, __first2, __last2, __result, __comp)
  53. .second;
  54. }
  55. template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
  56. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator set_difference(
  57. _InputIterator1 __first1,
  58. _InputIterator1 __last1,
  59. _InputIterator2 __first2,
  60. _InputIterator2 __last2,
  61. _OutputIterator __result) {
  62. return std::__set_difference<_ClassicAlgPolicy>(__first1, __last1, __first2, __last2, __result, __less<>()).second;
  63. }
  64. _LIBCPP_END_NAMESPACE_STD
  65. #endif // _LIBCPP___ALGORITHM_SET_DIFFERENCE_H