-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix.h
136 lines (125 loc) · 3.22 KB
/
Matrix.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
#include <iostream>
#define INF 0x3f3f3f3f
class Matrix { // 下三角存储
int n, length;
double *mat;
public:
~Matrix();
Matrix(int);
Matrix(const Matrix &);
void setValue(int, int, double);
double getValue(int, int);
void merge(int, int, double, Matrix &);
void del(int);
void print();
void show(); // TODO temp
};
Matrix::~Matrix() {
n = 0 ,length = 0;
delete mat;
mat = NULL;
}
Matrix::Matrix(int n) {
this->n = n;
length = (n + 1) * n / 2;
mat = new double[length];
for (int i = 0; i < length; i++) mat[i] = INF;
}
Matrix::Matrix(const Matrix &m) {
this->n = m.n;
this->length = m.length;
this->mat = new double[length];
for (int i = 0; i < length; i++) this->mat[i] = m.mat[i];
}
void Matrix::setValue(int row, int col, double w) {
if ((row >= n || col >= n) || (row < 0 || col < 0)) {
std::cout << "Set Error!" << std::endl;
return;
}
if (row >= col)
mat[row * (row + 1) / 2 + col] = w;
else
mat[col * (col + 1) / 2 + row] = w;
}
double Matrix::getValue(int row, int col) {
if (row < col) {
int temp = row;
row = col;
col = temp;
}
return mat[row * (row + 1) / 2 + col];
}
void Matrix::merge(int v1, int v2, double w, Matrix &m) {
int i, begin, end, new_n = this->n + m.n, new_length;
new_length = this->length + m.length + this->n * m.n;
double *new_mat = new double[new_length];
std::copy(mat, mat + length, new_mat);
i = length;
for (int row = 0; row < m.n; row++) {
for (int j = 0; j < this->n; j++, i++) new_mat[i] = INF;
begin = row * (row + 1) / 2;
end = begin + row + 1;
std::copy(m.mat + begin, m.mat + end, new_mat + i);
i += end - begin;
}
delete mat;
v2 += n;
n = new_n;
mat = new_mat;
length = new_length;
setValue(v1, v2, w);
n = new_n;
}
void Matrix::del(int x) {
if ((x < 0 || x >= n) || mat == NULL) {
std::cout << "Delete Error!" << std::endl;
return;
}
int i = 0;
double *new_mat;
new_mat = new double[length - n];
for (int row = 0; row < n; row++) {
if (row == x) continue;
for (int col = 0; col < row + 1; col++) {
if (col == x) continue;
new_mat[i] = getValue(row, col);
i++;
}
}
delete mat;
mat = new_mat;
length = length - n;
n--;
}
void Matrix::print() {
if (n < 1) {
std::cout << "Empty Matrix!" << std::endl;
return;
}
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
double w = getValue(row, col);
if (w == INF)
std::cout << "* ";
else
std::cout << getValue(row, col) << " ";
}
std::cout << std::endl;
}
}
void Matrix::show() {
if (n < 1) {
std::cout << "Empty Matrix!" << std::endl;
return;
}
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
double w = getValue(row, col);
if (w == INF)
std::cout << "* ";
else
std::cout << 1 << " ";
}
std::cout << std::endl;
}
}