Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:07

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___ALGORITHM_EQUAL_RANGE_H
0010 #define _LIBCPP___ALGORITHM_EQUAL_RANGE_H
0011 
0012 #include <__algorithm/comp.h>
0013 #include <__algorithm/comp_ref_type.h>
0014 #include <__algorithm/half_positive.h>
0015 #include <__algorithm/iterator_operations.h>
0016 #include <__algorithm/lower_bound.h>
0017 #include <__algorithm/upper_bound.h>
0018 #include <__config>
0019 #include <__functional/identity.h>
0020 #include <__type_traits/invoke.h>
0021 #include <__type_traits/is_callable.h>
0022 #include <__type_traits/is_constructible.h>
0023 #include <__utility/move.h>
0024 #include <__utility/pair.h>
0025 
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 #  pragma GCC system_header
0028 #endif
0029 
0030 _LIBCPP_PUSH_MACROS
0031 #include <__undef_macros>
0032 
0033 _LIBCPP_BEGIN_NAMESPACE_STD
0034 
0035 template <class _AlgPolicy, class _Compare, class _Iter, class _Sent, class _Tp, class _Proj>
0036 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter, _Iter>
0037 __equal_range(_Iter __first, _Sent __last, const _Tp& __value, _Compare&& __comp, _Proj&& __proj) {
0038   auto __len  = _IterOps<_AlgPolicy>::distance(__first, __last);
0039   _Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
0040   while (__len != 0) {
0041     auto __half_len = std::__half_positive(__len);
0042     _Iter __mid     = _IterOps<_AlgPolicy>::next(__first, __half_len);
0043     if (std::__invoke(__comp, std::__invoke(__proj, *__mid), __value)) {
0044       __first = ++__mid;
0045       __len -= __half_len + 1;
0046     } else if (std::__invoke(__comp, __value, std::__invoke(__proj, *__mid))) {
0047       __end = __mid;
0048       __len = __half_len;
0049     } else {
0050       _Iter __mp1 = __mid;
0051       return pair<_Iter, _Iter>(std::__lower_bound<_AlgPolicy>(__first, __mid, __value, __comp, __proj),
0052                                 std::__upper_bound<_AlgPolicy>(++__mp1, __end, __value, __comp, __proj));
0053     }
0054   }
0055   return pair<_Iter, _Iter>(__first, __first);
0056 }
0057 
0058 template <class _ForwardIterator, class _Tp, class _Compare>
0059 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
0060 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
0061   static_assert(__is_callable<_Compare&, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");
0062   static_assert(is_copy_constructible<_ForwardIterator>::value, "Iterator has to be copy constructible");
0063   return std::__equal_range<_ClassicAlgPolicy>(
0064       std::move(__first),
0065       std::move(__last),
0066       __value,
0067       static_cast<__comp_ref_type<_Compare> >(__comp),
0068       std::__identity());
0069 }
0070 
0071 template <class _ForwardIterator, class _Tp>
0072 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
0073 equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
0074   return std::equal_range(std::move(__first), std::move(__last), __value, __less<>());
0075 }
0076 
0077 _LIBCPP_END_NAMESPACE_STD
0078 
0079 _LIBCPP_POP_MACROS
0080 
0081 #endif // _LIBCPP___ALGORITHM_EQUAL_RANGE_H