partition_point.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_PARTITION_POINT_H
  9. #define _LIBCPP___ALGORITHM_PARTITION_POINT_H
  10. #include <__algorithm/half_positive.h>
  11. #include <__config>
  12. #include <iterator>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. _LIBCPP_BEGIN_NAMESPACE_STD
  17. template<class _ForwardIterator, class _Predicate>
  18. _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
  19. partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
  20. {
  21. typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
  22. difference_type __len = _VSTD::distance(__first, __last);
  23. while (__len != 0)
  24. {
  25. difference_type __l2 = _VSTD::__half_positive(__len);
  26. _ForwardIterator __m = __first;
  27. _VSTD::advance(__m, __l2);
  28. if (__pred(*__m))
  29. {
  30. __first = ++__m;
  31. __len -= __l2 + 1;
  32. }
  33. else
  34. __len = __l2;
  35. }
  36. return __first;
  37. }
  38. _LIBCPP_END_NAMESPACE_STD
  39. #endif // _LIBCPP___ALGORITHM_PARTITION_POINT_H