Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:15

0001 #ifndef UtilAlgos_CollectionFilterTrait_h
0002 #define UtilAlgos_CollectionFilterTrait_h
0003 /* \class CollectionFilterTrait<C, S, N>
0004  *
0005  * \author Luca Lista, INFN
0006  *
0007  */
0008 #include "CommonTools/UtilAlgos/interface/AnySelector.h"
0009 #include "CommonTools/UtilAlgos/interface/MinNumberSelector.h"
0010 
0011 namespace helper {
0012 
0013   template <typename C, typename S, typename N>
0014   struct CollectionFilter {
0015     static bool filter(const C& source, const S& select, const N& sizeSelect) {
0016       size_t n = 0;
0017       for (typename C::const_iterator i = source.begin(); i != source.end(); ++i)
0018         if (select(*i))
0019           n++;
0020       return sizeSelect(n);
0021     }
0022   };
0023 
0024   template <typename C, typename S>
0025   struct CollectionFilter<C, S, MinNumberSelector> {
0026     static bool filter(const C& source, const S& select, const MinNumberSelector& sizeSelect) {
0027       size_t n = 0;
0028       for (typename C::const_iterator i = source.begin(); i != source.end(); ++i) {
0029         if (select(*i))
0030           n++;
0031         if (sizeSelect(n))
0032           return true;
0033       }
0034       return false;
0035     }
0036   };
0037 
0038   template <typename C, typename N>
0039   struct CollectionSizeFilter {
0040     template <typename S>
0041     static bool filter(const C& source, const S&, const N& sizeSelect) {
0042       return sizeSelect(source.size());
0043     }
0044   };
0045 
0046   template <typename C, typename S, typename N>
0047   struct CollectionFilterTrait {
0048     typedef CollectionFilter<C, S, N> type;
0049   };
0050 
0051   template <typename C, typename N>
0052   struct CollectionFilterTrait<C, AnySelector, N> {
0053     typedef CollectionSizeFilter<C, N> type;
0054   };
0055 
0056 }  // namespace helper
0057 
0058 #endif