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

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

fixes #454 - throw exception when detecting funky dimensions

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