ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
intarray.h
Go to the documentation of this file.
1#ifndef INTARRAY_H
2#define INTARRAY_H
3
4#include <cassert>
5#include <fstream>
6#include <iomanip>
7#include <iostream>
8#include <stdexcept>
9
10namespace ModuleBase
11{
17{
18 public:
19 int * ptr = nullptr;
20
27 IntArray(const int d1 = 1, const int d2 = 1);
28 IntArray(const int d1, const int d2, const int d3);
29 IntArray(const int d1, const int d2, const int d3, const int d4);
30 IntArray(const int d1, const int d2, const int d3, const int d4, const int d5);
31 IntArray(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6);
32 // Copy constructor
33 IntArray(const IntArray& other);
34 // Move constructor
35 IntArray(IntArray&& other) noexcept;
36
37 ~IntArray();
38
45 void create(const int d1, const int d2);
46 void create(const int d1, const int d2, const int d3);
47 void create(const int d1, const int d2, const int d3, const int d4);
48 void create(const int d1, const int d2, const int d3, const int d4, const int d5);
49 void create(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6);
50
58 {
59 if(this != &other)
60 {
61 delete[] ptr;
62 size = other.size;
63 dim = other.dim;
64 bound1 = other.bound1;
65 bound2 = other.bound2;
66 bound3 = other.bound3;
67 bound4 = other.bound4;
68 bound5 = other.bound5;
69 bound6 = other.bound6;
70 try
71 {
72 ptr = new int[size];
73 for (int i = 0;i < size;i++)
74 {
75 ptr[i] = other.ptr[i];
76 }
77 }
78 catch (const std::bad_alloc& e)
79 {
80 std::cerr << "Allocation error in IntArray copy assignment: " << e.what() << std::endl;
81 ptr = nullptr;
82 size = 0;
83 throw;
84 }
85 }
86 return *this;
87 }
88
89 // Move assignment operator
90 IntArray& operator=(IntArray&& other) noexcept;
91
92
100 const IntArray &operator=(const int &right)
101 {
102 if (ptr != nullptr && size > 0) {
103 for (int i = 0;i < size;i++)
104 {
105 ptr[i] = right;
106 }
107 }
108 return *this;// enables x = y = z;
109 }
110
118 int &operator()(const int d1, const int d2)
119 {
120 assert( d1 >= 0 && d1 < bound1 );
121 assert( d2 >= 0 && d2 < bound2 );
122 return ptr[ d1 * bound2 + d2 ];
123 }
124 int &operator()(const int d1, const int d2, const int d3)
125 {
126 assert( d1 >= 0 && d1 < bound1 );
127 assert( d2 >= 0 && d2 < bound2 );
128 assert( d3 >= 0 && d3 < bound3 );
129 return ptr[ (d1 * bound2 + d2) * bound3 + d3 ];
130 }
131 int &operator()(const int d1, const int d2, const int d3, const int d4)
132 {
133 assert( d1 >= 0 && d1 < bound1 );
134 assert( d2 >= 0 && d2 < bound2 );
135 assert( d3 >= 0 && d3 < bound3 );
136 assert( d4 >= 0 && d4 < bound4 );
137 return ptr[ ((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4 ];
138 }
139 int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5)
140 {
141 assert( d1 >= 0 && d1 < bound1 );
142 assert( d2 >= 0 && d2 < bound2 );
143 assert( d3 >= 0 && d3 < bound3 );
144 assert( d4 >= 0 && d4 < bound4 );
145 assert( d5 >= 0 && d5 < bound5 );
146 return ptr[ (((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5 ];
147 }
148 int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6)
149 {
150 assert( d1 >= 0 && d1 < bound1 );
151 assert( d2 >= 0 && d2 < bound2 );
152 assert( d3 >= 0 && d3 < bound3 );
153 assert( d4 >= 0 && d4 < bound4 );
154 assert( d5 >= 0 && d5 < bound5 );
155 assert( d6 >= 0 && d6 < bound6 );
156 return ptr[ ((((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5) * bound6 + d6 ];
157 }
158
167 const int &operator()(const int d1, const int d2) const
168 {
169 assert( d1 >= 0 && d1 < bound1 );
170 assert( d2 >= 0 && d2 < bound2 );
171 return ptr[ d1 * bound2 + d2 ];
172 }
173 const int &operator()(const int d1, const int d2, const int d3) const
174 {
175 assert( d1 >= 0 && d1 < bound1 );
176 assert( d2 >= 0 && d2 < bound2 );
177 assert( d3 >= 0 && d3 < bound3 );
178 return ptr[ (d1 * bound2 + d2) * bound3 + d3 ];
179 }
180 const int &operator()(const int d1, const int d2, const int d3, const int d4) const
181 {
182 assert( d1 >= 0 && d1 < bound1 );
183 assert( d2 >= 0 && d2 < bound2 );
184 assert( d3 >= 0 && d3 < bound3 );
185 assert( d4 >= 0 && d4 < bound4 );
186 return ptr[ ((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4 ];
187 }
188 const int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5) const
189 {
190 assert( d1 >= 0 && d1 < bound1 );
191 assert( d2 >= 0 && d2 < bound2 );
192 assert( d3 >= 0 && d3 < bound3 );
193 assert( d4 >= 0 && d4 < bound4 );
194 assert( d5 >= 0 && d5 < bound5 );
195 return ptr[ (((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5 ];
196 }
197 const int &operator()(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6) const
198 {
199 assert( d1 >= 0 && d1 < bound1 );
200 assert( d2 >= 0 && d2 < bound2 );
201 assert( d3 >= 0 && d3 < bound3 );
202 assert( d4 >= 0 && d4 < bound4 );
203 assert( d5 >= 0 && d5 < bound5 );
204 assert( d6 >= 0 && d6 < bound6 );
205 return ptr[ ((((d1 * bound2 + d2) * bound3 + d3) * bound4 + d4) * bound5 + d5) * bound6 + d6 ];
206 }
207
212 void zero_out(void);
213
214 int getSize() const
215 {
216 return size;
217 }
218 int getDim() const
219 {
220 return dim;
221 }
222 int getBound1() const
223 {
224 return bound1;
225 }
226 int getBound2() const
227 {
228 return bound2;
229 }
230 int getBound3() const
231 {
232 return bound3;
233 }
234 int getBound4() const
235 {
236 return bound4;
237 }
238 int getBound5() const
239 {
240 return bound5;
241 }
242 int getBound6() const
243 {
244 return bound6;
245 }
246
247 private:
248 int size=0;
249 int dim=0;
250 int bound1=0;
251 int bound2=0;
252 int bound3=0;
253 int bound4=0;
254 int bound5=0;
255 int bound6=0;
256 void freemem();
257};
258} // namespace ModuleBase
259
260#endif // IntArray class
const std::complex< double > i
Definition cal_pLpR.cpp:46
Integer array.
Definition intarray.h:17
void freemem()
Definition intarray.cpp:191
int getBound5() const
Definition intarray.h:238
const IntArray & operator=(const int &right)
Equal all elements of an IntArray to an integer.
Definition intarray.h:100
int getBound6() const
Definition intarray.h:242
const int & operator()(const int d1, const int d2) const
Access elements by using "()" through pointer without changing its elements.
Definition intarray.h:167
int bound1
Definition intarray.h:250
~IntArray()
Definition intarray.cpp:186
const int & operator()(const int d1, const int d2, const int d3, const int d4) const
Definition intarray.h:180
int * ptr
Definition intarray.h:19
int dim
Definition intarray.h:249
int bound5
Definition intarray.h:254
void create(const int d1, const int d2)
Create integer arrays.
Definition intarray.cpp:307
int & operator()(const int d1, const int d2, const int d3, const int d4, const int d5)
Definition intarray.h:139
int getDim() const
Definition intarray.h:218
int getBound3() const
Definition intarray.h:230
int bound6
Definition intarray.h:255
int & operator()(const int d1, const int d2, const int d3, const int d4)
Definition intarray.h:131
int getBound4() const
Definition intarray.h:234
const int & operator()(const int d1, const int d2, const int d3) const
Definition intarray.h:173
int bound3
Definition intarray.h:252
int getSize() const
Definition intarray.h:214
const int & operator()(const int d1, const int d2, const int d3, const int d4, const int d5) const
Definition intarray.h:188
void zero_out(void)
Set all elements of an IntArray to zero.
Definition intarray.cpp:331
int getBound2() const
Definition intarray.h:226
IntArray & operator=(const IntArray &other)
copy assignment
Definition intarray.h:57
int & operator()(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6)
Definition intarray.h:148
int & operator()(const int d1, const int d2, const int d3)
Definition intarray.h:124
int getBound1() const
Definition intarray.h:222
int size
Definition intarray.h:248
int & operator()(const int d1, const int d2)
Access elements by using operator "()".
Definition intarray.h:118
int bound2
Definition intarray.h:251
int bound4
Definition intarray.h:253
const int & operator()(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6) const
Definition intarray.h:197
Definition clebsch_gordan_coeff.cpp:8