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  ValueMap& operator=(ValueMap const& rhs) {
124  ValueMap temp(rhs);
125  this->swap(temp);
126  return *this;
127  }
128 
129  template <typename RefKey>
130  const_reference_type operator[](const RefKey& r) const {
131  return get(r.id(), r.key());
132  }
133  // raw index of a given (id,key) pair
134  size_t rawIndexOf(ProductID id, size_t idx) const {
135  typename id_offset_vector::const_iterator f = getIdOffset(id);
136  if (f == ids_.end())
138  offset off = f->second;
139  size_t j = off + idx;
140  if (j >= values_.size())
141  throwIndexBound();
142  return j;
143  }
144  const_reference_type get(ProductID id, size_t idx) const { return values_[rawIndexOf(id, idx)]; }
145  template <typename RefKey>
146  reference_type operator[](const RefKey& r) {
147  return get(r.id(), r.key());
148  }
149  reference_type get(ProductID id, size_t idx) { return values_[rawIndexOf(id, idx)]; }
150 
152  add(o);
153  return *this;
154  }
155  bool contains(ProductID id) const { return getIdOffset(id) != ids_.end(); }
156  size_t size() const { return values_.size(); }
157  size_t idSize() const { return ids_.size(); }
158  bool empty() const { return values_.empty(); }
159  void clear() {
160  values_.clear();
161  ids_.clear();
162  }
163  void shrink_to_fit() {
164  ids_.shrink_to_fit();
165  values_.shrink_to_fit();
166  }
167 
169 
170  struct const_iterator {
171  typedef ptrdiff_t difference_type;
172  const_iterator() : values_(nullptr) {}
173  ProductID id() const { return i_->first; }
174  typename container::const_iterator begin() const { return values_->begin() + i_->second; }
175  typename container::const_iterator end() const {
176  if (i_ == end_)
177  return values_->end();
178  id_offset_vector::const_iterator end = i_;
179  ++end;
180  if (end == end_)
181  return values_->end();
182  return values_->begin() + end->second;
183  }
184  size_t size() const { return end() - begin(); }
185  const T& operator[](size_t i) { return *(begin() + i); }
187  ++i_;
188  return *this;
189  }
191  const_iterator ci = *this;
192  ++i_;
193  return ci;
194  }
196  --i_;
197  return *this;
198  }
200  const_iterator ci = *this;
201  --i_;
202  return ci;
203  }
204  difference_type operator-(const const_iterator& o) const { return i_ - o.i_; }
207  bool operator<(const const_iterator& o) const { return i_ < o.i_; }
208  bool operator==(const const_iterator& ci) const { return i_ == ci.i_; }
209  bool operator!=(const const_iterator& ci) const { return i_ != ci.i_; }
211  i_ += d;
212  return *this;
213  }
215  i_ -= d;
216  return *this;
217  }
218 
219  private:
220  const_iterator(const id_offset_vector::const_iterator& i_,
221  const id_offset_vector::const_iterator& end,
222  const container* values)
223  : values_(values), i_(i_), end_(end) {}
225  id_offset_vector::const_iterator i_, end_;
226  friend class ValueMap<T>;
227  };
228 
229  const_iterator begin() const { return const_iterator(ids_.begin(), ids_.end(), &values_); }
230  const_iterator end() const { return const_iterator(ids_.end(), ids_.end(), &values_); }
231 
233  const id_offset_vector& ids() const { return ids_; }
235  const_reference_type get(size_t idx) const { return values_[idx]; }
236 
237  //Used by ROOT storage
239 
240  protected:
243 
244  typename id_offset_vector::const_iterator getIdOffset(ProductID id) const {
245  typename id_offset_vector::const_iterator i = std::lower_bound(ids_.begin(), ids_.end(), id, IDComparator());
246  if (i == ids_.end())
247  return i;
248  return i->first == id ? i : ids_.end();
249  }
250 
251  void throwIndexBound() const {
252  Exception::throwThis(errors::InvalidReference, "ValueMap: index out of upper boundary\n");
253  }
254 
255  private:
256  struct IDComparator {
257  bool operator()(const std::pair<ProductID, offset>& p, const ProductID& id) { return p.first < id; }
258  };
259  void throwNotExisting() const {
260  Exception::throwThis(errors::InvalidReference, "ValueMap: no associated value for given product and index\n");
261  }
262 
263  void add(const ValueMap<T>& o) {
264  Filler filler(*this);
265  filler.add(o);
266  filler.fill();
267  }
268 
269  friend class helper::Filler<ValueMap<T> >;
270  };
271 
272  template <typename T>
273  inline ValueMap<T> operator+(const ValueMap<T>& a1, const ValueMap<T>& a2) {
274  ValueMap<T> a = a1;
275  a += a2;
276  return a;
277  }
278 
279  // Free swap function
280  template <typename T>
281  inline void swap(ValueMap<T>& lhs, ValueMap<T>& rhs) {
282  lhs.swap(rhs);
283  }
284 
285 } // namespace edm
286 #endif
size
Write out results.
bool operator!=(const const_iterator &ci) const
Definition: ValueMap.h:209
unsigned int offset
Definition: ValueMap.h:111
Definition: helper.py:1
id_offset_vector::const_iterator i_
Definition: ValueMap.h:225
id_offset_vector::const_iterator getIdOffset(ProductID id) const
Definition: ValueMap.h:244
void clear()
Definition: ValueMap.h:159
const_iterator(const id_offset_vector::const_iterator &i_, const id_offset_vector::const_iterator &end, const container *values)
Definition: ValueMap.h:220
Association< C > operator+(const Association< C > &a1, const Association< C > &a2)
Definition: Association.h:122
size_t idSize() const
Definition: ValueMap.h:157
const_iterator & operator-=(difference_type d)
Definition: ValueMap.h:214
ValueMap< T > & operator+=(const ValueMap< T > &o)
Definition: ValueMap.h:151
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:204
id_offset_vector ids_
Definition: ValueMap.h:242
#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:233
size_t rawIndexOf(ProductID id, size_t idx) const
Definition: ValueMap.h:134
const_iterator & operator+=(difference_type d)
Definition: ValueMap.h:210
bool operator==(const const_iterator &ci) const
Definition: ValueMap.h:208
bool operator()(const std::pair< ProductID, offset > &p, const ProductID &id)
Definition: ValueMap.h:257
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
const_iterator & operator++()
Definition: ValueMap.h:186
void throwAdd() const
Definition: ValueMap.h:99
const_iterator end() const
Definition: ValueMap.h:230
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:130
const_iterator operator--(int)
Definition: ValueMap.h:199
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:195
void add(const ValueMap< T > &o)
Definition: ValueMap.h:263
const std::complex< double > I
Definition: I.h:8
double f[11][100]
ProductID id() const
Definition: ValueMap.h:173
d
Definition: ztail.py:151
void shrink_to_fit()
Definition: ValueMap.h:163
container::const_iterator end() const
Definition: ValueMap.h:175
const_iterator begin() const
Definition: ValueMap.h:229
container::const_iterator begin() const
Definition: ValueMap.h:174
helper::Filler< ValueMap< T > > Filler
Definition: ValueMap.h:168
const_iterator operator++(int)
Definition: ValueMap.h:190
void throwIndexBound() const
Definition: ValueMap.h:251
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:225
reference_type operator[](const RefKey &r)
Definition: ValueMap.h:146
ValueMap & operator=(ValueMap const &rhs)
Definition: ValueMap.h:123
value_map values_
Definition: ValueMap.h:86
std::vector< size_t > index_vector
Definition: ValueMap.h:24
size_t size() const
Definition: ValueMap.h:156
HLT enums.
const_iterator operator+(difference_type n) const
Definition: ValueMap.h:205
double a
Definition: hdecay.h:119
std::vector< value_type > container
Definition: ValueMap.h:110
const_iterator operator-(difference_type n) const
Definition: ValueMap.h:206
void throwNotExisting() const
Definition: ValueMap.h:259
Map::id_offset_vector id_offset_vector
Definition: ValueMap.h:28
const container * values_
Definition: ValueMap.h:224
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:158
const T & operator[](size_t i)
Definition: ValueMap.h:185
bool contains(ProductID id) const
Definition: ValueMap.h:155
container values_
Definition: ValueMap.h:241
EventID const & max(EventID const &lh, EventID const &rh)
Definition: EventID.h:118
bool operator<(const const_iterator &o) const
Definition: ValueMap.h:207