-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMatrix.ipp
156 lines (140 loc) · 4.31 KB
/
SMatrix.ipp
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
151
152
153
154
155
156
#ifdef __SMatrix__
template <typename __matrix_elem_type>
void SMatrix<__matrix_elem_type>::assign(const IMatrix<__matrix_elem_type>& r) {
for (size_t i = this->row - 1; i != (size_t)-1; --i) {
for (size_t j = this->col - 1; j != (size_t)-1; --j) {
if (auto v = r.get(i, j)) set(i, j, v);
//set(i, j, r.get(i, j));
}
}
}
template <typename __matrix_elem_type>
void SMatrix<__matrix_elem_type>::clear() {
node* n = head;
while (n) {
node* tmp = n->next;
delete n;
n = tmp;
}
head = nullptr;
}
template <typename __matrix_elem_type>
void SMatrix<__matrix_elem_type>::re_alloc(size_t r, size_t c) {
clear();
this->row = r, this->col = c;
}
template <typename __matrix_elem_type>
SMatrix<__matrix_elem_type>::SMatrix(size_t r, size_t c) {
this->row = r, this->col = c;
head = nullptr;
}
template <typename __matrix_elem_type>
SMatrix<__matrix_elem_type>::SMatrix(SMatrix<__matrix_elem_type>&& r) noexcept : IMatrix<__matrix_elem_type>(r) {
head = r.head;
r.head = nullptr;
}
template <typename __matrix_elem_type>
SMatrix<__matrix_elem_type>::SMatrix(const SMatrix<__matrix_elem_type>& r) : IMatrix<__matrix_elem_type>(r) {
assign(r);
}
template <typename __matrix_elem_type>
SMatrix<__matrix_elem_type>::SMatrix(const IMatrix<__matrix_elem_type>& r) : IMatrix<__matrix_elem_type>(r) {
assign(r);
}
template <typename __matrix_elem_type>
IMatrix<__matrix_elem_type>& SMatrix<__matrix_elem_type>::operator=(SMatrix<__matrix_elem_type>&& r) noexcept {
if (this == &r) return *this;
head = r.head;
r.head = nullptr;
return *this;
}
template <typename __matrix_elem_type>
IMatrix<__matrix_elem_type>& SMatrix<__matrix_elem_type>::operator=(const SMatrix<__matrix_elem_type>& r) {
if (this == &r) return *this;
clear();
assign(r);
return *this;
}
template <typename __matrix_elem_type>
IMatrix<__matrix_elem_type>& SMatrix<__matrix_elem_type>::operator=(const IMatrix<__matrix_elem_type>& r) {
if (this == &r) return *this;
clear();
assign(r);
return *this;
}
template <typename __matrix_elem_type>
SMatrix<__matrix_elem_type>::~SMatrix() {
clear();
}
template <typename __matrix_elem_type>
void SMatrix<__matrix_elem_type>::set(size_t r, size_t c, __matrix_elem_type value, void(*f)(void)) {
if (r > this->row || c > this->col) f();
if (!head) {
if (!value) return;
head = new node;
head->r = r; head->c = c; head->value = value; head->next = nullptr;
return;
}
if (head->r == r && head->c == c) {
if (!value) {
node* del = head;
head = head->next;
delete del;
return;
}
head->value = value;
return;
}
if (head->r > r || head->r == r && head->c > c) {
if (!value) return;
node* tmp = new node;
tmp->r = r; tmp->c = c; tmp->value = value; tmp->next = head;
//*tmp = { r, c, value, head };
head = tmp;
return;
}
node* cur;
for (cur = head; cur->next; cur = cur->next) {
if (cur->next->r == r && cur->next->c == c) {
if (!value) {
node* del = cur->next;
cur->next = cur->next->next;
delete del;
return;
}
cur->next->value = value;
return;
}
if (!cur->next || cur->next->r > r || cur->next->r == r && cur->next->c > c) {
if (!value) return;
node* tmp = new node;
tmp->r = r; tmp->c = c; tmp->value = value;
tmp->next = cur->next; cur->next = tmp;
return;
}
}
cur->next = new node;
cur->next->r = r; cur->next->c = c; cur->next->value = value; cur->next->next = nullptr;
}
template <typename __matrix_elem_type>
__matrix_elem_type SMatrix<__matrix_elem_type>::get(size_t r, size_t c, __matrix_elem_type(*def)(void)) const {
if (r > this->row || c > this->col) return def();
for (node* cur = head; cur; cur = cur->next) {
if (cur->r == r && cur->c == c) return cur->value;
if (cur->r > r || cur->r == r && cur->c > c) return __matrix_elem_type();
}
return __matrix_elem_type();
}
template <typename __matrix_elem_type>
SMatrix<__matrix_elem_type> SMatrix<__matrix_elem_type>::operator+(const IMatrix<__matrix_elem_type>& r) const {
SMatrix res(*this);
res += r;
return res;
}
template <typename __matrix_elem_type>
SMatrix<__matrix_elem_type> SMatrix<__matrix_elem_type>::operator*(const IMatrix<__matrix_elem_type>& r) const {
SMatrix res(*this);
res *= r;
return res;
}
#endif // __SMatrix__