1 | // $Id: MatrixWeighted.cc 1789 2009-02-10 16:11:04Z 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 | |
---|
36 | namespace theplu { |
---|
37 | namespace yat { |
---|
38 | namespace 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 | columns_ = data.columns(); |
---|
116 | resize(data.rows(), data.columns()); |
---|
117 | assert(rows()==data.rows()); |
---|
118 | assert(columns()==data.columns()); |
---|
119 | std::copy(data.begin(), data.end(), data_iterator(vec_.begin())); |
---|
120 | BinaryWeight()(data.begin(), data.end(), weight_iterator(vec_.begin())); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | MatrixWeighted::iterator MatrixWeighted::end(void) |
---|
125 | { |
---|
126 | return iterator(vec_.end()); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | MatrixWeighted::const_iterator MatrixWeighted::end(void) const |
---|
131 | { |
---|
132 | return const_iterator(vec_.end()); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | MatrixWeighted::column_iterator MatrixWeighted::end_column(size_t i) |
---|
137 | { |
---|
138 | return column_iterator(vec_.begin()+i+vec_.size(), columns_); |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | MatrixWeighted::const_column_iterator |
---|
143 | MatrixWeighted::end_column(size_t i) const |
---|
144 | { |
---|
145 | return const_column_iterator(vec_.begin()+i+vec_.size(), columns_); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | MatrixWeighted::row_iterator MatrixWeighted::end_row(size_t i) |
---|
150 | { |
---|
151 | return row_iterator(vec_.begin()+columns_*(i+1)); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | MatrixWeighted::const_row_iterator MatrixWeighted::end_row(size_t i) const |
---|
156 | { |
---|
157 | return const_row_iterator(vec_.begin()+columns_*(i+1)); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | void MatrixWeighted::swap(MatrixWeighted& other) |
---|
162 | { |
---|
163 | std::swap(vec_, other.vec_); |
---|
164 | std::swap(columns_, other.columns_); |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | void MatrixWeighted::resize(size_t rows, size_t columns) |
---|
169 | { |
---|
170 | columns_ = columns; |
---|
171 | vec_.resize(rows*columns); |
---|
172 | assert(this->rows()==rows); |
---|
173 | assert(this->columns()==columns); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | size_t MatrixWeighted::rows(void) const |
---|
178 | { |
---|
179 | if (vec_.size()) |
---|
180 | return vec_.size()/columns_; |
---|
181 | return 0; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | DataWeight& MatrixWeighted::operator()(size_t row, size_t column) |
---|
186 | { |
---|
187 | assert(column<columns_); |
---|
188 | assert(row*column < vec_.size()); |
---|
189 | return vec_[row*columns_ + column]; |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | const DataWeight& MatrixWeighted::operator()(size_t row, size_t column) const |
---|
194 | { |
---|
195 | assert(column<columns_); |
---|
196 | assert(row*column < vec_.size()); |
---|
197 | return vec_[row*columns_ + column]; |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | void swap(MatrixWeighted& lhs, MatrixWeighted& rhs) |
---|
202 | { |
---|
203 | lhs.swap(rhs); |
---|
204 | } |
---|
205 | |
---|
206 | }}} // of namespace utility, yat and thep |
---|