Canoe
Comprehensive Atmosphere N' Ocean Engine
ndarrays.hpp
Go to the documentation of this file.
1 #ifndef SRC_UTILS_NDARRAYS_HPP_
2 #define SRC_UTILS_NDARRAYS_HPP_
3 
4 template <typename T>
5 void NewCArray(T**& a, int n1, int n2) {
6  a = new T*[n1];
7  a[0] = new T[n1 * n2];
8 
9  for (int i = 0; i < n1; ++i) a[i] = a[0] + i * n2;
10 }
11 
12 template <typename T>
13 void FreeCArray(T** a) {
14  delete[] a[0];
15  delete[] a;
16 }
17 
18 template <typename T>
19 void FreeCArray2(T** a) {
20  delete[] a[0];
21  delete[] a;
22 }
23 
24 template <typename T>
25 void NewCArray(T***& a, int n1, int n2, int n3) {
26  a = new T**[n1];
27  a[0] = new T*[n1 * n2];
28  a[0][0] = new T[n1 * n2 * n3];
29 
30  for (int i = 0; i < n1; ++i) {
31  a[i] = a[0] + i * n2;
32  for (int j = 0; j < n2; ++j) a[i][j] = a[0][0] + i * n2 * n3 + j * n3;
33  }
34 }
35 
36 template <typename T>
37 void FreeCArray(T*** a) {
38  delete[] a[0][0];
39  delete[] a[0];
40  delete[] a;
41 }
42 
43 template <typename T>
44 void FreeCArray3(T*** a) {
45  delete[] a[0][0];
46  delete[] a[0];
47  delete[] a;
48 }
49 
50 template <typename T>
51 void NewCArray(T****& a, int n1, int n2, int n3, int n4) {
52  a = new T***[n1];
53  a[0] = new T**[n1 * n2];
54  a[0][0] = new T*[n1 * n2 * n3];
55  a[0][0][0] = new T[n1 * n2 * n3 * n4];
56 
57  for (int i = 0; i < n1; ++i) {
58  a[i] = a[0] + i * n2;
59  for (int j = 0; j < n2; ++j) {
60  a[i][j] = a[0][0] + i * n2 * n3 + j * n3;
61  for (int k = 0; k < n3; ++k)
62  a[i][j][k] = a[0][0][0] + i * n2 * n3 * n4 + j * n3 * n4 + k * n4;
63  }
64  }
65 }
66 
67 template <typename T>
68 void FreeCArray(T**** a) {
69  delete[] a[0][0][0];
70  delete[] a[0][0];
71  delete[] a[0];
72  delete[] a;
73 }
74 
75 template <typename T>
76 void FreeCArray4(T**** a) {
77  delete[] a[0][0][0];
78  delete[] a[0][0];
79  delete[] a[0];
80  delete[] a;
81 }
82 
83 #endif // SRC_UTILS_NDARRAYS_HPP_
void FreeCArray3(T ***a)
Definition: ndarrays.hpp:44
void FreeCArray(T **a)
Definition: ndarrays.hpp:13
void FreeCArray4(T ****a)
Definition: ndarrays.hpp:76
void FreeCArray2(T **a)
Definition: ndarrays.hpp:19
void NewCArray(T **&a, int n1, int n2)
Definition: ndarrays.hpp:5