Go to the documentation of this file.00001
00002
00003
00004 #ifndef CRS_hh
00005 #define CRS_hh
00006
00007 #include <sys/types.h>
00008 #include <map>
00009 #include "toolbox/array.hh"
00010 #include "basics/debug.hh"
00011
00012 #define CRS_rowSorting_D 0
00013 #define CCS_rowSorting_D 0
00014
00015 namespace concepts {
00016
00017
00018
00025 template<class F>
00026 class CRSConvertable {
00027 public:
00029 virtual uint used() const = 0;
00034 virtual void convertCRS(F* a, int* asub, int* xa) const = 0;
00040 virtual void convertCCS(F* a, int* asub, int* xa) const = 0;
00041
00042 virtual ~CRSConvertable() {}
00043 };
00044
00045
00046
00053 template<typename F>
00054 void sparseLineToArrays(std::map<int, F>& line, F* a, int* asub) {
00055 uint nnz = 0;
00056 typename std::map<int, F>::const_iterator j = line.begin();
00057 for(; j != line.end(); ++j) {
00058 a[nnz] = j->second;
00059 asub[nnz++] = j->first;
00060 }
00061 }
00062
00063
00064
00078 template<class F>
00079 void convertCRS_rowSorting(F& m, typename F::value_type* a, int* asub,
00080 int* xa) {
00081 DEBUGL(CRS_rowSorting_D, "CRS format of " << m);
00082
00083 typedef typename F::value_type value;
00084
00085 int nnz = 0;
00086 uint r = 0;
00087 xa[0] = 0;
00088 std::map<int, value> row;
00089
00090 typename F::const_iterator i = m.begin();
00091 DEBUGL(CRS_rowSorting_D, "i = " << i);
00092 for(; i != m.end(); ++i) {
00093 DEBUGL((CRS_rowSorting_D && i != m.begin()), "i = " << i);
00094 if (i.row() > r) {
00095 sparseLineToArrays(row, a + nnz, asub + nnz);
00096 nnz += row.size();
00097 row.clear();
00098 for(; r < i.row();)
00099 xa[++r] = nnz;
00100 }
00101 row[i.col()] = *i;
00102 }
00103 sparseLineToArrays(row, a + nnz, asub + nnz);
00104 xa[r+1] = nnz + row.size();
00105 }
00106
00107
00108
00122 template<class F>
00123 void convertCCS_rowSorting(F& m, typename F::type* a, int* asub, int* xa) {
00124 DEBUGL(CCS_rowSorting_D, "CCS format of " << m);
00125
00126 typedef typename F::type value;
00127
00128 uint nnz = 0;
00129 uint n = m.nofCols();
00130
00131 for(uint c = 0; c < n; ++c)
00132 xa[c] = 0;
00133
00134
00135 for(typename F::const_iterator i = m.begin(); i != m.end(); ++i) {
00136 if (i.col() < n-1) ++xa[i.col()+1];
00137 ++nnz;
00138 }
00139
00140
00141 for(uint c = 2; c < n; ++c)
00142 xa[c] += xa[c-1];
00143 xa[n] = nnz;
00144
00145
00146 Array<int> idx(xa, n+1);
00147 DEBUGL(CCS_rowSorting_D, "col_ptr = " << idx);
00148 for(typename F::const_iterator i = m.begin(); i != m.end(); ++i) {
00149 int &j = idx[i.col()];
00150 a[j] = *i ;
00151 asub[j] = i.row();
00152 DEBUGL(CCS_rowSorting_D, "write (" << i.row() << ", " << i.col() <<
00153 ", " << *i << ") to idx " << j);
00154 ++j;
00155 }
00156 }
00157
00158
00159
00160 }
00161
00162 #endif // CRS_hh