CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
FindIf.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
14
15namespace CeresEngine {
16
17 class FindAlgorithmFunctor;
18 class FindIfNotAlgorithmFunctor;
19
21 class FindIfAlgorithmFunctor : public UnaryAlgorithmFunctor<FindIfAlgorithmFunctor> {
25
27 template<class Executor, class I> class Graph {
28 private:
29 const Executor& mExecutor;
30
31 public:
32 explicit Graph(const Executor& executor) : mExecutor(executor) {}
33
34 template<class P, class S, class Fun> Async<I> launch(P p, I first, S last, S overallLast, Fun f) {
35 const I middle = p(first, last);
37 constexpr traits::iter_difference_t<I> isNotParallelize = traits::is_forward_iterator_v<I>;
39 return with(mExecutor, [first, last, f, overallLast]() {
40 const I found = std::find_if(first, last, f);
41 if(found == last) {
42 return overallLast;
43 }
44 return found;
45 });
46 }
47
48 return when_all(
49 // Create task that launches tasks for rhs: [middle, last]
50 launch(p, middle, last, overallLast, f),
51
52 // Launch tasks for lhs: [first, middle]
53 launch(p, first, middle, overallLast, f))
54 .then([overallLast](const I& lhs, const I& rhs) {
55 if(lhs != overallLast) {
56 return lhs;
57 }
58 return rhs;
59 });
60 }
61
62 template<class P, class S, class Fun> Async<I> launch(P p, I first, S last, Fun f) { return launch(p, first, last, last, f); }
63 };
64
73 template<class I, class S, class Fun>
74 requires(
75 // clang-format off
76 traits::is_input_iterator_v<I> &&
77 traits::is_sentinel_for_v<S, I> &&
78 traits::is_indirectly_unary_invocable_v<Fun, I> &&
79 std::is_copy_constructible_v<Fun>
80 // clang-format on
81 ) static constexpr I run(I first, S last, Fun p) {
82 for(; first != last; ++first) {
83 if(p(*first)) {
84 return first;
85 }
86 }
87 return last;
88 }
89
102 template<class E, class P, class I, class S, class Fun>
103 requires(
104 // clang-format off
105 traits::is_executor_v<E> &&
106 traits::is_partitioner_v<P, I, S> &&
107 traits::is_input_iterator_v<I> &&
108 traits::is_sentinel_for_v<S, I> &&
109 traits::is_indirectly_unary_invocable_v<Fun, I> &&
110 std::is_copy_constructible_v<Fun>
111 // clang-format on
112 ) static Async<I> run(const E& executor, P p, I first, S last, Fun f) {
113 if constexpr(traits::is_inline_executor_v<E>) {
114 co_return run(first, last, f);
115 } else {
116 co_return co_await Graph<E, I>(executor).launch(p, first, last, f);
117 }
118 }
119 };
120
123
124} // 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...
Async< I > launch(P p, I first, S last, S overallLast, Fun f)
Definition FindIf.hpp:34
Graph(const Executor &executor)
Definition FindIf.hpp:32
const Executor & mExecutor
Definition FindIf.hpp:29
Async< I > launch(P p, I first, S last, Fun f)
Definition FindIf.hpp:62
Functor representing the overloads for the find_if function.
Definition FindIf.hpp:21
static constexpr I run(I first, S last, Fun p)
Complete overload of the find_if algorithm.
Definition FindIf.hpp:81
static Async< I > run(const E &executor, P p, I first, S last, Fun f)
Complete overload of the find_if algorithm.
Definition FindIf.hpp:112
friend FindIfNotAlgorithmFunctor
Definition FindIf.hpp:24
friend FindAlgorithmFunctor
Definition FindIf.hpp:23
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 FindIfAlgorithmFunctor find_if
Finds the first element satisfying specific criteria.
Definition FindIf.hpp:122
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
decltype(&*std::declval< I >() found)(const I &it, C &container)
Returns a pointer to the value if found, otherwise nullptr.
Definition Iterator.hpp:572