| [658] | 1 | /***************************************************************************
 | 
|---|
 | 2 |  * blitz/matgen.h       Declarations for RowMajor and ColumnMajor matrices
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  * $Id: matgen.h,v 1.1.1.1 1999-11-26 16:37:03 ansari Exp $
 | 
|---|
 | 5 |  *
 | 
|---|
 | 6 |  * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
 | 
|---|
 | 7 |  *
 | 
|---|
 | 8 |  * This program is free software; you can redistribute it and/or
 | 
|---|
 | 9 |  * modify it under the terms of the GNU General Public License
 | 
|---|
 | 10 |  * as published by the Free Software Foundation; either version 2
 | 
|---|
 | 11 |  * of the License, or (at your option) any later version.
 | 
|---|
 | 12 |  *
 | 
|---|
 | 13 |  * This program is distributed in the hope that it will be useful,
 | 
|---|
 | 14 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 16 |  * GNU General Public License for more details.
 | 
|---|
 | 17 |  *
 | 
|---|
 | 18 |  * Suggestions:          blitz-suggest@cybervision.com
 | 
|---|
 | 19 |  * Bugs:                 blitz-bugs@cybervision.com
 | 
|---|
 | 20 |  *
 | 
|---|
 | 21 |  * For more information, please see the Blitz++ Home Page:
 | 
|---|
 | 22 |  *    http://seurat.uwaterloo.ca/blitz/
 | 
|---|
 | 23 |  *
 | 
|---|
 | 24 |  ***************************************************************************
 | 
|---|
 | 25 |  * $Log: not supported by cvs2svn $
 | 
|---|
 | 26 |  * Revision 1.1.1.1  1999/04/09  17:59:00  ansari
 | 
|---|
 | 27 |  * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
 | 
|---|
 | 28 |  *
 | 
|---|
 | 29 |  * Revision 1.3  1998/03/14 00:04:47  tveldhui
 | 
|---|
 | 30 |  * 0.2-alpha-05
 | 
|---|
 | 31 |  *
 | 
|---|
 | 32 |  * Revision 1.2  1997/01/24 14:42:00  tveldhui
 | 
|---|
 | 33 |  * Periodic RCS update
 | 
|---|
 | 34 |  *
 | 
|---|
 | 35 |  * Revision 1.1  1997/01/13 22:19:58  tveldhui
 | 
|---|
 | 36 |  * Periodic RCS update
 | 
|---|
 | 37 |  *
 | 
|---|
 | 38 |  *
 | 
|---|
 | 39 |  */
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | #ifndef BZ_MATGEN_H
 | 
|---|
 | 42 | #define BZ_MATGEN_H
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | #ifndef BZ_MSTRUCT_H
 | 
|---|
 | 45 |  #error <blitz/matgen.h> must be included via <blitz/mstruct.h>
 | 
|---|
 | 46 | #endif // BZ_MSTRUCT_H
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | BZ_NAMESPACE(blitz)
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | class GeneralMatrix : public AsymmetricMatrix {
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | public:
 | 
|---|
 | 53 |     GeneralMatrix()
 | 
|---|
 | 54 |     { }
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 |     GeneralMatrix(unsigned rows, unsigned cols)
 | 
|---|
 | 57 |         : AsymmetricMatrix(rows, cols)
 | 
|---|
 | 58 |     {
 | 
|---|
 | 59 |     }
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |     unsigned firstInRow(unsigned i) const
 | 
|---|
 | 62 |     { return 0; }
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |     unsigned lastInRow(unsigned i) const
 | 
|---|
 | 65 |     { return cols_ - 1; }
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |     unsigned firstInCol(unsigned j) const
 | 
|---|
 | 68 |     { return 0; }
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |     unsigned lastInCol(unsigned j) const
 | 
|---|
 | 71 |     { return rows_ - 1; }
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |     unsigned numElements() const
 | 
|---|
 | 74 |     { return rows_ * cols_; }
 | 
|---|
 | 75 | };
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | class GeneralIterator {
 | 
|---|
 | 78 | public:
 | 
|---|
 | 79 |     GeneralIterator(unsigned rows, unsigned cols)
 | 
|---|
 | 80 |     {
 | 
|---|
 | 81 |         rows_ = rows;
 | 
|---|
 | 82 |         cols_ = cols;
 | 
|---|
 | 83 |         i_ = 0;
 | 
|---|
 | 84 |         j_ = 0;
 | 
|---|
 | 85 |         offset_ = 0;
 | 
|---|
 | 86 |         good_ = true;
 | 
|---|
 | 87 |     }
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |     unsigned offset() const
 | 
|---|
 | 90 |     { return offset_; }
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |     operator _bz_bool() const
 | 
|---|
 | 93 |     { return good_; }
 | 
|---|
 | 94 |    
 | 
|---|
 | 95 |     unsigned row() const
 | 
|---|
 | 96 |     { return i_; }
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |     unsigned col() const
 | 
|---|
 | 99 |     { return j_; }
 | 
|---|
 | 100 |  
 | 
|---|
 | 101 | protected:
 | 
|---|
 | 102 |     unsigned rows_, cols_;
 | 
|---|
 | 103 |     unsigned offset_;
 | 
|---|
 | 104 |     unsigned i_, j_;
 | 
|---|
 | 105 |     _bz_bool good_;
 | 
|---|
 | 106 | };
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | class RowMajorIterator : public GeneralIterator {
 | 
|---|
 | 109 | public:
 | 
|---|
 | 110 |     RowMajorIterator(unsigned rows, unsigned cols)
 | 
|---|
 | 111 |         : GeneralIterator(rows, cols)
 | 
|---|
 | 112 |     { }
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 |     void operator++()
 | 
|---|
 | 115 |     {
 | 
|---|
 | 116 |         ++offset_;
 | 
|---|
 | 117 |         ++j_;
 | 
|---|
 | 118 |         if (j_ == cols_)
 | 
|---|
 | 119 |         {
 | 
|---|
 | 120 |             j_ = 0;
 | 
|---|
 | 121 |             ++i_;
 | 
|---|
 | 122 |             if (i_ == rows_)
 | 
|---|
 | 123 |                 good_ = false;
 | 
|---|
 | 124 |         }
 | 
|---|
 | 125 |     }
 | 
|---|
 | 126 | };
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 | class RowMajor : public GeneralMatrix {
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | public:
 | 
|---|
 | 131 |     typedef RowMajorIterator T_iterator;
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 |     RowMajor()
 | 
|---|
 | 134 |     { }
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 |     RowMajor(unsigned rows, unsigned cols)
 | 
|---|
 | 137 |         : GeneralMatrix(rows, cols)
 | 
|---|
 | 138 |     { }
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 |     unsigned coordToOffset(unsigned i, unsigned j) const
 | 
|---|
 | 141 |     {
 | 
|---|
 | 142 |         return i*cols_+j;
 | 
|---|
 | 143 |     }
 | 
|---|
 | 144 | 
 | 
|---|
 | 145 |     template<class T_numtype>
 | 
|---|
 | 146 |     T_numtype get(const T_numtype * _bz_restrict data,
 | 
|---|
 | 147 |         unsigned i, unsigned j) const
 | 
|---|
 | 148 |     {
 | 
|---|
 | 149 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
 | 150 |         return data[coordToOffset(i,j)];
 | 
|---|
 | 151 |     }
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 |     template<class T_numtype>
 | 
|---|
 | 154 |     T_numtype& get(T_numtype * _bz_restrict data, unsigned i, unsigned j)
 | 
|---|
 | 155 |     {
 | 
|---|
 | 156 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
 | 157 |         return data[coordToOffset(i,j)];
 | 
|---|
 | 158 |     }
 | 
|---|
 | 159 | };
 | 
