Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created July 30, 2023 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttsugriy/be42b9596c0210b671c713ddbd3733da to your computer and use it in GitHub Desktop.
Save ttsugriy/be42b9596c0210b671c713ddbd3733da to your computer and use it in GitHub Desktop.
equal_range in LLVM
template <class _AlgPolicy, class _Compare, class _Iter, class _Sent, class _Tp, class _Proj>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter, _Iter>
__equal_range(_Iter __first, _Sent __last, const _Tp& __value, _Compare&& __comp, _Proj&& __proj) {
auto __len = _IterOps<_AlgPolicy>::distance(__first, __last);
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
while (__len != 0) {
auto __half_len = std::__half_positive(__len);
_Iter __mid = _IterOps<_AlgPolicy>::next(__first, __half_len);
if (std::__invoke(__comp, std::__invoke(__proj, *__mid), __value)) {
__first = ++__mid;
__len -= __half_len + 1;
} else if (std::__invoke(__comp, __value, std::__invoke(__proj, *__mid))) {
__end = __mid;
__len = __half_len;
} else {
_Iter __mp1 = __mid;
return pair<_Iter, _Iter>(
std::__lower_bound<_AlgPolicy>(__first, __mid, __value, __comp, __proj),
std::__upper_bound<_AlgPolicy>(++__mp1, __end, __value, __comp, __proj));
}
}
return pair<_Iter, _Iter>(__first, __first);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment