Program Listing for File likelihoods.hpp

Return to documentation for file (include/beluga/views/likelihoods.hpp)

// Copyright 2025 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_VIEWS_LIKELIHOODS_HPP
#define BELUGA_VIEWS_LIKELIHOODS_HPP

#include <utility>

#include <beluga/views/particles.hpp>
#include <range/v3/view/transform.hpp>

namespace beluga::views {

namespace detail {

struct likelihoods_fn {
  template <class Model>
  constexpr auto operator()(Model model) const {
    // This is the core of the ranges composition pattern.
    // A view is created by chaining together other views using the pipe operator `|`.
    //
    // 1. `beluga::views::states`: This is the first adaptor in the chain. It takes a range
    //    of particles and produces a view of just their `state` members.
    //
    // 2. `ranges::views::transform(std::move(model))`: This is the second adaptor. It takes a
    //    range of elements (which will be the particle states from the previous step)
    //    and applies the `model` function to each one, producing a view of the results (the likelihoods).
    //
    // The result of piping these two adaptors together is a *new adaptor*. This new adaptor
    // is a "range adaptor closure" object that can be stored and later piped with an actual
    // range of particles to create the final view.
    return beluga::views::states | ranges::views::transform(std::move(model));
  }
};

}  // namespace detail

inline constexpr detail::likelihoods_fn likelihoods;

}  // namespace beluga::views

#endif  // BELUGA_VIEWS_LIKELIHOODS_HPP