|---|
 | 160 | 
 | 
|---|
 | 161 | class ColumnMajorIterator : public GeneralIterator {
 | 
|---|
 | 162 | public:
 | 
|---|
 | 163 |     ColumnMajorIterator(unsigned rows, unsigned cols)
 | 
|---|
 | 164 |         : GeneralIterator(rows, cols)
 | 
|---|
 | 165 |     {
 | 
|---|
 | 166 |     }
 | 
|---|
 | 167 | 
 | 
|---|
 | 168 |     void operator++()
 | 
|---|
 | 169 |     {
 | 
|---|
 | 170 |         ++offset_;
 | 
|---|
 | 171 |         ++i_;
 | 
|---|
 | 172 |         if (i_ == rows_)
 | 
|---|
 | 173 |         {
 | 
|---|
 | 174 |             i_ = 0;
 | 
|---|
 | 175 |             ++j_;
 | 
|---|
 | 176 |             if (j_ == cols_)
 | 
|---|
 | 177 |                 good_ = false;
 | 
|---|
 | 178 |         }
 | 
|---|
 | 179 |     }
 | 
|---|
 | 180 | };
 | 
|---|
 | 181 | 
 | 
|---|
 | 182 | class ColumnMajor : public GeneralMatrix {
 | 
|---|
 | 183 | 
 | 
|---|
 | 184 | public:
 | 
|---|
 | 185 |     ColumnMajor()
 | 
|---|
 | 186 |     { }
 | 
|---|
 | 187 | 
 | 
|---|
 | 188 |     ColumnMajor(unsigned rows, unsigned cols)
 | 
|---|
 | 189 |         : GeneralMatrix(rows, cols)
 | 
|---|
 | 190 |     { }
 | 
|---|
 | 191 | 
 | 
|---|
 | 192 |     unsigned coordToOffset(unsigned i, unsigned j) const
 | 
|---|
 | 193 |     {
 | 
|---|
 | 194 |         return j*rows_ + i;
 | 
|---|
 | 195 |     }
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 |     template<class T_numtype>
 | 
|---|
 | 198 |     T_numtype get(const T_numtype * _bz_restrict data,
 | 
|---|
 | 199 |         unsigned i, unsigned j) const
 | 
|---|
 | 200 |     {
 | 
|---|
 | 201 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
 | 202 |         return data[coordToOffset(i,j)];
 | 
|---|
 | 203 |     }
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 |     template<class T_numtype>
 | 
|---|
 | 206 |     T_numtype& get(T_numtype * _bz_restrict data, unsigned i, unsigned j)
 | 
|---|
 | 207 |     {
 | 
|---|
 | 208 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
 | 209 |         return data[coordToOffset(i,j)];
 | 
|---|
 | 210 |     }
 | 
|---|
 | 211 | };
 | 
|---|
 | 212 | 
 | 
|---|
 | 213 | BZ_NAMESPACE_END
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 | #endif // BZ_MATGEN_H
 | 
|---|
 | 216 | 
 | 
|---|