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