propagate.hpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BELUGA_ACTIONS_PROPAGATE_HPP
16 #define BELUGA_ACTIONS_PROPAGATE_HPP
17 
18 #include <algorithm>
19 #include <execution>
20 #include <type_traits>
21 
24 
25 #include <range/v3/action/action.hpp>
26 #include <range/v3/utility/random.hpp>
27 #include <range/v3/view/common.hpp>
28 
34 namespace beluga::actions {
35 
36 namespace detail {
37 
41 
51  template <
52  class ExecutionPolicy,
53  class Range,
54  class StateSamplingFunction,
55  std::enable_if_t<std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>, int> = 0,
56  std::enable_if_t<ranges::range<Range>, int> = 0>
57  constexpr auto operator()(ExecutionPolicy&& policy, Range& range, StateSamplingFunction fn) const -> Range& {
58  static_assert(beluga::is_particle_range_v<Range>);
59  auto states = range | beluga::views::states | ranges::views::common;
60 
61  auto unary_fn = [&]() {
62  using States = decltype(states);
63  using State = ranges::range_value_t<States>;
64  using Generator = decltype(ranges::detail::get_random_engine());
65  if constexpr (std::is_invocable_v<StateSamplingFunction, State, Generator>) {
66  return [fn = std::move(fn)](const State& state) { return fn(state, ranges::detail::get_random_engine()); };
67  } else {
68  return std::move(fn);
69  }
70  }();
71 
72  std::transform(
73  policy, // rvalue policies are not supported in some STL implementations
74  std::begin(states), //
75  std::end(states), //
76  std::begin(states), //
77  std::move(unary_fn));
78  return range;
79  }
80 
82  template <
83  class Range,
84  class StateSamplingFunction,
85  class ExecutionPolicy,
86  std::enable_if_t<ranges::range<Range>, int> = 0,
87  std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
88  constexpr auto operator()(Range&& range, StateSamplingFunction fn, ExecutionPolicy policy) const -> Range& {
89  return (*this)(std::move(policy), std::forward<Range>(range), std::move(fn));
90  }
91 
93  template <
94  class ExecutionPolicy, //
95  class StateSamplingFunction, //
96  std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
97  constexpr auto operator()(ExecutionPolicy policy, StateSamplingFunction fn) const {
98  return ranges::make_action_closure(ranges::bind_back(propagate_base_fn{}, std::move(fn), std::move(policy)));
99  }
100 };
101 
104  using propagate_base_fn::operator();
105 
107  template <
108  class Range, //
109  class StateSamplingFunction, //
110  std::enable_if_t<ranges::range<Range>, int> = 0>
111  constexpr auto operator()(Range&& range, StateSamplingFunction fn) const -> Range& {
112  return (*this)(std::execution::seq, std::forward<Range>(range), std::move(fn));
113  }
114 
116  template <class StateSamplingFunction>
117  constexpr auto operator()(StateSamplingFunction fn) const {
118  return ranges::make_action_closure(ranges::bind_back(propagate_fn{}, std::move(fn)));
119  }
120 };
121 
122 } // namespace detail
123 
126 
132 
133 } // namespace beluga::actions
134 
135 #endif
beluga::actions::detail::propagate_base_fn::operator()
constexpr auto operator()(ExecutionPolicy &&policy, Range &range, StateSamplingFunction fn) const -> Range &
Overload that implements the propagate algorithm.
Definition: propagate.hpp:57
beluga::actions
Definition: assign.hpp:28
beluga::actions::detail::propagate_fn::operator()
constexpr auto operator()(StateSamplingFunction fn) const
Overload that returns a view closure to compose with other views.
Definition: propagate.hpp:117
beluga::state
constexpr state_detail::state_fn state
Customization point object for accessing the state of a particle.
Definition: primitives.hpp:163
beluga::actions::detail::propagate_base_fn::operator()
constexpr auto operator()(Range &&range, StateSamplingFunction fn, ExecutionPolicy policy) const -> Range &
Overload that re-orders arguments from a view closure.
Definition: propagate.hpp:88
particles.hpp
Implementation of views related to particle ranges.
beluga::actions::propagate
constexpr detail::propagate_fn propagate
Definition: propagate.hpp:131
particle_traits.hpp
Implementation of traits for particle types, see the Particle named requirements.
beluga::actions::detail::propagate_fn
Implementation detail for a propagate range adaptor object with a default execution policy.
Definition: propagate.hpp:103
beluga::policy
Forward declaration of policy.
Definition: policy.hpp:24
beluga::actions::detail::propagate_base_fn
Implementation detail for a propagate range adaptor object.
Definition: propagate.hpp:39
beluga::actions::detail::propagate_base_fn::operator()
constexpr auto operator()(ExecutionPolicy policy, StateSamplingFunction fn) const
Overload that returns a view closure to compose with other views.
Definition: propagate.hpp:97
beluga::actions::detail::propagate_fn::operator()
constexpr auto operator()(Range &&range, StateSamplingFunction fn) const -> Range &
Overload that defines a default execution policy.
Definition: propagate.hpp:111
beluga::views::states
constexpr auto states
Definition: particles.hpp:30


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53