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

Last change on this file since 1706 was 1706, checked in by Peter, 12 years ago

implementing swap function

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1#ifndef _theplu_yat_utility_matrix_weighted_
2#define _theplu_yat_utility_matrix_weighted_
3
4// $Id: MatrixWeighted.h 1706 2009-01-08 21:36:27Z 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  Copyright (C) 2009 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 "DataWeight.h"
30#include "StrideIterator.h"
31
32#include <vector>
33
34namespace theplu {
35namespace yat {
36namespace utility {
37
38  class Matrix;
39
40  ///
41  /// @brief Weighted Matrix
42  ///
43  class MatrixWeighted
44  {
45  public:
46    /**
47       value_type is DataWeight
48
49       \since New in yat 0.5
50     */
51    typedef DataWeight value_type;
52
53    /**
54       reference type is DataWeight&
55     */
56    typedef DataWeight& reference;
57
58    /**
59       const_reference type is const DataWeight&
60     */
61    typedef const DataWeight& const_reference;
62
63    /**
64       Mutable iterator that iterates over all elements
65     */
66    typedef std::vector<DataWeight>::iterator iterator;
67
68    /**
69       Read-only iterator that iterates over all elements
70     */
71    typedef std::vector<DataWeight>::const_iterator const_iterator;
72
73    /**
74       Mutable iterator that iterates over one column
75     */
76    typedef StrideIterator<std::vector<DataWeight>::iterator> column_iterator;
77
78    /**
79       Read-only iterator that iterates over one column
80     */
81    typedef StrideIterator<std::vector<DataWeight>::const_iterator> 
82    const_column_iterator;
83
84    /**
85       Mutable iterator that iterates over one row
86     */
87    typedef column_iterator row_iterator;
88
89    /**
90       Read-only iterator that iterates over one row
91     */
92    typedef const_column_iterator const_row_iterator;
93
94    /**
95       @brief The default constructor.
96   
97       This contructor does not initialize underlying (essential)
98       structures.
99    */
100    MatrixWeighted(void);
101
102    /**
103       \brief Constructor allocating memory space for \a r times \a c
104       elements, and sets all elements to
105       DataWeight(\a init_value, \a init_weight)
106    */
107    MatrixWeighted(size_t r, size_t c, double init_value=0,
108                   double init_weight=1.0);
109
110    /**
111       \brief The copy constructor.
112    */
113    MatrixWeighted(const MatrixWeighted&);
114
115   
116    /**
117       \brief constructor.
118
119       Data is copied from \a other and weights are calculated using
120       the nan function.
121    */
122    explicit MatrixWeighted(const Matrix& other);
123
124    /**
125       \brief The istream constructor.
126
127       This constructor first creates a Matrix using the istream
128       constructor, and then constructs MatrixWeighted using
129       MatrixWeighted(const Matrix&).
130    */
131    explicit MatrixWeighted(std::istream &, char sep='\0'); 
132
133    /**
134       Iterator iterates along a row. When end of row is reached it
135       jumps to beginning of next row.
136
137       \return iterator pointing to upper-left element.
138     */
139    iterator begin(void);
140
141    /**
142       Iterator iterates along a row. When end of row is reached it
143       jumps to beginning of next row.
144
145       \return const_iterator pointing to upper-left element.
146     */
147    const_iterator begin(void) const;
148
149    /**
150       Iterator iterates along a column.
151
152       \return iterator pointing to first element of column \a i.
153     */
154    column_iterator begin_column(size_t i);
155
156    /**
157       Iterator iterates along a column.
158
159       \return const_iterator pointing to first element of column \a i.
160     */
161    const_column_iterator begin_column(size_t i) const;
162
163    /**
164       Iterator iterates along a row.
165
166       \return iterator pointing to first element of row \a i.
167     */
168    row_iterator begin_row(size_t i);
169
170    /**
171       Iterator iterates along a row.
172
173       \return const_iterator pointing to first element of row \a i.
174     */
175    const_row_iterator begin_row(size_t i) const;
176
177    /**
178       \return The number of columns in the matrix.
179    */
180    size_t columns(void) const;
181
182    /**
183       \return iterator pointing to end of matrix
184     */
185    iterator end(void);
186
187    /**
188       \return const_iterator pointing to end of matrix
189     */
190    const_iterator end(void) const;
191
192    /**
193       \return iterator pointing to end of column \a i
194     */
195    column_iterator end_column(size_t i);
196
197    /**
198       \return const_iterator pointing to end of column \a i
199     */
200    const_column_iterator end_column(size_t i) const;
201
202    /**
203       \return iterator pointing to end of row \a i
204     */
205    row_iterator end_row(size_t i);
206
207    /**
208       \return const_iterator pointing to end of row \a i
209     */
210    const_row_iterator end_row(size_t i) const;
211
212    /**
213       \brief swap objects
214
215       Takes constant time. Invalidates iterators.
216     */
217    void swap(MatrixWeighted& other);
218
219    /**
220       \brief Resize Matrix
221
222       \note this function may invalidate iterators.
223    */
224    void resize(size_t, size_t);
225
226    /**
227       \return The number of rows in the matrix.
228    */
229    size_t rows(void) const;
230
231    /**
232       \brief Element access operator.
233
234       \return Reference to the element position (\a row, \a column).
235    */
236    DataWeight& operator()(size_t row,size_t column);
237
238    /**
239       \brief Element access operator.
240
241       \return Const reference to the element position (\a row, \a
242       column).
243    */
244    const DataWeight& operator()(size_t row,size_t column) const;
245
246
247    /**
248       \brief The assignment operator.
249
250       \return A const reference to the resulting Matrix.
251    */
252    const MatrixWeighted& operator=(const MatrixWeighted& other);
253
254  private:
255    void copy(const Matrix&);
256    void copy(const Matrix& data, const Matrix& weight);
257
258    std::vector<DataWeight> vec_;
259    size_t columns_;
260
261  };
262
263  /**
264     \brief Exchange all elements.
265
266     Takes constant time.
267
268     \see MatrixWeighted::swap(MatrixWeighted&)
269  */
270  void swap(MatrixWeighted&, MatrixWeighted&);
271
272}}} // of namespace utility, yat, and theplu
273
274#endif
Note: See TracBrowser for help on using the repository browser.