Skip to content
Snippets Groups Projects
Commit 122a8430 authored by eric's avatar eric
Browse files

added access operator

parent 775d88b0
No related branches found
No related tags found
No related merge requests found
#include <iostream> #include <iostream>
#include <numeric>
#include <vector> #include <vector>
template <class T> template <class T>
...@@ -11,9 +12,9 @@ public: ...@@ -11,9 +12,9 @@ public:
using size_type = std::size_t; using size_type = std::size_t;
private: private:
std::vector<value_type> values; std::vector<value_type> values_;
std::vector<size_type> indices; std::vector<size_type> indices_;
std::vector<size_type> offset; std::vector<size_type> offset_;
size_type rows_; size_type rows_;
size_type cols_; size_type cols_;
...@@ -24,9 +25,17 @@ public: ...@@ -24,9 +25,17 @@ public:
rows_ = rows; rows_ = rows;
cols_ = cols; cols_ = cols;
values = std::vector<value_type>(nzpr * cols, 0.0); values_ = std::vector<value_type>(nzpr * cols, 0.0);
indices = std::vector<size_type>(nzpr * cols, 0); indices_ = std::vector<size_type>(nzpr * cols, 0);
offset = std::vector<size_type>(rows + 1); offset_ = std::vector<size_type>(rows + 1);
}
CSR (std::vector<value_type> values, std::vector<size_type> indices, std::vector<size_type> offset) {
rows_ = offset.size() - 1;
cols_ = 1;
values_ = values;
indices_ = indices;
offset_ = offset;
} }
size_type rows () { size_type rows () {
return rows_; return rows_;
...@@ -35,21 +44,37 @@ public: ...@@ -35,21 +44,37 @@ public:
size_type cols () { size_type cols () {
return cols_; return cols_;
} }
value_type operator() (size_type const i, size_type const j){
size_type col_entries_before = std::accumulate(offset_.begin(), offset_.begin() + i, 0);
size_type col_entries = offset_[i];
auto first = indices_.begin() + sum;
auto last = first + col_entries;
auto low = std::lower_bound(first, last, j);
if (low != last && *low == j)
return *(values_.begin() + std::distance(indices_.begin(), low));
else
return 0.0;
}
void set (size_type row, size_type col, value_type val) { void set (size_type row, size_type col, value_type val) {
if ( values.back() != 0.0) if ( values_.back() != 0.0)
throw "value cannot be saved, already too many non zero values existing in this row"; throw "value cannot be saved, already too many non zero values existing in this row";
size_type start_index = offset[row]; size_type start_index = offset_[row];
size_type end_index; size_type end_index;
if (row < rows()){ if (row < rows()) {
end_index = offset[row + 1] - 1; end_index = offset_[row + 1] - 1;
} else { } else {
end_index = indices.size() - 2; end_index = indices_.size() - 2;
} }
size_type* start_row = &(values[start_index]); size_type* start_row = &(values_[start_index]);
size_type* end_row = &(values[end_index]); size_type* end_row = &(values_[end_index]);
auto low = std::lower_bound(start_row, end_row, val); auto low = std::lower_bound(start_row, end_row, val);
...@@ -61,7 +86,7 @@ public: ...@@ -61,7 +86,7 @@ public:
private: private:
// shifting all entries starting at position pos to the right; the last element is lost // shifting all entries starting at position pos to the right; the last element is lost
void shift_r(std::vector<value_type> &v, size_type pos) { void shift_r(std::vector<value_type> &v, size_type pos) {
for (int i = v.size() - 1; i > pos && i < v.size(); --i) { for (size_type i = v.size() - 1; i > pos && i < v.size(); --i) {
v[i] = v[i-1]; v[i] = v[i-1];
} }
v[pos] = 0.0; v[pos] = 0.0;
...@@ -109,9 +134,38 @@ int main () { ...@@ -109,9 +134,38 @@ int main () {
double* ptr = &(v[3]); double* ptr = &(v[3]);
std::cout << "v[4] = ptr + 1 = " << *(ptr + 1); std::cout << "v[4] = ptr + 1 = " << *(ptr + 1) << std::endl;
// start a sparse matrix
std::cout << "\n---> start a example..." << std::endl;
std::vector<double> values {1.0, 2.0, 5.0, 3.0, 4.0};
std::vector<size_type> indices {2, 0, 1, 1, 3};
std::vector<size_type> offset {1, 2, 2};
CSR<double> bsp(values, indices, offset);
std::vector<int> vec{3};
std::cout << "*vec.begin() = " << *(vec.begin()) << std::endl;
std::cout << "*vec.end() - 1 = " << *(vec.end() - 1) << std::endl;
std::cout << "*vec.begin() + 1 = " << *(vec.begin() + 1) << std::endl;
std::cout << "*offset.rbegin() = " << *(offset.rbegin() + 2) << std::endl;
std::cout << std::accumulate(offset.begin(), offset.begin() + 2, 0) << std::endl;
std::cout << "bsp(2,3) = " << bsp(2,3) << std::endl;
std::cout << "\n... fertig." << std::endl; std::cout << "\n... fertig." << std::endl;
return 1; return 1;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment