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

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

Speeding up copying from Matrix to MatrixWeighted? using
BinaryWeight?. Moving BinaryWeight? from stl_utility.h to
utility.h. Improved docs in MatrixWeighted?.

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