-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
269 lines (252 loc) · 7.8 KB
/
array.h
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
/*
* =====================================================================================
*
* Filename: array.h
* Description: array implementation
* Version: 1.0
* Created: 05/02/2006 05:12:34 PM CEST
* Modified: 2008-02-13 15:23:55 Wednesday Week 06
* Revision: none
* Compiler: gcc
* Author: Nanjiang Shu (Shu), [email protected]
* Company: Structural Chemistry, Stockholm Univesity
* =====================================================================================
*/
#ifndef HAS_ARRAY_H
#define HAS_ARRAY_H
#include <iostream>
//Array1D
template <class T> class Array1D // can be accessed both by .array1D and []
{
protected:
unsigned int size;
// T* array1D;
public:
T* array1D; // can also accessed by array1D directory, quicker
Array1D(unsigned int size);
~Array1D(void);
T& operator [] (unsigned int x);
unsigned int GetSize(void);
void Init(T initValue);
};
// Array1D
template <class T> Array1D<T>::Array1D(unsigned int size)/*{{{*/
{
array1D = new T[size];
this->size = size ;
}/*}}}*/
template <class T> Array1D<T>::~Array1D(void) /*{{{*/
{
delete [] array1D ;
array1D = NULL;
this->size = 0;
}/*}}}*/
template <class T> void Array1D<T>::Init(T initValue) /*{{{*/
{
unsigned int i;
for(i = 0; i < size; i ++)
{
array1D[i] = initValue;
}
}/*}}}*/
template <class T> unsigned int Array1D<T>::GetSize(void) { return size;}
template <class T> T& Array1D<T>::operator [] (unsigned int i) { return array1D[i]; }
//array 2D
// old-style, Create2DArray and Delete2DArray pair
template <class T> T **Create2DArray(T **array, unsigned int xSize, unsigned int ySize);
template <class T> void Delete2DArray(T **array, unsigned int xSize);
/*****************************************************************************
* Array1D, Array2D, Array3D, new style, will be deconstructed automatically
* when the procedure is over
****************************************************************************/
template <class T> class Array2D //accessed by .array2D
{
public:
T** array2D;
Array2D(unsigned int rowSize,unsigned int colSize);
~Array2D(void);
unsigned int GetRowSize(void);
unsigned int GetColSize(void);
void Init(T initValue = T(0));
private:
unsigned int rowSize;
unsigned int colSize;
};
//**********************************************************
//Create 2-dimensional array, which can be accessed by array[row][col]
//**********************************************************
template <class T> T **Create2DArray(T **array, unsigned int xSize, unsigned int ySize)/*{{{*/
{
unsigned int i ;
array = new T*[xSize] ;
for ( i = 0 ; i < xSize ; i ++ )
array[i] = new T[ySize] ;
return array ;
}/*}}}*/
template <class T> void Delete2DArray(T **array, unsigned int xSize)/*{{{*/
{
unsigned int i ;
for ( i = 0 ; i < xSize ; i ++ )
delete [] array[i] ;
delete [] array ;
}/*}}}*/
// Array2D Class,
template <class T> Array2D<T>::Array2D(unsigned int rowSize, unsigned int colSize)
{
this->rowSize = rowSize;
this->colSize = colSize;
unsigned int i ;
array2D = new T*[rowSize] ;
for ( i = 0 ; i < rowSize ; i ++ )
array2D[i] = new T[colSize] ;
}
// destroy the 2D array
template <class T> Array2D<T>::~Array2D(void)/*{{{*/
{
unsigned int i ;
for ( i = 0 ; i < rowSize ; i ++ )
delete [] array2D[i] ;
delete [] array2D ;
array2D = NULL;
this->rowSize = 0;
this->colSize = 0;
}/*}}}*/
template <class T> void Array2D<T>::Init(T initValue) /*{{{*/
{
unsigned int i,j;
for (i = 0 ; i < rowSize; i ++)
{
for(j = 0; j < colSize; j ++)
{
array2D[i][j] = initValue;
}
}
} /*}}}*/
template <class T> unsigned int Array2D<T>::GetRowSize(void) { return rowSize;}
template <class T> unsigned int Array2D<T>::GetColSize(void) { return colSize;}
//Array3D
// old-style, Create3DArray and Delete3DArray pair
template <class T> T*** Create3DArray(T ***array, unsigned int xSize, unsigned int ySize, unsigned int zSize);
template <class T> void Delete3DArray(T ***array, unsigned int xSize, unsigned int ySize);
template <class T> class Array3D //accessed by .array3D
{
public:
T*** array3D;
Array3D(unsigned int xSize,unsigned int ySize, unsigned int zSize);
~Array3D(void);
unsigned int GetXSize(void);
unsigned int GetYSize(void);
unsigned int GetZSize(void);
void Init(T initValue = T(0));
private:
unsigned int xSize;
unsigned int ySize;
unsigned int zSize;
};
//**********************************************************
//Create 3-dimensional array, which can be accessed by array[x][y][z]
//**********************************************************
template <class T> T*** Create3DArray(T ***array, unsigned int xSize, unsigned int ySize, unsigned int zSize)/*{{{*/
{
unsigned int i ;
array = new T**[xSize] ;
for ( i = 0 ; i < xSize ; i ++ )
array[i] = Create2DArray(array[i], ySize, zSize);
return array ;
}/*}}}*/
/*****************************************************************************
* Delete3DArray()
* free the memory allocated to 3d array
****************************************************************************/
template <class T> void Delete3DArray(T ***array, unsigned int xSize, unsigned int ySize)/*{{{*/
{
unsigned int i ;
for ( i = 0 ; i < xSize ; i ++ )
Delete2DArray(array[i], ySize) ;
delete [] array ;
}/*}}}*/
// Array3D
template <class T> Array3D<T>::Array3D(unsigned int xSize, unsigned int ySize, unsigned int zSize)/*{{{*/
{
this->xSize = xSize;
this->ySize = ySize;
this->zSize = zSize;
unsigned int i,j ;
array3D = new T**[xSize] ;
for ( i = 0 ; i < xSize ; i ++ )
{
array3D[i] = new T*[ySize] ;
for( j = 0 ; j < ySize ; j ++ )
{ array3D[i][j] = new T[zSize]; }
}
}/*}}}*/
template <class T> Array3D<T>::~Array3D(void)/*{{{*/
{
unsigned int i,j ;
for ( i = 0 ; i < xSize ; i ++ )
{
for( j = 0 ; j < ySize ; j ++ )
{ delete [] array3D[i][j] ; }
delete [] array3D[i];
}
delete [] array3D ;
array3D = NULL;
this->xSize = 0;
this->ySize = 0;
this->zSize = 0;
}/*}}}*/
template <class T> void Array3D<T>::Init(T initValue) /*{{{*/
{
unsigned int i,j,k ;
for ( i = 0 ; i < xSize ; i ++ )
{
for( j = 0 ; j < ySize ; j ++ )
{
for(k = 0 ; k < zSize; k++)
{ array3D[i][j][k] = initValue; }
}
}
} /*}}}*/
template <class T> unsigned int Array3D<T>::GetXSize(void) { return xSize;}
template <class T> unsigned int Array3D<T>::GetYSize(void) { return ySize;}
template <class T> unsigned int Array3D<T>::GetZSize(void) { return zSize;}
//array2d_sub
template <class T> class Array2D_Sub // can be accessed by [][]
// using [] operator, but much slower
{
protected:
unsigned int rowSize;
unsigned int colSize;
T* array;
public:
class Row
{
Array2D_Sub& array2D;
unsigned int const row;
public:
Row(Array2D_Sub& _array2D,unsigned int _row):
array2D (_array2D),row(_row) {}
T& operator [] (unsigned int col) const
{ return array2D.Select(row,col);}
};
Array2D_Sub(unsigned int rowSize,unsigned int colSize)
{
this->rowSize = rowSize;
this->colSize = colSize;
array = new T[rowSize*colSize];
}
~Array2D_Sub()
{
delete [] array;
this->rowSize = 0 ;
this->colSize = 0 ;
}
T& Select(unsigned int row, unsigned int col)
{
return array[row*colSize+col];
}
Row operator [] (unsigned int row) {return Row(*this,row);}
unsigned int GetRowSize(void) {return this->rowSize; }
unsigned int GetColSize(void) {return this->colSize; }
};
#endif /*HAS_ARRAY_H*/