Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:27:27

0001 #ifndef RecoParticleFlow_PFProducer_CommutativePairs_h
0002 #define RecoParticleFlow_PFProducer_CommutativePairs_h
0003 
0004 #include <utility>
0005 #include <vector>
0006 
0007 /**
0008  * Wrapper around std::vector<std::pair<T, T>> when the order of the pair elements is not relevant.
0009  *
0010  * @tparam T the type of data stored in the pairs
0011  */
0012 
0013 template <class T>
0014 class CommutativePairs {
0015 public:
0016   // Insert a new pair
0017   void insert(T const& a, T const& b) { pairs_.emplace_back(a, b); }
0018 
0019   // Check if this contains (a,b) or (b,a)
0020   bool contains(T const& a, T const& b) const {
0021     for (auto const& p : pairs_) {
0022       if ((a == p.first && b == p.second) || (b == p.first && a == p.second)) {
0023         return true;
0024       }
0025     }
0026     return false;
0027   }
0028 
0029   // Check if this contains (a,b) or (b,a), where b is arbitrary
0030   bool contains(T const& a) const {
0031     for (auto const& p : pairs_) {
0032       if (a == p.first || a == p.second) {
0033         return true;
0034       }
0035     }
0036     return false;
0037   }
0038 
0039   /// Add the pairs from another CommutativePairs to this
0040   void concatenate(CommutativePairs<T> const& other) {
0041     pairs_.insert(pairs_.end(), other.pairs_.begin(), other.pairs_.end());
0042   }
0043 
0044 private:
0045   std::vector<std::pair<T, T>> pairs_;
0046 };
0047 
0048 #endif