source: trunk/yat/utility/MatrixWeighted.cc @ 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: 4.6 KB
Line 
1// $Id: MatrixWeighted.cc 1706 2009-01-08 21:36:27Z 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 "WeightIterator.h"
31
32#include <cassert>
33#include <vector>
34
35namespace theplu {
36namespace yat {
37namespace utility {
38
39
40  MatrixWeighted::MatrixWeighted(void)
41    : columns_(0)
42  {
43  }
44
45
46  MatrixWeighted::MatrixWeighted(size_t r, size_t c, double x, double w)
47    : vec_(std::vector<DataWeight>(r*c, DataWeight(x,w))), columns_(c)
48  {}
49
50 
51  MatrixWeighted::MatrixWeighted(const MatrixWeighted& other)
52    : vec_(other.vec_), columns_(other.columns_)
53  {}
54
55
56  MatrixWeighted::MatrixWeighted(const Matrix& data)
57  {
58    copy(data);
59  }
60
61 
62  MatrixWeighted::MatrixWeighted(std::istream& is, char sep)
63  {
64    Matrix data(is, sep);
65    copy(data);
66  }
67
68 
69  MatrixWeighted::iterator MatrixWeighted::begin(void)
70  {
71    return iterator(vec_.begin());
72  }
73
74
75  MatrixWeighted::const_iterator MatrixWeighted::begin(void) const
76  {
77    return const_iterator(vec_.begin());
78  }
79
80
81  MatrixWeighted::column_iterator MatrixWeighted::begin_column(size_t i)
82  {
83    return column_iterator(vec_.begin()+i, columns_);
84  }
85
86
87  MatrixWeighted::const_column_iterator
88  MatrixWeighted::begin_column(size_t i) const
89  {
90    return const_column_iterator(vec_.begin()+i, columns_);
91  }
92
93
94  MatrixWeighted::row_iterator MatrixWeighted::begin_row(size_t i)
95  {
96    return row_iterator(vec_.begin()+columns_*i);
97  }
98
99
100  MatrixWeighted::const_row_iterator MatrixWeighted::begin_row(size_t i) const
101  {
102    return const_row_iterator(vec_.begin()+columns_*i);
103  }
104
105
106  size_t MatrixWeighted::columns(void) const
107  {
108    return columns_;
109  }
110
111
112  void MatrixWeighted::copy(const Matrix& data)
113  {
114    Matrix weight;
115    nan(data, weight);
116    copy(data, weight);
117  }
118
119
120  void MatrixWeighted::copy(const Matrix& data, const Matrix& weight)
121  {
122    assert(data.rows()==weight.rows());
123    assert(data.columns()==weight.columns());
124    columns_ = data.columns();
125    resize(data.rows(), data.columns());
126    assert(rows()==data.rows());
127    assert(columns()==data.columns());
128    std::copy(data.begin(), data.end(), data_iterator(vec_.begin()));
129    std::copy(weight.begin(), weight.end(), weight_iterator(vec_.begin()));
130  }
131
132
133  MatrixWeighted::iterator MatrixWeighted::end(void)
134  {
135    return iterator(vec_.end());
136  }
137
138
139  MatrixWeighted::const_iterator MatrixWeighted::end(void) const
140  {
141    return const_iterator(vec_.end());
142  }
143
144
145  MatrixWeighted::column_iterator MatrixWeighted::end_column(size_t i)
146  {
147    return column_iterator(vec_.begin()+i+vec_.size(), columns_);
148  }
149
150
151  MatrixWeighted::const_column_iterator
152  MatrixWeighted::end_column(size_t i) const
153  {
154    return const_column_iterator(vec_.begin()+i+vec_.size(), columns_);
155  }
156
157
158  MatrixWeighted::row_iterator MatrixWeighted::end_row(size_t i)
159  {
160    return row_iterator(vec_.begin()+columns_*(i+1));
161  }
162
163
164  MatrixWeighted::const_row_iterator MatrixWeighted::end_row(size_t i) const
165  {
166    return const_row_iterator(vec_.begin()+columns_*(i+1));
167  }
168
169
170  void MatrixWeighted::swap(MatrixWeighted& other)
171  {
172    std::swap(vec_, other.vec_);
173    std::swap(columns_, other.columns_);
174  }
175
176
177  void MatrixWeighted::resize(size_t rows, size_t columns)
178  {
179    columns_ = columns;
180    vec_.resize(rows*columns);
181    assert(this->rows()==rows);
182    assert(this->columns()==columns);
183  }
184
185 
186  size_t MatrixWeighted::rows(void) const
187  {
188    if (vec_.size())
189      return vec_.size()/columns_;
190    return 0;
191  }
192
193
194  DataWeight& MatrixWeighted::operator()(size_t row, size_t column)
195  {
196    assert(column<columns_);
197    assert(row*column < vec_.size());
198    return vec_[row*columns_ + column];
199  }
200
201
202  const DataWeight& MatrixWeighted::operator()(size_t row, size_t column) const
203  {
204    assert(column<columns_);
205    assert(row*column < vec_.size());
206    return vec_[row*columns_ + column];
207  }
208
209
210  void swap(MatrixWeighted& lhs, MatrixWeighted& rhs)
211  {
212    lhs.swap(rhs);
213  }
214
215}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.