Path: blob/main/contrib/llvm-project/libcxx/include/__pstl/backends/serial.h
35266 views
// -*- 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//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___PSTL_BACKENDS_SERIAL_H10#define _LIBCPP___PSTL_BACKENDS_SERIAL_H1112#include <__algorithm/find_if.h>13#include <__algorithm/for_each.h>14#include <__algorithm/merge.h>15#include <__algorithm/stable_sort.h>16#include <__algorithm/transform.h>17#include <__config>18#include <__numeric/transform_reduce.h>19#include <__pstl/backend_fwd.h>20#include <__utility/empty.h>21#include <__utility/forward.h>22#include <__utility/move.h>23#include <optional>2425#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)26# pragma GCC system_header27#endif2829_LIBCPP_PUSH_MACROS30#include <__undef_macros>3132_LIBCPP_BEGIN_NAMESPACE_STD33namespace __pstl {3435//36// This partial PSTL backend runs everything serially.37//38// TODO: Right now, the serial backend must be used with another backend39// like the "default backend" because it doesn't implement all the40// necessary PSTL operations. It would be better to dispatch all41// algorithms to their serial counterpart directly, since this can42// often be more efficient than the "default backend"'s implementation43// if we end up running serially anyways.44//4546template <class _ExecutionPolicy>47struct __find_if<__serial_backend_tag, _ExecutionPolicy> {48template <class _Policy, class _ForwardIterator, class _Pred>49_LIBCPP_HIDE_FROM_ABI optional<_ForwardIterator>50operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Pred&& __pred) const noexcept {51return std::find_if(std::move(__first), std::move(__last), std::forward<_Pred>(__pred));52}53};5455template <class _ExecutionPolicy>56struct __for_each<__serial_backend_tag, _ExecutionPolicy> {57template <class _Policy, class _ForwardIterator, class _Function>58_LIBCPP_HIDE_FROM_ABI optional<__empty>59operator()(_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _Function&& __func) const noexcept {60std::for_each(std::move(__first), std::move(__last), std::forward<_Function>(__func));61return __empty{};62}63};6465template <class _ExecutionPolicy>66struct __merge<__serial_backend_tag, _ExecutionPolicy> {67template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardOutIterator, class _Comp>68_LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> operator()(69_Policy&&,70_ForwardIterator1 __first1,71_ForwardIterator1 __last1,72_ForwardIterator2 __first2,73_ForwardIterator2 __last2,74_ForwardOutIterator __outit,75_Comp&& __comp) const noexcept {76return std::merge(77std::move(__first1),78std::move(__last1),79std::move(__first2),80std::move(__last2),81std::move(__outit),82std::forward<_Comp>(__comp));83}84};8586template <class _ExecutionPolicy>87struct __stable_sort<__serial_backend_tag, _ExecutionPolicy> {88template <class _Policy, class _RandomAccessIterator, class _Comp>89_LIBCPP_HIDE_FROM_ABI optional<__empty>90operator()(_Policy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp&& __comp) const noexcept {91std::stable_sort(std::move(__first), std::move(__last), std::forward<_Comp>(__comp));92return __empty{};93}94};9596template <class _ExecutionPolicy>97struct __transform<__serial_backend_tag, _ExecutionPolicy> {98template <class _Policy, class _ForwardIterator, class _ForwardOutIterator, class _UnaryOperation>99_LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> operator()(100_Policy&&, _ForwardIterator __first, _ForwardIterator __last, _ForwardOutIterator __outit, _UnaryOperation&& __op)101const noexcept {102return std::transform(103std::move(__first), std::move(__last), std::move(__outit), std::forward<_UnaryOperation>(__op));104}105};106107template <class _ExecutionPolicy>108struct __transform_binary<__serial_backend_tag, _ExecutionPolicy> {109template <class _Policy,110class _ForwardIterator1,111class _ForwardIterator2,112class _ForwardOutIterator,113class _BinaryOperation>114_LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>115operator()(_Policy&&,116_ForwardIterator1 __first1,117_ForwardIterator1 __last1,118_ForwardIterator2 __first2,119_ForwardOutIterator __outit,120_BinaryOperation&& __op) const noexcept {121return std::transform(122std::move(__first1),123std::move(__last1),124std::move(__first2),125std::move(__outit),126std::forward<_BinaryOperation>(__op));127}128};129130template <class _ExecutionPolicy>131struct __transform_reduce<__serial_backend_tag, _ExecutionPolicy> {132template <class _Policy, class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation>133_LIBCPP_HIDE_FROM_ABI optional<_Tp>134operator()(_Policy&&,135_ForwardIterator __first,136_ForwardIterator __last,137_Tp __init,138_BinaryOperation&& __reduce,139_UnaryOperation&& __transform) const noexcept {140return std::transform_reduce(141std::move(__first),142std::move(__last),143std::move(__init),144std::forward<_BinaryOperation>(__reduce),145std::forward<_UnaryOperation>(__transform));146}147};148149template <class _ExecutionPolicy>150struct __transform_reduce_binary<__serial_backend_tag, _ExecutionPolicy> {151template <class _Policy,152class _ForwardIterator1,153class _ForwardIterator2,154class _Tp,155class _BinaryOperation1,156class _BinaryOperation2>157_LIBCPP_HIDE_FROM_ABI optional<_Tp> operator()(158_Policy&&,159_ForwardIterator1 __first1,160_ForwardIterator1 __last1,161_ForwardIterator2 __first2,162_Tp __init,163_BinaryOperation1&& __reduce,164_BinaryOperation2&& __transform) const noexcept {165return std::transform_reduce(166std::move(__first1),167std::move(__last1),168std::move(__first2),169std::move(__init),170std::forward<_BinaryOperation1>(__reduce),171std::forward<_BinaryOperation2>(__transform));172}173};174175} // namespace __pstl176_LIBCPP_END_NAMESPACE_STD177178_LIBCPP_POP_MACROS179180#endif // _LIBCPP___PSTL_BACKENDS_SERIAL_H181182183