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