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>
21 
22 namespace edm {
23  class ProductID;
24  template <typename T, typename P = ClonePolicy<T> >
25  class OwnVector {
26  public:
27 #if defined(CMS_USE_DEBUGGING_ALLOCATOR)
28  typedef std::vector<T*, debugging_allocator<T> > base;
29 #else
30  typedef std::vector<T*> base;
31 #endif
32 
33  public:
34  typedef typename base::size_type size_type;
35  typedef T value_type;
36  typedef T* pointer;
37  typedef T& reference;
38  typedef T const& const_reference;
39  typedef P policy_type;
40 
41  class iterator;
43  public:
44  typedef T value_type;
45  typedef T* pointer;
46  typedef T const& reference;
47  typedef ptrdiff_t difference_type;
48  typedef typename base::const_iterator::iterator_category iterator_category;
49  const_iterator(typename base::const_iterator const& it) : i(it) { }
50  const_iterator(iterator const& it) : i(it.i) { }
52  const_iterator& operator++() { ++i; return *this; }
53  const_iterator operator++(int) { const_iterator ci = *this; ++i; return ci; }
54  const_iterator& operator--() { --i; return *this; }
55  const_iterator operator--(int) { const_iterator ci = *this; --i; return ci; }
56  difference_type operator-(const_iterator const& o) const { return i - o.i; }
59  bool operator<(const_iterator const& o) const { return i < o.i; }
60  bool operator==(const_iterator const& ci) const { return i == ci.i; }
61  bool operator!=(const_iterator const& ci) const { return i != ci.i; }
62  T const& operator *() const { return **i; }
63  // operator T const*() const { return & **i; }
64  T const* operator->() const { return & (operator*()); }
65  const_iterator & operator +=(difference_type d) { i += d; return *this; }
66  const_iterator & operator -=(difference_type d) { i -= d; return *this; }
67  reference operator[](difference_type d) const { return *const_iterator(i+d); } // for boost::iterator_range []
68  private:
69  typename base::const_iterator i;
70  };
71  class iterator {
72  public:
73  typedef T value_type;
74  typedef T * pointer;
75  typedef T & reference;
76  typedef ptrdiff_t difference_type;
77  typedef typename base::iterator::iterator_category iterator_category;
78  iterator(typename base::iterator const& it) : i(it) { }
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  typename base::iterator i;
99  friend class const_iterator;
100  friend class OwnVector<T, P>;
101  };
102 
103 
104  OwnVector();
106  OwnVector(OwnVector const&);
107 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
109 #endif
110 
112 
113  iterator begin();
114  iterator end();
115  const_iterator begin() const;
116  const_iterator end() const;
117  size_type size() const;
118  bool empty() const;
120  const_reference operator[](size_type) const;
121 
122  OwnVector<T, P>& operator=(OwnVector<T, P> const&);
123 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
125 #endif
126 
127  void shrink_to_fit() {
128 #ifndef CMS_NOCXX11
129  data_.shrink_to_fit();
130 #endif
131  }
132 
133 
134  void reserve(size_t);
135  template <typename D> void push_back(D*& d);
136  template <typename D> void push_back(D* const& d);
137  template <typename D> void push_back(std::auto_ptr<D> d);
138  void push_back(T const& valueToCopy);
139  bool is_back_safe() const;
140  void pop_back();
141  reference back();
142  const_reference back() const;
143  reference front();
144  const_reference front() const;
145  base const& data() const;
146  void clear();
147  iterator erase(iterator pos);
148  iterator erase(iterator first, iterator last);
149  void reverse() { std::reverse(data_.begin(),data_.end());}
150  template<typename S>
151  void sort(S s);
152  void sort();
153 
154  void swap(OwnVector<T, P>& other) noexcept;
155 
156  void fillView(ProductID const& id,
157  std::vector<void const*>& pointers,
158  helper_vector& helpers) const;
159 
160  void setPtr(std::type_info const& toType,
161  unsigned long index,
162  void const*& ptr) const;
163 
164  void fillPtrVector(std::type_info const& toType,
165  std::vector<unsigned long> const& indices,
166  std::vector<void const*>& ptrs) const;
167 
168 
169  //Used by ROOT storage
171 
172  private:
173  void destroy() noexcept;
174  template<typename O>
175  struct Ordering {
176  Ordering(O const& c) : comp(c) { }
177  bool operator()(T const* t1, T const* t2) const {
178  return comp(*t1, *t2);
179  }
180  private:
181  O comp;
182  };
183  template<typename O>
184  static Ordering<O> ordering(O const& comp) {
185  return Ordering<O>(comp);
186  }
188  };
189 
190  template<typename T, typename P>
191  inline OwnVector<T, P>::OwnVector() : data_() {
192  }
193 
194  template<typename T, typename P>
195  inline OwnVector<T, P>::OwnVector(size_type n) : data_(n) {
196  }
197 
198  template<typename T, typename P>
199  inline OwnVector<T, P>::OwnVector(OwnVector<T, P> const& o) : data_(o.size()) {
200  size_type current = 0;
201  for (const_iterator i = o.begin(), e = o.end(); i != e; ++i,++current)
202  data_[current] = policy_type::clone(*i);
203  }
204 
205 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
206  template<typename T, typename P>
207  inline OwnVector<T, P>::OwnVector(OwnVector<T, P>&& o) noexcept{
208  data_.swap(o.data_);
209  }
210 #endif
211 
212  template<typename T, typename P>
213  inline OwnVector<T, P>::~OwnVector() noexcept {
214  destroy();
215  }
216 
217  template<typename T, typename P>
219  OwnVector<T,P> temp(o);
220  swap(temp);
221  return *this;
222  }
223 
224 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
225  template<typename T, typename P>
227  data_.swap(o.data_);
228  return *this;
229  }
230 #endif
231 
232 
233  template<typename T, typename P>
235  return iterator(data_.begin());
236  }
237 
238  template<typename T, typename P>
240  return iterator(data_.end());
241  }
242 
243  template<typename T, typename P>
245  return const_iterator(data_.begin());
246  }
247 
248  template<typename T, typename P>
250  return const_iterator(data_.end());
251  }
252 
253  template<typename T, typename P>
255  return data_.size();
256  }
257 
258  template<typename T, typename P>
259  inline bool OwnVector<T, P>::empty() const {
260  return data_.empty();
261  }
262 
263  template<typename T, typename P>
265  return *data_[n];
266  }
267 
268  template<typename T, typename P>
270  return *data_[n];
271  }
272 
273  template<typename T, typename P>
274  inline void OwnVector<T, P>::reserve(size_t n) {
275  data_.reserve(n);
276  }
277 
278  template<typename T, typename P>
279  template<typename D>
280  inline void OwnVector<T, P>::push_back(D*& d) {
281  // C++ does not yet support rvalue references, so d should only be
282  // able to bind to an lvalue.
283  // This should be called only for lvalues.
284  data_.push_back(d);
285  d = 0;
286  }
287 
288  template<typename T, typename P>
289  template<typename D>
290  inline void OwnVector<T, P>::push_back(D* const& d) {
291 
292  // C++ allows d to be bound to an lvalue or rvalue. But the other
293  // signature should be a better match for an lvalue (because it
294  // does not require an lvalue->rvalue conversion). Thus this
295  // signature should only be chosen for rvalues.
296  data_.push_back(d);
297  }
298 
299 
300  template<typename T, typename P>
301  template<typename D>
302  inline void OwnVector<T, P>::push_back(std::auto_ptr<D> d) {
303  data_.push_back(d.release());
304  }
305 
306 
307  template<typename T, typename P>
308  inline void OwnVector<T, P>::push_back(T const& d) {
309  data_.push_back(policy_type::clone(d));
310  }
311 
312 
313  template<typename T, typename P>
315  // We have to delete the pointed-to thing, before we squeeze it
316  // out of the vector...
317  delete data_.back();
318  data_.pop_back();
319  }
320 
321  template <typename T, typename P>
322  inline bool OwnVector<T, P>::is_back_safe() const {
323  return data_.back() != 0;
324  }
325 
326  template<typename T, typename P>
328  T* result = data_.back();
329  if (result == 0) {
331  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
332  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
333  "pointer at the end of the collection is not null before calling back()\n"
334  "if you wish to avoid this exception.\n"
335  "Consider using OwnVector::is_back_safe()\n");
336  }
337  return *data_.back();
338  }
339 
340  template<typename T, typename P>
342  T* result = data_.back();
343  if (result == 0) {
345  "In OwnVector::back() we have intercepted an attempt to dereference a null pointer\n"
346  "Since OwnVector is allowed to contain null pointers, you much assure that the\n"
347  "pointer at the end of the collection is not null before calling back()\n"
348  "if you wish to avoid this exception.\n"
349  "Consider using OwnVector::is_back_safe()\n");
350  }
351  return *data_.back();
352  }
353 
354  template<typename T, typename P>
356  return *data_.front();
357  }
358 
359  template<typename T, typename P>
361  return *data_.front();
362  }
363 
364  template<typename T, typename P>
365  inline void OwnVector<T, P>::destroy() noexcept {
366  typename base::const_iterator b = data_.begin(), e = data_.end();
367  for(typename base::const_iterator i = b; i != e; ++ i)
368  delete * i;
369  }
370 
371  template<typename T, typename P>
372  inline typename OwnVector<T, P>::base const& OwnVector<T, P>::data() const {
373  return data_;
374  }
375 
376  template<typename T, typename P>
377  inline void OwnVector<T, P>::clear() {
378  destroy();
379  data_.clear();
380  }
381 
382  template<typename T, typename P>
384  delete * pos.i;
385  return iterator(data_.erase(pos.i));
386  }
387 
388  template<typename T, typename P>
390  typename base::iterator b = first.i, e = last.i;
391  for(typename base::iterator i = b; i != e; ++ i)
392  delete * i;
393  return iterator(data_.erase(b, e));
394  }
395 
396  template<typename T, typename P> template<typename S>
398  std::sort(data_.begin(), data_.end(), ordering(comp));
399  }
400 
401  template<typename T, typename P>
403  std::sort(data_.begin(), data_.end(), ordering(std::less<value_type>()));
404  }
405 
406  template<typename T, typename P>
407  inline void OwnVector<T, P>::swap(OwnVector<T, P>& other) noexcept {
408  data_.swap(other.data_);
409  }
410 
411  template<typename T, typename P>
413  std::vector<void const*>& pointers,
414  helper_vector& helpers) const {
415  typedef Ref<OwnVector> ref_type ;
416  typedef reftobase::RefHolder<ref_type> holder_type;
417 
418  size_type numElements = this->size();
419  pointers.reserve(numElements);
420  helpers.reserve(numElements);
421  size_type key = 0;
422  for(typename base::const_iterator i=data_.begin(), e=data_.end(); i!=e; ++i, ++key) {
423 
424  if (*i == 0) {
426  "In OwnVector::fillView() we have intercepted an attempt to put a null pointer\n"
427  "into a View and that is not allowed. It is probably an error that the null\n"
428  "pointer was in the OwnVector in the first place.\n");
429  }
430  else {
431  pointers.push_back(*i);
432  holder_type h(ref_type(id, *i, key,this));
433  helpers.push_back(&h);
434  }
435  }
436  }
437 
438  template<typename T, typename P>
439  inline void swap(OwnVector<T, P>& a, OwnVector<T, P>& b) noexcept {
440  a.swap(b);
441  }
442 
443  //----------------------------------------------------------------------
444  //
445  // Free function template to support creation of Views.
446 
447  template <typename T, typename P>
448  inline
449  void
451  ProductID const& id,
452  std::vector<void const*>& pointers,
453  helper_vector& helpers) {
454  obj.fillView(id, pointers, helpers);
455  }
456 
457 
458  template <typename T, typename P>
459  struct has_fillView<edm::OwnVector<T, P> > {
460  static bool const value = true;
461  };
462 
463 
464  // Free function templates to support the use of edm::Ptr.
465 
466  template <typename T, typename P>
467  inline
468  void
469  OwnVector<T,P>::setPtr(std::type_info const& toType,
470  unsigned long index,
471  void const*& ptr) const {
472  detail::reallySetPtr<OwnVector<T,P> >(*this, toType, index, ptr);
473  }
474 
475  template <typename T, typename P>
476  inline
477  void
479  std::type_info const& toType,
480  unsigned long index,
481  void const*& ptr) {
482  obj.setPtr(toType, index, ptr);
483  }
484 
485  template <typename T, typename P>
486  inline
487  void
488  OwnVector<T,P>::fillPtrVector(std::type_info const& toType,
489  std::vector<unsigned long> const& indices,
490  std::vector<void const*>& ptrs) const {
491  detail::reallyfillPtrVector(*this, toType, indices, ptrs);
492  }
493 
494 
495  template <typename T, typename P>
496  inline
497  void
499  std::type_info const& toType,
500  std::vector<unsigned long> const& indices,
501  std::vector<void const*>& ptrs) {
502  obj.fillPtrVector(toType, indices, ptrs);
503  }
504 
505 
506  template <typename T, typename P>
507  struct has_setPtr<edm::OwnVector<T,P> > {
508  static bool const value = true;
509  };
510 
511 
512 }
513 
514 
515 #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:98
int i
Definition: DBlmapReader.cc:9
static bool const value
Definition: traits.h:173
T const * operator->() const
Definition: OwnVector.h:64
reference back()
Definition: OwnVector.h:327
std::vector< T * > base
Definition: OwnVector.h:30
iterator operator-(difference_type n) const
Definition: OwnVector.h:86
base::const_iterator::iterator_category iterator_category
Definition: OwnVector.h:48
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, helper_vector &helpers)
const_iterator & operator++()
Definition: OwnVector.h:52
reference operator[](size_type)
Definition: OwnVector.h:264
bool operator()(T const *t1, T const *t2) const
Definition: OwnVector.h:177
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:88
iterator & operator+=(difference_type d)
Definition: OwnVector.h:94
size_type size() const
Definition: OwnVector.h:254
T const & operator*() const
Definition: OwnVector.h:62
#define noexcept
iterator & operator-=(difference_type d)
Definition: OwnVector.h:95
#define P
iterator operator--(int)
Definition: OwnVector.h:83
void destroy()
Definition: OwnVector.h:365
bool operator!=(iterator const &ci) const
Definition: OwnVector.h:89
bool operator==(const_iterator const &ci) const
Definition: OwnVector.h:60
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
OwnVector< T, P > & operator=(OwnVector< T, P > const &)
Definition: OwnVector.h:218
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:75
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:55
iterator begin()
Definition: OwnVector.h:234
list ordering
Definition: config.py:7
bool operator<(const_iterator const &o) const
Definition: OwnVector.h:59
reference operator[](difference_type d) const
Definition: OwnVector.h:67
bool operator<(iterator const &o) const
Definition: OwnVector.h:87
void reverse()
Definition: OwnVector.h:149
iterator & operator--()
Definition: OwnVector.h:82
bool operator==(iterator const &ci) const
Definition: OwnVector.h:88
void push_back(D *&d)
Definition: OwnVector.h:280
reference operator[](difference_type d) const
Definition: OwnVector.h:96
const_iterator(typename base::const_iterator const &it)
Definition: OwnVector.h:49
bool empty() const
Definition: OwnVector.h:259
base::const_iterator i
Definition: OwnVector.h:69
tuple result
Definition: query.py:137
void clear()
Definition: OwnVector.h:377
iterator erase(iterator pos)
Definition: OwnVector.h:383
void setPtr(std::type_info const &toType, unsigned long index, void const *&ptr) const
Definition: OwnVector.h:469
void shrink_to_fit()
Definition: OwnVector.h:127
ptrdiff_t difference_type
Definition: OwnVector.h:76
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void fillPtrVector(std::type_info const &toType, std::vector< unsigned long > const &indices, std::vector< void const * > &ptrs) const
Definition: OwnVector.h:488
iterator & operator++()
Definition: OwnVector.h:80
const_iterator operator++(int)
Definition: OwnVector.h:53
bool is_back_safe() const
Definition: OwnVector.h:322
iterator end()
Definition: OwnVector.h:239
const_iterator(iterator const &it)
Definition: OwnVector.h:50
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:150
T const & const_reference
Definition: OwnVector.h:38
base::size_type size_type
Definition: OwnVector.h:34
double b
Definition: hdecay.h:120
string const
Definition: compareJSON.py:14
virtual void reserve(size_type n)=0
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
double S(const TLorentzVector &, const TLorentzVector &)
Definition: Particle.cc:99
#define private
Definition: FWFileEntry.h:17
base const & data() const
Definition: OwnVector.h:372
list key
Definition: combine.py:13
iterator operator++(int)
Definition: OwnVector.h:81
double a
Definition: hdecay.h:121
iterator(typename base::iterator const &it)
Definition: OwnVector.h:78
difference_type operator-(const_iterator const &o) const
Definition: OwnVector.h:56
const_iterator & operator--()
Definition: OwnVector.h:54
static Ordering< O > ordering(O const &comp)
Definition: OwnVector.h:184
virtual void push_back(RefHolderBase const *r)=0
iterator operator+(difference_type n) const
Definition: OwnVector.h:85
void pop_back()
Definition: OwnVector.h:314
const_iterator & operator-=(difference_type d)
Definition: OwnVector.h:66
T first(std::pair< T, U > const &p)
T * operator->() const
Definition: OwnVector.h:93
long double T
void swap(OwnVector< T, P > &other)
Definition: OwnVector.h:407
const_iterator operator+(difference_type n) const
Definition: OwnVector.h:57
reference front()
Definition: OwnVector.h:355
tuple size
Write out results.
bool operator!=(const_iterator const &ci) const
Definition: OwnVector.h:61
void reserve(size_t)
Definition: OwnVector.h:274
static bool const value
Definition: traits.h:124
def template
Definition: svgfig.py:520
const_iterator & operator+=(difference_type d)
Definition: OwnVector.h:65
void fillView(ProductID const &id, std::vector< void const * > &pointers, helper_vector &helpers) const
Definition: OwnVector.h:412
base::iterator::iterator_category iterator_category
Definition: OwnVector.h:77
const_iterator operator-(difference_type n) const
Definition: OwnVector.h:58