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