File indexing completed on 2024-04-06 12:03:50
0001 #ifndef DataFormats_Common_ContainerMask_h
0002 #define DataFormats_Common_ContainerMask_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <vector>
0024 #include <cassert>
0025 #include <cstddef>
0026 #include <algorithm>
0027
0028
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
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
0041
0042
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
0057
0058
0059 void swap(ContainerMask<T>& iOther);
0060
0061
0062 CMS_CLASS_VERSION(10)
0063
0064 private:
0065
0066
0067
0068
0069
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 }
0103
0104 #endif