Skip to content

Commit 0987680

Browse files
committed
optimized vector values access - thanks to @sergiosvieira [Closes #14]
This replaces calling std::vector::at() with direct operator[] access. It is somehow faster - maybe compiler-related optimization or lack of out-of-range checking when using operator[]. Out-of-range access is prevented thanks to validateCoordinates() method
1 parent 331f80e commit 0987680

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/SparseMatrix/SparseMatrix.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ T SparseMatrix<T>::get(int row, int col) const
123123

124124
int currCol;
125125

126-
for (int pos = this->rows->at(row - 1) - 1; pos < this->rows->at(row) - 1; pos++) {
127-
currCol = this->cols->at(pos);
126+
for (int pos = (*(this->rows))[row - 1] - 1; pos < (*(this->rows))[row] - 1; ++pos) {
127+
currCol = (*(this->cols))[pos];
128128

129129
if (currCol == col) {
130-
return this->vals->at(pos);
130+
return (*(this->vals))[pos];
131131

132132
} else if (currCol > col) {
133133
break;
@@ -143,11 +143,11 @@ SparseMatrix<T> & SparseMatrix<T>::set(T val, int row, int col)
143143
{
144144
this->validateCoordinates(row, col);
145145

146-
int pos = this->rows->at(row - 1) - 1;
146+
int pos = (*(this->rows))[row - 1] - 1;
147147
int currCol = 0;
148148

149-
for (; pos < this->rows->at(row) - 1; pos++) {
150-
currCol = this->cols->at(pos);
149+
for (; pos < (*(this->rows))[row] - 1; pos++) {
150+
currCol = (*(this->cols))[pos];
151151

152152
if (currCol >= col) {
153153
break;
@@ -163,7 +163,7 @@ SparseMatrix<T> & SparseMatrix<T>::set(T val, int row, int col)
163163
this->remove(pos, row);
164164

165165
} else {
166-
this->vals->at(pos) = val;
166+
(*(this->vals))[pos] = val;
167167
}
168168

169169
return *this;
@@ -184,8 +184,8 @@ vector<T> SparseMatrix<T>::multiply(const vector<T> & x) const
184184
if (this->vals != NULL) { // only if any value set
185185
for (int i = 0; i < this->m; i++) {
186186
T sum = T();
187-
for (int j = this->rows->at(i); j < this->rows->at(i + 1); j++) {
188-
sum = sum + this->vals->at(j - 1) * x[this->cols->at(j - 1) - 1];
187+
for (int j = (*(this->rows))[i]; j < (*(this->rows))[i + 1]; j++) {
188+
sum = sum + (*(this->vals))[j - 1] * x[(*(this->cols))[j - 1] - 1];
189189
}
190190

191191
result[i] = sum;
@@ -322,7 +322,7 @@ void SparseMatrix<T>::insert(int index, int row, int col, T val)
322322
}
323323

324324
for (int i = row; i <= this->m; i++) {
325-
this->rows->at(i) = this->rows->at(i) + 1;
325+
(*(this->rows))[i] = (*(this->rows))[i] + 1;
326326
}
327327
}
328328

@@ -334,7 +334,7 @@ void SparseMatrix<T>::remove(int index, int row)
334334
this->cols->erase(this->cols->begin() + index);
335335

336336
for (int i = row; i <= this->m; i++) {
337-
this->rows->at(i) = this->rows->at(i) - 1;
337+
(*(this->rows))[i] = (*(this->rows))[i] - 1;
338338
}
339339
}
340340

0 commit comments

Comments
 (0)