-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSR.cpp
137 lines (113 loc) · 2.65 KB
/
CSR.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
#pragma once
#include <vector>
// #include "utils.cpp"
using namespace std;
template <class T>
class CSRMat
{
public:
CSRMat(int cols)
{
rowIdx.resize(cols+ 1);
rowIdx[0] = 0;
};
vector<vector<double>> restore(int rows, int cols)
{
vector<vector<double>> restoredMat(rows, vector<double>(cols));
auto border = rowIdx[1];
int row = 0;
for(auto i = 0; i < dataSequence.size(); i++)
{
if(i >= border)
{
row++;
border = rowIdx[row + 1];
}
restoredMat[row][colIdx[i]] = dataSequence[i];
}
// showMat(restoredMat, rows, cols);
return restoredMat;
};
void insert(int row, int col, double data)
{
// rowIdx[row + 1] ++;
for(int i = row + 1; i < rowIdx.size(); i++)
{
rowIdx[i]++;
}
dataSequence.push_back(data);
colIdx.push_back(col);
}
CSRMat(vector<vector<T>> & data)
{
rowIdx.resize(data.size() + 1);
rowIdx[0] = 0;
for(int i = 0; i < data.size(); i++) // row
{
for(int j = 0; j < data[0].size(); j++) // col
{
if(data[i][j] != 0)
{
dataSequence.push_back(data[i][j]);
colIdx.push_back(j);
rowIdx[i + 1]++;
}
}
}
// rowIdxEnd = rowIdx;
for(int i = 2; i <= data.size(); i++)
{
rowIdx[i] += rowIdx[i - 1];
}
// cout<<"hi"<<endl;
};
void show()
{
cout<<"data>>>>"<<endl;
for(int i = 0; i < dataSequence.size(); i++)
{
cout<<dataSequence[i]<<" ";
}
cout<<endl;
cout<<"colIdx>>>>"<<endl;
for(int i = 0; i < colIdx.size(); i++)
{
cout<<colIdx[i]<<" ";
}
cout<<endl;
cout<<"rowIdx>>>>"<<endl;
for(int i = 0; i < rowIdx.size(); i++)
{
cout<<rowIdx[i]<<" ";
}
cout<<endl;
}
// int getRowIdxEnd(int row)
// {
// return rowIdxEnd[row];
// }
// int getRowIdx(int row)
// {
// return rowIdxEnd[row];
// }
// void updateRowIdxEnd(int row)
// {
// rowIdxEnd[row] -= 1;
// }
auto getData()
{
return dataSequence;
}
auto getColIdx()
{
return colIdx;
}
auto getRowIdx()
{
return rowIdx;
}
private:
vector<T> dataSequence;
vector<int> colIdx;
vector<int> rowIdx;
};