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 <__functional/invoke.h> 21 #include <__iterator/advance.h> 22 #include <__iterator/distance.h> 23 #include <__iterator/iterator_traits.h> 24 #include <__type_traits/is_callable.h> 25 #include <__type_traits/is_copy_constructible.h> 26 #include <__utility/move.h> 27 #include <__utility/pair.h> 28 29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 30 # pragma GCC system_header 31 #endif 32 33 _LIBCPP_BEGIN_NAMESPACE_STD 34 35 template <class _AlgPolicy, class _Compare, class _Iter, class _Sent, class _Tp, class _Proj> 36 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_Iter, _Iter> 37 __equal_range(_Iter __first, _Sent __last, const _Tp& __value, _Compare&& __comp, _Proj&& __proj) { 38 auto __len = _IterOps<_AlgPolicy>::distance(__first, __last); 39 _Iter __end = _IterOps<_AlgPolicy>::next(__first, __last); 40 while (__len != 0) { 41 auto __half_len = std::__half_positive(__len); 42 _Iter __mid = _IterOps<_AlgPolicy>::next(__first, __half_len); 43 if (std::__invoke(__comp, std::__invoke(__proj, *__mid), __value)) { 44 __first = ++__mid; 45 __len -= __half_len + 1; 46 } else if (std::__invoke(__comp, __value, std::__invoke(__proj, *__mid))) { 47 __end = __mid; 48 __len = __half_len; 49 } else { 50 _Iter __mp1 = __mid; 51 return pair<_Iter, _Iter>( 52 std::__lower_bound_impl<_AlgPolicy>(__first, __mid, __value, __comp, __proj), 53 std::__upper_bound<_AlgPolicy>(++__mp1, __end, __value, __comp, __proj)); 54 } 55 } 56 return pair<_Iter, _Iter>(__first, __first); 57 } 58 59 template <class _ForwardIterator, class _Tp, class _Compare> 60 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator> 61 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) { 62 static_assert(__is_callable<_Compare, decltype(*__first), const _Tp&>::value, 63 "The comparator has to be callable"); 64 static_assert(is_copy_constructible<_ForwardIterator>::value, 65 "Iterator has to be copy constructible"); 66 typedef typename __comp_ref_type<_Compare>::type _Comp_ref; 67 return std::__equal_range<_ClassicAlgPolicy>( 68 std::move(__first), std::move(__last), __value, static_cast<_Comp_ref>(__comp), std::__identity()); 69 } 70 71 template <class _ForwardIterator, class _Tp> 72 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator> 73 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) { 74 return std::equal_range( 75 std::move(__first), 76 std::move(__last), 77 __value, 78 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); 79 } 80 81 _LIBCPP_END_NAMESPACE_STD 82 83 #endif // _LIBCPP___ALGORITHM_EQUAL_RANGE_H 84