-
Notifications
You must be signed in to change notification settings - Fork 0
/
std.cpp
150 lines (130 loc) · 3.71 KB
/
std.cpp
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
class Data
{
private: // for test
static size_t new_cnt, delete_cnt;
public:
int *entry; // the starting address of entries
size_t row, col; // the shape of entries
size_t ref_cnt; // the count of matrices referring to this Data
Data(size_t row, size_t col):
row(row), col(col), ref_cnt(0)
{
new_cnt++;
entry = new int[row * col]{};
}
~Data()
{
delete_cnt++;
delete[] entry;
}
static size_t check_cnt()
{ return new_cnt - delete_cnt; }
};
size_t Data::new_cnt = 0;
size_t Data::delete_cnt = 0;
class Matrix
{
public:
Data *data; // the ptr pointing to the entries
size_t start; // the starting index of ROI
size_t row, col; // the shape of ROI
Matrix():
data(nullptr), start(0), row(0), col(0) {}
~Matrix()
{
if (!data)
return;
if (!--data->ref_cnt)
delete data;
}
};
void print_matrix(Matrix &mat)
{
for (size_t r = 0; r < mat.row; r++)
{
size_t head = mat.start+r*mat.data->col;
for (size_t c = 0; c < mat.col; c++)
cout << mat.data->entry[head + c] << ' ';
cout << '\n';
}
cout << endl;
}
void unload_data(Matrix &mat)
{
if (!mat.data)
return;
if (!--mat.data->ref_cnt)
delete mat.data;
mat.data = nullptr;
mat.start = mat.row = mat.col = 0;
}
void create_data(Matrix &mat, size_t row, size_t col)
{
unload_data(mat);
mat.data = new Data(row, col);
mat.data->ref_cnt = 1;
mat.start = 0, mat.row = row, mat.col = col;
}
void load_data(Matrix &mat, Data *data, size_t start, size_t row, size_t col)
{
unload_data(mat);
mat.data = data;
data->ref_cnt++;
mat.start = start;
mat.row = row;
mat.col = col;
}
void shallow_copy(Matrix &dest, Matrix &src)
{
unload_data(dest);
dest = src;
src.data->ref_cnt++;
}
void deep_copy(Matrix &dest, Matrix &src)
{
size_t row = src.row, col = src.col;
create_data(dest, row, col);
for (size_t r = 0; r < row; r++)
memcpy(dest.data->entry + r * col, src.data->entry + src.start + r * src.data->col, col * sizeof(int));
}
bool equal_matrix(Matrix &a, Matrix &b)
{
if (a.row != b.row || a.col != b.col)
return false;
size_t row = a.row, col = b.col;
for (size_t r = 0; r < row; r++)
for (size_t c = 0; c < col; c++)
if (a.data->entry[a.start + r * a.data->col + c] != b.data->entry[b.start + r * b.data->col + c])
return false;
return true;
}
void add_matrix(Matrix &dest, Matrix &a, Matrix &b)
{
size_t row = a.row, col = b.col;
create_data(dest, row, col);
for (size_t r = 0; r < row; r++)
for (size_t c = 0; c < col; c++)
dest.data->entry[r * col + c] = a.data->entry[a.start + r * a.data->col + c] + b.data->entry[b.start + r * b.data->col + c];
}
void minus_matrix(Matrix &dest, Matrix &a, Matrix &b)
{
size_t row = a.row, col = b.col;
create_data(dest, row, col);
for (size_t r = 0; r < row; r++)
for (size_t c = 0; c < col; c++)
dest.data->entry[r * col + c] = a.data->entry[a.start + r * a.data->col + c] - b.data->entry[b.start + r * b.data->col + c];
}
void multiply_matrix(Matrix &dest, Matrix &a, Matrix &b)
{
size_t row = a.row, col = b.col, K = a.col;
create_data(dest, row, col);
for (size_t i = 0; i < row; i++)
for (size_t j = 0; j < col; j++)
for (size_t k = 0; k < K; k++)
dest.data->entry[i * col + j] += a.data->entry[a.start + i * a.data->col + k] * b.data->entry[b.start + k * b.data->col + j];
}