-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharraytools.h
More file actions
74 lines (58 loc) · 1.63 KB
/
arraytools.h
File metadata and controls
74 lines (58 loc) · 1.63 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
/*
* File: arraytools.h
* Author: zoizoi
*
* Created on 05 March 2009, 11:04
*/
#ifndef _ARRAYTOOLS_H
#define _ARRAYTOOLS_H
class ArrayTools {
public:
ArrayTools();
ArrayTools(const ArrayTools& orig);
virtual ~ArrayTools();
template < typename T >
static T** allocate2DArray(int nRows, int nCols) {
//(step 1) allocate memory for array of elements of column
T **ppi = new T*[nRows];
//(step 2) allocate memory for array of elements of each row
T *curPtr = new T [nRows * nCols];
// Now point the pointers in the right place
for (int i = 0; i < nRows; ++i) {
ppi[i] = curPtr;
curPtr += nCols;
}
return ppi;
}
template < typename T >
static T ***allocate3DArray(int nRows, int nCols, int nPlanez) {
T ***ppp = new T**[nRows];
for (int i = 0; i < nRows; ++i) {
ppp[i]=allocate2DArray<T>(nCols,nPlanez);
}
return ppp;
}
template < typename T >
static T ***allocate3DArrayB(int nRows, int nCols, int nPlanez) {
T ***ppp = new T**[nPlanez];
T* alloc = new T[nPlanez*nCols*nRows];
for (int i = 0; i < nPlanez; ++i) {
ppp[i]=alloc;
alloc+=nCols*nRows;
}
return ppp;
}
template < typename T >
static void free2DArray(T** Array) {
delete [] * Array;
delete [] Array;
}
template < typename T >
static void free3DArray(T*** Array) {
delete [] **Array;
delete [] * Array;
delete [] Array;
}
private:
};
#endif /* _ARRAYTOOLS_H */