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 
24 
25 #include "boost/static_assert.hpp"
26 
27 namespace edm {
28  namespace helper {
29 
31  template<typename T>
32  static T const& get(T const& t, ProductID) { return t; }
33  };
34 
35  template<typename T>
38  };
39 
40  template<typename REFPROD>
41  struct RefFromRefProdTrait { };
42 
43  template<typename C>
46  };
47 
48  template<typename T>
51  };
52  }
53 
54  template<typename KeyRefProd, typename CVal,
55  typename KeyRef = typename helper::RefFromRefProdTrait<KeyRefProd>::ref_type,
56  typename SizeType = unsigned int,//the type used here can not change when go from 32bit to 64bit or across platforms
57  typename KeyReferenceHelper = typename helper::AssociationKeyReferenceTrait<KeyRef>::type>
61 
62  public:
63  typedef KeyRefProd refprod_type;
64  typedef typename KeyRefProd::product_type CKey;
65  typedef SizeType size_type;
66  typedef typename KeyRef::value_type key_type;
67  typedef typename std::pair<KeyRef, typename CVal::value_type> value_type;
68  typedef std::vector<value_type> transient_vector_type;
69  typedef value_type const& const_reference;
71  AssociationVector(KeyRefProd const& ref, CKey const* = 0);
74 
75  size_type size() const;
76  bool empty() const;
78  typename CVal::const_reference operator[](KeyRef const& k) const;
79  typename CVal::reference operator[](KeyRef const& k);
80 
81  self& operator=(self const&);
82 
83  void clear();
84  void swap(self& other);
85  KeyRefProd const& keyProduct() const { return ref_; }
86  KeyRef key(size_type i) const { return KeyRef(ref_, i); }
87  typename CVal::value_type const value(size_type i) const { return data_[ i ]; }
88  void setValue(size_type i, typename CVal::value_type const& val);
89  void fillView(ProductID const& id,
90  std::vector<void const*>& pointers,
91  helper_vector& helpers) const;
92 
93  typedef typename transient_vector_type::const_iterator const_iterator;
94 
95  const_iterator begin() const { return transientVector().begin(); }
96  const_iterator end() const { return transientVector().end(); }
97 
98  //Used by ROOT storage
100 
101  private:
102  CVal data_;
103  KeyRefProd ref_;
105  mutable bool fixed_;
107  void fixup() const;
108  };
109 
110  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
112  data_(), ref_(), transientVector_(), fixed_(false) { }
113 
114  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
116  CKey const* coll) :
117  data_(coll == 0 ? ref->size() : coll->size()), ref_(ref),
118  transientVector_(coll == 0 ? ref->size() : coll->size()), fixed_(true) { }
119 
120  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
123  data_(o.data_), ref_(o.ref_), transientVector_(o.transientVector_), fixed_(o.fixed_) { }
124 
125  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
127 
128  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
131  return transientVector()[ n ];
132  }
133 
134  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
135  inline typename CVal::const_reference
137  KeyRef keyRef = KeyReferenceHelper::get(k, ref_.id());
138  checkForWrongProduct(keyRef.id(), ref_.id());
139  return data_[ keyRef.key() ];
140  }
141 
142 
143  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
144  inline typename CVal::reference
146  KeyRef keyRef = KeyReferenceHelper::get(k, ref_.id());
147  fixed_ = false;
148  checkForWrongProduct(keyRef.id(), ref_.id());
149  return data_[ keyRef.key() ];
150  }
151 
152  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
155  if(this == &o) {
156  return *this;
157  }
158  data_ = o.data_;
159  ref_ = o.ref_;
160  fixed_ = false;
161  return *this;
162  }
163 
164  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
166  data_[ i ] = val;
167  KeyRef ref(ref_, i);
168  transientVector_[ i ].first = ref;
169  transientVector_[ i ].second = data_[ i ];
170  }
171 
172  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
175  return data_.size();
176  }
177 
178  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
180  return data_.empty();
181  }
182 
183  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
185  data_.clear();
186  transientVector_.clear();
187  ref_ = KeyRefProd();
188  fixed_ = true;
189  }
190 
191  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
193  data_.swap(other.data_);
194  transientVector_.swap(other.transientVector_);
195  ref_.swap(other.ref_);
196  std::swap(fixed_, other.fixed_);
197  }
198 
199  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
201  if (!fixed_) {
202  fixed_ = true;
203  transientVector_.resize(size());
204  for(size_type i = 0; i != size(); ++i) {
205  transientVector_[ i ] = std::make_pair(KeyRef(ref_, i), data_[ i ]);
206  }
207  }
208  }
209 
210  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
212  std::vector<void const*>& pointers,
213  helper_vector& helpers) const
214  {
215  detail::reallyFillView(*this, id, pointers, helpers);
216 // pointers.reserve(this->size());
217 // for(typename CVal::const_iterator i=data_.begin(), e=data_.end(); i!=e; ++i)
218 // pointers.push_back(&(*i));
219 // // helpers is not yet filled in.
220 // //Exception::throwThis(errors::UnimplementedFeature, "AssociationVector<T>::fillView(...)");
221  }
222 
223  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
226  a.swap(b);
227  }
228 
229  //----------------------------------------------------------------------
230  //
231  // Free function template to support creation of Views.
232 
233  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
234  inline
235  void
237  ProductID const& id,
238  std::vector<void const*>& pointers,
239  helper_vector& helpers) {
240  obj.fillView(id, pointers, helpers);
241  }
242 
243  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
244  struct has_fillView<AssociationVector<KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper> > {
245  static bool const value = true;
246  };
247 
248 }
249 
250 #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
std::vector< value_type > transient_vector_type
#define CMS_CLASS_VERSION(_version_)
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
AssociationIdenticalKeyReference type
transient_vector_type const & transientVector() const
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
Container::value_type value_type
KeyRef key(size_type i) const
int k[5][pyjets_maxn]
JetCorrectorParametersCollection coll
Definition: classes.h:16
double b
Definition: hdecay.h:120
string const
Definition: compareJSON.py:14
KeyRef::value_type key_type
#define private
Definition: FWFileEntry.h:18
double a
Definition: hdecay.h:121
KeyRefProd const & keyProduct() const
long double T
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