source: trunk/yat/utility/MatrixWeighted.cc @ 1866

Last change on this file since 1866 was 1866, checked in by Peter, 13 years ago

remove multiple assignment

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1// $Id: MatrixWeighted.cc 1866 2009-03-17 19:02:57Z peter $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include "MatrixWeighted.h"
27
28#include "DataIterator.h"
29#include "Matrix.h"
30#include "utility.h"
31#include "WeightIterator.h"
32
33#include <cassert>
34#include <vector>
35
36namespace theplu {
37namespace yat {
38namespace utility {
39
40
41  MatrixWeighted::MatrixWeighted(void)
42    : columns_(0)
43  {
44  }
45
46
47  MatrixWeighted::MatrixWeighted(size_t r, size_t c, double x, double w)
48    : vec_(std::vector<DataWeight>(r*c, DataWeight(x,w))), columns_(c)
49  {}
50
51 
52  MatrixWeighted::MatrixWeighted(const MatrixWeighted& other)
53    : vec_(other.vec_), columns_(other.columns_)
54  {}
55
56
57  MatrixWeighted::MatrixWeighted(const Matrix& data)
58  {
59    copy(data);
60  }
61
62 
63  MatrixWeighted::MatrixWeighted(std::istream& is, char sep)
64  {
65    Matrix data(is, sep);
66    copy(data);
67  }
68
69 
70  MatrixWeighted::iterator MatrixWeighted::begin(void)
71  {
72    return iterator(vec_.begin());
73  }
74
75
76  MatrixWeighted::const_iterator MatrixWeighted::begin(void) const
77  {
78    return const_iterator(vec_.begin());
79  }
80
81
82  MatrixWeighted::column_iterator MatrixWeighted::begin_column(size_t i)
83  {
84    return column_iterator(vec_.begin()+i, columns_);
85  }
86
87
88  MatrixWeighted::const_column_iterator
89  MatrixWeighted::begin_column(size_t i) const
90  {
91    return const_column_iterator(vec_.begin()+i, columns_);
92  }
93
94
95  MatrixWeighted::row_iterator MatrixWeighted::begin_row(size_t i)
96  {
97    return row_iterator(vec_.begin()+columns_*i);
98  }
99
100
101  MatrixWeighted::const_row_iterator MatrixWeighted::begin_row(size_t i) const
102  {
103    return const_row_iterator(vec_.begin()+columns_*i);
104  }
105
106
107  size_t MatrixWeighted::columns(void) const
108  {
109    return columns_;
110  }
111
112
113  void MatrixWeighted::copy(const Matrix& data)
114  {
115    resize(data.rows(), data.columns());
116    assert(rows()==data.rows());
117    assert(columns()==data.columns());
118    std::copy(data.begin(), data.end(), data_iterator(vec_.begin()));
119    binary_weight(data.begin(), data.end(), weight_iterator(vec_.begin()));
120  }
121
122
123  MatrixWeighted::iterator MatrixWeighted::end(void)
124  {
125    return iterator(vec_.end());
126  }
127
128
129  MatrixWeighted::const_iterator MatrixWeighted::end(void) const
130  {
131    return const_iterator(vec_.end());
132  }
133
134
135  MatrixWeighted::column_iterator MatrixWeighted::end_column(size_t i)
136  {
137    return column_iterator(vec_.begin()+i+vec_.size(), columns_);
138  }
139
140
141  MatrixWeighted::const_column_iterator
142  MatrixWeighted::end_column(size_t i) const
143  {
144    return const_column_iterator(vec_.begin()+i+vec_.size(), columns_);
145  }
146
147
148  MatrixWeighted::row_iterator MatrixWeighted::end_row(size_t i)
149  {
150    return row_iterator(vec_.begin()+columns_*(i+1));
151  }
152
153
154  MatrixWeighted::const_row_iterator MatrixWeighted::end_row(size_t i) const
155  {
156    return const_row_iterator(vec_.begin()+columns_*(i+1));
157  }
158
159
160  void MatrixWeighted::swap(MatrixWeighted& other)
161  {
162    std::swap(vec_, other.vec_);
163    std::swap(columns_, other.columns_);
164  }
165
166
167  void MatrixWeighted::resize(size_t rows, size_t columns)
168  {
169    columns_ = columns;
170    vec_.resize(rows*columns);
171    assert(this->rows()==rows);
172    assert(this->columns()==columns);
173  }
174
175 
176  size_t MatrixWeighted::rows(void) const
177  {
178    if (vec_.size())
179      return vec_.size()/columns_;
180    return 0;
181  }
182
183
184  DataWeight& MatrixWeighted::operator()(size_t row, size_t column)
185  {
186    assert(column<columns_);
187    assert(row*column < vec_.size());
188    return vec_[row*columns_ + column];
189  }
190
191
192  const DataWeight& MatrixWeighted::operator()(size_t row, size_t column) const
193  {
194    assert(column<columns_);
195    assert(row*column < vec_.size());
196    return vec_[row*columns_ + column];
197  }
198
199
200  void swap(MatrixWeighted& lhs, MatrixWeighted& rhs)
201  {
202    lhs.swap(rhs);
203  }
204
205}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.