15 #ifndef BELUGA_ACTIONS_NORMALIZE_HPP
16 #define BELUGA_ACTIONS_NORMALIZE_HPP
21 #include <range/v3/action/action.hpp>
22 #include <range/v3/numeric/accumulate.hpp>
23 #include <range/v3/view/common.hpp>
48 class ExecutionPolicy,
50 std::enable_if_t<std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>,
int> = 0,
51 std::enable_if_t<ranges::range<Range>,
int> = 0>
52 constexpr
auto operator()(ExecutionPolicy&&
policy, Range& range,
double factor)
const -> Range& {
53 if (std::abs(factor - 1.0) < std::numeric_limits<double>::epsilon()) {
58 if constexpr (beluga::is_particle_range_v<Range>) {
61 return range | ranges::views::common;
70 [factor](
const auto w) {
return w / factor; });
79 class ExecutionPolicy,
81 std::enable_if_t<std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>,
int> = 0,
82 std::enable_if_t<ranges::range<Range>,
int> = 0>
85 if constexpr (beluga::is_particle_range_v<Range>) {
88 return range | ranges::views::common;
92 const double total_weight = ranges::accumulate(
weights, 0.0);
93 return (*
this)(std::forward<ExecutionPolicy>(
policy), range, total_weight);
99 class ExecutionPolicy,
100 std::enable_if_t<ranges::range<Range>,
int> = 0,
101 std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>,
int> = 0>
102 constexpr
auto operator()(Range&& range,
double factor, ExecutionPolicy
policy)
const -> Range& {
103 return (*
this)(std::move(
policy), std::forward<Range>(range), factor);
109 class ExecutionPolicy,
110 std::enable_if_t<ranges::range<Range>,
int> = 0,
111 std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>,
int> = 0>
113 return (*
this)(std::move(
policy), std::forward<Range>(range));
117 template <
class ExecutionPolicy, std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>,
int> = 0>
123 template <
class ExecutionPolicy, std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>,
int> = 0>
131 using normalize_base_fn::operator();
134 template <
class Range, std::enable_if_t<ranges::range<Range>,
int> = 0>
135 constexpr
auto operator()(Range&& range,
double factor)
const -> Range& {
136 return (*
this)(std::execution::seq, std::forward<Range>(range), factor);
140 template <
class Range, std::enable_if_t<ranges::range<Range>,
int> = 0>
142 return (*
this)(std::execution::seq, std::forward<Range>(range));
147 return ranges::make_action_closure(ranges::bind_back(
normalize_fn{}, factor));
162 inline constexpr ranges::actions::action_closure<detail::normalize_fn>
normalize;