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