-
Notifications
You must be signed in to change notification settings - Fork 1
/
DynArray.cpp
66 lines (46 loc) · 1.7 KB
/
DynArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
template<typename T>
class DynArray {
T * a=nullptr;
unsigned int s=0;
public :
DynArray(unsigned char * storage, unsigned int isize) : a((T*)(storage)), s(isize){
for (auto i=0U; i<s; ++i) new((begin()+i)) T();
}
DynArray(unsigned char * storage, unsigned int isize, T const& it) : a((T*)(storage)), s(isize){
for (auto i=0U; i<s; ++i) new((begin()+i)) T(it);
}
~DynArray() { for (auto i=0U; i<s; ++i) a[i].~T(); }
T & operator[](unsigned int i) { return a[i];}
T * begin() { return a;}
T * end() { return a+s;}
T const & operator[](unsigned int i) const { return a[i];}
T const * begin() const { return a;}
T const * end() const { return a+s;}
unsigned int size() const { return s;}
};
#define declareDynArray(T,n,x) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage,n)
#define initDynArray(T,n,x,i) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage,n,i)
struct A {
int i=-3;
double k=0.1;
virtual ~A(){}
};
#include<cassert>
#include<iostream>
int main(int s, char **) {
using T=A;
unsigned n = 4*s;
// alignas(alignof(T)) unsigned char a_storage[sizeof(T)*n];
// DynArray<T> a(a_storage,n);
declareDynArray(T,n,a);
// T b[n];
declareDynArray(T,n,b);
auto pa = [&](auto i) { a[1].k=0.3; return a[i].k; };
auto pb = [&](auto i) { b[1].k=0.5; return b[i].k; };
auto loop = [&](auto k) { for(auto const & q : a) k = std::min(k,q.k); return k;};
std::cout << a[n-1].k << ' ' << pa(1) << ' ' << loop(2.3) << std::endl;
std::cout << b[n-1].k << ' ' << pb(1) << std::endl;
initDynArray(bool,n,q,true);
if (q[n-1]) std::cout << "ok" << std::endl;
return 0;
};