source: trunk/yat/utility/MatrixWeighted.h

Last change on this file was 3938, checked in by Peter, 13 months ago

let configure fail if compiler is not a C++11 compiler. YAT_HAVE_RVALUE and friends are no longer defined in 'config.h' and guards araound declarations and implementations are removed. refs #949

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