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
00040 #ifndef MAT_MATRIXHIERARCHICBASE
00041 #define MAT_MATRIXHIERARCHICBASE
00042 #include "matInclude.h"
00043 #include "allocate.h"
00044 namespace mat{
00051 template<class Treal, class Telement = Treal>
00052 class MatrixHierarchicBase {
00053 public:
00054
00055 inline bool operator==(int k) const {
00056 if (k == 0)
00057 return this->is_zero();
00058 else
00059 throw Failure("Matrix::operator== only implemented for k == 0");
00060 }
00061
00062 #if 1
00063 inline const int& nScalarsRows() const
00064 {return rows.getNScalars();}
00065 inline const int& nScalarsCols() const
00066 {return cols.getNScalars();}
00067 #endif
00068
00069 inline const int& nrows() const
00070 {return rows.getNBlocks();}
00071 inline const int& ncols() const
00072 {return cols.getNBlocks();}
00073
00074 inline Telement& operator()
00075 (int row, int col) {
00076 assert(elements);
00077 assert(row >= 0);
00078 assert(col >= 0);
00079 assert(row < nrows());
00080 assert(col < ncols());
00081 return elements[row + col * nrows()];
00082 }
00083 inline const Telement& operator()
00084 (int row, int col) const {
00085 assert(elements);
00086 assert(row >= 0);
00087 assert(col >= 0);
00088 assert(row < nrows());
00089 assert(col < ncols());
00090 return elements[row + col * nrows()];
00091 }
00092
00093 inline Telement& operator[]
00094 (int index) {
00095 assert(elements);
00096 assert(index >= 0);
00097 assert(index < nElements());
00098 return elements[index];
00099 }
00100 inline Telement const & operator[]
00101 (int index) const {
00102 assert(elements);
00103 assert(index >= 0);
00104 assert(index < nElements());
00105 return elements[index];
00106 }
00107
00108 inline bool is_zero() const {return !elements;}
00109
00110 inline int nElements() const {
00111 return rows.getNBlocks() * cols.getNBlocks();
00112 }
00113
00114 inline void resetRows(SizesAndBlocks const & newRows) {
00115 freeElements(elements);
00116 elements = 0;
00117 rows = newRows;
00118 }
00119 inline void resetCols(SizesAndBlocks const & newCols) {
00120 freeElements(elements);
00121 elements = 0;
00122 cols = newCols;
00123 }
00124
00125 inline void getRows(SizesAndBlocks & rowsCopy) const {
00126 rowsCopy = rows;
00127 }
00128 inline void getCols(SizesAndBlocks & colsCopy) const {
00129 colsCopy = cols;
00130 }
00131
00132
00133 inline bool highestLevel() const {
00134 return (rows.getNTotalScalars() == rows.getNScalars() &&
00135 cols.getNTotalScalars() == cols.getNScalars());
00136 }
00137
00143 inline bool is_empty() const {
00144 return rows.is_empty() || cols.is_empty();
00145 }
00146 protected:
00147
00148
00149 MatrixHierarchicBase()
00150 : elements(0) {}
00151 MatrixHierarchicBase(SizesAndBlocks const & rowsInp,
00152 SizesAndBlocks const & colsInp)
00153 : rows(rowsInp), cols(colsInp), elements(0) {}
00154 MatrixHierarchicBase(const MatrixHierarchicBase<Treal, Telement>& mat);
00155
00156
00157 MatrixHierarchicBase<Treal, Telement>&
00158 operator=(const MatrixHierarchicBase<Treal, Telement>& mat);
00159
00160 static void swap(MatrixHierarchicBase<Treal, Telement>& A,
00161 MatrixHierarchicBase<Treal, Telement>& B);
00162
00163 virtual ~MatrixHierarchicBase();
00164 SizesAndBlocks rows;
00165 SizesAndBlocks cols;
00166 Telement* elements;
00167
00168
00169
00170 #if 0
00171 inline void assert_alloc() {
00172 if (this->cap < this->nel) {
00173 freeElements(this->elements);
00174 this->cap = this->nel;
00175 this->elements = allocateElements<Telement>(this->cap);
00176 for (int ind = 0; ind < this->cap; ind++)
00177 this->elements[ind] = 0;
00178 }
00179 }
00180 #endif
00181 private:
00182
00183 };
00184
00185
00186
00187 template<class Treal, class Telement>
00188 MatrixHierarchicBase<Treal, Telement>::
00189 MatrixHierarchicBase(const MatrixHierarchicBase<Treal, Telement>& mat)
00190 : rows(mat.rows), cols(mat.cols), elements(0) {
00191 if (!mat.is_zero()) {
00192 elements = allocateElements<Telement>(nElements());
00193 for (int i = 0; i < nElements(); i++)
00194 elements[i] = mat.elements[i];
00195 }
00196 }
00197
00198
00199 template<class Treal, class Telement>
00200 MatrixHierarchicBase<Treal, Telement>&
00201 MatrixHierarchicBase<Treal, Telement>::
00202 operator=(const MatrixHierarchicBase<Treal, Telement>& mat) {
00203 if (mat.is_zero()) {
00204 rows = mat.rows;
00205 cols = mat.cols;
00206 freeElements(elements);
00207 elements = 0;
00208 return *this;
00209 }
00210 if (is_zero() || (nElements() != mat.nElements())) {
00211 freeElements(elements);
00212 elements = allocateElements<Telement>(mat.nElements());
00213 }
00214 rows = mat.rows;
00215 cols = mat.cols;
00216 for (int i = 0; i < nElements(); i++)
00217 elements[i] = mat.elements[i];
00218 return *this;
00219 }
00220
00221 template<class Treal, class Telement>
00222 void MatrixHierarchicBase<Treal, Telement>::
00223 swap(MatrixHierarchicBase<Treal, Telement>& A,
00224 MatrixHierarchicBase<Treal, Telement>& B) {
00225 assert(A.rows == B.rows && A.cols == B.cols);
00226 Telement* elementsTmp = A.elements;
00227 A.elements = B.elements;
00228 B.elements = elementsTmp;
00229 }
00230
00231
00232 template<class Treal, class Telement>
00233 MatrixHierarchicBase<Treal, Telement>::~MatrixHierarchicBase() {
00234 freeElements(elements);
00235 elements = 0;
00236 }
00237
00238
00239 }
00240
00241 #endif