00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _DynamicSparseRowMatrix_H_
00032 #define _DynamicSparseRowMatrix_H_
00033
00034 #include <stdlib.h>
00035 #include "SparseRowMatrix.h"
00036 #include "error.h"
00037
00038 namespace FDDlib {
00039
00049 template <class T>
00050 class DynamicSparseRowMatrix : public SparseRowMatrix<T>
00051 {
00052 private:
00054 SparseRowEntry<T>* setHelper(int row, int col);
00055
00056 public:
00058 DynamicSparseRowMatrix();
00059
00061 DynamicSparseRowMatrix(int maxrows, int max_per_row);
00062
00064 DynamicSparseRowMatrix(const DynamicSparseRowMatrix<T>& ESR);
00065
00067 int set(int row, int col, T val);
00068
00078 int setEntriesPerRow(int newmax);
00079
00080 };
00081
00082
00083 template<class T>
00084 DynamicSparseRowMatrix<T>::DynamicSparseRowMatrix() :
00085 SparseRowMatrix<T>()
00086 {}
00087
00088 template<class T>
00089 DynamicSparseRowMatrix<T>::DynamicSparseRowMatrix(int maxrows,int max_per_row):
00090 SparseRowMatrix<T>(maxrows, max_per_row)
00091 {}
00092
00093 template<class T>
00094 DynamicSparseRowMatrix<T>::DynamicSparseRowMatrix(const
00095 DynamicSparseRowMatrix<T>& ESR) : SparseRowMatrix<T>(ESR)
00096 {}
00097
00098 template<class T>
00099 inline int DynamicSparseRowMatrix<T>::setEntriesPerRow(int newmax)
00100 {
00101 int i, j;
00102 const int oldmax = EntriesPerRow_;
00103 const int newlen = MaxRows_*newmax;
00104 const int arrlendiff = (newlen)-(MaxRows_*oldmax);
00105 int offset = 0;
00106 void* newaddr;
00107
00108 if (newmax <= oldmax)
00109 return ERR_ARGS;
00110
00111
00112 newaddr = realloc(Entries_, sizeof(SparseRowEntry<T>)*newlen);
00113
00114
00115 if (newaddr == NULL) return ERR_DATA;
00116 else Entries_ = (struct SparseRowEntry<T>*)newaddr;
00117
00118
00119
00120 for (i = (MaxRows_*oldmax)-1; i >= oldmax; i -= oldmax) {
00121 for (j = i; j > (i-oldmax); j--) {
00122 Entries_[j + (arrlendiff-(newmax-oldmax)) - offset] = Entries_[j];
00123 }
00124
00125
00126 offset += (newmax-oldmax);
00127 }
00128
00129 EntriesPerRow_ = newmax;
00130 return SUCCESS;
00131 }
00132
00133 template<class T>
00134 inline SparseRowEntry<T>* DynamicSparseRowMatrix<T>::setHelper(int row, int col)
00135 {
00136 int i;
00137 SparseRowEntry<T>* ep;
00138
00139 ep = getRowEntry(row);
00140 for (i = 0; i < NumCols_[row] && ep->column != col; i++)
00141 {
00142
00143
00144 ep++;
00145 }
00146
00147
00148
00149 return ep;
00150 }
00151
00152 template<class T>
00153 inline int DynamicSparseRowMatrix<T>::set(int row, int col, T val)
00154 {
00155 int i;
00156 SparseRowEntry<T>* ep;
00157 SparseRowEntry<T>* r;
00158
00159 ep = setHelper(row, col);
00160 r = getRowEntry(row);
00161
00162
00163 if (ep - r >= EntriesPerRow_) {
00164 if (setEntriesPerRow(EntriesPerRow_+1)) return ERR_DATA;
00165
00166
00167
00168
00169 ep = setHelper(row, col);
00170 r = getRowEntry(row);
00171
00172 }
00173
00174
00175 if (ep - r == NumCols_[row])
00176 (NumCols_[row])++;
00177
00178 ep->val = val;
00179 ep->column = col;
00180
00181 return SUCCESS;
00182
00183 }
00184
00185 }
00186
00187 #endif