** Warning **

Issuing rollback() due to DESTROY without explicit disconnect() of DBD::mysql::db handle dbname=lxr at /lxr/lib/LXR/Common.pm line 1103.

Last-Modified: Sat, 1 Jul 2022 23:03:13 GMT Content-Type: text/html; charset=utf-8 /CMSSW_12_5_X_2022-07-01-2300/DataFormats/Common/interface/IDVectorMap.h
Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-08 22:26:09

0001 #ifndef DataFormats_Common_IDVectorMap_h
0002 #define DataFormats_Common_IDVectorMap_h
0003 #include <map>
0004 #include <cstddef>
0005 
0006 namespace edm {
0007 
0008   template <typename ID, typename C, typename P>
0009   class IDVectorMap {
0010   public:
0011     typedef typename C::value_type value_type;
0012     typedef typename C::const_iterator container_iterator;
0013     typedef std::map<ID, C> map;
0014     typedef typename map::const_iterator map_iterator;
0015     struct const_iterator {
0016       typedef typename IDVectorMap::value_type value_type;
0017       typedef value_type* pointer;
0018       typedef value_type& reference;
0019       typedef typename map_iterator::iterator_category iterator_category;
0020       const_iterator() {}
0021       const_iterator(const map_iterator& e, const map_iterator& m, const container_iterator& c) : im(m), em(e), ic(c) {}
0022       const_iterator(const map_iterator& e) : im(e), em(e) {}
0023       const_iterator& operator=(const const_iterator& it) {
0024         im = it.im;
0025         em = it.em;
0026         ic = it.ic;
0027         return *this;
0028       }
0029       const_iterator& operator++() {
0030         ++ic;
0031         while (ic == im->second.end()) {
0032           ++im;
0033           if (im == em)
0034             return *this;
0035           ic = im->second.begin();
0036         }
0037         return *this;
0038       }
0039       const_iterator operator++(int) {
0040         const_iterator ci = *this;
0041         operator++();
0042         return ci;
0043       }
0044       const_iterator& operator--() {
0045         if (im == em) {
0046           --im;
0047           ic = im->second.end();
0048         }
0049         while (ic == im->second.begin()) {
0050           --im;
0051           ic = im->second.end();
0052         }
0053         --ic;
0054         return *this;
0055       }
0056       const_iterator operator--(int) {
0057         const_iterator ci = *this;
0058         operator--();
0059         return ci;
0060       }
0061       bool operator==(const const_iterator& ci) const {
0062         if (im == em && ci.im == im && ci.em == em)
0063           return true;
0064         return im == ci.im && ic == ci.ic;
0065       }
0066       bool operator!=(const const_iterator& ci) const { return !operator==(ci); }
0067       const value_type& operator*() const { return *ic; }
0068 
0069     private:
0070       map_iterator im, em;
0071       container_iterator ic;
0072     };
0073 
0074     IDVectorMap() {}
0075     const_iterator begin() const { return const_iterator(map_.end(), map_.begin(), map_.begin()->second.begin()); }
0076     const_iterator end() const { return const_iterator(map_.end()); }
0077     void insert(ID id, const value_type& t) { map_[id].push_back(P::clone(t)); }
0078     template <typename CI>
0079     void insert(ID id, CI begin, CI end) {
0080       C& c = map_[id];
0081       for (CI i = begin; i != end; ++i)
0082         c.push_back(P::clone(*i));
0083     }
0084 
0085     struct range {
0086       range(const container_iterator& b, const container_iterator& e) : begin(b), end(e) {}
0087       container_iterator begin, end;
0088     };
0089     range get(ID id) const {
0090       container_iterator begin, end;
0091       map_iterator i = map_.find(id);
0092       if (i != map_.end()) {
0093         begin = i->second.begin();
0094         end = i->second.end();
0095       } else {
0096         begin = end;
0097       }
0098       return range(begin, end);
0099     }
0100 
0101     template <typename M>
0102     struct match_iterator {
0103       typedef typename IDVectorMap::value_type value_type;
0104       typedef value_type* pointer;
0105       typedef value_type& reference;
0106       typedef typename map_iterator::iterator_category iterator_category;
0107       match_iterator() {}
0108       match_iterator(const M& ma, const map_iterator& e, const map_iterator& m, const container_iterator& c)
0109           : match(ma), im(m), em(e), ic(c) {}
0110       match_iterator(const M& ma, const map_iterator& e) : match(ma), im(e), em(e) {}
0111       match_iterator& operator=(const match_iterator& it) {
0112         match = it.match;
0113         im = it.im;
0114         em = it.em;
0115         ic = it.ic;
0116         return *this;
0117       }
0118       match_iterator& operator++() {
0119         ++ic;
0120         while (ic == im->second.end()) {
0121           do {
0122             ++im;
0123           } while (!match(im->first) && im != em);
0124           if (im == em)
0125             return *this;
0126           ic = im->second.begin();
0127         }
0128         return *this;
0129       }
0130       match_iterator operator++(int) {
0131         match_iterator ci = *this;
0132         operator++();
0133         return ci;
0134       }
0135       bool operator==(const match_iterator& ci) const {
0136         if (im == em && ci.im == im && ci.em == em)
0137           return true;
0138         return im == ci.im && ic == ci.ic;
0139       }
0140       bool operator!=(const match_iterator& ci) const { return !operator==(ci); }
0141       const value_type& operator*() const { return *ic; }
0142 
0143     private:
0144       M match;
0145       map_iterator im, em;
0146       container_iterator ic;
0147     };
0148 
0149     template <typename M>
0150     match_iterator<M> begin(const M& m) const {
0151       return match_iterator<M>(m, map_.end(), map_.begin(), map_.begin()->second.begin());
0152     }
0153     template <typename M>
0154     match_iterator<M> end(const M& m) const {
0155       return match_iterator<M>(m, map_.end());
0156     }
0157 
0158     struct id_iterator {
0159       typedef ID value_type;
0160       typedef ID* pointer;
0161       typedef ID& reference;
0162       typedef typename map_iterator::iterator_category iterator_category;
0163       id_iterator() {}
0164       id_iterator(map_iterator o) : i(o) {}
0165       id_iterator& operator=(const id_iterator& it) {
0166         i = it.i;
0167         return *this;
0168       }
0169       id_iterator& operator++() {
0170         ++i;
0171         return *this;
0172       }
0173       id_iterator operator++(int) {
0174         id_iterator ci = *this;
0175         ++i;
0176         return ci;
0177       }
0178       id_iterator& operator--() {
0179         --i;
0180         return *this;
0181       }
0182       id_iterator operator--(int) {
0183         id_iterator ci = *this;
0184         --i;
0185         return ci;
0186       }
0187       bool operator==(const id_iterator& ci) const { return i == ci.i; }
0188       bool operator!=(const id_iterator& ci) const { return i != ci.i; }
0189       const ID operator*() const { return i->first; }
0190 
0191     private:
0192       map_iterator i;
0193     };
0194     id_iterator id_begin() const { return id_iterator(map_.begin()); }
0195     id_iterator id_end() const { return id_iterator(map_.end()); }
0196     size_t id_size() const { return map_.size(); }
0197     void swap(IDVectorMap& other);
0198     IDVectorMap& operator=(IDVectorMap const& rhs);
0199 
0200   private:
0201     C collection_;
0202     map map_;
0203   };
0204 
0205   template <typename ID, typename C, typename P>
0206   inline void IDVectorMap<ID, C, P>::swap(IDVectorMap<ID, C, P>& other) {
0207     collection_.swap(other.collection_);
0208     map_.swap(other.map_);
0209   }
0210 
0211   template <typename ID, typename C, typename P>
0212   inline IDVectorMap<ID, C, P>& IDVectorMap<ID, C, P>::operator=(IDVectorMap<ID, C, P> const& rhs) {
0213     IDVectorMap<ID, C, P> temp(rhs);
0214     this->swap(temp);
0215     return *this;
0216   }
0217 
0218   // free swap function
0219   template <typename ID, typename C, typename P>
0220   inline void swap(IDVectorMap<ID, C, P>& a, IDVectorMap<ID, C, P>& b) {
0221     a.swap(b);
0222   }
0223 
0224 }  // namespace edm
0225 
0226 #endif