CMS 3D CMS Logo

IDVectorMap.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_IDVectorMap_h
2 #define DataFormats_Common_IDVectorMap_h
3 #include <map>
4 
5 namespace edm {
6 
7  template <typename ID, typename C, typename P>
8  class IDVectorMap {
9  public:
10  typedef typename C::value_type value_type;
11  typedef typename C::const_iterator container_iterator;
12  typedef std::map<ID, C> map;
13  typedef typename map::const_iterator map_iterator;
14  struct const_iterator {
16  typedef value_type* pointer;
17  typedef value_type& reference;
18  typedef typename map_iterator::iterator_category iterator_category;
20  const_iterator(const map_iterator& e, const map_iterator& m, const container_iterator& c) : im(m), em(e), ic(c) {}
21  const_iterator(const map_iterator& e) : im(e), em(e) {}
23  im = it.im;
24  em = it.em;
25  ic = it.ic;
26  return *this;
27  }
29  ++ic;
30  while (ic == im->second.end()) {
31  ++im;
32  if (im == em)
33  return *this;
34  ic = im->second.begin();
35  }
36  return *this;
37  }
39  const_iterator ci = *this;
40  operator++();
41  return ci;
42  }
44  if (im == em) {
45  --im;
46  ic = im->second.end();
47  }
48  while (ic == im->second.begin()) {
49  --im;
50  ic = im->second.end();
51  }
52  --ic;
53  return *this;
54  }
56  const_iterator ci = *this;
57  operator--();
58  return ci;
59  }
60  bool operator==(const const_iterator& ci) const {
61  if (im == em && ci.im == im && ci.em == em)
62  return true;
63  return im == ci.im && ic == ci.ic;
64  }
65  bool operator!=(const const_iterator& ci) const { return !operator==(ci); }
66  const value_type& operator*() const { return *ic; }
67 
68  private:
69  map_iterator im, em;
70  container_iterator ic;
71  };
72 
74  const_iterator begin() const { return const_iterator(map_.end(), map_.begin(), map_.begin()->second.begin()); }
75  const_iterator end() const { return const_iterator(map_.end()); }
76  void insert(ID id, const value_type& t) { map_[id].push_back(P::clone(t)); }
77  template <typename CI>
78  void insert(ID id, CI begin, CI end) {
79  C& c = map_[id];
80  for (CI i = begin; i != end; ++i)
81  c.push_back(P::clone(*i));
82  }
83 
84  struct range {
85  range(const container_iterator& b, const container_iterator& e) : begin(b), end(e) {}
86  container_iterator begin, end;
87  };
88  range get(ID id) const {
89  container_iterator begin, end;
90  map_iterator i = map_.find(id);
91  if (i != map_.end()) {
92  begin = i->second.begin();
93  end = i->second.end();
94  } else {
95  begin = end;
96  }
97  return range(begin, end);
98  }
99 
100  template <typename M>
101  struct match_iterator {
103  typedef value_type* pointer;
104  typedef value_type& reference;
105  typedef typename map_iterator::iterator_category iterator_category;
107  match_iterator(const M& ma, const map_iterator& e, const map_iterator& m, const container_iterator& c)
108  : match(ma), im(m), em(e), ic(c) {}
109  match_iterator(const M& ma, const map_iterator& e) : match(ma), im(e), em(e) {}
111  match = it.match;
112  im = it.im;
113  em = it.em;
114  ic = it.ic;
115  return *this;
116  }
118  ++ic;
119  while (ic == im->second.end()) {
120  do {
121  ++im;
122  } while (!match(im->first) && im != em);
123  if (im == em)
124  return *this;
125  ic = im->second.begin();
126  }
127  return *this;
128  }
130  match_iterator ci = *this;
131  operator++();
132  return ci;
133  }
134  bool operator==(const match_iterator& ci) const {
135  if (im == em && ci.im == im && ci.em == em)
136  return true;
137  return im == ci.im && ic == ci.ic;
138  }
139  bool operator!=(const match_iterator& ci) const { return !operator==(ci); }
140  const value_type& operator*() const { return *ic; }
141 
142  private:
143  M match;
144  map_iterator im, em;
145  container_iterator ic;
146  };
147 
148  template <typename M>
149  match_iterator<M> begin(const M& m) const {
150  return match_iterator<M>(m, map_.end(), map_.begin(), map_.begin()->second.begin());
151  }
152  template <typename M>
153  match_iterator<M> end(const M& m) const {
154  return match_iterator<M>(m, map_.end());
155  }
156 
157  struct id_iterator {
158  typedef ID value_type;
159  typedef ID* pointer;
160  typedef ID& reference;
161  typedef typename map_iterator::iterator_category iterator_category;
163  id_iterator(map_iterator o) : i(o) {}
165  i = it.i;
166  return *this;
167  }
169  ++i;
170  return *this;
171  }
173  id_iterator ci = *this;
174  ++i;
175  return ci;
176  }
178  --i;
179  return *this;
180  }
182  id_iterator ci = *this;
183  --i;
184  return ci;
185  }
186  bool operator==(const id_iterator& ci) const { return i == ci.i; }
187  bool operator!=(const id_iterator& ci) const { return i != ci.i; }
188  const ID operator*() const { return i->first; }
189 
190  private:
191  map_iterator i;
192  };
193  id_iterator id_begin() const { return id_iterator(map_.begin()); }
194  id_iterator id_end() const { return id_iterator(map_.end()); }
195  size_t id_size() const { return map_.size(); }
196  void swap(IDVectorMap& other);
197  IDVectorMap& operator=(IDVectorMap const& rhs);
198 
199  private:
201  map map_;
202  };
203 
204  template <typename ID, typename C, typename P>
206  collection_.swap(other.collection_);
207  map_.swap(other.map_);
208  }
209 
210  template <typename ID, typename C, typename P>
213  this->swap(temp);
214  return *this;
215  }
216 
217  // free swap function
218  template <typename ID, typename C, typename P>
220  a.swap(b);
221  }
222 
223 } // namespace edm
224 
225 #endif
id_iterator operator--(int)
Definition: IDVectorMap.h:181
const_iterator & operator=(const const_iterator &it)
Definition: IDVectorMap.h:22
const_iterator(const map_iterator &e)
Definition: IDVectorMap.h:21
const_iterator begin() const
Definition: IDVectorMap.h:74
bool operator==(const id_iterator &ci) const
Definition: IDVectorMap.h:186
const_iterator(const map_iterator &e, const map_iterator &m, const container_iterator &c)
Definition: IDVectorMap.h:20
void swap(IDVectorMap &other)
Definition: IDVectorMap.h:205
uint32_t ID
Definition: Definitions.h:24
const value_type & operator*() const
Definition: IDVectorMap.h:66
const_iterator operator--(int)
Definition: IDVectorMap.h:55
IDVectorMap & operator=(IDVectorMap const &rhs)
Definition: IDVectorMap.h:211
const_iterator end() const
Definition: IDVectorMap.h:75
match_iterator< M > begin(const M &m) const
Definition: IDVectorMap.h:149
const ID operator*() const
Definition: IDVectorMap.h:188
match_iterator & operator++()
Definition: IDVectorMap.h:117
std::map< ID, C > map
Definition: IDVectorMap.h:12
match_iterator operator++(int)
Definition: IDVectorMap.h:129
container_iterator end
Definition: IDVectorMap.h:86
match_iterator(const M &ma, const map_iterator &e)
Definition: IDVectorMap.h:109
bool operator!=(const const_iterator &ci) const
Definition: IDVectorMap.h:65
map_iterator::iterator_category iterator_category
Definition: IDVectorMap.h:18
C::const_iterator container_iterator
Definition: IDVectorMap.h:11
map_iterator::iterator_category iterator_category
Definition: IDVectorMap.h:161
bool operator!=(const match_iterator &ci) const
Definition: IDVectorMap.h:139
const_iterator & operator++()
Definition: IDVectorMap.h:28
id_iterator id_end() const
Definition: IDVectorMap.h:194
IDVectorMap::value_type value_type
Definition: IDVectorMap.h:102
size_t id_size() const
Definition: IDVectorMap.h:195
void insert(ID id, const value_type &t)
Definition: IDVectorMap.h:76
id_iterator id_begin() const
Definition: IDVectorMap.h:193
const_iterator operator++(int)
Definition: IDVectorMap.h:38
match_iterator & operator=(const match_iterator &it)
Definition: IDVectorMap.h:110
id_iterator & operator=(const id_iterator &it)
Definition: IDVectorMap.h:164
id_iterator operator++(int)
Definition: IDVectorMap.h:172
range(const container_iterator &b, const container_iterator &e)
Definition: IDVectorMap.h:85
match_iterator< M > end(const M &m) const
Definition: IDVectorMap.h:153
map_iterator::iterator_category iterator_category
Definition: IDVectorMap.h:105
double b
Definition: hdecay.h:118
IDVectorMap::value_type value_type
Definition: IDVectorMap.h:15
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
C::value_type value_type
Definition: IDVectorMap.h:10
bool operator==(const match_iterator &ci) const
Definition: IDVectorMap.h:134
bool operator!=(const id_iterator &ci) const
Definition: IDVectorMap.h:187
HLT enums.
double a
Definition: hdecay.h:119
map::const_iterator map_iterator
Definition: IDVectorMap.h:13
void insert(ID id, CI begin, CI end)
Definition: IDVectorMap.h:78
const value_type & operator*() const
Definition: IDVectorMap.h:140
bool operator==(const const_iterator &ci) const
Definition: IDVectorMap.h:60
match_iterator(const M &ma, const map_iterator &e, const map_iterator &m, const container_iterator &c)
Definition: IDVectorMap.h:107
const_iterator & operator--()
Definition: IDVectorMap.h:43
std::string match(BranchDescription const &a, BranchDescription const &b, std::string const &fileName)