test
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  ids_.shrink_to_fit();
163  values_.shrink_to_fit();
164  }
165 
166 
167 
169 
170  struct const_iterator {
171  typedef ptrdiff_t difference_type;
173  ProductID id() const { return i_->first; }
174  typename container::const_iterator begin() const {
175  return values_->begin() + i_->second;
176  }
177  typename container::const_iterator end() const {
178  if(i_ == end_) return values_->end();
179  id_offset_vector::const_iterator end = i_; ++end;
180  if(end == end_) return values_->end();
181  return values_->begin() + end->second;
182  }
183  size_t size() const { return end() - begin(); }
184  const T & operator[](size_t i) { return *(begin()+i); }
185  const_iterator& operator++() { ++i_; return *this; }
186  const_iterator operator++(int) { const_iterator ci = *this; ++i_; return ci; }
187  const_iterator& operator--() { --i_; return *this; }
188  const_iterator operator--(int) { const_iterator ci = *this; --i_; return ci; }
189  difference_type operator-(const const_iterator & o) const { return i_ - o.i_; }
192  bool operator<(const const_iterator & o) const { return i_ < o.i_; }
193  bool operator==(const const_iterator& ci) const { return i_ == ci.i_; }
194  bool operator!=(const const_iterator& ci) const { return i_ != ci.i_; }
195  const_iterator & operator +=(difference_type d) { i_ += d; return *this; }
196  const_iterator & operator -=(difference_type d) { i_ -= d; return *this; }
197  private:
198  const_iterator(const id_offset_vector::const_iterator & i_,
199  const id_offset_vector::const_iterator & end,
200  const container * values) :
201  values_(values), i_(i_), end_(end) { }
203  id_offset_vector::const_iterator i_, end_;
204  friend class ValueMap<T>;
205  };
206 
207  const_iterator begin() const { return const_iterator(ids_.begin(), ids_.end(), &values_); }
208  const_iterator end() const { return const_iterator(ids_.end(), ids_.end(), &values_); }
209 
211  const id_offset_vector & ids() const { return ids_; }
213  const_reference_type get(size_t idx) const { return values_[idx]; }
214 
215  //Used by ROOT storage
217 
218  protected:
221 
222  typename id_offset_vector::const_iterator getIdOffset(ProductID id) const {
223  typename id_offset_vector::const_iterator i = std::lower_bound(ids_.begin(), ids_.end(), id, IDComparator());
224  if(i==ids_.end()) return i;
225  return i->first == id ? i : ids_.end();
226  }
227 
228  void throwIndexBound() const {
229  Exception::throwThis(errors::InvalidReference, "ValueMap: index out of upper boundary\n");
230  }
231 
232  private:
233  struct IDComparator {
234  bool operator()(const std::pair<ProductID, offset> & p, const ProductID & id) {
235  return p.first < id;
236  }
237  };
238  void throwNotExisting() const {
239  Exception::throwThis(errors::InvalidReference, "ValueMap: no associated value for given product and index\n");
240  }
241 
242  void add( const ValueMap<T> & o ) {
243  Filler filler(*this);
244  filler.add(o);
245  filler.fill();
246  }
247 
248  friend class helper::Filler<ValueMap<T> >;
249  };
250 
251  template<typename T>
252  inline ValueMap<T> operator+( const ValueMap<T> & a1,
253  const ValueMap<T> & a2 ) {
254  ValueMap<T> a = a1;
255  a += a2;
256  return a;
257  }
258 
259  // Free swap function
260  template <typename T>
261  inline
262  void swap(ValueMap<T>& lhs, ValueMap<T>& rhs) {
263  lhs.swap(rhs);
264  }
265 
266 }
267 #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:192
const_iterator end() const
Definition: ValueMap.h:208
id_offset_vector::const_iterator i_
Definition: ValueMap.h:203
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:198
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
const_iterator & operator-=(difference_type d)
Definition: ValueMap.h:196
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:211
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:189
const_iterator operator-(difference_type n) const
Definition: ValueMap.h:191
id_offset_vector ids_
Definition: ValueMap.h:220
#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:195
bool operator()(const std::pair< ProductID, offset > &p, const ProductID &id)
Definition: ValueMap.h:234
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
const_iterator & operator++()
Definition: ValueMap.h:185
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:228
tuple d
Definition: ztail.py:151
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:188
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:190
const_iterator & operator--()
Definition: ValueMap.h:187
bool operator!=(const const_iterator &ci) const
Definition: ValueMap.h:194
void add(const ValueMap< T > &o)
Definition: ValueMap.h:242
int j
Definition: DBlmapReader.cc:9
void throwNotExisting() const
Definition: ValueMap.h:238
const std::complex< double > I
Definition: I.h:8
double f[11][100]
#define end
Definition: vmac.h:37
id_offset_vector::const_iterator getIdOffset(ProductID id) const
Definition: ValueMap.h:222
void shrink_to_fit()
Definition: ValueMap.h:161
helper::Filler< ValueMap< T > > Filler
Definition: ValueMap.h:168
const_iterator operator++(int)
Definition: ValueMap.h:186
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:207
std::vector< typename Map::value_type > value_vector
Definition: ValueMap.h:24
container::const_iterator begin() const
Definition: ValueMap.h:174
id_offset_vector::const_iterator end_
Definition: ValueMap.h:203
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:193
#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:173
void throwFillID(ProductID id) const
Definition: ValueMap.h:91
const container * values_
Definition: ValueMap.h:202
container::const_iterator end() const
Definition: ValueMap.h:177
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:184
tuple size
Write out results.
void throwAdd() const
Definition: ValueMap.h:96
container values_
Definition: ValueMap.h:219
EventID const & max(EventID const &lh, EventID const &rh)
Definition: EventID.h:142