-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagedArray.hpp
More file actions
237 lines (183 loc) · 3.02 KB
/
ManagedArray.hpp
File metadata and controls
237 lines (183 loc) · 3.02 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#ifndef MANAGED_ARRAY_HPP
#define MANAGED_ARRAY_HPP
class ManagedIntList
{
private:
int* Data = NULL;
void _Free(int*& mem)
{
if (mem != NULL)
{
delete[] mem;
mem = NULL;
}
}
int* _IntList(int size)
{
auto temp = new int[size];
for (auto i = 0; i < size; i++)
temp[i] = i;
return temp;
}
public:
int x = 0;
ManagedIntList(int size)
{
x = size;
Data = _IntList(size);
}
// 1D arrays
int& operator()(int ix)
{
return Data[ix];
}
int Length()
{
return x;
}
void Free()
{
_Free(Data);
x = 0;
Data = NULL;
}
};
class ManagedArray
{
private:
double* Data = NULL;
double* _New(int size, bool initialize = true)
{
auto temp = new double[size];
if (initialize)
{
for (auto i = 0; i < size; i++)
temp[i] = 0.0;
}
return temp;
}
void _Free(double*& mem)
{
if (mem != NULL)
{
delete[] mem;
mem = NULL;
}
}
public:
int x = 0;
int y = 0;
int z = 0;
int i = 0;
int j = 0;
ManagedArray()
{
}
ManagedArray(int size, bool initialize = true)
{
Resize(size, initialize);
}
ManagedArray(int sizex, int sizey, bool initialize = true)
{
Resize(sizex, sizey, initialize);
}
ManagedArray(int sizex, int sizey, int sizez, bool initialize = true)
{
Resize(sizex, sizey, sizez, initialize);
}
// For 4D arrays of type: [i][j] of [x][y] and [i] of [x][y][z]
ManagedArray(int sizex, int sizey, int sizez, int sizei, int sizej, bool initialize = true)
{
Resize(sizex, sizey, sizez, sizei, sizej, initialize);
}
// 1D arrays
double& operator()(int ix)
{
return Data[ix];
}
// 2D arrays
double& operator()(int ix, int iy)
{
return Data[iy * x + ix];
}
// 3D arrays
double& operator()(int ix, int iy, int iz)
{
return Data[(iz * y + iy) * x + ix];
}
void Resize(int size, bool initialize = true)
{
_Free(Data);
x = size;
y = 1;
z = 1;
i = 1;
j = 1;
Data = _New(size, initialize);
}
void Resize(int sizex, int sizey, bool initialize = true)
{
_Free(Data);
x = sizex;
y = sizey;
z = 1;
i = 1;
j = 1;
Data = _New(x * y, initialize);
}
void Resize(int sizex, int sizey, int sizez, bool initialize = true)
{
_Free(Data);
x = sizex;
y = sizey;
z = sizez;
i = 1;
j = 1;
Data = _New(x * y * z, initialize);
}
// For 4D arrays of type: [i][j] of [x][y] and [i] of [x][y][z]
void Resize(int sizex, int sizey, int sizez, int sizei, int sizej, bool initialize = true)
{
_Free(Data);
x = sizex;
y = sizey;
z = sizez;
i = sizei;
j = sizej;
Data = _New(x * y * z * i * j, initialize);
}
int Length()
{
return x * y* z* i* j;
}
// Reshape without modifying data
void Reshape(int ix = 1, int iy = 1, int iz = 1, int ii = 1, int ij = 1)
{
x = ix;
y = iy;
z = iz;
i = ii;
j = ij;
}
void Resize(ManagedArray& a, bool initialize = true)
{
_Free(Data);
x = a.x;
y = a.y;
z = a.z;
i = a.i;
j = a.j;
Data = _New(x * y * z * i * j, initialize);
}
void Free()
{
_Free(Data);
x = 0;
y = 0;
z = 0;
i = 0;
j = 0;
Data = NULL;
}
};
#endif