-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagedOps.hpp
More file actions
286 lines (236 loc) · 6.36 KB
/
ManagedOps.hpp
File metadata and controls
286 lines (236 loc) · 6.36 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef MANAGED_OPS_HPP
#define MANAGED_OPS_HPP
#include "ManagedArray.hpp"
class ManagedOps
{
public:
static void MemCopy(ManagedArray& dst, int dstoffset, ManagedArray& src, int srcoffset, int count)
{
for (auto i = 0; i < count; i++)
dst(dstoffset + i) = src(srcoffset + i);
}
static void Set(ManagedArray& dst, double value)
{
for (auto i = 0; i < dst.Length(); i++)
dst(i) = value;
}
static void Set(ManagedIntList& dst, int value)
{
for (auto i = 0; i < dst.Length(); i++)
dst(i) = value;
}
// Copy 2D[minx + x][miny + y]
static void Copy2D(ManagedArray& dst, ManagedArray& src, int minx, int miny)
{
if (miny >= 0 && miny < src.y)
{
for (auto y = 0; y < dst.y; y++)
{
auto dstoffset = y * dst.x;
auto srcoffset = (miny + y) * src.x + minx;
MemCopy(dst, dstoffset, src, srcoffset, dst.x);
}
}
}
// Copy 2D[index_list][y]
static void Copy2DX(ManagedArray& dst, ManagedArray& src, ManagedIntList& index_list, int minx)
{
for (auto y = 0; y < dst.y; y++)
{
auto dstoffset = y * dst.x;
auto srcoffset = y * src.x;
auto xx = index_list(minx);
MemCopy(dst, dstoffset, src, srcoffset + xx, dst.x);
}
}
// Copy 2D[x][y] to 2D[minx + x][miny + y]
static void Copy2DOffset(ManagedArray& dst, ManagedArray& src, int minx, int miny)
{
if (miny >= 0 && miny < dst.y && src.y > 0)
{
for (auto y = 0; y < src.y; y++)
{
auto dstoffset = (miny + y) * dst.x + minx;
auto srcoffset = y * src.x;
MemCopy(dst, dstoffset, src, srcoffset, src.x);
}
}
}
// Copy 3D[minx + x][miny + y][minz + z]
static void Copy3D(ManagedArray& dst, ManagedArray& src, int minx, int miny, int minz)
{
if (minx >= 0 && minx < src.x && miny >= 0 && miny < src.y && minz >= 0 && minz < src.z)
{
for (auto z = 0; z < dst.z; z++)
{
auto offsetd = z * dst.y;
auto offsets = (minz + z) * src.y + miny;
for (auto y = 0; y < dst.y; y++)
{
auto dstoffset = (offsetd + y) * dst.x;
auto srcoffset = (offsets + y) * src.x + minx;
MemCopy(dst, dstoffset, src, srcoffset, dst.x);
}
}
}
}
// Copy 3D[x][y][index_list]
static void Copy3DZ(ManagedArray& dst, ManagedArray& src, ManagedIntList& index_list, int minz)
{
if (minz < src.z)
{
for (auto z = 0; z < dst.z; z++)
{
auto zz = index_list(minz + z);
for (auto y = 0; y < dst.y; y++)
{
auto dstoffset = (z * dst.y + y) * dst.x;
auto srcoffset = (zz * src.y + y) * src.x;
MemCopy(dst, dstoffset, src, srcoffset, dst.x);
}
}
}
}
// Copies a 4D [index][x][y][z] to 3D [x][y][z]
static void Copy4D3D(ManagedArray& dst, ManagedArray& src, int index)
{
MemCopy(dst, 0, src, index * dst.Length(), dst.Length());
}
// Copies a 3D [x][y][z] to 4D [index][x][y][z] with subsampling
static void Copy3D4D(ManagedArray& dst, ManagedArray& src, int index, int step)
{
if (dst.z == src.z)
{
for (auto z = 0; z < dst.z; z++)
{
auto offsetd = index * dst.z * dst.y + z * dst.y;
auto offsets = z * src.y;
for (auto y = 0; y < dst.y; y++)
{
auto dstoffset = (offsetd + y) * dst.x;
auto srcoffset = (offsets + y * step) * src.x;
for (auto x = 0; x < dst.x; x++)
{
dst(dstoffset + x) = src(srcoffset + x * step);
}
}
}
}
}
// Copies a 3D [x][y][z] to 4D [index][x][y][z] with maxpooling
static void Pool3D4D(ManagedArray& dst, ManagedArray& src, int index, int step)
{
if (dst.z == src.z)
{
for (auto z = 0; z < dst.z; z++)
{
auto offsetd = index * dst.z * dst.y + z * dst.y;
auto offsets = z * src.y;
for (auto y = 0; y < dst.y; y++)
{
auto dstoffset = (offsetd + y) * dst.x;
auto ys = y * step;
for (auto x = 0; x < dst.x; x++)
{
auto maxval = std::numeric_limits<double>::min();
auto xs = x * step;
for (auto yy = 0; yy < step; yy++)
{
auto dy = ys + yy;
auto vstep = (offsets + dy) * src.x;
for (auto xx = 0; xx < step; xx++)
{
auto dx = xs + xx;
if (dx < src.x && dy < src.y)
{
auto val = src(vstep + dx);
if (val > maxval)
maxval = val;
}
}
}
dst(dstoffset + x) = maxval;
}
}
}
}
}
// Copies a 3D [x][y][z] to 4D [index][x][y][z]
static void Copy3D4D(ManagedArray& dst, ManagedArray& src, int index)
{
MemCopy(dst, index * src.Length(), src, 0, src.Length());
}
// Copies a 2D [x][y] to 3D [index][x][y]
static void Copy2D3D(ManagedArray& dst, ManagedArray& src, int index)
{
auto size2D = src.x * src.y;
if (index >= 0 && index < dst.z && src.x == dst.x && src.y == dst.y)
{
auto dstoffset = index * size2D;
for (auto y = 0; y < src.y; y++)
{
auto srcoffset = y * src.x;
MemCopy(dst, dstoffset + srcoffset, src, srcoffset, src.x);
}
}
}
// Copies a 2D [x][y] to 4D [index][x][y][z]
static void Copy2D4D(ManagedArray& dst, ManagedArray& src, int z, int index)
{
auto size2D = src.x * src.y;
auto size3D = size2D * dst.z;
if (index >= 0 && src.x == dst.x && src.y == dst.y)
{
auto dstoffset = index * size3D + z * size2D;
for (auto y = 0; y < src.x; y++)
{
auto srcoffset = y * src.x;
MemCopy(dst, srcoffset + dstoffset, src, srcoffset, src.x);
}
}
}
// Copies a 4D [index][x][y][z] to 2D [x][y]
static void Copy4D2D(ManagedArray& dst, ManagedArray& src, int z, int index)
{
auto size2D = dst.x * dst.y;
auto size3D = size2D * src.z;
if (index >= 0 && src.x == dst.x && src.y == dst.y)
{
auto srcoffset = index * size3D + z * size2D;
for (auto y = 0; y < dst.y; y++)
{
auto dstoffset = y * dst.x;
MemCopy(dst, dstoffset, src, srcoffset + dstoffset, dst.x);
}
}
}
// Copies a 4D [i][j][x][y] to a 2D [x][y] array
static void Copy4DIJ2D(ManagedArray& dst, ManagedArray& src, int i, int j)
{
auto size2D = dst.x * dst.y;
auto srcoffset = (i * src.j + j) * size2D;
if (j < src.j && i < src.i)
{
MemCopy(dst, 0, src, srcoffset, size2D);
}
}
// Copies a 2D [x][y] array to a 4D [i][j][x][y]
static void Copy2D4DIJ(ManagedArray& dst, ManagedArray& src, int i, int j)
{
auto size2D = src.x * src.y;
auto dstoffset = (i * dst.j + j) * size2D;
if (j >= 0 && j < dst.j && i >= 0 && i < dst.i)
{
MemCopy(dst, dstoffset, src, 0, size2D);
}
}
static void Free(ManagedArray& item)
{
item.Free();
}
static void Free(ManagedIntList& item)
{
item.Free();
}
};
#endif