CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
IDVectorMap.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_IDVectorMap_h
2 #define DataFormats_Common_IDVectorMap_h
3 // $Id: IDVectorMap.h,v 1.12 2008/03/31 21:12:11 wmtan Exp $
4 #include <map>
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;
18  typedef value_type & reference;
19  typedef typename map_iterator::iterator_category iterator_category;
22  im(m), em(e), ic(c) {
23  }
25  im(e), em(e) {
26  }
28  im = it.im; em = it.em; ic = it.ic;
29  return *this;
30  }
32  ++ic;
33  while (ic == im->second.end()) {
34  ++im;
35  if (im == em) return *this;
36  ic = im->second.begin();
37  }
38  return *this;
39  }
40  const_iterator operator++(int) { const_iterator ci = *this; operator++(); return ci; }
42  if (im == em) { --im; ic = im->second.end(); }
43  while (ic == im->second.begin()) {
44  --im;
45  ic = im->second.end();
46  }
47  --ic;
48  return *this;
49  }
50  const_iterator operator--(int) { const_iterator ci = *this; operator--(); return ci; }
51  bool operator==(const const_iterator& ci) const {
52  if (im == em && ci.im == im && ci.em == em) return true;
53  return im == ci.im && ic == ci.ic;
54  }
55  bool operator!=(const const_iterator& ci) const { return ! operator==(ci); }
56  const value_type & operator *() const { return *ic; }
57  private:
60  };
61 
64  return const_iterator(map_.end(), map_.begin(), map_.begin()->second.begin());
65  }
66  const_iterator end() const {
67  return const_iterator(map_.end());
68  }
69  void insert(ID id, const value_type & t) {
70  map_[ id ].push_back(P::clone(t));
71  }
72  template<typename CI>
73  void insert(ID id, CI begin, CI end) {
74  C & c = map_[ id ];
75  for(CI i = begin; i != end; ++i)
76  c.push_back(P::clone(*i));
77  }
78 
79  struct range {
81  begin(b), end(e) { }
83  };
84  range get(ID id) const {
86  map_iterator i = map_.find(id);
87  if (i != map_.end()) {
88  begin = i->second.begin();
89  end = i->second.end();
90  } else {
91  begin = end;
92  }
93  return range(begin, end);
94  }
95 
96  template<typename M>
97  struct match_iterator {
99  typedef value_type * pointer;
101  typedef typename map_iterator::iterator_category iterator_category;
103  match_iterator(const M & ma, const map_iterator & e, const map_iterator & m, const container_iterator & c) :
104  match(ma), im(m), em(e), ic(c) {
105  }
106  match_iterator(const M & ma, const map_iterator & e) :
107  match(ma), im(e), em(e) {
108  }
110  match = it.match; im = it.im; em = it.em; ic = it.ic;
111  return *this;
112  }
114  ++ic;
115  while (ic == im->second.end()) {
116  do { ++im; } while (! match(im->first) && im != em);
117  if (im == em) return *this;
118  ic = im->second.begin();
119  }
120  return *this;
121  }
122  match_iterator operator++(int) { match_iterator ci = *this; operator++(); return ci; }
123  bool operator==(const match_iterator& ci) const {
124  if (im == em && ci.im == im && ci.em == em) return true;
125  return im == ci.im && ic == ci.ic;
126  }
127  bool operator!=(const match_iterator& ci) const { return ! operator==(ci); }
128  const value_type & operator * () const { return *ic; }
129  private:
130  M match;
133  };
134 
135  template<typename M>
136  match_iterator<M> begin(const M & m) const {
137  return match_iterator<M>(m, map_.end(), map_.begin(), map_.begin()->second.begin());
138  }
139  template<typename M>
140  match_iterator<M> end(const M & m) const {
141  return match_iterator<M>(m, map_.end());
142  }
143 
144  struct id_iterator {
145  typedef ID value_type;
146  typedef ID * pointer;
147  typedef ID & reference;
148  typedef typename map_iterator::iterator_category iterator_category;
151  id_iterator & operator=(const id_iterator & it) { i = it.i; return *this; }
152  id_iterator& operator++() { ++i; return *this; }
153  id_iterator operator++(int) { id_iterator ci = *this; ++i; return ci; }
154  id_iterator& operator--() { --i; return *this; }
155  id_iterator operator--(int) { id_iterator ci = *this; --i; return ci; }
156  bool operator==(const id_iterator& ci) const { return i == ci.i; }
157  bool operator!=(const id_iterator& ci) const { return i != ci.i; }
158  const ID operator *() const { return i->first; }
159  private:
161  };
162  id_iterator id_begin() const { return id_iterator(map_.begin()); }
163  id_iterator id_end() const { return id_iterator(map_.end()); }
164  size_t id_size() const { return map_.size(); }
165  void swap (IDVectorMap & other);
166  IDVectorMap& operator=(IDVectorMap const& rhs);
167  private:
170  };
171 
172  template <typename ID, typename C, typename P>
173  inline
174  void
176  collection_.swap(other.collection_);
177  map_.swap(other.map_);
178  }
179 
180  template <typename ID, typename C, typename P>
181  inline
185  this->swap(temp);
186  return *this;
187  }
188 
189  // free swap function
190  template <typename ID, typename C, typename P>
191  inline
192  void
194  a.swap(b);
195  }
196 
197 }
198 
199 #endif
id_iterator operator--(int)
Definition: IDVectorMap.h:155
const_iterator & operator=(const const_iterator &it)
Definition: IDVectorMap.h:27
const_iterator(const map_iterator &e)
Definition: IDVectorMap.h:24
int i
Definition: DBlmapReader.cc:9
const_iterator begin() const
Definition: IDVectorMap.h:63
bool operator==(const id_iterator &ci) const
Definition: IDVectorMap.h:156
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:175
uint32_t ID
Definition: Definitions.h:26
const value_type & operator*() const
Definition: IDVectorMap.h:56
const_iterator operator--(int)
Definition: IDVectorMap.h:50
IDVectorMap & operator=(IDVectorMap const &rhs)
Definition: IDVectorMap.h:183
const_iterator end() const
Definition: IDVectorMap.h:66
match_iterator< M > begin(const M &m) const
Definition: IDVectorMap.h:136
const ID operator*() const
Definition: IDVectorMap.h:158
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
match_iterator & operator++()
Definition: IDVectorMap.h:113
std::map< ID, C > map
Definition: IDVectorMap.h:13
match_iterator operator++(int)
Definition: IDVectorMap.h:122
container_iterator end
Definition: IDVectorMap.h:82
match_iterator(const M &ma, const map_iterator &e)
Definition: IDVectorMap.h:106
bool operator!=(const const_iterator &ci) const
Definition: IDVectorMap.h:55
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:82
map_iterator::iterator_category iterator_category
Definition: IDVectorMap.h:148
bool operator!=(const match_iterator &ci) const
Definition: IDVectorMap.h:127
const_iterator & operator++()
Definition: IDVectorMap.h:31
id_iterator id_end() const
Definition: IDVectorMap.h:163
IDVectorMap::value_type value_type
Definition: IDVectorMap.h:98
size_t id_size() const
Definition: IDVectorMap.h:164
Container::value_type value_type
void insert(ID id, const value_type &t)
Definition: IDVectorMap.h:69
id_iterator id_begin() const
Definition: IDVectorMap.h:162
const_iterator operator++(int)
Definition: IDVectorMap.h:40
match_iterator & operator=(const match_iterator &it)
Definition: IDVectorMap.h:109
id_iterator & operator=(const id_iterator &it)
Definition: IDVectorMap.h:151
id_iterator operator++(int)
Definition: IDVectorMap.h:153
range(const container_iterator &b, const container_iterator &e)
Definition: IDVectorMap.h:80
match_iterator< M > end(const M &m) const
Definition: IDVectorMap.h:140
map_iterator::iterator_category iterator_category
Definition: IDVectorMap.h:101
double b
Definition: hdecay.h:120
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
bool operator==(const match_iterator &ci) const
Definition: IDVectorMap.h:123
bool operator!=(const id_iterator &ci) const
Definition: IDVectorMap.h:157
double a
Definition: hdecay.h:121
map::const_iterator map_iterator
Definition: IDVectorMap.h:14
void insert(ID id, CI begin, CI end)
Definition: IDVectorMap.h:73
const value_type & operator*() const
Definition: IDVectorMap.h:128
bool operator==(const const_iterator &ci) const
Definition: IDVectorMap.h:51
match_iterator(const M &ma, const map_iterator &e, const map_iterator &m, const container_iterator &c)
Definition: IDVectorMap.h:103
const_iterator & operator--()
Definition: IDVectorMap.h:41