86 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___ALGORITHM_EQUAL_RANGE_H10#define _LIBCPP___CXX03___ALGORITHM_EQUAL_RANGE_H11 12#include <__cxx03/__algorithm/comp.h>13#include <__cxx03/__algorithm/comp_ref_type.h>14#include <__cxx03/__algorithm/half_positive.h>15#include <__cxx03/__algorithm/iterator_operations.h>16#include <__cxx03/__algorithm/lower_bound.h>17#include <__cxx03/__algorithm/upper_bound.h>18#include <__cxx03/__config>19#include <__cxx03/__functional/identity.h>20#include <__cxx03/__iterator/advance.h>21#include <__cxx03/__iterator/distance.h>22#include <__cxx03/__iterator/iterator_traits.h>23#include <__cxx03/__iterator/next.h>24#include <__cxx03/__type_traits/invoke.h>25#include <__cxx03/__type_traits/is_callable.h>26#include <__cxx03/__type_traits/is_constructible.h>27#include <__cxx03/__utility/move.h>28#include <__cxx03/__utility/pair.h>29 30#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)31# pragma GCC system_header32#endif33 34_LIBCPP_PUSH_MACROS35#include <__cxx03/__undef_macros>36 37_LIBCPP_BEGIN_NAMESPACE_STD38 39template <class _AlgPolicy, class _Compare, class _Iter, class _Sent, class _Tp, class _Proj>40_LIBCPP_HIDE_FROM_ABI pair<_Iter, _Iter>41__equal_range(_Iter __first, _Sent __last, const _Tp& __value, _Compare&& __comp, _Proj&& __proj) {42 auto __len = _IterOps<_AlgPolicy>::distance(__first, __last);43 _Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);44 while (__len != 0) {45 auto __half_len = std::__half_positive(__len);46 _Iter __mid = _IterOps<_AlgPolicy>::next(__first, __half_len);47 if (std::__invoke(__comp, std::__invoke(__proj, *__mid), __value)) {48 __first = ++__mid;49 __len -= __half_len + 1;50 } else if (std::__invoke(__comp, __value, std::__invoke(__proj, *__mid))) {51 __end = __mid;52 __len = __half_len;53 } else {54 _Iter __mp1 = __mid;55 return pair<_Iter, _Iter>(std::__lower_bound<_AlgPolicy>(__first, __mid, __value, __comp, __proj),56 std::__upper_bound<_AlgPolicy>(++__mp1, __end, __value, __comp, __proj));57 }58 }59 return pair<_Iter, _Iter>(__first, __first);60}61 62template <class _ForwardIterator, class _Tp, class _Compare>63_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _ForwardIterator>64equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {65 static_assert(__is_callable<_Compare, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");66 static_assert(is_copy_constructible<_ForwardIterator>::value, "Iterator has to be copy constructible");67 return std::__equal_range<_ClassicAlgPolicy>(68 std::move(__first),69 std::move(__last),70 __value,71 static_cast<__comp_ref_type<_Compare> >(__comp),72 std::__identity());73}74 75template <class _ForwardIterator, class _Tp>76_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _ForwardIterator>77equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {78 return std::equal_range(std::move(__first), std::move(__last), __value, __less<>());79}80 81_LIBCPP_END_NAMESPACE_STD82 83_LIBCPP_POP_MACROS84 85#endif // _LIBCPP___CXX03___ALGORITHM_EQUAL_RANGE_H86