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