source: trunk/yat/utility/MatrixWeighted.h @ 1583

Last change on this file since 1583 was 1583, checked in by Peter, 14 years ago

removed layer of StrideIterator? for iterators having stride=1 all the time. Also corrected type of iterator returned (hm).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1#ifndef _theplu_yat_utility_matrix_weighted_
2#define _theplu_yat_utility_matrix_weighted_
3
4// $Id: MatrixWeighted.h 1583 2008-10-15 18:29:47Z peter $
5
6/*
7  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
8  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
10  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
11
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 3 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with yat. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28#include "DataWeight.h"
29#include "StrideIterator.h"
30
31#include <vector>
32
33namespace theplu {
34namespace yat {
35namespace utility {
36
37  class Matrix;
38
39  ///
40  /// @brief Weighted Matrix
41  ///
42  class MatrixWeighted
43  {
44  public:
45    /**
46       value_type is DataWeight
47
48       \since New in yat 0.5
49     */
50    typedef DataWeight value_type;
51
52    /**
53       reference type is DataWeight&
54     */
55    typedef DataWeight& reference;
56
57    /**
58       const_reference type is const DataWeight&
59     */
60    typedef const DataWeight& const_reference;
61
62    /**
63       Mutable iterator that iterates over all elements
64     */
65    typedef std::vector<DataWeight>::iterator iterator;
66
67    /**
68       Read-only iterator that iterates over all elements
69     */
70    typedef std::vector<DataWeight>::const_iterator const_iterator;
71
72    /**
73       Mutable iterator that iterates over one column
74     */
75    typedef StrideIterator<std::vector<DataWeight>::iterator> column_iterator;
76
77    /**
78       Read-only iterator that iterates over one column
79     */
80    typedef StrideIterator<std::vector<DataWeight>::const_iterator> 
81    const_column_iterator;
82
83    /**
84       Mutable iterator that iterates over one row
85     */
86    typedef std::vector<DataWeight>::iterator row_iterator;
87
88    /**
89       Read-only iterator that iterates over one row
90     */
91    typedef std::vector<DataWeight>::const_iterator const_row_iterator;
92
93    /**
94       @brief The default constructor.
95   
96       This contructor does not initialize underlying (essential)
97       structures.
98    */
99    MatrixWeighted(void);
100
101    /**
102       \brief Constructor allocating memory space for \a r times \a c
103       elements, and sets all elements to
104       DataWeight(\a init_value, \a init_weight)
105    */
106    MatrixWeighted(size_t r, size_t c, double init_value=0,
107                   double init_weight=1.0);
108
109    /**
110       \brief The copy constructor.
111    */
112    MatrixWeighted(const MatrixWeighted&);
113
114   
115    /**
116       \brief The istream constructor.
117
118       Data is copied from \a other and weights are calculated using
119       the nan function.
120    */
121    explicit MatrixWeighted(const Matrix& other);
122
123    /**
124       \brief The istream constructor.
125
126       This constructor first creates a Matrix using the istream
127       constructor, and then constructs MatrixWeighted using
128       MatrixWeighted(const Matrix&).
129    */
130    explicit MatrixWeighted(std::istream &, char sep='\0'); 
131
132    /**
133       Iterator iterates along a row. When end of row is reached it
134       jumps to beginning of next row.
135
136       \return iterator pointing to upper-left element.
137     */
138    iterator begin(void);
139
140    /**
141       Iterator iterates along a row. When end of row is reached it
142       jumps to beginning of next row.
143
144       \return const_iterator pointing to upper-left element.
145     */
146    const_iterator begin(void) const;
147
148    /**
149       Iterator iterates along a column.
150
151       \return iterator pointing to first element of column \a i.
152     */
153    column_iterator begin_column(size_t i);
154
155    /**
156       Iterator iterates along a column.
157
158       \return const_iterator pointing to first element of column \a i.
159     */
160    const_column_iterator begin_column(size_t i) const;
161
162    /**
163       Iterator iterates along a row.
164
165       \return iterator pointing to first element of row \a i.
166     */
167    row_iterator begin_row(size_t i);
168
169    /**
170       Iterator iterates along a row.
171
172       \return const_iterator pointing to first element of row \a i.
173     */
174    const_row_iterator begin_row(size_t i) const;
175
176    /**
177       \return The number of columns in the matrix.
178    */
179    size_t columns(void) const;
180
181    /**
182       \return iterator pointing to end of matrix
183     */
184    iterator end(void);
185
186    /**
187       \return const_iterator pointing to end of matrix
188     */
189    const_iterator end(void) const;
190
191    /**
192       \return iterator pointing to end of column \a i
193     */
194    column_iterator end_column(size_t i);
195
196    /**
197       \return const_iterator pointing to end of column \a i
198     */
199    const_column_iterator end_column(size_t i) const;
200
201    /**
202       \return iterator pointing to end of row \a i
203     */
204    row_iterator end_row(size_t i);
205
206    /**
207       \return const_iterator pointing to end of row \a i
208     */
209    const_row_iterator end_row(size_t i) const;
210
211    /**
212       \brief Resize Matrix
213
214       \note this function may invalidate iterators.
215    */
216    void resize(size_t, size_t);
217
218    /**
219       \return The number of rows in the matrix.
220    */
221    size_t rows(void) const;
222
223    /**
224       \brief Element access operator.
225
226       \return Reference to the element position (\a row, \a column).
227    */
228    DataWeight& operator()(size_t row,size_t column);
229
230    /**
231       \brief Element access operator.
232
233       \return Const reference to the element position (\a row, \a
234       column).
235    */
236    const DataWeight& operator()(size_t row,size_t column) const;
237
238
239    /**
240       \brief The assignment operator.
241
242       \return A const reference to the resulting Matrix.
243    */
244    const MatrixWeighted& operator=(const MatrixWeighted& other);
245
246  private:
247    void copy(const Matrix&);
248    void copy(const Matrix& data, const Matrix& weight);
249
250    std::vector<DataWeight> vec_;
251    size_t columns_;
252
253  };
254
255  /**
256     \brief Exchange all elements.
257
258     Takes constant time.
259
260     \throw std::runtime_error if the two matrices disagree in size.
261  */
262  void swap(MatrixWeighted&, MatrixWeighted&);
263
264}}} // of namespace utility, yat, and theplu
265
266#endif
Note: See TracBrowser for help on using the repository browser.