.. _program_listing_file_include_beluga_actions_reweight.hpp: Program Listing for File reweight.hpp ===================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/beluga/actions/reweight.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // 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_REWEIGHT_HPP #define BELUGA_ACTIONS_REWEIGHT_HPP #include #include #include #include #include #include #include namespace beluga::actions { namespace detail { struct reweight_base_fn { template < class ExecutionPolicy, class Range, class Model, std::enable_if_t>, int> = 0, std::enable_if_t, int> = 0> constexpr auto operator()(ExecutionPolicy&& policy, Range& range, Model model) const -> Range& { static_assert(beluga::is_particle_range_v); auto states = range | beluga::views::states | ranges::views::common; auto weights = range | beluga::views::weights | ranges::views::common; std::transform( policy, // std::begin(states), // std::end(states), // std::begin(weights), // std::begin(weights), // [model = std::move(model)](const auto& s, auto w) { return w * model(s); }); return range; } template < class Range, class Model, class ExecutionPolicy, std::enable_if_t, int> = 0, std::enable_if_t, int> = 0> constexpr auto operator()(Range&& range, Model model, ExecutionPolicy policy) const -> Range& { return (*this)(std::move(policy), std::forward(range), std::move(model)); } template , int> = 0> constexpr auto operator()(ExecutionPolicy policy, Model model) const { return ranges::make_action_closure(ranges::bind_back(reweight_base_fn{}, std::move(model), std::move(policy))); } }; struct reweight_fn : public reweight_base_fn { using reweight_base_fn::operator(); template , int> = 0> constexpr auto operator()(Range&& range, Model model) const -> Range& { return (*this)(std::execution::seq, std::forward(range), std::move(model)); } template constexpr auto operator()(Model model) const { return ranges::make_action_closure(ranges::bind_back(reweight_fn{}, std::move(model))); } }; } // namespace detail inline constexpr detail::reweight_fn reweight; } // namespace beluga::actions #endif