normalize.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_NORMALIZE_HPP
16 #define BELUGA_ACTIONS_NORMALIZE_HPP
17 
18 #include <algorithm>
19 #include <execution>
20 
21 #include <range/v3/action/action.hpp>
22 #include <range/v3/numeric/accumulate.hpp>
23 #include <range/v3/view/common.hpp>
24 
27 
33 namespace beluga::actions {
34 
35 namespace detail {
36 
40 
47  template <
48  class ExecutionPolicy,
49  class Range,
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()) {
54  return range; // No change.
55  }
56 
57  auto weights = [&range]() {
58  if constexpr (beluga::is_particle_range_v<Range>) {
59  return range | beluga::views::weights | ranges::views::common;
60  } else {
61  return range | ranges::views::common;
62  }
63  }();
64 
65  std::transform(
66  policy, //
67  std::begin(weights), //
68  std::end(weights), //
69  std::begin(weights), //
70  [factor](const auto w) { return w / factor; });
71  return range;
72  }
73 
75 
78  template <
79  class ExecutionPolicy,
80  class Range,
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>
83  constexpr auto operator()(ExecutionPolicy&& policy, Range& range) const -> Range& {
84  auto weights = [&range]() {
85  if constexpr (beluga::is_particle_range_v<Range>) {
86  return range | beluga::views::weights | ranges::views::common;
87  } else {
88  return range | ranges::views::common;
89  }
90  }();
91 
92  const double total_weight = ranges::accumulate(weights, 0.0);
93  return (*this)(std::forward<ExecutionPolicy>(policy), range, total_weight);
94  }
95 
97  template <
98  class Range,
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);
104  }
105 
107  template <
108  class Range,
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>
112  constexpr auto operator()(Range&& range, ExecutionPolicy policy) const -> Range& {
113  return (*this)(std::move(policy), std::forward<Range>(range));
114  }
115 
117  template <class ExecutionPolicy, std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
118  constexpr auto operator()(ExecutionPolicy policy, double factor) const {
119  return ranges::make_action_closure(ranges::bind_back(normalize_base_fn{}, factor, std::move(policy)));
120  }
121 
123  template <class ExecutionPolicy, std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
124  constexpr auto operator()(ExecutionPolicy policy) const {
125  return ranges::make_action_closure(ranges::bind_back(normalize_base_fn{}, std::move(policy)));
126  }
127 };
128 
131  using normalize_base_fn::operator();
132 
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);
137  }
138 
140  template <class Range, std::enable_if_t<ranges::range<Range>, int> = 0>
141  constexpr auto operator()(Range&& range) const -> Range& {
142  return (*this)(std::execution::seq, std::forward<Range>(range));
143  }
144 
146  constexpr auto operator()(double factor) const {
147  return ranges::make_action_closure(ranges::bind_back(normalize_fn{}, factor));
148  }
149 };
150 
151 } // namespace detail
152 
155 
162 inline constexpr ranges::actions::action_closure<detail::normalize_fn> normalize;
163 
164 } // namespace beluga::actions
165 
166 #endif
beluga::actions::detail::normalize_base_fn::operator()
constexpr auto operator()(ExecutionPolicy &&policy, Range &range) const -> Range &
Overload that uses a default normalization factor.
Definition: normalize.hpp:83
beluga::actions::detail::normalize_fn
Implementation detail for a normalize range adaptor object with a default execution policy.
Definition: normalize.hpp:130
beluga::actions
Definition: assign.hpp:28
beluga::actions::detail::normalize_base_fn::operator()
constexpr auto operator()(Range &&range, ExecutionPolicy policy) const -> Range &
Overload that re-orders arguments from an action closure.
Definition: normalize.hpp:112
beluga::actions::detail::normalize_fn::operator()
constexpr auto operator()(Range &&range) const -> Range &
Overload that defines a default execution policy.
Definition: normalize.hpp:141
beluga::actions::detail::normalize_base_fn::operator()
constexpr auto operator()(ExecutionPolicy &&policy, Range &range, double factor) const -> Range &
Overload that implements the normalize algorithm.
Definition: normalize.hpp:52
particles.hpp
Implementation of views related to particle ranges.
beluga::actions::detail::normalize_base_fn::operator()
constexpr auto operator()(Range &&range, double factor, ExecutionPolicy policy) const -> Range &
Overload that re-orders arguments from an action closure.
Definition: normalize.hpp:102
beluga::actions::detail::normalize_base_fn::operator()
constexpr auto operator()(ExecutionPolicy policy) const
Overload that returns an action closure to compose with other actions.
Definition: normalize.hpp:124
particle_traits.hpp
Implementation of traits for particle types, see the Particle named requirements.
beluga::actions::detail::normalize_fn::operator()
constexpr auto operator()(Range &&range, double factor) const -> Range &
Overload that defines a default execution policy.
Definition: normalize.hpp:135
beluga::actions::normalize
constexpr ranges::actions::action_closure< detail::normalize_fn > normalize
Definition: normalize.hpp:162
beluga::policy
Forward declaration of policy.
Definition: policy.hpp:24
beluga::actions::detail::normalize_fn::operator()
constexpr auto operator()(double factor) const
Overload that returns an action closure to compose with other actions.
Definition: normalize.hpp:146
beluga::actions::detail::normalize_base_fn
Implementation detail for a normalize range adaptor object.
Definition: normalize.hpp:38
beluga::views::weights
constexpr auto weights
Definition: particles.hpp:34
beluga::actions::detail::normalize_base_fn::operator()
constexpr auto operator()(ExecutionPolicy policy, double factor) const
Overload that returns an action closure to compose with other actions.
Definition: normalize.hpp:118


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