-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyType.h
213 lines (168 loc) · 5.61 KB
/
MyType.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
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
203
204
205
206
207
208
209
210
211
212
213
#ifndef SCARA_MYTYPE_H
#define SCARA_MYTYPE_H
#include <vector>
#include <cmath>
#include <iostream>
#include <cassert>
#include "BasicDefinition.h"
class MyQueue {
private:
const VertexIdType mask;
std::vector<VertexIdType> queue;
VertexIdType num = 0;
VertexIdType idx_front = 0;
VertexIdType idx_last_plus_one = 0;
private:
static inline VertexIdType compute_queue_size(const VertexIdType &_numOfVertices) {
return (1u) << (uint32_t) ceil(log2(_numOfVertices + 2u));
}
public:
explicit MyQueue(const VertexIdType &_numOfVertices) :
mask(compute_queue_size(_numOfVertices) - 1),
queue(mask + 2u, 0) {}
inline void clear() {
idx_front = 0;
idx_last_plus_one = 0;
num = 0;
}
inline const VertexIdType &size() const {
return num;
}
inline const VertexIdType &front() const {
return queue[idx_front];
}
inline void pop() {
--num;
++idx_front;
idx_front &= mask;
}
inline void push(const VertexIdType &_elem) {
++num;
queue[idx_last_plus_one] = _elem;
++idx_last_plus_one;
idx_last_plus_one &= mask;
}
inline bool empty() const {
return idx_last_plus_one == idx_front;
}
};
struct FwdPushStructure {
// reserve one slot for the dummy vertex
MyQueue active_vertices;
// reserve one slot for the dummy vertex
std::vector<bool> is_active;
explicit FwdPushStructure(const VertexIdType &numOfVertices) :
active_vertices(numOfVertices + 1),
is_active(numOfVertices + 1, false) {}
};
/*
* Matrix in vector of vectors allow for fast row assign. Directly assign by std::vector.swap is 2x faster
*/
class MyMatrix {
private:
std::vector<std::vector<PageRankScoreType>> data;
VertexIdType nrow = 0;
VertexIdType ncol = 0;
public:
explicit MyMatrix() {}
explicit MyMatrix(const VertexIdType &_nrows, const VertexIdType &_ncols) :
data(_nrows, std::vector<PageRankScoreType>(_ncols)),
nrow(_nrows),
ncol(_ncols) {
// std::cout << "Init Matrix of: " << nrow << " " << ncol << std::endl;
}
void allocate(const VertexIdType &_nrows, const VertexIdType &_ncols) {
// data.resize(_nrows, std::vector<PageRankScoreType>(_ncols, 0));
data = std::vector<std::vector<PageRankScoreType>>(_nrows, std::vector<PageRankScoreType>(_ncols));
nrow = _nrows;
ncol = _ncols;
// std::cout << "Allocate Matrix of: " << nrow << " " << ncol << std::endl;
}
std::vector<PageRankScoreType> &operator[] (const VertexIdType &row) {
return data[row];
}
const std::vector<PageRankScoreType> &operator[] (const VertexIdType &row) const {
return data[row];
}
inline const VertexIdType size() const { return nrow; }
inline const VertexIdType nrows() const { return nrow; }
inline const VertexIdType ncols() const { return ncol; }
inline bool empty() const {
return nrow == 0;
}
inline bool is_regular() const {
for (VertexIdType i = 0; i < nrow; ++i) {
if (data[i].size() != ncol) {
return false;
}
}
return true;
}
inline bool is_regular(const VertexIdType &row) const {
return data[row].size() == ncol;
}
inline void set_col(const VertexIdType &col, const std::vector<PageRankScoreType> &_data) {
assert(col < ncol);
assert(_data.size() == nrow);
for (VertexIdType i = 0; i < nrow; ++i) {
data[i][col] = _data[i];
}
}
inline void set_row(const VertexIdType &row, const std::vector<PageRankScoreType> &_data) {
data[row] = _data;
data[row].resize(ncol);
}
};
/*
* Matrix in 1D vector.
*/
class My2DVector {
private:
std::vector<PageRankScoreType> data;
VertexIdType nrow = 0;
VertexIdType ncol = 0;
friend class My2DVectorRow;
class My2DVectorRow {
private:
My2DVector &parent;
VertexIdType row;
public:
My2DVectorRow(My2DVector &_parent, const VertexIdType &_row) :
parent(_parent), row(_row) {}
PageRankScoreType &operator[] (const VertexIdType &col) {
return parent.data[row * parent.ncol + col];
}
const PageRankScoreType &operator[] (const VertexIdType &col) const {
return parent.data[row * parent.ncol + col];
}
std::vector<PageRankScoreType>::iterator begin() {
return parent.data.begin() + row * parent.ncol;
}
};
public:
explicit My2DVector() {}
explicit My2DVector(const VertexIdType &_nrows, const VertexIdType &_ncols) :
data(_nrows * _ncols),
nrow(_nrows),
ncol(_ncols) {
// std::cout << "Init 2DVector of: " << nrow << " " << ncol << std::endl;
}
void allocate(const VertexIdType &_nrows, const VertexIdType &_ncols) {
data.resize(_nrows * _ncols);
nrow = _nrows;
ncol = _ncols;
// std::cout << "Allocate 2DVector of: " << nrow << " " << ncol << std::endl;
}
My2DVectorRow operator[] (const VertexIdType &row) {
return My2DVectorRow(*this, row);
}
inline const VertexIdType size() const { return nrow * ncol; }
inline const VertexIdType nrows() const { return nrow; }
inline const VertexIdType ncols() const { return ncol; }
inline std::vector<PageRankScoreType>& get_data() { return data; }
inline const std::vector<PageRankScoreType>& get_data() const { return data; }
inline bool empty() const {
return data.empty();
}
};
#endif //SCARA_MYTYPE_H