CMS 3D CMS Logo

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; }
55  const_iterator operator+(difference_type n) const { return const_iterator(i + n); }
56  const_iterator operator-(difference_type n) const { return const_iterator(i - n); }
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();
106  OwnVector(size_type);
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;
120  reference operator[](size_type);
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::unique_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::unique_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::unique_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 
165 
166  void fillView(ProductID const& id,
167  std::vector<void const*>& pointers,
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  }
197  base data_;
198  };
199 
200  template<typename T, typename P>
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>
218  data_.swap(o.data_);
219  }
220 #endif
221 
222  template<typename T, typename P>
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  template<typename T, typename P>
310  template<typename D>
311  inline void OwnVector<T, P>::push_back(std::unique_ptr<D> d) {
312  data_.push_back(d.release());
313  }
314 
315  template<typename T, typename P>
316  inline void OwnVector<T, P>::push_back(T const& d) {
317  data_.push_back(policy_type::clone(d));
318  }
319 
320  template<typename T, typename P>
321  template<typename D>
322  inline void OwnVector<T, P>::set(size_t i, D*& d) {
323  // see push_back for documentation
324  if (d == data_[i]) return;
325  delete data_[i];
326  data_[i] = d;
327  d = 0;
328  }
329 
330  template<typename T, typename P>
331  template<typename D>
332  inline void OwnVector<T, P>::set(size_t i, D* const& d) {
333  // see push_back for documentation
334  if (d == data_[i]) return;
335  delete data_[i];
336  data_[i] = d;
337  }
338 
339  template<typename T, typename P>
340  template<typename D>
341  inline void OwnVector<T, P>::set(size_t i, std::unique_ptr<D> d) {
342  if (d.get() == data_[i]) return;
343  delete data_[i];
344  data_[i] = d.release();
345  }
346 
347  template<typename T, typename P>
348  inline void OwnVector<T, P>::set(size_t i, T const& d) {
349  if (&d == data_[i]) return;
350  delete data_[i];
351  data_[i] = policy_type::clone(d);
352  }
353 
354 
355  template<typename T, typename P>
356  template<typename D>
358  data_.insert(it.base_iter(), d);
359  d = 0;
360  }
361 
362  template<typename T, typename P>
363  template<typename D>
364  inline void OwnVector<T, P>::insert(const_iterator it, D* const& d) {
365  data_.insert(it.base_iter(), d);
366  }
367 
368  template<typename T, typename P>
369  template<typename D>
370  inline void OwnVector<T, P>::insert(const_iterator it, std::unique_ptr<D> d) {
371  data_.insert(it.base_iter(), d.release());
372  }
373 
374  template<typename T, typename P>
375  inline void OwnVector<T, P>::insert(const_iterator it, T const& d) {
376  data_.insert(it.base_iter(), policy_type::clone(d));
377  }
378 
379 
380 
381  template<typename T, typename P>
383  // We have to delete the pointed-to thing, before we squeeze it
384  // out of the vector...
385  delete data_.back();
386  data_.pop_back();
387  }
388 
389  template <typename T, typename P>
390  inline bool OwnVector<T, P>::is_back_safe() const {
391  return data_.back() != 0;
392  }
393 
394  template<typename T, typename P>
396  T* result = data_.back();
397  if (result == 0) {
399  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
400  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
401  "pointer at the end of the collection is not null before calling back()\n"
402  "if you wish to avoid this exception.\n"
403  "Consider using OwnVector::is_back_safe()\n");
404  }
405  return *data_.back();
406  }
407 
408  template<typename T, typename P>
410  T* result = data_.back();
411  if (result == 0) {
413  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
414  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
415  "pointer at the end of the collection is not null before calling back()\n"
416  "if you wish to avoid this exception.\n"
417  "Consider using OwnVector::is_back_safe()\n");
418  }
419  return *data_.back();
420  }
421 
422  template<typename T, typename P>
424  return *data_.front();
425  }
426 
427  template<typename T, typename P>
429  return *data_.front();
430  }
431 
432  template<typename T, typename P>
434  typename base::const_iterator b = data_.begin(), e = data_.end();
435  for(typename base::const_iterator i = b; i != e; ++ i)
436  delete * i;
437  }
438 
439  template<typename T, typename P>
440  inline typename OwnVector<T, P>::base const& OwnVector<T, P>::data() const {
441  return data_;
442  }
443 
444  template<typename T, typename P>
445  inline void OwnVector<T, P>::clear() {
446  destroy();
447  data_.clear();
448  }
449 
450  template<typename T, typename P>
452  delete * pos.i;
453  return iterator(data_.erase(pos.i));
454  }
455 
456  template<typename T, typename P>
458  typename base::iterator b = first.i, e = last.i;
459  for(typename base::iterator i = b; i != e; ++ i)
460  delete * i;
461  return iterator(data_.erase(b, e));
462  }
463 
464  template<typename T, typename P> template<typename S>
466  std::sort(data_.begin(), data_.end(), ordering(comp));
467  }
468 
469  template<typename T, typename P>
471  std::sort(data_.begin(), data_.end(), ordering(std::less<value_type>()));
472  }
473 
474  template<typename T, typename P>
476  data_.swap(other.data_);
477  }
478 
479 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
480  template<typename T, typename P>
481  void OwnVector<T, P>::fillView(ProductID const& id,
482  std::vector<void const*>& pointers,
483  FillViewHelperVector& helpers) const {
484  size_type numElements = this->size();
485  pointers.reserve(numElements);
486  helpers.reserve(numElements);
487  size_type key = 0;
488  for(typename base::const_iterator i=data_.begin(), e=data_.end(); i!=e; ++i, ++key) {
489 
490  if (*i == 0) {
492  "In OwnVector::fillView() we have intercepted an attempt to put a null pointer\n"
493  "into a View and that is not allowed. It is probably an error that the null\n"
494  "pointer was in the OwnVector in the first place.\n");
495  }
496  else {
497  pointers.push_back(*i);
498  helpers.emplace_back(id,key);
499  }
500  }
501  }
502 #endif
503 
504  template<typename T, typename P>
506  a.swap(b);
507  }
508 
509  //----------------------------------------------------------------------
510  //
511  // Free function template to support creation of Views.
512 
513  template <typename T, typename P>
514  inline
515  void
517  ProductID const& id,
518  std::vector<void const*>& pointers,
519  FillViewHelperVector& helpers) {
520  obj.fillView(id, pointers, helpers);
521  }
522 
523 
524  template <typename T, typename P>
525  struct has_fillView<edm::OwnVector<T, P> > {
526  static bool const value = true;
527  };
528 
529 
530  // Free function templates to support the use of edm::Ptr.
531 
532  template <typename T, typename P>
533  inline
534  void
535  OwnVector<T,P>::setPtr(std::type_info const& toType,
536  unsigned long index,
537  void const*& ptr) const {
538  detail::reallySetPtr<OwnVector<T,P> >(*this, toType, index, ptr);
539  }
540 
541  template <typename T, typename P>
542  inline
543  void
545  std::type_info const& toType,
546  unsigned long index,
547  void const*& ptr) {
548  obj.setPtr(toType, index, ptr);
549  }
550 
551  template <typename T, typename P>
552  inline
553  void
554  OwnVector<T,P>::fillPtrVector(std::type_info const& toType,
555  std::vector<unsigned long> const& indices,
556  std::vector<void const*>& ptrs) const {
557  detail::reallyfillPtrVector(*this, toType, indices, ptrs);
558  }
559 
560 
561  template <typename T, typename P>
562  inline
563  void
565  std::type_info const& toType,
566  std::vector<unsigned long> const& indices,
567  std::vector<void const*>& ptrs) {
568  obj.fillPtrVector(toType, indices, ptrs);
569  }
570 
571 
572  template <typename T, typename P>
573  struct has_setPtr<edm::OwnVector<T,P> > {
574  static bool const value = true;
575  };
576 
577 
578 }
579 
580 
581 #endif
difference_type operator-(iterator const &o) const
Definition: OwnVector.h:84
base::iterator i
Definition: OwnVector.h:99
T const * operator->() const
Definition: OwnVector.h:62
reference back()
Definition: OwnVector.h:395
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
size_type size() const
Definition: OwnVector.h:264
T const & operator*() const
Definition: OwnVector.h:60
#define noexcept
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:433
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
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
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
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
void clear()
Definition: OwnVector.h:445
iterator erase(iterator pos)
Definition: OwnVector.h:451
void setPtr(std::type_info const &toType, unsigned long index, void const *&ptr) const
Definition: OwnVector.h:535
void shrink_to_fit()
Definition: OwnVector.h:128
void set(size_t i, D *&d)
Definition: OwnVector.h:322
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:520
ptrdiff_t difference_type
Definition: OwnVector.h:77
Definition: value.py:1
void fillPtrVector(std::type_info const &toType, std::vector< unsigned long > const &indices, std::vector< void const * > &ptrs) const
Definition: OwnVector.h:554
iterator & operator++()
Definition: OwnVector.h:80
const_iterator operator++(int)
Definition: OwnVector.h:51
bool is_back_safe() const
Definition: OwnVector.h:390
iterator end()
Definition: OwnVector.h:249
const_iterator(iterator const &it)
Definition: OwnVector.h:48
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:151
T const & const_reference
Definition: OwnVector.h:37
base::size_type size_type
Definition: OwnVector.h:33
double b
Definition: hdecay.h:120
std::pair< OmniClusterRef, TrackingParticleRef > P
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
double S(const TLorentzVector &, const TLorentzVector &)
Definition: Particle.cc:99
HLT enums.
base const & data() const
Definition: OwnVector.h:440
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:382
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:475
void insert(const_iterator i, D *&d)
Definition: OwnVector.h:357
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:423
bool operator!=(const_iterator const &ci) const
Definition: OwnVector.h:59
void reserve(size_t)
Definition: OwnVector.h:284
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