CMS 3D CMS Logo

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