CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Reduce.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "BinaryAlgorithm.hpp"
11
13
14#include <numeric>
15
16namespace CeresEngine {
17
19 class ReduceAlgorithmFunctor : public BinaryAlgorithmFunctor<ReduceAlgorithmFunctor> {
21
23 template<class Executor, class T> class Graph {
24 private:
25 Executor mExecutor;
26
27 public:
28 explicit Graph(const Executor& executor) : mExecutor(executor) {}
29
30 template<class P, class I, class S, class Fun> Async<T> launch(P p, I first, S last, T i, Fun f) {
31 auto middle = p(first, last);
33 constexpr traits::iter_difference_t<I> isNotParallelize = traits::is_forward_iterator_v<I>;
35 return with(mExecutor, [first, last, i, f]() { return run(first, last, i, f); });
36 }
37
38 // Create task that launches tasks for rhs: [middle, last]
39 return when_all(
40 // Create task that launches tasks for rhs: [middle, last]
41 launch(p, middle, last, i, f),
42
43 // Launch tasks for lhs: [first, middle]
44 launch(p, first, middle, i, f))
45 .then(f);
46 }
47 };
48
60 template<class I, class S, class T, class Fun = std::plus<>>
61 requires(
62 // clang-format off
63 traits::is_input_iterator_v<I> &&
64 traits::is_sentinel_for_v<S, I> &&
65 std::is_same_v<traits::iter_value_t<I>, T> &&
66 traits::is_indirectly_binary_invocable_v<Fun, I, I> &&
67 std::is_copy_constructible_v<Fun>
68 // clang-format on
69 ) static constexpr T run(I first, S last, T init, Fun op) {
70 return std::accumulate(first, last, std::move(init), std::move(op));
71 }
72
88 template<class E, class P, class I, class S, class T, class Fun = std::plus<>>
89 requires(
90 // clang-format off
91 traits::is_executor_v<E> &&
92 traits::is_partitioner_v<P, I, S> &&
93 traits::is_input_iterator_v<I> &&
94 traits::is_sentinel_for_v<S, I> &&
95 std::is_same_v<traits::iter_value_t<I>, T> &&
96 traits::is_indirectly_binary_invocable_v<Fun, I, I> &&
97 std::is_copy_constructible_v<Fun>
98 // clang-format on
99 ) static Async<T> run(const E& executor, P p, I first, S last, T i, Fun f = Fun()) {
100 if constexpr(traits::is_inline_executor_v<E>) {
101 co_return run(first, last, i, f);
102 } else {
103 co_return co_await Graph<E, T>(executor).launch(p, first, last, i, f);
104 }
105 }
106 };
107
111
112} // namespace CeresEngine
A partitioner is a light callable object that takes a pair of iterators and returns the middle of the...
Binary algorithm overloads.
Definition BinaryAlgorithm.hpp:26
Async< T > launch(P p, I first, S last, T i, Fun f)
Definition Reduce.hpp:30
Executor mExecutor
Definition Reduce.hpp:25
Graph(const Executor &executor)
Definition Reduce.hpp:28
Functor representing the overloads for the reduce function.
Definition Reduce.hpp:19
static constexpr T run(I first, S last, T init, Fun op)
Complete overload of the reduce algorithm.
Definition Reduce.hpp:69
static Async< T > run(const E &executor, P p, I first, S last, T i, Fun f=Fun())
Complete overload of the reduce algorithm.
Definition Reduce.hpp:99
typename iter_difference< T >::type iter_difference_t
A C++17 type trait equivalent to the C++20 iter_difference concept.
Definition iter_difference.hpp:85
Definition Application.hpp:19
auto with(Executor executor)
Definition ExecutionContext.hpp:546
cti::continuable< Args... > Async
Defines a non-copyable continuation type which uses the function2 backend for type erasure.
Definition Async.hpp:22
constexpr ReduceAlgorithmFunctor reduce
Sums up (or accumulate with a custom function) a range of elements, except out of order.
Definition Reduce.hpp:110
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25