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 
9 #ifndef _LIBCPP___ALGORITHM_EQUAL_RANGE_H
10 #define _LIBCPP___ALGORITHM_EQUAL_RANGE_H
11 
12 #include <__algorithm/comp.h>
13 #include <__algorithm/comp_ref_type.h>
14 #include <__algorithm/half_positive.h>
15 #include <__algorithm/iterator_operations.h>
16 #include <__algorithm/lower_bound.h>
17 #include <__algorithm/upper_bound.h>
18 #include <__config>
19 #include <__functional/identity.h>
20 #include <__iterator/advance.h>
21 #include <__iterator/distance.h>
22 #include <__iterator/iterator_traits.h>
23 #include <__utility/pair.h>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 template <class _Compare, class _ForwardIterator, class _Tp>
32 _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
33 __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
34 {
35     typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
36     difference_type __len = _VSTD::distance(__first, __last);
37     while (__len != 0)
38     {
39         difference_type __l2 = _VSTD::__half_positive(__len);
40         _ForwardIterator __m = __first;
41         _VSTD::advance(__m, __l2);
42         if (__comp(*__m, __value))
43         {
44             __first = ++__m;
45             __len -= __l2 + 1;
46         }
47         else if (__comp(__value, *__m))
48         {
49             __last = __m;
50             __len = __l2;
51         }
52         else
53         {
54             auto __proj = std::__identity();
55             _ForwardIterator __mp1 = __m;
56             return pair<_ForwardIterator, _ForwardIterator>
57                    (
58                       _VSTD::__lower_bound_impl<_ClassicAlgPolicy>(__first, __m, __value, __comp, __proj),
59                       _VSTD::__upper_bound<_Compare>(++__mp1, __last, __value, __comp)
60                    );
61         }
62     }
63     return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
64 }
65 
66 template <class _ForwardIterator, class _Tp, class _Compare>
67 _LIBCPP_NODISCARD_EXT inline
68 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
69 pair<_ForwardIterator, _ForwardIterator>
70 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
71 {
72     typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
73     return _VSTD::__equal_range<_Comp_ref>(__first, __last, __value, __comp);
74 }
75 
76 template <class _ForwardIterator, class _Tp>
77 _LIBCPP_NODISCARD_EXT inline
78 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
79 pair<_ForwardIterator, _ForwardIterator>
80 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
81 {
82     return _VSTD::equal_range(__first, __last, __value,
83                              __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
84 }
85 
86 _LIBCPP_END_NAMESPACE_STD
87 
88 #endif // _LIBCPP___ALGORITHM_EQUAL_RANGE_H
89