xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/equal_range.h (revision d56accc7c3dcc897489b6a07834763a03b9f3d68)
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/lower_bound.h>
16 #include <__algorithm/upper_bound.h>
17 #include <__config>
18 #include <iterator>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 template <class _Compare, class _ForwardIterator, class _Tp>
27 _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
28 __equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
29 {
30     typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
31     difference_type __len = _VSTD::distance(__first, __last);
32     while (__len != 0)
33     {
34         difference_type __l2 = _VSTD::__half_positive(__len);
35         _ForwardIterator __m = __first;
36         _VSTD::advance(__m, __l2);
37         if (__comp(*__m, __value_))
38         {
39             __first = ++__m;
40             __len -= __l2 + 1;
41         }
42         else if (__comp(__value_, *__m))
43         {
44             __last = __m;
45             __len = __l2;
46         }
47         else
48         {
49             _ForwardIterator __mp1 = __m;
50             return pair<_ForwardIterator, _ForwardIterator>
51                    (
52                       _VSTD::__lower_bound<_Compare>(__first, __m, __value_, __comp),
53                       _VSTD::__upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
54                    );
55         }
56     }
57     return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
58 }
59 
60 template <class _ForwardIterator, class _Tp, class _Compare>
61 _LIBCPP_NODISCARD_EXT inline
62 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
63 pair<_ForwardIterator, _ForwardIterator>
64 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
65 {
66     typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
67     return _VSTD::__equal_range<_Comp_ref>(__first, __last, __value_, __comp);
68 }
69 
70 template <class _ForwardIterator, class _Tp>
71 _LIBCPP_NODISCARD_EXT inline
72 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
73 pair<_ForwardIterator, _ForwardIterator>
74 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
75 {
76     return _VSTD::equal_range(__first, __last, __value_,
77                              __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
78 }
79 
80 _LIBCPP_END_NAMESPACE_STD
81 
82 #endif // _LIBCPP___ALGORITHM_EQUAL_RANGE_H
83