blitz  Version 0.9
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
matgen.h
Go to the documentation of this file.
1 /***************************************************************************
2  * blitz/matgen.h Declarations for RowMajor and ColumnMajor matrices
3  *
4  * $Id: matgen.h,v 1.4 2003/12/11 03:44:22 julianc Exp $
5  *
6  * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
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-dev@oonumerics.org
19  * Bugs: blitz-bugs@oonumerics.org
20  *
21  * For more information, please see the Blitz++ Home Page:
22  * http://oonumerics.org/blitz/
23  *
24  ***************************************************************************/
25 
26 #ifndef BZ_MATGEN_H
27 #define BZ_MATGEN_H
28 
29 #ifndef BZ_MSTRUCT_H
30  #error <blitz/matgen.h> must be included via <blitz/mstruct.h>
31 #endif // BZ_MSTRUCT_H
32 
33 BZ_NAMESPACE(blitz)
34 
36 
37 public:
39  { }
40 
41  GeneralMatrix(unsigned rows, unsigned cols)
42  : AsymmetricMatrix(rows, cols)
43  {
44  }
45 
46  unsigned firstInRow(unsigned i) const
47  { return 0; }
48 
49  unsigned lastInRow(unsigned i) const
50  { return cols_ - 1; }
51 
52  unsigned firstInCol(unsigned j) const
53  { return 0; }
54 
55  unsigned lastInCol(unsigned j) const
56  { return rows_ - 1; }
57 
58  unsigned numElements() const
59  { return rows_ * cols_; }
60 };
61 
63 public:
64  GeneralIterator(unsigned rows, unsigned cols)
65  {
66  rows_ = rows;
67  cols_ = cols;
68  i_ = 0;
69  j_ = 0;
70  offset_ = 0;
71  good_ = true;
72  }
73 
74  unsigned offset() const { return offset_; }
75  operator bool() const { return good_; }
76  unsigned row() const { return i_; }
77  unsigned col() const { return j_; }
78 
79 protected:
80  unsigned rows_, cols_;
81  unsigned offset_;
82  unsigned i_, j_;
83  bool good_;
84 };
85 
87 public:
88  RowMajorIterator(unsigned rows, unsigned cols)
89  : GeneralIterator(rows, cols)
90  { }
91 
92  void operator++()
93  {
94  ++offset_;
95  ++j_;
96  if (j_ == cols_)
97  {
98  j_ = 0;
99  ++i_;
100  if (i_ == rows_)
101  good_ = false;
102  }
103  }
104 };
105 
106 class RowMajor : public GeneralMatrix {
107 
108 public:
110 
112  { }
113 
114  RowMajor(unsigned rows, unsigned cols)
115  : GeneralMatrix(rows, cols)
116  { }
117 
118  unsigned coordToOffset(unsigned i, unsigned j) const
119  {
120  return i*cols_+j;
121  }
122 
123  template<typename T_numtype>
124  T_numtype get(const T_numtype * restrict data,
125  unsigned i, unsigned j) const
126  {
127  BZPRECONDITION(inRange(i,j));
128  return data[coordToOffset(i,j)];
129  }
130 
131  template<typename T_numtype>
132  T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j)
133  {
134  BZPRECONDITION(inRange(i,j));
135  return data[coordToOffset(i,j)];
136  }
137 };
138 
140 public:
141  ColumnMajorIterator(unsigned rows, unsigned cols)
142  : GeneralIterator(rows, cols)
143  {
144  }
145 
146  void operator++()
147  {
148  ++offset_;
149  ++i_;
150  if (i_ == rows_)
151  {
152  i_ = 0;
153  ++j_;
154  if (j_ == cols_)
155  good_ = false;
156  }
157  }
158 };
159 
160 class ColumnMajor : public GeneralMatrix {
161 
162 public:
164  { }
165 
166  ColumnMajor(unsigned rows, unsigned cols)
167  : GeneralMatrix(rows, cols)
168  { }
169 
170  unsigned coordToOffset(unsigned i, unsigned j) const
171  {
172  return j*rows_ + i;
173  }
174 
175  template<typename T_numtype>
176  T_numtype get(const T_numtype * restrict data,
177  unsigned i, unsigned j) const
178  {
179  BZPRECONDITION(inRange(i,j));
180  return data[coordToOffset(i,j)];
181  }
182 
183  template<typename T_numtype>
184  T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j)
185  {
186  BZPRECONDITION(inRange(i,j));
187  return data[coordToOffset(i,j)];
188  }
189 };
190 
192 
193 #endif // BZ_MATGEN_H
194