Path: blob/main/contrib/llvm-project/libcxx/include/__algorithm/equal_range.h
35232 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___ALGORITHM_EQUAL_RANGE_H9#define _LIBCPP___ALGORITHM_EQUAL_RANGE_H1011#include <__algorithm/comp.h>12#include <__algorithm/comp_ref_type.h>13#include <__algorithm/half_positive.h>14#include <__algorithm/iterator_operations.h>15#include <__algorithm/lower_bound.h>16#include <__algorithm/upper_bound.h>17#include <__config>18#include <__functional/identity.h>19#include <__functional/invoke.h>20#include <__iterator/advance.h>21#include <__iterator/distance.h>22#include <__iterator/iterator_traits.h>23#include <__iterator/next.h>24#include <__type_traits/is_callable.h>25#include <__type_traits/is_constructible.h>26#include <__utility/move.h>27#include <__utility/pair.h>2829#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)30# pragma GCC system_header31#endif3233_LIBCPP_PUSH_MACROS34#include <__undef_macros>3536_LIBCPP_BEGIN_NAMESPACE_STD3738template <class _AlgPolicy, class _Compare, class _Iter, class _Sent, class _Tp, class _Proj>39_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter, _Iter>40__equal_range(_Iter __first, _Sent __last, const _Tp& __value, _Compare&& __comp, _Proj&& __proj) {41auto __len = _IterOps<_AlgPolicy>::distance(__first, __last);42_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);43while (__len != 0) {44auto __half_len = std::__half_positive(__len);45_Iter __mid = _IterOps<_AlgPolicy>::next(__first, __half_len);46if (std::__invoke(__comp, std::__invoke(__proj, *__mid), __value)) {47__first = ++__mid;48__len -= __half_len + 1;49} else if (std::__invoke(__comp, __value, std::__invoke(__proj, *__mid))) {50__end = __mid;51__len = __half_len;52} else {53_Iter __mp1 = __mid;54return pair<_Iter, _Iter>(std::__lower_bound<_AlgPolicy>(__first, __mid, __value, __comp, __proj),55std::__upper_bound<_AlgPolicy>(++__mp1, __end, __value, __comp, __proj));56}57}58return pair<_Iter, _Iter>(__first, __first);59}6061template <class _ForwardIterator, class _Tp, class _Compare>62_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>63equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {64static_assert(__is_callable<_Compare, decltype(*__first), const _Tp&>::value, "The comparator has to be callable");65static_assert(is_copy_constructible<_ForwardIterator>::value, "Iterator has to be copy constructible");66return std::__equal_range<_ClassicAlgPolicy>(67std::move(__first),68std::move(__last),69__value,70static_cast<__comp_ref_type<_Compare> >(__comp),71std::__identity());72}7374template <class _ForwardIterator, class _Tp>75_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>76equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {77return std::equal_range(std::move(__first), std::move(__last), __value, __less<>());78}7980_LIBCPP_END_NAMESPACE_STD8182_LIBCPP_POP_MACROS8384#endif // _LIBCPP___ALGORITHM_EQUAL_RANGE_H858687