blitz Version 0.9
|
00001 /*************************************************************************** 00002 * blitz/matgen.h Declarations for RowMajor and ColumnMajor matrices 00003 * 00004 * $Id: matgen.h,v 1.4 2003/12/11 03:44:22 julianc Exp $ 00005 * 00006 * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU General Public License 00010 * as published by the Free Software Foundation; either version 2 00011 * of the License, or (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * Suggestions: blitz-dev@oonumerics.org 00019 * Bugs: blitz-bugs@oonumerics.org 00020 * 00021 * For more information, please see the Blitz++ Home Page: 00022 * http://oonumerics.org/blitz/ 00023 * 00024 ***************************************************************************/ 00025 00026 #ifndef BZ_MATGEN_H 00027 #define BZ_MATGEN_H 00028 00029 #ifndef BZ_MSTRUCT_H 00030 #error <blitz/matgen.h> must be included via <blitz/mstruct.h> 00031 #endif // BZ_MSTRUCT_H 00032 00033 BZ_NAMESPACE(blitz) 00034 00035 class GeneralMatrix : public AsymmetricMatrix { 00036 00037 public: 00038 GeneralMatrix() 00039 { } 00040 00041 GeneralMatrix(unsigned rows, unsigned cols) 00042 : AsymmetricMatrix(rows, cols) 00043 { 00044 } 00045 00046 unsigned firstInRow(unsigned i) const 00047 { return 0; } 00048 00049 unsigned lastInRow(unsigned i) const 00050 { return cols_ - 1; } 00051 00052 unsigned firstInCol(unsigned j) const 00053 { return 0; } 00054 00055 unsigned lastInCol(unsigned j) const 00056 { return rows_ - 1; } 00057 00058 unsigned numElements() const 00059 { return rows_ * cols_; } 00060 }; 00061 00062 class GeneralIterator { 00063 public: 00064 GeneralIterator(unsigned rows, unsigned cols) 00065 { 00066 rows_ = rows; 00067 cols_ = cols; 00068 i_ = 0; 00069 j_ = 0; 00070 offset_ = 0; 00071 good_ = true; 00072 } 00073 00074 unsigned offset() const { return offset_; } 00075 operator bool() const { return good_; } 00076 unsigned row() const { return i_; } 00077 unsigned col() const { return j_; } 00078 00079 protected: 00080 unsigned rows_, cols_; 00081 unsigned offset_; 00082 unsigned i_, j_; 00083 bool good_; 00084 }; 00085 00086 class RowMajorIterator : public GeneralIterator { 00087 public: 00088 RowMajorIterator(unsigned rows, unsigned cols) 00089 : GeneralIterator(rows, cols) 00090 { } 00091 00092 void operator++() 00093 { 00094 ++offset_; 00095 ++j_; 00096 if (j_ == cols_) 00097 { 00098 j_ = 0; 00099 ++i_; 00100 if (i_ == rows_) 00101 good_ = false; 00102 } 00103 } 00104 }; 00105 00106 class RowMajor : public GeneralMatrix { 00107 00108 public: 00109 typedef RowMajorIterator T_iterator; 00110 00111 RowMajor() 00112 { } 00113 00114 RowMajor(unsigned rows, unsigned cols) 00115 : GeneralMatrix(rows, cols) 00116 { } 00117 00118 unsigned coordToOffset(unsigned i, unsigned j) const 00119 { 00120 return i*cols_+j; 00121 } 00122 00123 template<typename T_numtype> 00124 T_numtype get(const T_numtype * restrict data, 00125 unsigned i, unsigned j) const 00126 { 00127 BZPRECONDITION(inRange(i,j)); 00128 return data[coordToOffset(i,j)]; 00129 } 00130 00131 template<typename T_numtype> 00132 T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j) 00133 { 00134 BZPRECONDITION(inRange(i,j)); 00135 return data[coordToOffset(i,j)]; 00136 } 00137 }; 00138 00139 class ColumnMajorIterator : public GeneralIterator { 00140 public: 00141 ColumnMajorIterator(unsigned rows, unsigned cols) 00142 : GeneralIterator(rows, cols) 00143 { 00144 } 00145 00146 void operator++() 00147 { 00148 ++offset_; 00149 ++i_; 00150 if (i_ == rows_) 00151 { 00152 i_ = 0; 00153 ++j_; 00154 if (j_ == cols_) 00155 good_ = false; 00156 } 00157 } 00158 }; 00159 00160 class ColumnMajor : public GeneralMatrix { 00161 00162 public: 00163 ColumnMajor() 00164 { } 00165 00166 ColumnMajor(unsigned rows, unsigned cols) 00167 : GeneralMatrix(rows, cols) 00168 { } 00169 00170 unsigned coordToOffset(unsigned i, unsigned j) const 00171 { 00172 return j*rows_ + i; 00173 } 00174 00175 template<typename T_numtype> 00176 T_numtype get(const T_numtype * restrict data, 00177 unsigned i, unsigned j) const 00178 { 00179 BZPRECONDITION(inRange(i,j)); 00180 return data[coordToOffset(i,j)]; 00181 } 00182 00183 template<typename T_numtype> 00184 T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j) 00185 { 00186 BZPRECONDITION(inRange(i,j)); 00187 return data[coordToOffset(i,j)]; 00188 } 00189 }; 00190 00191 BZ_NAMESPACE_END 00192 00193 #endif // BZ_MATGEN_H 00194