CMS 3D CMS Logo

DynArray.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_DynArray_H
2 #define CommonTools_Utils_DynArray_H
3 
4 template <typename T>
5 class DynArray {
6 public:
7  T* a = nullptr;
8  unsigned int s = 0;
9 
10 public:
11  using size_type = unsigned int;
12  using value_type = T;
13  using reference = T&;
14  using const_reference = T const&;
15 
16  DynArray() {}
17 
18  explicit DynArray(unsigned char* storage) : a((T*)(storage)), s(0) {}
19 
20  DynArray(unsigned char* storage, unsigned int isize) : a((T*)(storage)), s(isize) {
21  for (auto i = 0U; i < s; ++i)
22  new ((begin() + i)) T();
23  }
24  DynArray(unsigned char* storage, unsigned int isize, T const& it) : a((T*)(storage)), s(isize) {
25  for (auto i = 0U; i < s; ++i)
26  new ((begin() + i)) T(it);
27  }
28 
29  DynArray(DynArray const&) = delete;
30  DynArray& operator=(DynArray const&) = delete;
31 
33  a = other.a;
34  s = other.s;
35  other.s = 0;
36  other.a = nullptr;
37  }
39  a = other.a;
40  s = other.s;
41  other.s = 0;
42  other.a = nullptr;
43  return *this;
44  }
45 
47  for (auto i = 0U; i < s; ++i)
48  a[i].~T();
49  }
50 
51  T& operator[](unsigned int i) { return a[i]; }
52  T* begin() { return a; }
53  T* end() { return a + s; }
54  T& front() { return a[0]; }
55  T& back() { return a[s - 1]; }
56 
57  T const& operator[](unsigned int i) const { return a[i]; }
58  T const* begin() const { return a; }
59  T const* end() const { return a + s; }
60  unsigned int size() const { return s; }
61  bool empty() const { return 0 == s; }
62 
63  T const* data() const { return a; }
64  T const& front() const { return a[0]; }
65  T const& back() const { return a[s - 1]; }
66 
67  void pop_back() {
68  back().~T();
69  --s;
70  }
71  void push_back(T const& t) {
72  new ((begin() + s)) T(t);
73  ++s;
74  }
75  void push_back(T&& t) {
76  new ((begin() + s)) T(t);
77  ++s;
78  }
79 };
80 
81 namespace dynarray {
82  template <typename T>
83  inline T num(T s) {
84  return s > 0 ? s : T(1);
85  }
86 } // namespace dynarray
87 
88 #define unInitDynArray(T, n, x) \
89  alignas(alignof(T)) unsigned char x##_storage[sizeof(T) * dynarray::num(n)]; \
90  DynArray<T> x(x##_storage)
91 #define declareDynArray(T, n, x) \
92  alignas(alignof(T)) unsigned char x##_storage[sizeof(T) * dynarray::num(n)]; \
93  DynArray<T> x(x##_storage, n)
94 #define initDynArray(T, n, x, i) \
95  alignas(alignof(T)) unsigned char x##_storage[sizeof(T) * dynarray::num(n)]; \
96  DynArray<T> x(x##_storage, n, i)
97 
98 #endif // CommonTools_Utils_DynArray_H
DynArray::empty
bool empty() const
Definition: DynArray.h:61
DynArray::value_type
T value_type
Definition: DynArray.h:12
mps_fire.i
i
Definition: mps_fire.py:428
DynArray::~DynArray
~DynArray()
Definition: DynArray.h:46
DynArray::pop_back
void pop_back()
Definition: DynArray.h:67
DynArray::a
T * a
Definition: DynArray.h:7
DynArray::operator[]
T const & operator[](unsigned int i) const
Definition: DynArray.h:57
DynArray::begin
T * begin()
Definition: DynArray.h:52
DynArray::operator=
DynArray & operator=(DynArray &&other)
Definition: DynArray.h:38
DynArray::DynArray
DynArray(DynArray &&other)
Definition: DynArray.h:32
DynArray::front
T const & front() const
Definition: DynArray.h:64
alignCSCRings.s
s
Definition: alignCSCRings.py:92
DynArray::const_reference
T const & const_reference
Definition: DynArray.h:14
DynArray::back
T & back()
Definition: DynArray.h:55
DynArray::DynArray
DynArray(unsigned char *storage, unsigned int isize)
Definition: DynArray.h:20
DynArray
Definition: DynArray.h:5
DynArray::DynArray
DynArray(unsigned char *storage, unsigned int isize, T const &it)
Definition: DynArray.h:24
dynarray::num
T num(T s)
Definition: DynArray.h:83
DynArray::s
unsigned int s
Definition: DynArray.h:8
trackingPlots.other
other
Definition: trackingPlots.py:1467
DynArray::operator=
DynArray & operator=(DynArray const &)=delete
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
DynArray::front
T & front()
Definition: DynArray.h:54
DynArray::data
T const * data() const
Definition: DynArray.h:63
DynArray::DynArray
DynArray()
Definition: DynArray.h:16
createfilelist.int
int
Definition: createfilelist.py:10
DynArray::begin
T const * begin() const
Definition: DynArray.h:58
DynArray::push_back
void push_back(T &&t)
Definition: DynArray.h:75
DynArray::DynArray
DynArray(unsigned char *storage)
Definition: DynArray.h:18
DynArray::end
T const * end() const
Definition: DynArray.h:59
T
long double T
Definition: Basic3DVectorLD.h:48
dynarray
Definition: DynArray.h:81
DynArray::back
T const & back() const
Definition: DynArray.h:65
DynArray::operator[]
T & operator[](unsigned int i)
Definition: DynArray.h:51
DynArray::end
T * end()
Definition: DynArray.h:53
DynArray::size
unsigned int size() const
Definition: DynArray.h:60
DynArray::reference
T & reference
Definition: DynArray.h:13
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
DynArray::push_back
void push_back(T const &t)
Definition: DynArray.h:71
DynArray::size_type
unsigned int size_type
Definition: DynArray.h:11