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  public:
30  explicit Filler(Map & map) :
31  map_(map), totSize_(0) {
32  add(map);
33  }
34  void add(const Map & map) {
35  if (map.empty()) return;
36  typename id_offset_vector::const_iterator j = map.ids_.begin();
37  const typename id_offset_vector::const_iterator end = map.ids_.end();
38  size_t i = 0;
39  const size_t size = map.values_.size();
40  // std::pair<ProductID, offset> id = *j;
41  do {
42  ProductID id = j->first;
43  ++j;
44  size_t max = (j == end ? size : j->second);
45  typename value_map::iterator f = values_.find(id);
46  if(f!=values_.end()) 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) throwFillSize();
57  typename value_map::const_iterator f = values_.find(id);
58  if(f != values_.end()) throwFillID(id);
59  value_vector & values = values_.insert(make_pair(id, value_vector(size))).first->second;
60  std::copy(begin, end, values.begin());
61  totSize_+=size;
62  }
63  void fill() {
64  map_.clear();
65  offset off = 0;
66  map_.ids_.reserve(values_.size());
67  map_.values_.reserve(totSize_);
68  for(typename value_map::const_iterator i = values_.begin(); i != values_.end(); ++i) {
69  ProductID id = i->first;
70  map_.ids_.push_back(std::make_pair(id, off));
71  const value_vector & values = i->second;
72  for(typename value_vector::const_iterator j = values.begin(); j != values.end(); ++j) {
73  map_.values_.push_back( *j );
74  ++off;
75  }
76  }
77  map_.shrink_to_fit();
78  }
79 
80  protected:
81  Map & map_;
82 
83  private:
84  value_map values_;
85  size_t totSize_;
86 
87  void throwFillSize() const {
89  "ValueMap::Filler: handle and reference "
90  "collections should the same size\n");
91  }
92  void throwFillID(ProductID id) const {
94  e << "index map has already been filled for id: " << id << "\n";
95  e.raise();
96  }
97  void throwAdd() const {
99  "ValueMap: trying to add entries for an already existing product\n");
100  }
101  };
102  }
103 
104  template<typename T>
105  class ValueMap {
106  public:
107  typedef T value_type;
108  typedef std::vector<value_type> container;
109  typedef unsigned int offset;
110  typedef std::vector<std::pair<ProductID, offset> > id_offset_vector;
112  typedef typename container::const_reference const_reference_type;
113 
114  ValueMap() { }
115 
116  void swap(ValueMap& other) {
117  values_.swap(other.values_);
118  ids_.swap(other.ids_);
119  }
120 
121  ValueMap& operator=(ValueMap const& rhs) {
122  ValueMap temp(rhs);
123  this->swap(temp);
124  return *this;
125  }
126 
127  template<typename RefKey>
128  const_reference_type operator[](const RefKey & r) const {
129  return get(r.id(), r.key());
130  }
131  // raw index of a given (id,key) pair
132  size_t rawIndexOf(ProductID id, size_t idx) const {
133  typename id_offset_vector::const_iterator f = getIdOffset(id);
134  if(f==ids_.end()) throwNotExisting();
135  offset off = f->second;
136  size_t j = off+idx;
137  if(j >= values_.size()) throwIndexBound();
138  return j;
139  }
140  const_reference_type get(ProductID id, size_t idx) const {
141  return values_[rawIndexOf(id,idx)];
142  }
143  template<typename RefKey>
144  reference_type operator[](const RefKey & r) {
145  return get(r.id(), r.key());
146  }
147  reference_type get(ProductID id, size_t idx) {
148  return values_[rawIndexOf(id,idx)];
149  }
150 
152  add(o);
153  return *this;
154  }
155  bool contains(ProductID id) const {
156  return getIdOffset(id) != ids_.end();
157  }
158  size_t size() const { return values_.size(); }
159  size_t idSize() const { return ids_.size(); }
160  bool empty() const { return values_.empty(); }
161  void clear() { values_.clear(); ids_.clear(); }
162  void shrink_to_fit() {
163  ids_.shrink_to_fit();
164  values_.shrink_to_fit();
165  }
166 
167 
168 
170 
171  struct const_iterator {
172  typedef ptrdiff_t difference_type;
174  ProductID id() const { return i_->first; }
175  typename container::const_iterator begin() const {
176  return values_->begin() + i_->second;
177  }
178  typename container::const_iterator end() const {
179  if(i_ == end_) return values_->end();
180  id_offset_vector::const_iterator end = i_; ++end;
181  if(end == end_) 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); }
186  const_iterator& operator++() { ++i_; return *this; }
187  const_iterator operator++(int) { const_iterator ci = *this; ++i_; return ci; }
188  const_iterator& operator--() { --i_; return *this; }
189  const_iterator operator--(int) { const_iterator ci = *this; --i_; return ci; }
190  difference_type operator-(const const_iterator & o) const { return i_ - o.i_; }
191  const_iterator operator+(difference_type n) const { return const_iterator(i_ + n, end_, values_); }
192  const_iterator operator-(difference_type n) const { return const_iterator(i_ - n, end_, values_); }
193  bool operator<(const const_iterator & o) const { return i_ < o.i_; }
194  bool operator==(const const_iterator& ci) const { return i_ == ci.i_; }
195  bool operator!=(const const_iterator& ci) const { return i_ != ci.i_; }
196  const_iterator & operator +=(difference_type d) { i_ += d; return *this; }
197  const_iterator & operator -=(difference_type d) { i_ -= d; return *this; }
198  private:
199  const_iterator(const id_offset_vector::const_iterator & i_,
200  const id_offset_vector::const_iterator & end,
201  const container * values) :
202  values_(values), i_(i_), end_(end) { }
203  const container * values_;
204  id_offset_vector::const_iterator i_, end_;
205  friend class ValueMap<T>;
206  };
207 
208  const_iterator begin() const { return const_iterator(ids_.begin(), ids_.end(), &values_); }
209  const_iterator end() const { return const_iterator(ids_.end(), ids_.end(), &values_); }
210 
212  const id_offset_vector & ids() const { return ids_; }
214  const_reference_type get(size_t idx) const { return values_[idx]; }
215 
216  //Used by ROOT storage
218 
219  protected:
220  container values_;
221  id_offset_vector ids_;
222 
223  typename id_offset_vector::const_iterator getIdOffset(ProductID id) const {
224  typename id_offset_vector::const_iterator i = std::lower_bound(ids_.begin(), ids_.end(), id, IDComparator());
225  if(i==ids_.end()) return i;
226  return i->first == id ? i : ids_.end();
227  }
228 
229  void throwIndexBound() const {
230  Exception::throwThis(errors::InvalidReference, "ValueMap: index out of upper boundary\n");
231  }
232 
233  private:
234  struct IDComparator {
235  bool operator()(const std::pair<ProductID, offset> & p, const ProductID & id) {
236  return p.first < id;
237  }
238  };
239  void throwNotExisting() const {
240  Exception::throwThis(errors::InvalidReference, "ValueMap: no associated value for given product and index\n");
241  }
242 
243  void add( const ValueMap<T> & o ) {
244  Filler filler(*this);
245  filler.add(o);
246  filler.fill();
247  }
248 
249  friend class helper::Filler<ValueMap<T> >;
250  };
251 
252  template<typename T>
253  inline ValueMap<T> operator+( const ValueMap<T> & a1,
254  const ValueMap<T> & a2 ) {
255  ValueMap<T> a = a1;
256  a += a2;
257  return a;
258  }
259 
260  // Free swap function
261  template <typename T>
262  inline
263  void swap(ValueMap<T>& lhs, ValueMap<T>& rhs) {
264  lhs.swap(rhs);
265  }
266 
267 }
268 #endif
size
Write out results.
bool empty() const
Definition: ValueMap.h:160
unsigned int offset
Definition: ValueMap.h:109
bool operator<(const const_iterator &o) const
Definition: ValueMap.h:193
Definition: helper.py:1
const_iterator end() const
Definition: ValueMap.h:209
id_offset_vector::const_iterator i_
Definition: ValueMap.h:204
void clear()
Definition: ValueMap.h:161
const_iterator(const id_offset_vector::const_iterator &i_, const id_offset_vector::const_iterator &end, const container *values)
Definition: ValueMap.h:199
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
Association< C > operator+(const Association< C > &a1, const Association< C > &a2)
Definition: Association.h:121
def copy(args, dbName)
ValueMap< T > & operator+=(const ValueMap< T > &o)
Definition: ValueMap.h:151
const id_offset_vector & ids() const
meant to be used in AssociativeIterator, not by the ordinary user
Definition: ValueMap.h:212
void swap(ValueMap &other)
Definition: ValueMap.h:116
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
Basic3DVector & operator-=(const Basic3DVector< U > &p)
void raise()
Definition: Exception.h:98
difference_type operator-(const const_iterator &o) const
Definition: ValueMap.h:190
const_iterator operator-(difference_type n) const
Definition: ValueMap.h:192
id_offset_vector ids_
Definition: ValueMap.h:221
#define CMS_CLASS_VERSION(_version_)
Filler(Map &map)
Definition: ValueMap.h:30
bool operator()(const std::pair< ProductID, offset > &p, const ProductID &id)
Definition: ValueMap.h:235
const_iterator & operator++()
Definition: ValueMap.h:186
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:83
size_t rawIndexOf(ProductID id, size_t idx) const
Definition: ValueMap.h:132
void throwIndexBound() const
Definition: ValueMap.h:229
container::reference reference_type
Definition: ValueMap.h:111
bool contains(ProductID id) const
Definition: ValueMap.h:155
void throwFillSize() const
Definition: ValueMap.h:87
void swap(IndexRangeAssociation &lhs, IndexRangeAssociation &rhs)
const_iterator operator--(int)
Definition: ValueMap.h:189
container::const_reference const_reference_type
Definition: ValueMap.h:112
std::vector< std::pair< ProductID, offset > > id_offset_vector
Definition: ValueMap.h:110
const_iterator operator+(difference_type n) const
Definition: ValueMap.h:191
const_iterator & operator--()
Definition: ValueMap.h:188
bool operator!=(const const_iterator &ci) const
Definition: ValueMap.h:195
void add(const ValueMap< T > &o)
Definition: ValueMap.h:243
void throwNotExisting() const
Definition: ValueMap.h:239
const std::complex< double > I
Definition: I.h:8
double f[11][100]
#define end
Definition: vmac.h:39
void shrink_to_fit()
Definition: ValueMap.h:162
helper::Filler< ValueMap< T > > Filler
Definition: ValueMap.h:169
const_iterator operator++(int)
Definition: ValueMap.h:187
size_t idSize() const
Definition: ValueMap.h:159
std::map< ProductID, value_vector > value_map
Definition: ValueMap.h:26
const_iterator begin() const
Definition: ValueMap.h:208
std::vector< typename Map::value_type > value_vector
Definition: ValueMap.h:25
container::const_iterator begin() const
Definition: ValueMap.h:175
reference_type operator[](const RefKey &r)
Definition: ValueMap.h:144
ValueMap & operator=(ValueMap const &rhs)
Definition: ValueMap.h:121
value_map values_
Definition: ValueMap.h:84
std::vector< size_t > index_vector
Definition: ValueMap.h:24
bool operator==(const const_iterator &ci) const
Definition: ValueMap.h:194
#define begin
Definition: vmac.h:32
HLT enums.
size_t size() const
Definition: ValueMap.h:158
double a
Definition: hdecay.h:121
std::vector< value_type > container
Definition: ValueMap.h:108
const_reference_type operator[](const RefKey &r) const
Definition: ValueMap.h:128
Map::id_offset_vector id_offset_vector
Definition: ValueMap.h:28
ProductID id() const
Definition: ValueMap.h:174
void throwFillID(ProductID id) const
Definition: ValueMap.h:92
const container * values_
Definition: ValueMap.h:203
container::const_iterator end() const
Definition: ValueMap.h:178
ProductIndex id() const
Definition: ProductID.h:38
void add(const Map &map)
Definition: ValueMap.h:34
long double T
const T & operator[](size_t i)
Definition: ValueMap.h:185
void throwAdd() const
Definition: ValueMap.h:97
Basic3DVector & operator+=(const Basic3DVector< U > &p)
container values_
Definition: ValueMap.h:220
EventID const & max(EventID const &lh, EventID const &rh)
Definition: EventID.h:142