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 
16 
25 
27 
28 #include "boost/static_assert.hpp"
29 
30 namespace edm {
31  namespace helper {
32 
34  template<typename T>
35  static T const& get(T const& t, ProductID) { return t; }
36  };
37 
38  template<typename T>
41  };
42 
43  template<typename REFPROD>
44  struct RefFromRefProdTrait { };
45 
46  template<typename C>
49  };
50 
51  template<typename T>
54  };
55  }
56 
57  template<typename KeyRefProd, typename CVal,
58  typename KeyRef = typename helper::RefFromRefProdTrait<KeyRefProd>::ref_type,
59  typename SizeType = unsigned int,//the type used here can not change when go from 32bit to 64bit or across platforms
60  typename KeyReferenceHelper = typename helper::AssociationKeyReferenceTrait<KeyRef>::type>
64 
65  public:
66  typedef KeyRefProd refprod_type;
67  typedef typename KeyRefProd::product_type CKey;
68  typedef SizeType size_type;
69  typedef typename KeyRef::value_type key_type;
70  typedef typename std::pair<KeyRef, typename CVal::value_type> value_type;
71  typedef std::vector<value_type> transient_vector_type;
72  typedef value_type const& const_reference;
74  AssociationVector(KeyRefProd const& ref, CKey const* = 0);
77 
78  size_type size() const;
79  bool empty() const;
81  typename CVal::const_reference operator[](KeyRef const& k) const;
82  typename CVal::reference operator[](KeyRef const& k);
83 
84  self& operator=(self const&);
85 
86  void clear();
87  void swap(self& other);
88  KeyRefProd const& keyProduct() const { return ref_; }
89  KeyRef key(size_type i) const { return KeyRef(ref_, i); }
90  typename CVal::value_type const value(size_type i) const { return data_[ i ]; }
91  void setValue(size_type i, typename CVal::value_type const& val);
92  void fillView(ProductID const& id,
93  std::vector<void const*>& pointers,
94  helper_vector& helpers) const;
95 
96  typedef typename transient_vector_type::const_iterator const_iterator;
97 
98  const_iterator begin() const { return transientVector().begin(); }
99  const_iterator end() const { return transientVector().end(); }
100 
101  //Used by ROOT storage
103 
104  private:
105  CVal data_;
106  KeyRefProd ref_;
108  mutable bool fixed_;
110  void fixup() const;
111  };
112 
113  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
115  data_(), ref_(), transientVector_(), fixed_(false) { }
116 
117  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
119  CKey const* coll) :
120  data_(coll == 0 ? ref->size() : coll->size()), ref_(ref),
121  transientVector_(coll == 0 ? ref->size() : coll->size()), fixed_(true) { }
122 
123  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
126  data_(o.data_), ref_(o.ref_), transientVector_(o.transientVector_), fixed_(o.fixed_) { }
127 
128  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
130 
131  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
134  return transientVector()[ n ];
135  }
136 
137  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
138  inline typename CVal::const_reference
140  KeyRef keyRef = KeyReferenceHelper::get(k, ref_.id());
141  checkForWrongProduct(keyRef.id(), ref_.id());
142  return data_[ keyRef.key() ];
143  }
144 
145 
146  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
147  inline typename CVal::reference
149  KeyRef keyRef = KeyReferenceHelper::get(k, ref_.id());
150  fixed_ = false;
151  checkForWrongProduct(keyRef.id(), ref_.id());
152  return data_[ keyRef.key() ];
153  }
154 
155  template<typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
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
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
Container::value_type value_type
KeyRef key(size_type i) const
int k[5][pyjets_maxn]
JetCorrectorParametersCollection coll
Definition: classes.h:14
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