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