CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CountIf.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 "UnaryAlgorithm.hpp"
11
15
16namespace CeresEngine {
17
18 class CountAlgorithmFunctor;
19
21 class CountIfAlgorithmFunctor : public UnaryAlgorithmFunctor<CountIfAlgorithmFunctor> {
24
26 template<class Executor, class I> class Graph {
27 private:
28 const Executor& mExecutor;
29
30 public:
31 explicit Graph(const Executor& executor) : mExecutor(executor) {}
32
33 template<class P, class S, class Fun> Async<traits::iter_difference_t<I>> launch(P p, I first, S last, Fun f) {
34 auto middle = p(first, last);
36 constexpr traits::iter_difference_t<I> isNotParallelize = traits::is_forward_iterator_v<I>;
38 return with(mExecutor, [first, last, f]() { return run(first, last, f); });
39 }
40
41 return when_all(
42 // Create task that launches tasks for rhs: [middle, last]
43 launch(p, middle, last, f),
44
45 // Launch tasks for lhs: [first, middle]
46 launch(p, first, middle, f))
47 .then(std::plus{});
48 }
49 };
50
59 template<class I, class S, class Fun>
60 requires(
61 // clang-format off
62 traits::is_input_iterator_v<I> &&
63 traits::is_sentinel_for_v<S, I> &&
64 traits::is_indirectly_unary_invocable_v<Fun, I> &&
65 std::is_copy_constructible_v<Fun>
66 // clang-format on
67 ) static constexpr traits::iter_difference_t<I> run(I first, S last, Fun p) {
68 return std::count_if(first, last, std::move(p));
69 }
70
83 template<class E, class P, class I, class S, class Fun>
84 requires(
85 // clang-format off
86 traits::is_executor_v<E> &&
87 traits::is_partitioner_v<P, I, S> &&
88 traits::is_input_iterator_v<I> &&
89 traits::is_sentinel_for_v<S, I> &&
90 traits::is_indirectly_unary_invocable_v<Fun, I> &&
91 std::is_copy_constructible_v<Fun>
92 // clang-format on
93 ) static Async<traits::iter_difference_t<I>> run(const E& executor, P p, I first, S last, Fun f) {
94 if constexpr(traits::is_inline_executor_v<E>) {
95 co_return run(first, last, std::move(f));
96 } else {
97 co_return co_await Graph<E, I>(executor).launch(p, first, last, f);
98 }
99 }
100 };
101
104
105} // namespace CeresEngine
A partitioner is a light callable object that takes a pair of iterators and returns the middle of the...
Identify traits for algorithms, like we do for other types The traits help us generate auxiliary algo...
const Executor & mExecutor
Definition CountIf.hpp:28
Graph(const Executor &executor)
Definition CountIf.hpp:31
Async< traits::iter_difference_t< I > > launch(P p, I first, S last, Fun f)
Definition CountIf.hpp:33
Functor representing the overloads for the count_if function.
Definition CountIf.hpp:21
friend CountAlgorithmFunctor
Definition CountIf.hpp:23
static constexpr traits::iter_difference_t< I > run(I first, S last, Fun p)
Complete overload of the count_if algorithm.
Definition CountIf.hpp:67
static Async< traits::iter_difference_t< I > > run(const E &executor, P p, I first, S last, Fun f)
Complete overload of the count_if algorithm.
Definition CountIf.hpp:93
Overloads for unary invoke algorithms.
Definition UnaryAlgorithm.hpp:34
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 CountIfAlgorithmFunctor count_if
Returns the number of elements satisfying specific criteria.
Definition CountIf.hpp:103
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25