forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintarray.h
More file actions
260 lines (245 loc) · 7.15 KB
/
intarray.h
File metadata and controls
260 lines (245 loc) · 7.15 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
#ifndef INTARRAY_H
#define INTARRAY_H
#include <cassert>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <stdexcept>
namespace ModuleBase
{
/**
* @brief Integer array
*
*/
class IntArray
{
public:
int * ptr = nullptr;
/**
* @brief Construct a new Int Array object
*
* @param d1 The first dimension size
* @param d2 The second dimension size
*/
IntArray(const int d1 = 1, const int d2 = 1);
IntArray(const int d1, const int d2, const int d3);
IntArray(const int d1, const int d2, const int d3, const int d4);
IntArray(const int d1, const int d2, const int d3, const int d4, const int d5);
IntArray(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6);
// Copy constructor
IntArray(const IntArray& other);
// Move constructor
IntArray(IntArray&& other) noexcept;
~IntArray();
/**
* @brief Create integer arrays
*
* @param[in] d1
* @param[in] d2
*/
void create(const int d1, const int d2);
void create(const int d1, const int d2, const int d3);
void create(const int d1, const int d2, const int d3, const int d4);
void create(const int d1, const int d2, const int d3, const int d4, const int d5);
void create(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6);
/**
* @brief copy assignment
*
* @param right
* @return const IntArray&
*/
IntArray &operator=(const IntArray &other)
{
if(this != &other)
{
delete[] ptr;
size = other.size;
dim = other.dim;
bound1 = other.bound1;
bound2 = other.bound2;
bound3 = other.bound3;
bound4 = other.bound4;
bound5 = other.bound5;
bound6 = other.bound6;
try
{
ptr = new int[size];
for (int i = 0;i < size;i++)
{
ptr[i] = other.ptr[i];
}
}
catch (const std::bad_alloc& e)
{
std::cerr << "Allocation error in IntArray copy assignment: " << e.what() << std::endl;
ptr = nullptr;
size = 0;
throw;
}
}
return *this;
}
// Move assignment operator
IntArray& operator=(IntArray&& other) noexcept;
/**
* @brief Equal all elements of an IntArray to an
* integer
*
* @param right
* @return const IntArray&
*/
const IntArray &operator=(const int &right)
{
if (ptr != nullptr && size > 0) {
for (int i = 0;i < size;i++)
{
ptr[i] = right;
}
}
return *this;// enables x = y = z;
}
/**
* @brief Access elements by using operator "()"
*
* @param d1
* @param d2
* @return int&
*/
int &operator()(const int d1, const int d2)
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
return ptr[ d1 * bound2 + d2 ];
}
int &operator()(const int d1, const int d2, const int d3)
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
assert( d3 >= 0 && d3 < bound3 );
return ptr[ (d1 * bound2 + d2) * bound3 + d3 ];
}
int &operator()(const int d1, const int d2, const int d3, const int d4)
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
assert( d3 >= 0 && d3 < bound3 );
assert( d4 >= 0 && d4 < bound4 );
return ptr[ ((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4 ];
}
int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5)
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
assert( d3 >= 0 && d3 < bound3 );
assert( d4 >= 0 && d4 < bound4 );
assert( d5 >= 0 && d5 < bound5 );
return ptr[ (((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5 ];
}
int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6)
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
assert( d3 >= 0 && d3 < bound3 );
assert( d4 >= 0 && d4 < bound4 );
assert( d5 >= 0 && d5 < bound5 );
assert( d6 >= 0 && d6 < bound6 );
return ptr[ ((((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5) * bound6 + d6 ];
}
/**
* @brief Access elements by using "()" through pointer
* without changing its elements
*
* @param d1
* @param d2
* @return const int&
*/
const int &operator()(const int d1, const int d2) const
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
return ptr[ d1 * bound2 + d2 ];
}
const int &operator()(const int d1, const int d2, const int d3) const
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
assert( d3 >= 0 && d3 < bound3 );
return ptr[ (d1 * bound2 + d2) * bound3 + d3 ];
}
const int &operator()(const int d1, const int d2, const int d3, const int d4) const
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
assert( d3 >= 0 && d3 < bound3 );
assert( d4 >= 0 && d4 < bound4 );
return ptr[ ((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4 ];
}
const int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5) const
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
assert( d3 >= 0 && d3 < bound3 );
assert( d4 >= 0 && d4 < bound4 );
assert( d5 >= 0 && d5 < bound5 );
return ptr[ (((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5 ];
}
const int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6) const
{
assert( d1 >= 0 && d1 < bound1 );
assert( d2 >= 0 && d2 < bound2 );
assert( d3 >= 0 && d3 < bound3 );
assert( d4 >= 0 && d4 < bound4 );
assert( d5 >= 0 && d5 < bound5 );
assert( d6 >= 0 && d6 < bound6 );
return ptr[ ((((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5) * bound6 + d6 ];
}
/**
* @brief Set all elements of an IntArray to zero
*
*/
void zero_out(void);
int getSize() const
{
return size;
}
int getDim() const
{
return dim;
}
int getBound1() const
{
return bound1;
}
int getBound2() const
{
return bound2;
}
int getBound3() const
{
return bound3;
}
int getBound4() const
{
return bound4;
}
int getBound5() const
{
return bound5;
}
int getBound6() const
{
return bound6;
}
private:
int size=0;
int dim=0;
int bound1=0;
int bound2=0;
int bound3=0;
int bound4=0;
int bound5=0;
int bound6=0;
void freemem();
};
} // namespace ModuleBase
#endif // IntArray class