forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_wrapper.h
More file actions
92 lines (77 loc) · 2.11 KB
/
matrix_wrapper.h
File metadata and controls
92 lines (77 loc) · 2.11 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
//==========================================================
// AUTHOR : Peize Lin
// DATE : 2018-07-31
//==========================================================
#ifndef MATRIX_WRAPPER_TIANHE2
#ifndef MATRIX_WRAPPER_H
#define MATRIX_WRAPPER_H
#include "matrix.h"
#include <cstring>
// !!! Attention: c is very dangerous, may be changed by other class,
// e.g. changed the value, size!=nr*nc, deleted
namespace ModuleBase
{
class Matrix_Wrapper
{
public:
int nr;
int nc;
double *c;
bool flag_delete_c;
Matrix_Wrapper(): nr(0), nc(0), c(nullptr), flag_delete_c(false){}
Matrix_Wrapper( const matrix &m ): nr(m.nr), nc(m.nc), c(m.c), flag_delete_c(false){}
inline void create( const int nr_in, const int nc_in, const bool flag_zero );
inline matrix to_matrix();
~Matrix_Wrapper(){ if(flag_delete_c) delete[]c; }
Matrix_Wrapper( const Matrix_Wrapper &m )=delete;
Matrix_Wrapper( Matrix_Wrapper &m )=delete;
inline Matrix_Wrapper( Matrix_Wrapper &&m );
inline Matrix_Wrapper&operator=( const Matrix_Wrapper&m );
inline Matrix_Wrapper&operator=( Matrix_Wrapper&&m );
};
inline void Matrix_Wrapper::create( const int nr_in, const int nc_in, const bool flag_zero )
{
nr = nr_in; nc = nc_in;
if(flag_delete_c)
delete[] c;
c = new double[nr*nc];
flag_delete_c = true;
if(flag_zero)
memset( c, 0, sizeof(double)*nr*nc );
}
inline matrix Matrix_Wrapper::to_matrix()
{
assert( flag_delete_c==true );
flag_delete_c = false;
matrix m;
m.nr = nr; m.nc = nc;
m.c = c;
return m;
}
inline Matrix_Wrapper::Matrix_Wrapper( Matrix_Wrapper &&m )
:nr(m.nr),
nc(m.nc),
c(m.c),
flag_delete_c(m.flag_delete_c)
{
m.nr = m.nc = 0;
m.c = nullptr;
m.flag_delete_c = false;
}
inline Matrix_Wrapper& Matrix_Wrapper::operator=( const Matrix_Wrapper&m )
{
assert( !m.flag_delete_c );
nr = m.nr; nc = m.nc; c = m.c; flag_delete_c = m.flag_delete_c;
return *this;
}
inline Matrix_Wrapper& Matrix_Wrapper::operator=( Matrix_Wrapper&&m )
{
nr = m.nr; nc = m.nc; c = m.c; flag_delete_c = m.flag_delete_c;
m.nr = 0; m.nc = 0; m.c = nullptr; m.flag_delete_c = false;
return *this;
}
}
#endif
#else
#include "matrix_wrapper_tianhe2.h"
#endif