Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:52:57

0001 #ifndef DataFormats_Common_ContainerMask_h
0002 #define DataFormats_Common_ContainerMask_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Common
0006 // Class  :     ContainerMask
0007 //
0008 /**\class ContainerMask ContainerMask.h DataFormats/Common/interface/ContainerMask.h
0009 
0010  Description: Provides a 'mask' associated with one container
0011 
0012  Usage:
0013     This class is used in conjunction with a container in the event. The container 
0014     must be 'indexable', i.e. the elements must be addressable via an unsigned int.
0015 
0016 */
0017 //
0018 // Original Author:
0019 //         Created:  Fri Sep 23 17:05:43 CDT 2011
0020 //
0021 
0022 // system include files
0023 #include <vector>
0024 #include <cassert>
0025 #include <cstddef>
0026 #include <algorithm>
0027 
0028 // user include files
0029 #include "DataFormats/Common/interface/RefProd.h"
0030 #include "DataFormats/Common/interface/ContainerMaskTraits.h"
0031 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
0032 
0033 // forward declarations
0034 namespace edm {
0035   template <typename T>
0036   class ContainerMask {
0037   public:
0038     ContainerMask() {}
0039     ContainerMask(const edm::RefProd<T>& iProd, const std::vector<bool>& iMask);
0040     //virtual ~ContainerMask();
0041 
0042     // ---------- const member functions ---------------------
0043     bool mask(unsigned int iIndex) const {
0044       if (iIndex < m_mask.size()) {
0045         return m_mask[iIndex];
0046       }
0047       return false;
0048     }
0049     bool mask(const typename ContainerMaskTraits<T>::value_type*);
0050     void applyOrTo(std::vector<bool>&) const;
0051     void copyMaskTo(std::vector<bool>&) const;
0052 
0053     size_t size() const { return m_mask.size(); }
0054 
0055     const edm::RefProd<T>& refProd() const { return m_prod; }
0056     // ---------- static member functions --------------------
0057 
0058     // ---------- member functions ---------------------------
0059     void swap(ContainerMask<T>& iOther);
0060 
0061     //Used by ROOT storage
0062     CMS_CLASS_VERSION(10)
0063 
0064   private:
0065     //ContainerMask(const ContainerMask&); // stop default
0066 
0067     //const ContainerMask& operator=(const ContainerMask&); // stop default
0068 
0069     // ---------- member data --------------------------------
0070     edm::RefProd<T> m_prod;
0071     std::vector<bool> m_mask;
0072   };
0073 
0074   template <typename T>
0075   ContainerMask<T>::ContainerMask(const edm::RefProd<T>& iProd, const std::vector<bool>& iMask)
0076       : m_prod(iProd), m_mask(iMask) {
0077     assert(iMask.size() <= ContainerMaskTraits<T>::size(m_prod.product()));
0078   }
0079 
0080   template <typename T>
0081   bool ContainerMask<T>::mask(const typename ContainerMaskTraits<T>::value_type* iElement) {
0082     unsigned int index = ContainerMaskTraits<T>::indexFor(iElement, m_prod.product());
0083     return this->mask(index);
0084   }
0085 
0086   template <typename T>
0087   void ContainerMask<T>::copyMaskTo(std::vector<bool>& iTo) const {
0088     iTo.assign(m_mask.begin(), m_mask.end());
0089   }
0090 
0091   template <typename T>
0092   void ContainerMask<T>::applyOrTo(std::vector<bool>& iTo) const {
0093     assert(iTo.size() == m_mask.size());
0094     std::transform(m_mask.begin(), m_mask.end(), iTo.begin(), iTo.begin(), std::logical_or<bool>());
0095   }
0096 
0097   template <typename T>
0098   void ContainerMask<T>::swap(ContainerMask<T>& iOther) {
0099     m_prod.swap(iOther.m_prod);
0100     std::swap(m_mask, iOther.m_mask);
0101   }
0102 }  // namespace edm
0103 
0104 #endif