20 #ifndef MIRTK_Allocate_H 21 #define MIRTK_Allocate_H 23 #include "mirtk/Exception.h" 70 if ((matrix =
new (nothrow) Type[n]) ==
nullptr) {
71 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", n *
sizeof(Type),
" bytes");
88 inline void CAllocate(Type *&matrix,
int n,
const Type &init = Type())
94 for (
int i = 0; i < n; i++) matrix[i] = init;
100 template <
class Type>
101 inline Type *
CAllocate(
int n,
const Type *init =
nullptr)
104 const Type default_value = Type();
105 if (!init) init = &default_value;
112 template <
typename Type>
118 if (matrix) memset(matrix, 0, n *
sizeof(Type *));
123 template <
typename Type>
137 template <
class Type>
138 inline void CAllocate(Type **&matrix,
int x,
int y,
const Type &init = Type())
140 const size_t n =
static_cast<size_t>(x) * static_cast<size_t>(y);
147 if ((matrix =
new (nothrow) Type *[y]) ==
nullptr) {
148 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", y *
sizeof(Type *),
" bytes");
151 if ((matrix[0] =
new (nothrow) Type[n]) ==
nullptr) {
152 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", n *
sizeof(Type),
" bytes");
155 for (
size_t i = 0; i < n; ++i) matrix[0][i] = init;
157 for (
int j = 1; j < y; ++j) {
158 matrix[j] = matrix[j-1] + x;
164 template <
typename Type>
165 inline Type **
CAllocate(
int x,
int y,
const Type *init =
nullptr)
168 const Type default_value = Type();
169 if (!init) init = &default_value;
176 template <
class Type>
177 inline void Allocate(Type **&matrix,
int x,
int y, Type *data =
nullptr)
179 const size_t n =
static_cast<size_t>(x) * static_cast<size_t>(y);
186 if ((matrix =
new (nothrow) Type *[y]) ==
nullptr) {
187 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", y *
sizeof(Type *),
" bytes");
190 if (data) matrix[0] = data;
191 else if ((matrix[0] =
new (nothrow) Type[n]) ==
nullptr) {
192 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", n *
sizeof(Type),
" bytes");
195 for (
int j = 1; j < y; ++j) {
196 matrix[j] = matrix[j-1] + x;
202 template <
typename Type>
203 inline Type **
Allocate(
int x,
int y, Type *data =
nullptr)
212 template <
class Type>
213 inline Type **
Reshape(Type **matrix,
int x,
int y)
216 Type *
const data = matrix[0];
220 if ((matrix =
new (nothrow) Type *[y]) ==
nullptr) {
221 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", y *
sizeof(Type *),
" bytes");
226 for (
int j = 1; j < y; ++j) {
227 matrix[j] = matrix[j-1] + x;
238 template <
class Type>
239 inline void CAllocate(Type ***&matrix,
int x,
int y,
int z,
const Type &init = Type())
241 const size_t n =
static_cast<size_t>(x)
242 * static_cast<size_t>(y)
243 *
static_cast<size_t>(z);
250 if ((matrix =
new (nothrow) Type **[z]) ==
nullptr) {
251 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", z *
sizeof(Type **),
" bytes");
253 if ((matrix[0] =
new (nothrow) Type *[z*y]) ==
nullptr) {
254 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", z * y *
sizeof(Type *),
" bytes");
257 if ((matrix[0][0] =
new (nothrow) Type[n]) ==
nullptr) {
258 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", n *
sizeof(Type),
" bytes");
261 for (
size_t i = 0; i < n; ++i) matrix[0][0][i] = init;
263 for (
int k = 0; k < z; ++k) {
264 if (k > 0) matrix[k] = matrix[k-1] + y;
265 for (
int j = 0; j < y; ++j) {
266 matrix[k][j] = matrix[0][0] + (k*y + j) * x;
273 template <
typename Type>
274 inline Type ***
CAllocate(
int x,
int y,
int z,
const Type *init =
nullptr)
277 const Type default_value = Type();
278 if (!init) init = &default_value;
285 template <
class Type>
286 inline void Allocate(Type ***&matrix,
int x,
int y,
int z, Type *data =
nullptr)
288 const size_t n =
static_cast<size_t>(x)
289 * static_cast<size_t>(y)
290 *
static_cast<size_t>(z);
297 if ((matrix =
new (nothrow) Type **[z]) ==
nullptr) {
298 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", z *
sizeof(Type **),
" bytes");
300 if ((matrix[0] =
new (nothrow) Type *[z*y]) ==
nullptr) {
301 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", z * y *
sizeof(Type *),
" bytes");
304 if (data) matrix[0][0] = data;
305 else if ((matrix[0][0] =
new (nothrow) Type[n]) ==
nullptr) {
306 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", n *
sizeof(Type),
" bytes");
309 for (
int k = 0; k < z; ++k) {
310 if (k > 0) matrix[k] = matrix[k-1] + y;
311 for (
int j = 0; j < y; ++j) {
312 matrix[k][j] = matrix[0][0] + (k * y + j) * x;
319 template <
typename Type>
320 inline Type ***
Allocate(
int x,
int y,
int z, Type *data =
nullptr)
329 template <
class Type>
330 inline Type ***
Reshape(Type ***matrix,
int x,
int y,
int z)
333 Type *
const data = matrix[0][0];
338 if ((matrix =
new (nothrow) Type **[z]) ==
nullptr) {
339 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", z *
sizeof(Type **),
" bytes");
341 if ((matrix[0] =
new (nothrow) Type *[z*y]) ==
nullptr) {
342 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", z * y *
sizeof(Type *),
" bytes");
347 for (
int k = 0; k < z; ++k) {
348 if (k > 0) matrix[k] = matrix[k-1] + y;
349 for (
int j = 0; j < y; ++j) {
350 matrix[k][j] = matrix[0][0] + (k * y + j) * x;
362 template <
class Type>
363 inline void CAllocate(Type ****&matrix,
int x,
int y,
int z,
int t,
const Type &init = Type())
365 const size_t n =
static_cast<size_t>(x)
366 * static_cast<size_t>(y)
367 *
static_cast<size_t>(z)
368 * static_cast<size_t>(t);
375 if ((matrix =
new (nothrow) Type ***[t]) ==
nullptr) {
376 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", t *
sizeof(Type ***),
" bytes");
378 if ((matrix[0] =
new (nothrow) Type **[t*z]) ==
nullptr) {
379 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", t * z *
sizeof(Type **),
" bytes");
381 if ((matrix[0][0] =
new (nothrow) Type *[t*z*y]) ==
nullptr) {
382 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", t * z * y *
sizeof(Type *),
" bytes");
385 if ((matrix[0][0][0] =
new (nothrow) Type[n]) ==
nullptr) {
386 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", n *
sizeof(Type),
" bytes");
389 for (
size_t i = 0; i < n; ++i) matrix[0][0][0][i] = init;
391 for (
int l = 0; l < t; ++l) {
392 if (l > 0) matrix[l] = matrix[l-1] + z;
393 for (
int k = 0; k < z; ++k) {
394 matrix[l][k] = matrix[0][0] + (l * z + k) * y;
395 for (
int j = 0; j < y; ++j) {
396 matrix[l][k][j] = matrix[0][0][0] + ((l * z + k) * y + j) * x;
404 template <
typename Type>
405 inline Type ****
CAllocate(
int x,
int y,
int z,
int t,
const Type *init =
nullptr)
408 const Type default_value = Type();
409 if (!init) init = &default_value;
416 template <
class Type>
417 inline void Allocate(Type ****&matrix,
int x,
int y,
int z,
int t, Type *data =
nullptr)
419 const size_t n =
static_cast<size_t>(x)
420 * static_cast<size_t>(y)
421 *
static_cast<size_t>(z)
422 * static_cast<size_t>(t);
429 if ((matrix =
new (nothrow) Type ***[t]) ==
nullptr) {
430 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", t *
sizeof(Type ***),
" bytes");
432 if ((matrix[0] =
new (nothrow) Type ** [t*z]) ==
nullptr) {
433 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", t * z *
sizeof(Type **),
" bytes");
435 if ((matrix[0][0] =
new (nothrow) Type * [t*z*y]) ==
nullptr) {
436 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", t * z * y *
sizeof(Type *),
" bytes");
439 if (data) matrix[0][0][0] = data;
440 else if ((matrix[0][0][0] =
new (nothrow) Type[n]) ==
nullptr) {
441 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", n *
sizeof(Type),
" bytes");
444 for (
int l = 0; l < t; ++l) {
445 if (l > 0) matrix[l] = matrix[l-1] + z;
446 for (
int k = 0; k < z; ++k) {
447 matrix[l][k] = matrix[0][0] + (l * z + k) * y;
448 for (
int j = 0; j < y; ++j) {
449 matrix[l][k][j] = matrix[0][0][0] + ((l * z + k) * y + j) * x;
457 template <
typename Type>
458 inline Type ****
Allocate(
int x,
int y,
int z,
int t, Type *data =
nullptr)
467 template <
class Type>
468 inline Type ****
Reshape(Type ****matrix,
int x,
int y,
int z,
int t)
471 Type *
const data = matrix[0][0][0];
473 delete[] matrix[0][0];
477 if ((matrix =
new (nothrow) Type ***[t]) ==
nullptr) {
478 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", t *
sizeof(Type ***),
" bytes");
480 if ((matrix[0] =
new (nothrow) Type **[t*z]) ==
nullptr) {
481 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", t * z *
sizeof(Type **),
" bytes");
483 if ((matrix[0][0] =
new (nothrow) Type *[t*z*y]) ==
nullptr) {
484 Throw(ERR_Memory, __FUNCTION__,
"Failed to allocate ", t * z * y *
sizeof(Type *),
" bytes");
487 matrix[0][0][0] = data;
489 for (
int l = 0; l < t; ++l) {
490 if (l > 0) matrix[l] = matrix[l-1] + z;
491 for (
int k = 0; k < z; ++k) {
492 matrix[l][k] = matrix[0][0] + (l * z + k) * y;
493 for (
int j = 0; j < y; ++j) {
494 matrix[l][k][j] = matrix[0][0][0] + ((l * z + k) * y + j) * x;
504 #endif // MIRTK_Allocate_H void PAllocate(Type **&matrix, int n)
Allocate 1D array of pointers initialized to nullptr.
void Allocate(Type *&matrix, int n)
Allocate 1D array.
void CAllocate(Type *&matrix, int n, const Type &init=Type())
Allocate 1D array and initialize it.
Type ** Reshape(Type **matrix, int x, int y)
Reshape 2D array stored in contiguous memory block.
void Throw(ErrorType err, const char *func, Args... args)