equal_range.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_EQUAL_RANGE_H
  9. #define _LIBCPP___ALGORITHM_EQUAL_RANGE_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/comp_ref_type.h>
  12. #include <__algorithm/half_positive.h>
  13. #include <__algorithm/iterator_operations.h>
  14. #include <__algorithm/lower_bound.h>
  15. #include <__algorithm/upper_bound.h>
  16. #include <__config>
  17. #include <__functional/identity.h>
  18. #include <__functional/invoke.h>
  19. #include <__iterator/advance.h>
  20. #include <__iterator/distance.h>
  21. #include <__iterator/iterator_traits.h>
  22. #include <__iterator/next.h>
  23. #include <__type_traits/is_callable.h>
  24. #include <__type_traits/is_copy_constructible.h>
  25. #include <__utility/move.h>
  26. #include <__utility/pair.h>
  27. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  28. # pragma GCC system_header
  29. #endif
  30. _LIBCPP_PUSH_MACROS
  31. #include <__undef_macros>
  32. _LIBCPP_BEGIN_NAMESPACE_STD
  33. template <class _AlgPolicy, class _Compare, class _Iter, class _Sent, class _Tp, class _Proj>
  34. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter, _Iter>
  35. __equal_range(_Iter __first, _Sent __last, const _Tp& __value, _Compare&& __comp, _Proj&& __proj) {
  36. auto __len = _IterOps<_AlgPolicy>::distance(__first, __last);
  37. _Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
  38. while (__len != 0) {
  39. auto __half_len = std::__half_positive(__len);
  40. _Iter __mid = _IterOps<_AlgPolicy>::next(__first, __half_len);
  41. if (std::__invoke(__comp, std::__invoke(__proj, *__mid), __value)) {
  42. __first = ++__mid;
  43. __len -= __half_len + 1;
  44. } else if (std::__invoke(__comp, __value, std::__invoke(__proj, *__mid))) {
  45. __end = __mid;
  46. __len = __half_len;
  47. } else {
  48. _Iter __mp1 = __mid;
  49. return pair<_Iter, _Iter>(std::__lower_bound<_AlgPolicy>(__first, __mid, __value, __comp, __proj),
  50. std::__upper_bound<_AlgPolicy>(++__mp1, __end, __value, __comp, __proj));
  51. }
  52. }
  53. return pair<_Iter, _Iter>(__first, __first);
  54. }
  55. template <class _ForwardIterator, class _Tp, class _Compare>
  56. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
  57. equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
  58. static_assert(__is_callable<_Compare, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
  59. static_assert(is_copy_constructible<_ForwardIterator>::value, "Iterator has to be copy constructible");
  60. return std::__equal_range<_ClassicAlgPolicy>(
  61. std::move(__first),
  62. std::move(__last),
  63. __value,
  64. static_cast<__comp_ref_type<_Compare> >(__comp),
  65. std::__identity());
  66. }
  67. template <class _ForwardIterator, class _Tp>
  68. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
  69. equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
  70. return std::equal_range(std::move(__first), std::move(__last), __value, __less<>());
  71. }
  72. _LIBCPP_END_NAMESPACE_STD
  73. _LIBCPP_POP_MACROS
  74. #endif // _LIBCPP___ALGORITHM_EQUAL_RANGE_H