CMS 3D CMS Logo

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