CMS 3D CMS Logo

ValueMap.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_ValueMap_h
2 #define DataFormats_Common_ValueMap_h
3 /* \class ValueMap<T>
4  *
5  * \author Luca Lista, INFN
6  *
7  *
8  */
9 
13 #include <vector>
14 #include <map>
15 #include <iterator>
16 #include <algorithm>
17 #include <cstddef>
18 
19 namespace edm {
20  namespace helper {
21  template <typename Map>
22  class Filler {
23  private:
24  typedef std::vector<size_t> index_vector;
25  typedef std::vector<typename Map::value_type> value_vector;
26  typedef std::map<ProductID, value_vector> value_map;
27  typedef typename Map::offset offset;
28  typedef typename Map::id_offset_vector id_offset_vector;
29 
30  public:
31  explicit Filler(Map& map) : map_(map), totSize_(0) { add(map); }
32  void add(const Map& map) {
33  if (map.empty())
34  return;
35  typename id_offset_vector::const_iterator j = map.ids_.begin();
36  const typename id_offset_vector::const_iterator end = map.ids_.end();
37  size_t i = 0;
38  const size_t size = map.values_.size();
39  // std::pair<ProductID, offset> id = *j;
40  do {
41  ProductID id = j->first;
42  ++j;
43  size_t max = (j == end ? size : j->second);
44  typename value_map::iterator f = values_.find(id);
45  if (f != values_.end())
46  throwAdd();
47  value_vector& values = values_.insert(std::make_pair(id, value_vector())).first->second;
48  while (i != max)
49  values.push_back(map.values_[i++]);
50  } while (j != end);
51  }
52  template <typename H, typename I>
53  void insert(const H& h, I begin, I end) {
54  ProductID id = h.id();
55  size_t size = h->size(), sizeIt = end - begin;
56  if (sizeIt != size)
57  throwFillSize();
58  typename value_map::const_iterator f = values_.find(id);
59  if (f != values_.end())
60  throwFillID(id);
61  value_vector& values = values_.insert(make_pair(id, value_vector(size))).first->second;
62  std::copy(begin, end, values.begin());
63  totSize_ += size;
64  }
65  void fill() {
66  map_.clear();
67  offset off = 0;
68  map_.ids_.reserve(values_.size());
69  map_.values_.reserve(totSize_);
70  for (typename value_map::const_iterator i = values_.begin(); i != values_.end(); ++i) {
71  ProductID id = i->first;
72  map_.ids_.push_back(std::make_pair(id, off));
73  const value_vector& values = i->second;
74  for (typename value_vector::const_iterator j = values.begin(); j != values.end(); ++j) {
75  map_.values_.push_back(*j);
76  ++off;
77  }
78  }
79  map_.shrink_to_fit();
80  }
81 
82  protected:
83  Map& map_;
84 
85  private:
87  size_t totSize_;
88 
89  void throwFillSize() const {
91  "ValueMap::Filler: handle and reference "
92  "collections should the same size\n");
93  }
94  void throwFillID(ProductID id) const {
96  e << "index map has already been filled for id: " << id << "\n";
97  e.raise();
98  }
99  void throwAdd() const {
101  "ValueMap: trying to add entries for an already existing product\n");
102  }
103  };
104  } // namespace helper
105 
106  template <typename T>
107  class ValueMap {
108  public:
109  typedef T value_type;
110  typedef std::vector<value_type> container;
111  typedef unsigned int offset;
112  typedef std::vector<std::pair<ProductID, offset> > id_offset_vector;
114  typedef typename container::const_reference const_reference_type;
115 
116  ValueMap() {}
117 
118  void swap(ValueMap& other) {
119  values_.swap(other.values_);
120  ids_.swap(other.ids_);
121  }
122 
123  template <typename RefKey>
124  const_reference_type operator[](const RefKey& r) const {
125  return get(r.id(), r.key());
126  }
127  // raw index of a given (id,key) pair
128  size_t rawIndexOf(ProductID id, size_t idx) const {
129  typename id_offset_vector::const_iterator f = getIdOffset(id);
130  if (f == ids_.end())
132  offset off = f->second;
133  size_t j = off + idx;
134  if (j >= values_.size())
135  throwIndexBound();
136  return j;
137  }
138  const_reference_type get(ProductID id, size_t idx) const { return values_[rawIndexOf(id, idx)]; }
139  template <typename RefKey>
140  reference_type operator[](const RefKey& r) {
141  return get(r.id(), r.key());
142  }
143  reference_type get(ProductID id, size_t idx) { return values_[rawIndexOf(id, idx)]; }
144 
146  add(o);
147  return *this;
148  }
149  bool contains(ProductID id) const { return getIdOffset(id) != ids_.end(); }
150  size_t size() const { return values_.size(); }
151  size_t idSize() const { return ids_.size(); }
152  bool empty() const { return values_.empty(); }
153  void clear() {
154  values_.clear();
155  ids_.clear();
156  }
157  void shrink_to_fit() {
158  ids_.shrink_to_fit();
159  values_.shrink_to_fit();
160  }
161 
163 
164  struct const_iterator {
165  typedef ptrdiff_t difference_type;
166  const_iterator() : values_(nullptr) {}
167  ProductID id() const { return i_->first; }
168  typename container::const_iterator begin() const { return values_->begin() + i_->second; }
169  typename container::const_iterator end() const {
170  if (i_ == end_)
171  return values_->end();
172  id_offset_vector::const_iterator end = i_;
173  ++end;
174  if (end == end_)
175  return values_->end();
176  return values_->begin() + end->second;
177  }
178  size_t size() const { return end() - begin(); }
179  const T& operator[](size_t i) { return *(begin() + i); }
181  ++i_;
182  return *this;
183  }
185  const_iterator ci = *this;
186  ++i_;
187  return ci;
188  }
190  --i_;
191  return *this;
192  }
194  const_iterator ci = *this;
195  --i_;
196  return ci;
197  }
198  difference_type operator-(const const_iterator& o) const { return i_ - o.i_; }
201  bool operator<(const const_iterator& o) const { return i_ < o.i_; }
202  bool operator==(const const_iterator& ci) const { return i_ == ci.i_; }
203  bool operator!=(const const_iterator& ci) const { return i_ != ci.i_; }
205  i_ += d;
206  return *this;
207  }
209  i_ -= d;
210  return *this;
211  }
212 
213  private:
214  const_iterator(const id_offset_vector::const_iterator& i_,
215  const id_offset_vector::const_iterator& end,
216  const container* values)
217  : values_(values), i_(i_), end_(end) {}
219  id_offset_vector::const_iterator i_, end_;
220  friend class ValueMap<T>;
221  };
222 
223  const_iterator begin() const { return const_iterator(ids_.begin(), ids_.end(), &values_); }
224  const_iterator end() const { return const_iterator(ids_.end(), ids_.end(), &values_); }
225 
227  const id_offset_vector& ids() const { return ids_; }
229  const_reference_type get(size_t idx) const { return values_[idx]; }
230 
231  //Used by ROOT storage
233 
234  protected:
237 
238  typename id_offset_vector::const_iterator getIdOffset(ProductID id) const {
239  typename id_offset_vector::const_iterator i = std::lower_bound(ids_.begin(), ids_.end(), id, IDComparator());
240  if (i == ids_.end())
241  return i;
242  return i->first == id ? i : ids_.end();
243  }
244 
245  void throwIndexBound() const {
246  Exception::throwThis(errors::InvalidReference, "ValueMap: index out of upper boundary\n");
247  }
248 
249  private:
250  struct IDComparator {
251  bool operator()(const std::pair<ProductID, offset>& p, const ProductID& id) { return p.first < id; }
252  };
253  void throwNotExisting() const {
254  Exception::throwThis(errors::InvalidReference, "ValueMap: no associated value for given product and index\n");
255  }
256 
257  void add(const ValueMap<T>& o) {
258  Filler filler(*this);
259  filler.add(o);
260  filler.fill();
261  }
262 
263  friend class helper::Filler<ValueMap<T> >;
264  };
265 
266  template <typename T>
268  ValueMap<T> a = a1;
269  a += a2;
270  return a;
271  }
272 
273  // Free swap function
274  template <typename T>
275  inline void swap(ValueMap<T>& lhs, ValueMap<T>& rhs) {
276  lhs.swap(rhs);
277  }
278 
279 } // namespace edm
280 #endif
bool operator!=(const const_iterator &ci) const
Definition: ValueMap.h:203
unsigned int offset
Definition: ValueMap.h:111
Definition: helper.py:1
id_offset_vector::const_iterator i_
Definition: ValueMap.h:219
id_offset_vector::const_iterator getIdOffset(ProductID id) const
Definition: ValueMap.h:238
void clear()
Definition: ValueMap.h:153
const_iterator(const id_offset_vector::const_iterator &i_, const id_offset_vector::const_iterator &end, const container *values)
Definition: ValueMap.h:214
Association< C > operator+(const Association< C > &a1, const Association< C > &a2)
Definition: Association.h:117
size_t idSize() const
Definition: ValueMap.h:151
const_iterator & operator-=(difference_type d)
Definition: ValueMap.h:208
ValueMap< T > & operator+=(const ValueMap< T > &o)
Definition: ValueMap.h:145
void swap(ValueMap &other)
Definition: ValueMap.h:118
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
difference_type operator-(const const_iterator &o) const
Definition: ValueMap.h:198
id_offset_vector ids_
Definition: ValueMap.h:236
#define CMS_CLASS_VERSION(_version_)
void throwFillID(ProductID id) const
Definition: ValueMap.h:94
Filler(Map &map)
Definition: ValueMap.h:31
const id_offset_vector & ids() const
meant to be used in AssociativeIterator, not by the ordinary user
Definition: ValueMap.h:227
size_t rawIndexOf(ProductID id, size_t idx) const
Definition: ValueMap.h:128
const_iterator & operator+=(difference_type d)
Definition: ValueMap.h:204
bool operator==(const const_iterator &ci) const
Definition: ValueMap.h:202
bool operator()(const std::pair< ProductID, offset > &p, const ProductID &id)
Definition: ValueMap.h:251
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
const_iterator & operator++()
Definition: ValueMap.h:180
void throwAdd() const
Definition: ValueMap.h:99
const_iterator end() const
Definition: ValueMap.h:224
Map::offset offset
Definition: ValueMap.h:27
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:85
container::reference reference_type
Definition: ValueMap.h:113
const_reference_type operator[](const RefKey &r) const
Definition: ValueMap.h:124
const_iterator operator--(int)
Definition: ValueMap.h:193
container::const_reference const_reference_type
Definition: ValueMap.h:114
std::vector< std::pair< ProductID, offset > > id_offset_vector
Definition: ValueMap.h:112
const_iterator & operator--()
Definition: ValueMap.h:189
void add(const ValueMap< T > &o)
Definition: ValueMap.h:257
const std::complex< double > I
Definition: I.h:8
double f[11][100]
ProductID id() const
Definition: ValueMap.h:167
d
Definition: ztail.py:151
void shrink_to_fit()
Definition: ValueMap.h:157
container::const_iterator end() const
Definition: ValueMap.h:169
const_iterator begin() const
Definition: ValueMap.h:223
container::const_iterator begin() const
Definition: ValueMap.h:168
helper::Filler< ValueMap< T > > Filler
Definition: ValueMap.h:162
const_iterator operator++(int)
Definition: ValueMap.h:184
void throwIndexBound() const
Definition: ValueMap.h:245
void throwFillSize() const
Definition: ValueMap.h:89
std::map< ProductID, value_vector > value_map
Definition: ValueMap.h:26
std::vector< typename Map::value_type > value_vector
Definition: ValueMap.h:25
id_offset_vector::const_iterator end_
Definition: ValueMap.h:219
reference_type operator[](const RefKey &r)
Definition: ValueMap.h:140
value_map values_
Definition: ValueMap.h:86
std::vector< size_t > index_vector
Definition: ValueMap.h:24
size_t size() const
Definition: ValueMap.h:150
HLT enums.
const_iterator operator+(difference_type n) const
Definition: ValueMap.h:199
double a
Definition: hdecay.h:121
std::vector< value_type > container
Definition: ValueMap.h:110
const_iterator operator-(difference_type n) const
Definition: ValueMap.h:200
void throwNotExisting() const
Definition: ValueMap.h:253
Map::id_offset_vector id_offset_vector
Definition: ValueMap.h:28
const container * values_
Definition: ValueMap.h:218
void add(const Map &map)
Definition: ValueMap.h:32
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
long double T
bool empty() const
Definition: ValueMap.h:152
const T & operator[](size_t i)
Definition: ValueMap.h:179
bool contains(ProductID id) const
Definition: ValueMap.h:149
container values_
Definition: ValueMap.h:235
EventID const & max(EventID const &lh, EventID const &rh)
Definition: EventID.h:118
bool operator<(const const_iterator &o) const
Definition: ValueMap.h:201