brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · e9c84e8 Raw
90 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___ALGORITHM_RANGES_FOR_EACH_H10#define _LIBCPP___ALGORITHM_RANGES_FOR_EACH_H11 12#include <__algorithm/for_each.h>13#include <__algorithm/for_each_n.h>14#include <__algorithm/in_fun_result.h>15#include <__concepts/assignable.h>16#include <__config>17#include <__functional/identity.h>18#include <__iterator/concepts.h>19#include <__iterator/projected.h>20#include <__ranges/access.h>21#include <__ranges/concepts.h>22#include <__ranges/dangling.h>23#include <__utility/move.h>24 25#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)26#  pragma GCC system_header27#endif28 29_LIBCPP_PUSH_MACROS30#include <__undef_macros>31 32#if _LIBCPP_STD_VER >= 2033 34_LIBCPP_BEGIN_NAMESPACE_STD35 36namespace ranges {37 38template <class _Iter, class _Func>39using for_each_result = in_fun_result<_Iter, _Func>;40 41struct __for_each {42private:43  template <class _Iter, class _Sent, class _Proj, class _Func>44  _LIBCPP_HIDE_FROM_ABI constexpr static for_each_result<_Iter, _Func>45  __for_each_impl(_Iter __first, _Sent __last, _Func& __func, _Proj& __proj) {46    // In the case where we have different iterator and sentinel types, the segmented iterator optimization47    // in std::for_each will not kick in. Therefore, we prefer std::for_each_n in that case (whenever we can48    // obtain the `n`).49    if constexpr (!std::assignable_from<_Iter&, _Sent> && std::sized_sentinel_for<_Sent, _Iter>) {50      auto __n   = __last - __first;51      auto __end = std::__for_each_n(std::move(__first), __n, __func, __proj);52      return {std::move(__end), std::move(__func)};53    } else {54      auto __end = std::__for_each(std::move(__first), std::move(__last), __func, __proj);55      return {std::move(__end), std::move(__func)};56    }57  }58 59public:60  template <input_iterator _Iter,61            sentinel_for<_Iter> _Sent,62            class _Proj = identity,63            indirectly_unary_invocable<projected<_Iter, _Proj>> _Func>64  _LIBCPP_HIDE_FROM_ABI constexpr for_each_result<_Iter, _Func>65  operator()(_Iter __first, _Sent __last, _Func __func, _Proj __proj = {}) const {66    return __for_each_impl(std::move(__first), std::move(__last), __func, __proj);67  }68 69  template <input_range _Range,70            class _Proj = identity,71            indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>> _Func>72  _LIBCPP_HIDE_FROM_ABI constexpr for_each_result<borrowed_iterator_t<_Range>, _Func>73  operator()(_Range&& __range, _Func __func, _Proj __proj = {}) const {74    return __for_each_impl(ranges::begin(__range), ranges::end(__range), __func, __proj);75  }76};77 78inline namespace __cpo {79inline constexpr auto for_each = __for_each{};80} // namespace __cpo81} // namespace ranges82 83_LIBCPP_END_NAMESPACE_STD84 85#endif // _LIBCPP_STD_VER >= 2086 87_LIBCPP_POP_MACROS88 89#endif // _LIBCPP___ALGORITHM_RANGES_FOR_EACH_H90