CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 public :
10  using size_type = unsigned int;
11  using value_type = T;
12  using reference = T&;
13  using const_reference = T const&;
14 
16 
17  explicit DynArray(unsigned char * storage) : a((T*)(storage)), s(0){}
18 
19  DynArray(unsigned char * storage, unsigned int isize) : a((T*)(storage)), s(isize){
20  for (auto i=0U; i<s; ++i) new((begin()+i)) T();
21  }
22  DynArray(unsigned char * storage, unsigned int isize, T const& it) : a((T*)(storage)), s(isize){
23  for (auto i=0U; i<s; ++i) new((begin()+i)) T(it);
24  }
25 
26  DynArray(DynArray const&) = delete;
27  DynArray & operator=(DynArray const &) = delete;
28 
29  DynArray(DynArray && other) { a=other.a; s=other.s; other.s=0; other.a=nullptr;}
30  DynArray & operator=(DynArray && other) { a=other.a; s=other.s; other.s=0; other.a=nullptr; return *this;}
31 
32 
33  ~DynArray() { for (auto i=0U; i<s; ++i) a[i].~T(); }
34 
35  T & operator[](unsigned int i) { return a[i];}
36  T * begin() { return a;}
37  T * end() { return a+s;}
38  T & front() { return a[0];}
39  T & back() { return a[s-1];}
40 
41  T const & operator[](unsigned int i) const { return a[i];}
42  T const * begin() const { return a;}
43  T const * end() const { return a+s;}
44  unsigned int size() const { return s;}
45  bool empty() const { return 0==s;}
46 
47  T const &front() const { return a[0];}
48  T const & back() const { return a[s-1];}
49 
50  void pop_back() {back().~T(); --s;}
51  void push_back(T const& t) {new((begin()+s)) T(t);++s;}
52  void push_back(T && t) {new((begin()+s)) T(t);++s;}
53 
54 
55 };
56 
57 #define unInitDynArray(T,n,x) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage)
58 #define declareDynArray(T,n,x) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage,n)
59 #define initDynArray(T,n,x,i) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage,n,i)
60 
61 
62 #endif // CommonTools_Utils_DynArray_H
int i
Definition: DBlmapReader.cc:9
DynArray & operator=(DynArray &&other)
Definition: DynArray.h:30
DynArray(DynArray &&other)
Definition: DynArray.h:29
T const & const_reference
Definition: DynArray.h:13
unsigned int size_type
Definition: DynArray.h:10
T const * begin() const
Definition: DynArray.h:42
T const * end() const
Definition: DynArray.h:43
T * a
Definition: DynArray.h:7
T * end()
Definition: DynArray.h:37
T const & back() const
Definition: DynArray.h:48
void pop_back()
Definition: DynArray.h:50
T const & front() const
Definition: DynArray.h:47
T const & operator[](unsigned int i) const
Definition: DynArray.h:41
T & back()
Definition: DynArray.h:39
T & front()
Definition: DynArray.h:38
DynArray & operator=(DynArray const &)=delete
DynArray()
Definition: DynArray.h:15
DynArray(unsigned char *storage, unsigned int isize, T const &it)
Definition: DynArray.h:22
T * begin()
Definition: DynArray.h:36
unsigned int s
Definition: DynArray.h:8
DynArray(unsigned char *storage, unsigned int isize)
Definition: DynArray.h:19
void push_back(T const &t)
Definition: DynArray.h:51
T & reference
Definition: DynArray.h:12
bool empty() const
Definition: DynArray.h:45
void push_back(T &&t)
Definition: DynArray.h:52
T value_type
Definition: DynArray.h:11
DynArray(unsigned char *storage)
Definition: DynArray.h:17
unsigned int size() const
Definition: DynArray.h:44
long double T
~DynArray()
Definition: DynArray.h:33
T & operator[](unsigned int i)
Definition: DynArray.h:35