54 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___ALGORITHM_FOR_EACH_H11#define _LIBCPP___ALGORITHM_FOR_EACH_H12 13#include <__algorithm/for_each_segment.h>14#include <__config>15#include <__functional/identity.h>16#include <__iterator/segmented_iterator.h>17#include <__type_traits/invoke.h>18#include <__type_traits/is_same.h>19 20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif23 24_LIBCPP_BEGIN_NAMESPACE_STD25 26template <class _InputIterator, class _Sent, class _Func, class _Proj>27_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator28__for_each(_InputIterator __first, _Sent __last, _Func& __func, _Proj& __proj) {29#ifndef _LIBCPP_CXX03_LANG30 if constexpr (is_same<_InputIterator, _Sent>::value && __is_segmented_iterator_v<_InputIterator>) {31 using __local_iterator_t = typename __segmented_iterator_traits<_InputIterator>::__local_iterator;32 std::__for_each_segment(__first, __last, [&](__local_iterator_t __lfirst, __local_iterator_t __llast) {33 std::__for_each(__lfirst, __llast, __func, __proj);34 });35 return __last;36 }37#endif38 for (; __first != __last; ++__first)39 std::__invoke(__func, std::__invoke(__proj, *__first));40 return __first;41}42 43template <class _InputIterator, class _Func>44_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Func45for_each(_InputIterator __first, _InputIterator __last, _Func __f) {46 __identity __proj;47 std::__for_each(__first, __last, __f, __proj);48 return __f;49}50 51_LIBCPP_END_NAMESPACE_STD52 53#endif // _LIBCPP___ALGORITHM_FOR_EACH_H54