CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OwnVector.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_OwnVector_h
2 #define DataFormats_Common_OwnVector_h
3 
10 
11 #if defined CMS_USE_DEBUGGING_ALLOCATOR
13 #endif
15 
16 #include <algorithm>
17 #include <functional>
18 #include <typeinfo>
19 #include <vector>
20 
21 namespace edm {
22  class ProductID;
23  template <typename T, typename P = ClonePolicy<T> >
24  class OwnVector {
25  public:
26 #if defined(CMS_USE_DEBUGGING_ALLOCATOR)
27  typedef std::vector<T*, debugging_allocator<T> > base;
28 #else
29  typedef std::vector<T*> base;
30 #endif
31 
32  public:
33  typedef typename base::size_type size_type;
34  typedef T value_type;
35  typedef T* pointer;
36  typedef T& reference;
37  typedef T const& const_reference;
38  typedef P policy_type;
39 
40  class iterator;
42  public:
43  typedef T value_type;
44  typedef T* pointer;
45  typedef T const& reference;
46  typedef ptrdiff_t difference_type;
47  typedef typename base::const_iterator::iterator_category iterator_category;
48  const_iterator(iterator const& it) : i(it.i) { }
50  const_iterator& operator++() { ++i; return *this; }
51  const_iterator operator++(int) { const_iterator ci = *this; ++i; return ci; }
52  const_iterator& operator--() { --i; return *this; }
53  const_iterator operator--(int) { const_iterator ci = *this; --i; return ci; }
54  difference_type operator-(const_iterator const& o) const { return i - o.i; }
57  bool operator<(const_iterator const& o) const { return i < o.i; }
58  bool operator==(const_iterator const& ci) const { return i == ci.i; }
59  bool operator!=(const_iterator const& ci) const { return i != ci.i; }
60  T const& operator *() const { return **i; }
61  // operator T const*() const { return & **i; }
62  T const* operator->() const { return & (operator*()); }
63  const_iterator & operator +=(difference_type d) { i += d; return *this; }
64  const_iterator & operator -=(difference_type d) { i -= d; return *this; }
65  reference operator[](difference_type d) const { return *const_iterator(i+d); } // for boost::iterator_range []
66  private:
67  const_iterator(typename base::const_iterator const& it) : i(it) { }
68  typename base::const_iterator base_iter() const { return i; }
69  typename base::const_iterator i;
70  friend class OwnVector<T,P>;
71  };
72  class iterator {
73  public:
74  typedef T value_type;
75  typedef T * pointer;
76  typedef T & reference;
77  typedef ptrdiff_t difference_type;
78  typedef typename base::iterator::iterator_category iterator_category;
79  iterator() {}
80  iterator& operator++() { ++i; return *this; }
81  iterator operator++(int) { iterator ci = *this; ++i; return ci; }
82  iterator& operator--() { --i; return *this; }
83  iterator operator--(int) { iterator ci = *this; --i; return ci; }
84  difference_type operator-(iterator const& o) const { return i - o.i; }
85  iterator operator+(difference_type n) const { return iterator(i + n); }
86  iterator operator-(difference_type n) const { return iterator(i - n); }
87  bool operator<(iterator const& o) const { return i < o.i; }
88  bool operator==(iterator const& ci) const { return i == ci.i; }
89  bool operator!=(iterator const& ci) const { return i != ci.i; }
90  T & operator *() const { return **i; }
91  // operator T *() const { return & **i; }
92  //T *& get() { return *i; }
93  T * operator->() const { return & (operator*()); }
94  iterator & operator +=(difference_type d) { i += d; return *this; }
95  iterator & operator -=(difference_type d) { i -= d; return *this; }
96  reference operator[](difference_type d) const { return *iterator(i+d); } // for boost::iterator_range []
97  private:
98  iterator(typename base::iterator const& it) : i(it) { }
99  typename base::iterator i;
100  friend class const_iterator;
101  friend class OwnVector<T, P>;
102  };
103 
104 
105  OwnVector();
107  OwnVector(OwnVector const&);
108 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
110 #endif
111 
113 
114  iterator begin();
115  iterator end();
116  const_iterator begin() const;
117  const_iterator end() const;
118  size_type size() const;
119  bool empty() const;
121  const_reference operator[](size_type) const;
122 
123  OwnVector<T, P>& operator=(OwnVector<T, P> const&);
124 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
126 #endif
127 
128  void shrink_to_fit() {
129  data_.shrink_to_fit();
130  }
131 
132 
133  void reserve(size_t);
134  template <typename D> void push_back(D*& d);
135  template <typename D> void push_back(D* const& d);
136  template <typename D> void push_back(std::auto_ptr<D> d);
137  void push_back(T const& valueToCopy);
138 
139  template <typename D> void set(size_t i, D*& d);
140  template <typename D> void set(size_t i, D* const & d);
141  template <typename D> void set(size_t i, std::auto_ptr<D> d);
142  void set(size_t i, T const& valueToCopy);
143 
144  template <typename D> void insert(const_iterator i, D*& d);
145  template <typename D> void insert(const_iterator i, D* const & d);
146  template <typename D> void insert(const_iterator i, std::auto_ptr<D> d);
147  void insert(const_iterator i, T const& valueToCopy);
148 
149  bool is_back_safe() const;
150  void pop_back();
151  reference back();
152  const_reference back() const;
153  reference front();
154  const_reference front() const;
155  base const& data() const;
156  void clear();
157  iterator erase(iterator pos);
158  iterator erase(iterator first, iterator last);
159  void reverse() { std::reverse(data_.begin(),data_.end());}
160  template<typename S>
161  void sort(S s);
162  void sort();
163 
164  void swap(OwnVector<T, P>& other) noexcept;
165 
166  void fillView(ProductID const& id,
167  std::vector<void const*>& pointers,
168  FillViewHelperVector& helpers) const;
169 
170  void setPtr(std::type_info const& toType,
171  unsigned long index,
172  void const*& ptr) const;
173 
174  void fillPtrVector(std::type_info const& toType,
175  std::vector<unsigned long> const& indices,
176  std::vector<void const*>& ptrs) const;
177 
178 
179  //Used by ROOT storage
181 
182  private:
183  void destroy() noexcept;
184  template<typename O>
185  struct Ordering {
186  Ordering(O const& c) : comp(c) { }
187  bool operator()(T const* t1, T const* t2) const {
188  return comp(*t1, *t2);
189  }
190  private:
191  O comp;
192  };
193  template<typename O>
194  static Ordering<O> ordering(O const& comp) {
195  return Ordering<O>(comp);
196  }
198  };
199 
200  template<typename T, typename P>
201  inline OwnVector<T, P>::OwnVector() : data_() {
202  }
203 
204  template<typename T, typename P>
205  inline OwnVector<T, P>::OwnVector(size_type n) : data_(n) {
206  }
207 
208  template<typename T, typename P>
209  inline OwnVector<T, P>::OwnVector(OwnVector<T, P> const& o) : data_(o.size()) {
210  size_type current = 0;
211  for (const_iterator i = o.begin(), e = o.end(); i != e; ++i,++current)
212  data_[current] = policy_type::clone(*i);
213  }
214 
215 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
216  template<typename T, typename P>
217  inline OwnVector<T, P>::OwnVector(OwnVector<T, P>&& o) noexcept{
218  data_.swap(o.data_);
219  }
220 #endif
221 
222  template<typename T, typename P>
223  inline OwnVector<T, P>::~OwnVector() noexcept {
224  destroy();
225  }
226 
227  template<typename T, typename P>
229  OwnVector<T,P> temp(o);
230  swap(temp);
231  return *this;
232  }
233 
234 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
235  template<typename T, typename P>
237  data_.swap(o.data_);
238  return *this;
239  }
240 #endif
241 
242 
243  template<typename T, typename P>
245  return iterator(data_.begin());
246  }
247 
248  template<typename T, typename P>
250  return iterator(data_.end());
251  }
252 
253  template<typename T, typename P>
255  return const_iterator(data_.begin());
256  }
257 
258  template<typename T, typename P>
260  return const_iterator(data_.end());
261  }
262 
263  template<typename T, typename P>
265  return data_.size();
266  }
267 
268  template<typename T, typename P>
269  inline bool OwnVector<T, P>::empty() const {
270  return data_.empty();
271  }
272 
273  template<typename T, typename P>
275  return *data_[n];
276  }
277 
278  template<typename T, typename P>
280  return *data_[n];
281  }
282 
283  template<typename T, typename P>
284  inline void OwnVector<T, P>::reserve(size_t n) {
285  data_.reserve(n);
286  }
287 
288  template<typename T, typename P>
289  template<typename D>
290  inline void OwnVector<T, P>::push_back(D*& d) {
291  // C++ does not yet support rvalue references, so d should only be
292  // able to bind to an lvalue.
293  // This should be called only for lvalues.
294  data_.push_back(d);
295  d = 0;
296  }
297 
298  template<typename T, typename P>
299  template<typename D>
300  inline void OwnVector<T, P>::push_back(D* const& d) {
301 
302  // C++ allows d to be bound to an lvalue or rvalue. But the other
303  // signature should be a better match for an lvalue (because it
304  // does not require an lvalue->rvalue conversion). Thus this
305  // signature should only be chosen for rvalues.
306  data_.push_back(d);
307  }
308 
309 
310  template<typename T, typename P>
311  template<typename D>
312  inline void OwnVector<T, P>::push_back(std::auto_ptr<D> d) {
313  data_.push_back(d.release());
314  }
315 
316 
317  template<typename T, typename P>
318  inline void OwnVector<T, P>::push_back(T const& d) {
319  data_.push_back(policy_type::clone(d));
320  }
321 
322  template<typename T, typename P>
323  template<typename D>
324  inline void OwnVector<T, P>::set(size_t i, D*& d) {
325  // see push_back for documentation
326  if (d == data_[i]) return;
327  delete data_[i];
328  data_[i] = d;
329  d = 0;
330  }
331 
332  template<typename T, typename P>
333  template<typename D>
334  inline void OwnVector<T, P>::set(size_t i, D* const& d) {
335  // see push_back for documentation
336  if (d == data_[i]) return;
337  delete data_[i];
338  data_[i] = d;
339  }
340 
341 
342  template<typename T, typename P>
343  template<typename D>
344  inline void OwnVector<T, P>::set(size_t i, std::auto_ptr<D> d) {
345  if (d.get() == data_[i]) return;
346  delete data_[i];
347  data_[i] = d.release();
348  }
349 
350 
351  template<typename T, typename P>
352  inline void OwnVector<T, P>::set(size_t i, T const& d) {
353  if (&d == data_[i]) return;
354  delete data_[i];
355  data_[i] = policy_type::clone(d);
356  }
357 
358 
359  template<typename T, typename P>
360  template<typename D>
362  data_.insert(it.base_iter(), d);
363  d = 0;
364  }
365 
366  template<typename T, typename P>
367  template<typename D>
368  inline void OwnVector<T, P>::insert(const_iterator it, D* const& d) {
369  data_.insert(it.base_iter(), d);
370  }
371 
372 
373  template<typename T, typename P>
374  template<typename D>
375  inline void OwnVector<T, P>::insert(const_iterator it, std::auto_ptr<D> d) {
376  data_.insert(it.base_iter(), d.release());
377  }
378 
379 
380  template<typename T, typename P>
381  inline void OwnVector<T, P>::insert(const_iterator it, T const& d) {
382  data_.insert(it.base_iter(), policy_type::clone(d));
383  }
384 
385 
386 
387  template<typename T, typename P>
389  // We have to delete the pointed-to thing, before we squeeze it
390  // out of the vector...
391  delete data_.back();
392  data_.pop_back();
393  }
394 
395  template <typename T, typename P>
396  inline bool OwnVector<T, P>::is_back_safe() const {
397  return data_.back() != 0;
398  }
399 
400  template<typename T, typename P>
402  T* result = data_.back();
403  if (result == 0) {
405  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
406  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
407  "pointer at the end of the collection is not null before calling back()\n"
408  "if you wish to avoid this exception.\n"
409  "Consider using OwnVector::is_back_safe()\n");
410  }
411  return *data_.back();
412  }
413 
414  template<typename T, typename P>
416  T* result = data_.back();
417  if (result == 0) {
419  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
420  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
421  "pointer at the end of the collection is not null before calling back()\n"
422  "if you wish to avoid this exception.\n"
423  "Consider using OwnVector::is_back_safe()\n");
424  }
425  return *data_.back();
426  }
427 
428  template<typename T, typename P>
430  return *data_.front();
431  }
432 
433  template<typename T, typename P>
435  return *data_.front();
436  }
437 
438  template<typename T, typename P>
439  inline void OwnVector<T, P>::destroy() noexcept {
440  typename base::const_iterator b = data_.begin(), e = data_.end();
441  for(typename base::const_iterator i = b; i != e; ++ i)
442  delete * i;
443  }
444 
445  template<typename T, typename P>
446  inline typename OwnVector<T, P>::base const& OwnVector<T, P>::data() const {
447  return data_;
448  }
449 
450  template<typename T, typename P>
451  inline void OwnVector<T, P>::clear() {
452  destroy();
453  data_.clear();
454  }
455 
456  template<typename T, typename P>
458  delete * pos.i;
459  return iterator(data_.erase(pos.i));
460  }
461 
462  template<typename T, typename P>
464  typename base::iterator b = first.i, e = last.i;
465  for(typename base::iterator i = b; i != e; ++ i)
466  delete * i;
467  return iterator(data_.erase(b, e));
468  }
469 
470  template<typename T, typename P> template<typename S>
472  std::sort(data_.begin(), data_.end(), ordering(comp));
473  }
474 
475  template<typename T, typename P>
477  std::sort(data_.begin(), data_.end(), ordering(std::less<value_type>()));
478  }
479 
480  template<typename T, typename P>
481  inline void OwnVector<T, P>::swap(OwnVector<T, P>& other) noexcept {
482  data_.swap(other.data_);
483  }
484 
485 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
486  template<typename T, typename P>
487  void OwnVector<T, P>::fillView(ProductID const& id,
488  std::vector<void const*>& pointers,
489  FillViewHelperVector& helpers) const {
490  size_type numElements = this->size();
491  pointers.reserve(numElements);
492  helpers.reserve(numElements);
493  size_type key = 0;
494  for(typename base::const_iterator i=data_.begin(), e=data_.end(); i!=e; ++i, ++key) {
495 
496  if (*i == 0) {
498  "In OwnVector::fillView() we have intercepted an attempt to put a null pointer\n"
499  "into a View and that is not allowed. It is probably an error that the null\n"
500  "pointer was in the OwnVector in the first place.\n");
501  }
502  else {
503  pointers.push_back(*i);
504  helpers.emplace_back(id,key);
505  }
506  }
507  }
508 #endif
509 
510  template<typename T, typename P>
511  inline void swap(OwnVector<T, P>& a, OwnVector<T, P>& b) noexcept {
512  a.swap(b);
513  }
514 
515  //----------------------------------------------------------------------
516  //
517  // Free function template to support creation of Views.
518 
519  template <typename T, typename P>
520  inline
521  void
523  ProductID const& id,
524  std::vector<void const*>& pointers,
525  FillViewHelperVector& helpers) {
526  obj.fillView(id, pointers, helpers);
527  }
528 
529 
530  template <typename T, typename P>
531  struct has_fillView<edm::OwnVector<T, P> > {
532  static bool const value = true;
533  };
534 
535 
536  // Free function templates to support the use of edm::Ptr.
537 
538  template <typename T, typename P>
539  inline
540  void
541  OwnVector<T,P>::setPtr(std::type_info const& toType,
542  unsigned long index,
543  void const*& ptr) const {
544  detail::reallySetPtr<OwnVector<T,P> >(*this, toType, index, ptr);
545  }
546 
547  template <typename T, typename P>
548  inline
549  void
551  std::type_info const& toType,
552  unsigned long index,
553  void const*& ptr) {
554  obj.setPtr(toType, index, ptr);
555  }
556 
557  template <typename T, typename P>
558  inline
559  void
560  OwnVector<T,P>::fillPtrVector(std::type_info const& toType,
561  std::vector<unsigned long> const& indices,
562  std::vector<void const*>& ptrs) const {
563  detail::reallyfillPtrVector(*this, toType, indices, ptrs);
564  }
565 
566 
567  template <typename T, typename P>
568  inline
569  void
571  std::type_info const& toType,
572  std::vector<unsigned long> const& indices,
573  std::vector<void const*>& ptrs) {
574  obj.fillPtrVector(toType, indices, ptrs);
575  }
576 
577 
578  template <typename T, typename P>
579  struct has_setPtr<edm::OwnVector<T,P> > {
580  static bool const value = true;
581  };
582 
583 
584 }
585 
586 
587 #endif
T & operator*() const
Definition: OwnVector.h:90
difference_type operator-(iterator const &o) const
Definition: OwnVector.h:84
base::iterator i
Definition: OwnVector.h:99
int i
Definition: DBlmapReader.cc:9
static bool const value
Definition: traits.h:173
T const * operator->() const
Definition: OwnVector.h:62
reference back()
Definition: OwnVector.h:401
std::vector< T * > base
Definition: OwnVector.h:29
iterator operator-(difference_type n) const
Definition: OwnVector.h:86
base::const_iterator::iterator_category iterator_category
Definition: OwnVector.h:47
const_iterator & operator++()
Definition: OwnVector.h:50
reference operator[](size_type)
Definition: OwnVector.h:274
bool operator()(T const *t1, T const *t2) const
Definition: OwnVector.h:187
void fillPtrVector(std::vector< T, A > const &obj, std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr)
Definition: fillPtrVector.h:85
iterator & operator+=(difference_type d)
Definition: OwnVector.h:94
size_type size() const
Definition: OwnVector.h:264
T const & operator*() const
Definition: OwnVector.h:60
#define noexcept
iterator & operator-=(difference_type d)
Definition: OwnVector.h:95
#define P
void fillView(ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers) const
iterator operator--(int)
Definition: OwnVector.h:83
void destroy()
Definition: OwnVector.h:439
bool operator!=(iterator const &ci) const
Definition: OwnVector.h:89
bool operator==(const_iterator const &ci) const
Definition: OwnVector.h:58
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
OwnVector< T, P > & operator=(OwnVector< T, P > const &)
Definition: OwnVector.h:228
uint16_t size_type
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
void setPtr(std::vector< T, A > const &obj, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
Definition: setPtr.h:72
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
void reallyfillPtrVector(COLLECTION const &coll, std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr)
Definition: fillPtrVector.h:38
const_iterator operator--(int)
Definition: OwnVector.h:53
iterator begin()
Definition: OwnVector.h:244
list ordering
Definition: config.py:7
bool operator<(const_iterator const &o) const
Definition: OwnVector.h:57
reference operator[](difference_type d) const
Definition: OwnVector.h:65
bool operator<(iterator const &o) const
Definition: OwnVector.h:87
tuple d
Definition: ztail.py:151
void reverse()
Definition: OwnVector.h:159
iterator & operator--()
Definition: OwnVector.h:82
bool operator==(iterator const &ci) const
Definition: OwnVector.h:88
void push_back(D *&d)
Definition: OwnVector.h:290
reference operator[](difference_type d) const
Definition: OwnVector.h:96
auto const T2 &decltype(t1.eta()) t2
Definition: deltaR.h:16
const_iterator(typename base::const_iterator const &it)
Definition: OwnVector.h:67
bool empty() const
Definition: OwnVector.h:269
base::const_iterator i
Definition: OwnVector.h:69
tuple result
Definition: query.py:137
void clear()
Definition: OwnVector.h:451
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers)
iterator erase(iterator pos)
Definition: OwnVector.h:457
void setPtr(std::type_info const &toType, unsigned long index, void const *&ptr) const
Definition: OwnVector.h:541
void shrink_to_fit()
Definition: OwnVector.h:128
void set(size_t i, D *&d)
Definition: OwnVector.h:324
ptrdiff_t difference_type
Definition: OwnVector.h:77
void fillPtrVector(std::type_info const &toType, std::vector< unsigned long > const &indices, std::vector< void const * > &ptrs) const
Definition: OwnVector.h:560
iterator & operator++()
Definition: OwnVector.h:80
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
const_iterator operator++(int)
Definition: OwnVector.h:51
bool is_back_safe() const
Definition: OwnVector.h:396
iterator end()
Definition: OwnVector.h:249
const_iterator(iterator const &it)
Definition: OwnVector.h:48
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:150
T const & const_reference
Definition: OwnVector.h:37
base::size_type size_type
Definition: OwnVector.h:33
double b
Definition: hdecay.h:120
string const
Definition: compareJSON.py:14
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
double S(const TLorentzVector &, const TLorentzVector &)
Definition: Particle.cc:99
base const & data() const
Definition: OwnVector.h:446
iterator operator++(int)
Definition: OwnVector.h:81
double a
Definition: hdecay.h:121
iterator(typename base::iterator const &it)
Definition: OwnVector.h:98
difference_type operator-(const_iterator const &o) const
Definition: OwnVector.h:54
const_iterator & operator--()
Definition: OwnVector.h:52
static Ordering< O > ordering(O const &comp)
Definition: OwnVector.h:194
iterator operator+(difference_type n) const
Definition: OwnVector.h:85
void pop_back()
Definition: OwnVector.h:388
const_iterator & operator-=(difference_type d)
Definition: OwnVector.h:64
T first(std::pair< T, U > const &p)
T * operator->() const
Definition: OwnVector.h:93
base::const_iterator base_iter() const
Definition: OwnVector.h:68
long double T
void swap(OwnVector< T, P > &other)
Definition: OwnVector.h:481
void insert(const_iterator i, D *&d)
Definition: OwnVector.h:361
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
const_iterator operator+(difference_type n) const
Definition: OwnVector.h:55
reference front()
Definition: OwnVector.h:429
tuple size
Write out results.
bool operator!=(const_iterator const &ci) const
Definition: OwnVector.h:59
void reserve(size_t)
Definition: OwnVector.h:284
static bool const value
Definition: traits.h:124
def template
Definition: svgfig.py:520
const_iterator & operator+=(difference_type d)
Definition: OwnVector.h:63
base::iterator::iterator_category iterator_category
Definition: OwnVector.h:78
const_iterator operator-(difference_type n) const
Definition: OwnVector.h:56