-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_1.cpp
More file actions
202 lines (194 loc) · 6.87 KB
/
project_1.cpp
File metadata and controls
202 lines (194 loc) · 6.87 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
//#include <conio.h>
#include <string>
#include "FHlist.h"
#include "FHvector.h"
using namespace std;
//------------------------------------------MatNode-----------------------
template <class Object>
class MatNode
{
protected:
int col;
public:
Object data;
MatNode(int cl = 0, Object dt = Object()) : col(cl), data(dt) {}
int getCol() const { return col; }
bool SetCol(int new_col) { col = new_col; return true; }
const Object & operator=(const Object &x) { return (data = x); }
};
//-----------------------------------------SparseMat-------------------------------
template <class Object>
class SparseMat {
protected:
typedef FHlist< MatNode<Object> > MatRow;
typedef FHvector<MatRow> MasterCol;
MasterCol rows;
Object defaultVal;
int rowSize, colSize;
const static int min_row_size = 1;
const static int min_col_size = 1;
public:
SparseMat(int, int, const Object &);
bool set(int, int, const Object &);
const Object & get(int, int) const;
void clear();
void showSubSquare(int, int);
~SparseMat();
};
template <class Object>
SparseMat<Object>::SparseMat(int set_row_size, int set_col_size, const Object & set_default) {
if (set_row_size >= min_row_size) {
rowSize = set_row_size;
rows.resize(rowSize);
}
else {
rowSize = min_row_size;
rows.resize(rowSize);
}
if (set_col_size >= min_col_size) {
colSize = set_col_size;
}
else {
colSize = min_col_size;
}
defaultVal = set_default;
}
template <class Object>
bool SparseMat<Object>::set(int row, int col, const Object & data_in) {
if (!(row >= rowSize || row < 0 || col >= colSize || col < 0)) { //boundary violations
typename MatRow::iterator list_iter = rows[row].begin();
if (rows[row].empty()) {
if (!(data_in == defaultVal)) {
rows[row].insert(list_iter, MatNode<Object>(col, data_in));
return true;
}
return true;
}
for (list_iter; list_iter != rows[row].end(); ++list_iter) {
if ((*list_iter).getCol() == col) {
if (data_in == defaultVal) {
rows[row].erase(list_iter);
return true;
}
else if (!(data_in == (*list_iter).data)) {
(*list_iter).data = data_in;
(*list_iter).SetCol(col);
return true;
}
return true;
}
}
if (data_in != defaultVal) {
rows[row].insert(list_iter, MatNode<Object>(col, data_in));
}
return true;
}
return false;
}
template <class Object>
const Object & SparseMat<Object>::get(int row, int col)const {
try {
if (!(row >= rowSize || row < 0 || col >= colSize || col < 0)) {
if (!(rows[row].empty())) {
typename MatRow::const_iterator list_iter = rows[row].begin();
for (list_iter; list_iter != rows[row].end(); ++list_iter) {
if ((*list_iter).getCol() == col) {
return (*list_iter).data;
}
}
}
return defaultVal;
}
throw string("Out of bounds");
}
catch (string out_of_bounds) {
cout << out_of_bounds << endl;
}
}
template <class Object>
void SparseMat<Object>::clear() {
for (int i = 0; i < rowSize; ++i) {
if (!(rows[i]).empty()) {
rows[i].clear();
}
}
}
template <class Object>
void SparseMat<Object>::showSubSquare(int start, int size) {
if (!(start >= rowSize || start >= colSize || start < 0)) { //check boundaries
for (int i = start; i < size; ++i) {
if (!rows[i].empty()) {
typename MatRow::iterator list_iter = rows[i].begin();
for (int k = start; k < size; ++k) {
if ((*list_iter).getCol() <= k) {
cout << (*list_iter).data << "\t";
++list_iter;
}
else {
cout << defaultVal << "\t";
}
}
}
else {
for (int j = start; j < size; ++j) {
cout << defaultVal << "\t";
}
}
cout << endl;
}
}
}
template <class Object>
SparseMat<Object>::~SparseMat() {}
//--------------------------------------------other--------------------------------------------------------
#define MAT_SIZE 100000
typedef SparseMat<float> SpMat;
//---------------------------------------------main--------------------------------------------------------
int main() {
SpMat mat(MAT_SIZE, MAT_SIZE, 0); // 100000 x 100000 filled with 0
// test mutators
mat.set(2, 5, 10);
mat.set(2, 5, 35); // should overwrite the 10
mat.set(3, 9, 21);
mat.set(MAT_SIZE, 1, 5); // should fail silently
mat.set(9, 9, mat.get(3, 9)); // should copy the 21 here
mat.set(4, 4, -9);
mat.set(4, 4, 0); // should remove the -9 node entirely
mat.set(MAT_SIZE - 1, MAT_SIZE - 1, 99);
// show top left 15x15
mat.showSubSquare(0, 15);
// show bottom right 15x15
mat.showSubSquare(MAT_SIZE - 15, 15);
//_getch();
return 0;
}
//-----------------------------------------------output from screen--------------------------------------------------
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 35 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 21 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 21 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
//----------------------------------------End of Submission----------------------------------------------------------