Program Listing for File normalize.hpp
↰ Return to documentation for file (include/beluga/actions/normalize.hpp
)
// Copyright 2024 Ekumen, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef BELUGA_ACTIONS_NORMALIZE_HPP
#define BELUGA_ACTIONS_NORMALIZE_HPP
#include <algorithm>
#include <execution>
#include <range/v3/action/action.hpp>
#include <range/v3/numeric/accumulate.hpp>
#include <range/v3/view/common.hpp>
#include <beluga/type_traits/particle_traits.hpp>
#include <beluga/views/particles.hpp>
namespace beluga::actions {
namespace detail {
struct normalize_base_fn {
template <
class ExecutionPolicy,
class Range,
std::enable_if_t<std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>, int> = 0,
std::enable_if_t<ranges::range<Range>, int> = 0>
constexpr auto operator()(ExecutionPolicy&& policy, Range& range, double factor) const -> Range& {
if (std::abs(factor - 1.0) < std::numeric_limits<double>::epsilon()) {
return range; // No change.
}
auto weights = [&range]() {
if constexpr (beluga::is_particle_range_v<Range>) {
return range | beluga::views::weights | ranges::views::common;
} else {
return range | ranges::views::common;
}
}();
std::transform(
policy, //
std::begin(weights), //
std::end(weights), //
std::begin(weights), //
[factor](const auto w) { return w / factor; });
return range;
}
template <
class ExecutionPolicy,
class Range,
std::enable_if_t<std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>, int> = 0,
std::enable_if_t<ranges::range<Range>, int> = 0>
constexpr auto operator()(ExecutionPolicy&& policy, Range& range) const -> Range& {
auto weights = [&range]() {
if constexpr (beluga::is_particle_range_v<Range>) {
return range | beluga::views::weights | ranges::views::common;
} else {
return range | ranges::views::common;
}
}();
const double total_weight = ranges::accumulate(weights, 0.0);
return (*this)(std::forward<ExecutionPolicy>(policy), range, total_weight);
}
template <
class Range,
class ExecutionPolicy,
std::enable_if_t<ranges::range<Range>, int> = 0,
std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
constexpr auto operator()(Range&& range, double factor, ExecutionPolicy policy) const -> Range& {
return (*this)(std::move(policy), std::forward<Range>(range), factor);
}
template <
class Range,
class ExecutionPolicy,
std::enable_if_t<ranges::range<Range>, int> = 0,
std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
constexpr auto operator()(Range&& range, ExecutionPolicy policy) const -> Range& {
return (*this)(std::move(policy), std::forward<Range>(range));
}
template <class ExecutionPolicy, std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
constexpr auto operator()(ExecutionPolicy policy, double factor) const {
return ranges::make_action_closure(ranges::bind_back(normalize_base_fn{}, factor, std::move(policy)));
}
template <class ExecutionPolicy, std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
constexpr auto operator()(ExecutionPolicy policy) const {
return ranges::make_action_closure(ranges::bind_back(normalize_base_fn{}, std::move(policy)));
}
};
struct normalize_fn : public normalize_base_fn {
using normalize_base_fn::operator();
template <class Range, std::enable_if_t<ranges::range<Range>, int> = 0>
constexpr auto operator()(Range&& range, double factor) const -> Range& {
return (*this)(std::execution::seq, std::forward<Range>(range), factor);
}
template <class Range, std::enable_if_t<ranges::range<Range>, int> = 0>
constexpr auto operator()(Range&& range) const -> Range& {
return (*this)(std::execution::seq, std::forward<Range>(range));
}
constexpr auto operator()(double factor) const {
return ranges::make_action_closure(ranges::bind_back(normalize_fn{}, factor));
}
};
} // namespace detail
inline constexpr ranges::actions::action_closure<detail::normalize_fn> normalize;
} // namespace beluga::actions
#endif