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) {
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  }
61  void fill() {
62  map_.clear();
63  offset off = 0;
64  for(typename value_map::const_iterator i = values_.begin(); i != values_.end(); ++i) {
65  ProductID id = i->first;
66  map_.ids_.push_back(std::make_pair(id, off));
67  const value_vector & values = i->second;
68  for(typename value_vector::const_iterator j = values.begin(); j != values.end(); ++j) {
69  map_.values_.push_back( *j );
70  ++off;
71  }
72  }
73  }
74 
75  protected:
76  Map & map_;
77 
78  private:
80  void throwFillSize() const {
82  "ValueMap::Filler: handle and reference "
83  "collections should the same size\n");
84  }
85  void throwFillID(ProductID id) const {
87  e << "index map has already been filled for id: " << id << "\n";
88  e.raise();
89  }
90  void throwAdd() const {
92  "ValueMap: trying to add entries for an already existing product\n");
93  }
94  };
95  }
96 
97  template<typename T>
98  class ValueMap {
99  public:
100  typedef T value_type;
101  typedef std::vector<value_type> container;
102  typedef unsigned int offset;
103  typedef std::vector<std::pair<ProductID, offset> > id_offset_vector;
105  typedef typename container::const_reference const_reference_type;
106 
107  ValueMap() { }
108 
109  void swap(ValueMap& other) {
110  values_.swap(other.values_);
111  ids_.swap(other.ids_);
112  }
113 
114  ValueMap& operator=(ValueMap const& rhs) {
115  ValueMap temp(rhs);
116  this->swap(temp);
117  return *this;
118  }
119 
120  template<typename RefKey>
121  const_reference_type operator[](const RefKey & r) const {
122  return get(r.id(), r.key());
123  }
124  // raw index of a given (id,key) pair
125  size_t rawIndexOf(ProductID id, size_t idx) const {
126  typename id_offset_vector::const_iterator f = getIdOffset(id);
127  if(f==ids_.end()) throwNotExisting();
128  offset off = f->second;
129  size_t j = off+idx;
130  if(j >= values_.size()) throwIndexBound();
131  return j;
132  }
133  const_reference_type get(ProductID id, size_t idx) const {
134  return values_[rawIndexOf(id,idx)];
135  }
136  template<typename RefKey>
137  reference_type operator[](const RefKey & r) {
138  return get(r.id(), r.key());
139  }
140  reference_type get(ProductID id, size_t idx) {
141  return values_[rawIndexOf(id,idx)];
142  }
143 
145  add(o);
146  return *this;
147  }
148  bool contains(ProductID id) const {
149  return getIdOffset(id) != ids_.end();
150  }
151  size_t size() const { return values_.size(); }
152  size_t idSize() const { return ids_.size(); }
153  bool empty() const { return values_.empty(); }
154  void clear() { values_.clear(); ids_.clear(); }
155 
157 
158  struct const_iterator {
159  typedef ptrdiff_t difference_type;
161  ProductID id() const { return i_->first; }
162  typename container::const_iterator begin() const {
163  return values_->begin() + i_->second;
164  }
165  typename container::const_iterator end() const {
166  if(i_ == end_) return values_->end();
167  id_offset_vector::const_iterator end = i_; ++end;
168  if(end == end_) return values_->end();
169  return values_->begin() + end->second;
170  }
171  size_t size() const { return end() - begin(); }
172  const T & operator[](size_t i) { return *(begin()+i); }
173  const_iterator& operator++() { ++i_; return *this; }
174  const_iterator operator++(int) { const_iterator ci = *this; ++i_; return ci; }
175  const_iterator& operator--() { --i_; return *this; }
176  const_iterator operator--(int) { const_iterator ci = *this; --i_; return ci; }
177  difference_type operator-(const const_iterator & o) const { return i_ - o.i_; }
180  bool operator<(const const_iterator & o) const { return i_ < o.i_; }
181  bool operator==(const const_iterator& ci) const { return i_ == ci.i_; }
182  bool operator!=(const const_iterator& ci) const { return i_ != ci.i_; }
183  const_iterator & operator +=(difference_type d) { i_ += d; return *this; }
184  const_iterator & operator -=(difference_type d) { i_ -= d; return *this; }
185  private:
186  const_iterator(const id_offset_vector::const_iterator & i_,
187  const id_offset_vector::const_iterator & end,
188  const container * values) :
189  values_(values), i_(i_), end_(end) { }
191  id_offset_vector::const_iterator i_, end_;
192  friend class ValueMap<T>;
193  };
194 
195  const_iterator begin() const { return const_iterator(ids_.begin(), ids_.end(), &values_); }
196  const_iterator end() const { return const_iterator(ids_.end(), ids_.end(), &values_); }
197 
199  const id_offset_vector & ids() const { return ids_; }
201  const_reference_type get(size_t idx) const { return values_[idx]; }
202 
203  //Used by ROOT storage
205 
206  protected:
209 
210  typename id_offset_vector::const_iterator getIdOffset(ProductID id) const {
211  typename id_offset_vector::const_iterator i = std::lower_bound(ids_.begin(), ids_.end(), id, IDComparator());
212  if(i==ids_.end()) return i;
213  return i->first == id ? i : ids_.end();
214  }
215 
216  void throwIndexBound() const {
217  Exception::throwThis(errors::InvalidReference, "ValueMap: index out of upper boundary\n");
218  }
219 
220  private:
221  struct IDComparator {
222  bool operator()(const std::pair<ProductID, offset> & p, const ProductID & id) {
223  return p.first < id;
224  }
225  };
226  void throwNotExisting() const {
227  Exception::throwThis(errors::InvalidReference, "ValueMap: no associated value for given product and index\n");
228  }
229 
230  void add( const ValueMap<T> & o ) {
231  Filler filler(*this);
232  filler.add(o);
233  filler.fill();
234  }
235 
236  friend class helper::Filler<ValueMap<T> >;
237  };
238 
239  template<typename T>
240  inline ValueMap<T> operator+( const ValueMap<T> & a1,
241  const ValueMap<T> & a2 ) {
242  ValueMap<T> a = a1;
243  a += a2;
244  return a;
245  }
246 
247  // Free swap function
248  template <typename T>
249  inline
250  void swap(ValueMap<T>& lhs, ValueMap<T>& rhs) {
251  lhs.swap(rhs);
252  }
253 
254 }
255 #endif
bool empty() const
Definition: ValueMap.h:153
int i
Definition: DBlmapReader.cc:9
unsigned int offset
Definition: ValueMap.h:102
bool operator<(const const_iterator &o) const
Definition: ValueMap.h:180
const_iterator end() const
Definition: ValueMap.h:196
id_offset_vector::const_iterator i_
Definition: ValueMap.h:191
void clear()
Definition: ValueMap.h:154
const_iterator(const id_offset_vector::const_iterator &i_, const id_offset_vector::const_iterator &end, const container *values)
Definition: ValueMap.h:186
Association< C > operator+(const Association< C > &a1, const Association< C > &a2)
Definition: Association.h:121
const_iterator & operator-=(difference_type d)
Definition: ValueMap.h:184
ValueMap< T > & operator+=(const ValueMap< T > &o)
Definition: ValueMap.h:144
const id_offset_vector & ids() const
meant to be used in AssociativeIterator, not by the ordinary user
Definition: ValueMap.h:199
void swap(ValueMap &other)
Definition: ValueMap.h:109
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:177
const_iterator operator-(difference_type n) const
Definition: ValueMap.h:179
id_offset_vector ids_
Definition: ValueMap.h:208
#define CMS_CLASS_VERSION(_version_)
Filler(Map &map)
Definition: ValueMap.h:29
const_iterator & operator+=(difference_type d)
Definition: ValueMap.h:183
bool operator()(const std::pair< ProductID, offset > &p, const ProductID &id)
Definition: ValueMap.h:222
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
const_iterator & operator++()
Definition: ValueMap.h:173
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:125
void throwIndexBound() const
Definition: ValueMap.h:216
container::reference reference_type
Definition: ValueMap.h:104
bool contains(ProductID id) const
Definition: ValueMap.h:148
void throwFillSize() const
Definition: ValueMap.h:80
const_iterator operator--(int)
Definition: ValueMap.h:176
container::const_reference const_reference_type
Definition: ValueMap.h:105
std::vector< std::pair< ProductID, offset > > id_offset_vector
Definition: ValueMap.h:103
const_iterator operator+(difference_type n) const
Definition: ValueMap.h:178
const_iterator & operator--()
Definition: ValueMap.h:175
bool operator!=(const const_iterator &ci) const
Definition: ValueMap.h:182
void add(const ValueMap< T > &o)
Definition: ValueMap.h:230
int j
Definition: DBlmapReader.cc:9
void throwNotExisting() const
Definition: ValueMap.h:226
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:210
unsigned int offset(bool)
helper::Filler< ValueMap< T > > Filler
Definition: ValueMap.h:156
const_iterator operator++(int)
Definition: ValueMap.h:174
size_t idSize() const
Definition: ValueMap.h:152
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:195
std::vector< typename Map::value_type > value_vector
Definition: ValueMap.h:24
container::const_iterator begin() const
Definition: ValueMap.h:162
id_offset_vector::const_iterator end_
Definition: ValueMap.h:191
reference_type operator[](const RefKey &r)
Definition: ValueMap.h:137
ValueMap & operator=(ValueMap const &rhs)
Definition: ValueMap.h:114
value_map values_
Definition: ValueMap.h:79
std::vector< size_t > index_vector
Definition: ValueMap.h:23
bool operator==(const const_iterator &ci) const
Definition: ValueMap.h:181
#define begin
Definition: vmac.h:30
size_t size() const
Definition: ValueMap.h:151
double a
Definition: hdecay.h:121
std::vector< value_type > container
Definition: ValueMap.h:101
const_reference_type operator[](const RefKey &r) const
Definition: ValueMap.h:121
Map::id_offset_vector id_offset_vector
Definition: ValueMap.h:27
ProductID id() const
Definition: ValueMap.h:161
#define protected
Definition: FWEveView.cc:35
void throwFillID(ProductID id) const
Definition: ValueMap.h:85
const container * values_
Definition: ValueMap.h:190
container::const_iterator end() const
Definition: ValueMap.h:165
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:172
tuple size
Write out results.
void throwAdd() const
Definition: ValueMap.h:90
container values_
Definition: ValueMap.h:207
EventID const & max(EventID const &lh, EventID const &rh)
Definition: EventID.h:137