Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-15 08:16:13

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2024 Simon Gardner
0003 
0004 #pragma once
0005 
0006 #include <spdlog/spdlog.h>
0007 #include <algorithms/algorithm.h>
0008 #include <string>
0009 #include <string_view>
0010 
0011 #include "services/log/Log_service.h"
0012 #include "algorithms/interfaces/WithPodConfig.h"
0013 
0014 namespace eicrecon {
0015 
0016 template <class ToFilterObjectT, class FilterByObjectT>
0017 using FilterMatchingAlgorithm =
0018     algorithms::Algorithm<typename algorithms::Input<typename ToFilterObjectT::collection_type,
0019                                                      typename FilterByObjectT::collection_type>,
0020                           typename algorithms::Output<typename ToFilterObjectT::collection_type,
0021                                                       typename ToFilterObjectT::collection_type>>;
0022 
0023 /// Filters a collection by the members of another collection
0024 /// The first collection is divided up into two collections where its elements either passed the filter or not
0025 /// The second collection provides a filter
0026 /// Functions need to be provided along with the collections to form the link between the data types
0027 /// These functions are envisioned to link the objectIDs of the collection/associations but could be anything
0028 template <typename ToFilterObjectT, auto ToFilterFunction, typename FilterByObjectT,
0029           auto FilterByFunction>
0030 class FilterMatching : public FilterMatchingAlgorithm<ToFilterObjectT, FilterByObjectT> {
0031 
0032 public:
0033   FilterMatching(std::string_view name)
0034       : FilterMatchingAlgorithm<ToFilterObjectT, FilterByObjectT>{
0035             name,
0036             {"inputCollection", "inputMatchedCollection"},
0037             {"outputMatchedAssociations", "outputUnmatchedAssociations"},
0038             "Filter by matching to a collection"} {};
0039 
0040   void init() final{};
0041 
0042   void
0043   process(const typename FilterMatchingAlgorithm<ToFilterObjectT, FilterByObjectT>::Input& input,
0044           const typename FilterMatchingAlgorithm<ToFilterObjectT, ToFilterObjectT>::Output& output)
0045       const final {
0046 
0047     const auto [toFilterEntries, filterByEntries] = input;
0048     auto [is_matched, is_not_matched]             = output;
0049 
0050     is_matched->setSubsetCollection();
0051     is_not_matched->setSubsetCollection();
0052 
0053     for (const auto& matchedEntry : *toFilterEntries) {
0054 
0055       auto ref_value = ToFilterFunction(&matchedEntry);
0056 
0057       bool found_match = false;
0058 
0059       // Tries to find the association in the entries
0060       for (const auto& entry : *filterByEntries) {
0061         auto other_value = FilterByFunction(&entry);
0062         if (other_value == ref_value) {
0063           is_matched->push_back(matchedEntry);
0064           found_match = true;
0065           break;
0066         }
0067       }
0068 
0069       if (!found_match) {
0070         is_not_matched->push_back(matchedEntry);
0071       }
0072     }
0073   };
0074 };
0075 
0076 } // namespace eicrecon