CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AssociationVector.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_AssociationVector_h
2 #define DataFormats_Common_AssociationVector_h
3 /* class edm::AssociationVector<CKey, CVal>
4  *
5  * adds to a std::vector<CVal> a edm::RefProd<CKey>, in such a way
6  * that, assuming that the CVal and CKey collections have the same
7  * size and are properly ordered, the two collections can be
8  * in one-to-one correspondance
9  *
10  * \author Luca Lista, INFN
11  *
12  */
13 
15 
24 
26 
27 #include "boost/static_assert.hpp"
28 
29 namespace edm {
30  namespace helper {
31 
33  template<typename T>
34  static T const& get(T const& t, ProductID) { return t; }
35  };
36 
37  template<typename T>
40  };
41 
42  template<typename REFPROD>
43  struct RefFromRefProdTrait { };
44 
45  template<typename C>
48  };
49 
50  template<typename T>
53  };
54  }
55 
56  template<typename KeyRefProd, typename CVal,
57  typename KeyRef = typename helper::RefFromRefProdTrait<KeyRefProd>::ref_type,
58  typename SizeType = unsigned int,//the type used here can not change when go from 32bit to 64bit or across platforms
59  typename KeyReferenceHelper = typename helper::AssociationKeyReferenceTrait<KeyRef>::type>
63 
64  public:
65  typedef KeyRefProd refprod_type;
66  typedef typename KeyRefProd::product_type CKey;
67  typedef SizeType size_type;
68  typedef typename KeyRef::value_type key_type;
69  typedef typename std::pair<KeyRef, typename CVal::value_type> value_type;
70  typedef std::vector<value_type> transient_vector_type;
71  typedef value_type const& const_reference;
73  AssociationVector(KeyRefProd const& ref, CKey const* = 0);
76 
77  size_type size() const;
78  bool empty() const;
80  typename CVal::const_reference operator[](KeyRef const& k) const;
81  typename CVal::reference operator[](KeyRef const& k);
82 
83  self& operator=(self const&);
84 
85  void clear();
86  void swap(self& other);
87  KeyRefProd const& keyProduct() const { return ref_; }
88  KeyRef key(size_type i) const { return KeyRef(ref_, i); }
89  typename CVal::value_type const value(size_type i) const { return data_[ i ]; }
90  void setValue(size_type i, typename CVal::value_type const& val);
91  void fillView(ProductID const& id,
92  std::vector<void const*>& pointers,
93  helper_vector& helpers) const;
94 
95  typedef typename transient_vector_type::const_iterator const_iterator;
96 
97  const_iterator begin() const { return transientVector().begin(); }
98  const_iterator end() const { return transientVector().end(); }
99 
100  private:
101  CVal data_;
102  KeyRefProd ref_;
104  mutable bool fixed_;
106  void fixup() const;
107  };
108 
109  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
111  data_(), ref_(), transientVector_(), fixed_(false) { }
112 
113  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
115  CKey const* coll) :
116  data_(coll == 0 ? ref->size() : coll->size()), ref_(ref),
117  transientVector_(coll == 0 ? ref->size() : coll->size()), fixed_(true) { }
118 
119  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
122  data_(o.data_), ref_(o.ref_), transientVector_(o.transientVector_), fixed_(o.fixed_) { }
123 
124  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
126 
127  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
130  return transientVector()[ n ];
131  }
132 
133  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
134  inline typename CVal::const_reference
136  KeyRef keyRef = KeyReferenceHelper::get(k, ref_.id());
137  checkForWrongProduct(keyRef.id(), ref_.id());
138  return data_[ keyRef.key() ];
139  }
140 
141 
142  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
143  inline typename CVal::reference
145  KeyRef keyRef = KeyReferenceHelper::get(k, ref_.id());
146  fixed_ = false;
147  checkForWrongProduct(keyRef.id(), ref_.id());
148  return data_[ keyRef.key() ];
149  }
150 
151  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
154  data_ = o.data_;
155  ref_ = o.ref_;
156  fixed_ = false;
157  return * this;
158  }
159 
160  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
162  data_[ i ] = val;
163  KeyRef ref(ref_, i);
164  transientVector_[ i ].first = ref;
165  transientVector_[ i ].second = data_[ i ];
166  }
167 
168  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
171  return data_.size();
172  }
173 
174  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
176  return data_.empty();
177  }
178 
179  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
181  data_.clear();
182  transientVector_.clear();
183  ref_ = KeyRefProd();
184  fixed_ = true;
185  }
186 
187  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
189  data_.swap(other.data_);
190  transientVector_.swap(other.transientVector_);
191  ref_.swap(other.ref_);
192  std::swap(fixed_, other.fixed_);
193  }
194 
195  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
197  if (!fixed_) {
198  fixed_ = true;
199  transientVector_.resize(size());
200  for(size_type i = 0; i != size(); ++i) {
201  transientVector_[ i ] = std::make_pair(KeyRef(ref_, i), data_[ i ]);
202  }
203  }
204  }
205 
206  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
208  std::vector<void const*>& pointers,
209  helper_vector& helpers) const
210  {
211  detail::reallyFillView(*this, id, pointers, helpers);
212 // pointers.reserve(this->size());
213 // for(typename CVal::const_iterator i=data_.begin(), e=data_.end(); i!=e; ++i)
214 // pointers.push_back(&(*i));
215 // // helpers is not yet filled in.
216 // //Exception::throwThis(errors::UnimplementedFeature, "AssociationVector<T>::fillView(...)");
217  }
218 
219  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
222  a.swap(b);
223  }
224 
225  //----------------------------------------------------------------------
226  //
227  // Free function template to support creation of Views.
228 
229  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
230  inline
231  void
233  ProductID const& id,
234  std::vector<void const*>& pointers,
235  helper_vector& helpers) {
236  obj.fillView(id, pointers, helpers);
237  }
238 
239  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
240  struct has_fillView<AssociationVector<KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper> > {
241  static bool const value = true;
242  };
243 
244 }
245 
246 #endif
void checkForWrongProduct(ProductID const &keyID, ProductID const &refID)
int i
Definition: DBlmapReader.cc:9
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector &helpers)
transient_vector_type::const_iterator const_iterator
Ref< typename RefProd< C >::product_type > ref_type
void reallyFillView(COLLECTION const &coll, ProductID const &id, std::vector< void const * > &ptrs, helper_vector &helpers)
Definition: FillView.h:49
void fillView(ProductID const &id, std::vector< void const * > &pointers, helper_vector &helpers) const
self & operator=(self const &)
const_iterator end() const
Container::value_type value_type
std::vector< value_type > transient_vector_type
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
AssociationIdenticalKeyReference type
transient_vector_type const & transientVector() const
tuple obj
Example code starts here #.
Definition: VarParsing.py:655
value_type const & const_reference
const_reference operator[](size_type n) const
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::pair< KeyRef, typename CVal::value_type > value_type
BOOST_STATIC_ASSERT((boost::is_convertible< SizeType, typename CVal::size_type >::value))
transient_vector_type transientVector_
void swap(self &other)
CVal::value_type const value(size_type i) const
KeyRefProd::product_type CKey
KeyRef key(size_type i) const
int k[5][pyjets_maxn]
JetCorrectorParametersCollection coll
Definition: classes.h:14
double b
Definition: hdecay.h:120
KeyRef::value_type key_type
double a
Definition: hdecay.h:121
KeyRefProd const & keyProduct() const
void setValue(size_type i, typename CVal::value_type const &val)
const_iterator begin() const
tuple size
Write out results.
T get(const Candidate &c)
Definition: component.h:56
static bool const value
Definition: traits.h:125
size_type size() const