ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
array_pool.h
Go to the documentation of this file.
1#ifndef ARRAY_POOL_H
2#define ARRAY_POOL_H
3
4
5namespace ModuleBase
6{
14 template <typename T>
16 {
17 public:
18 Array_Pool() = default;
19 Array_Pool(const int nr_in, const int nc_in);
23 Array_Pool(const Array_Pool<T>& other) = delete;
24 Array_Pool& operator=(const Array_Pool& other) = delete;
25
26 T** get_ptr_2D() const { return this->ptr_2D; }
27 T* get_ptr_1D() const { return this->ptr_1D; }
28 int get_nr() const { return this->nr; }
29 int get_nc() const { return this->nc; }
30 T* operator[](const int ir) const { return this->ptr_2D[ir]; }
31 private:
32 T** ptr_2D = nullptr;
33 T* ptr_1D = nullptr;
34 int nr = 0;
35 int nc = 0;
36 };
37
38 template <typename T>
39 Array_Pool<T>::Array_Pool(const int nr_in, const int nc_in) // Attention: uninitialized
40 : nr(nr_in),
41 nc(nc_in)
42 {
43 this->ptr_1D = new T[nr * nc]();
44 this->ptr_2D = new T*[nr];
45 for (int ir = 0; ir < nr; ++ir)
46 this->ptr_2D[ir] = &this->ptr_1D[ir * nc];
47 }
48
49 template <typename T>
51 {
52 delete[] this->ptr_2D;
53 delete[] this->ptr_1D;
54 }
55
56 template <typename T>
58 : ptr_2D(other.ptr_2D),
59 ptr_1D(other.ptr_1D),
60 nr(other.nr),
61 nc(other.nc)
62 {
63 other.ptr_2D = nullptr;
64 other.ptr_1D = nullptr;
65 other.nr = 0;
66 other.nc = 0;
67 }
68
69 template <typename T>
71 {
72 if (this != &other)
73 {
74 delete[] this->ptr_2D;
75 delete[] this->ptr_1D;
76 this->ptr_2D = other.ptr_2D;
77 this->ptr_1D = other.ptr_1D;
78 this->nr = other.nr;
79 this->nc = other.nc;
80 other.ptr_2D = nullptr;
81 other.ptr_1D = nullptr;
82 other.nr = 0;
83 other.nc = 0;
84 }
85 return *this;
86 }
87
88}
89#endif
Array_Pool is a class designed for dynamically allocating a two-dimensional array with all its elemen...
Definition array_pool.h:16
T * operator[](const int ir) const
Definition array_pool.h:30
~Array_Pool()
Definition array_pool.h:50
T * ptr_1D
Definition array_pool.h:33
Array_Pool(const int nr_in, const int nc_in)
Definition array_pool.h:39
Array_Pool(const Array_Pool< T > &other)=delete
T * get_ptr_1D() const
Definition array_pool.h:27
int get_nc() const
Definition array_pool.h:29
int nc
Definition array_pool.h:35
T ** ptr_2D
Definition array_pool.h:32
Array_Pool & operator=(Array_Pool< T > &&other)
Definition array_pool.h:70
Array_Pool & operator=(const Array_Pool &other)=delete
int get_nr() const
Definition array_pool.h:28
Array_Pool(Array_Pool< T > &&other)
Definition array_pool.h:57
int nr
Definition array_pool.h:34
T ** get_ptr_2D() const
Definition array_pool.h:26
#define T
Definition exp.cpp:237
Definition array_pool.h:6