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

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

fixing problems with MatrixWeighted? iterators

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1// $Id: MatrixWeighted.cc 1382 2008-07-17 14:32:16Z 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
9  This file is part of the yat library, http://trac.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "MatrixWeighted.h"
28
29#include "DataIterator.h"
30#include "Matrix.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(std::istream& is, char sep)
58  {
59    Matrix data(is, sep);
60    Matrix weight;
61    nan(data, weight);
62    columns_ = data.columns();
63    resize(data.columns(), data.rows());
64    std::copy(data.begin(), data.end(), make_data_iterator(vec_.begin()));
65    std::copy(weight.begin(), weight.end(), make_weight_iterator(vec_.begin()));
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::iterator MatrixWeighted::begin_column(size_t i)
82  {
83    return iterator(vec_.begin()+i, columns_);
84  }
85
86
87  MatrixWeighted::const_iterator MatrixWeighted::begin_column(size_t i) const
88  {
89    return const_iterator(vec_.begin()+i, columns_);
90  }
91
92
93  MatrixWeighted::iterator MatrixWeighted::begin_row(size_t i)
94  {
95    return iterator(vec_.begin()+columns_*i);
96  }
97
98
99  MatrixWeighted::const_iterator MatrixWeighted::begin_row(size_t i) const
100  {
101    return const_iterator(vec_.begin()+columns_*i);
102  }
103
104
105  size_t MatrixWeighted::columns(void) const
106  {
107    return columns_;
108  }
109
110
111  MatrixWeighted::iterator MatrixWeighted::end(void)
112  {
113    return iterator(vec_.end());
114  }
115
116
117  MatrixWeighted::const_iterator MatrixWeighted::end(void) const
118  {
119    return const_iterator(vec_.end());
120  }
121
122
123  MatrixWeighted::iterator MatrixWeighted::end_column(size_t i)
124  {
125    return iterator(vec_.begin()+i+vec_.size(), columns_);
126  }
127
128
129  MatrixWeighted::const_iterator MatrixWeighted::end_column(size_t i) const
130  {
131    return const_iterator(vec_.begin()+i+vec_.size(), columns_);
132  }
133
134
135  MatrixWeighted::iterator MatrixWeighted::end_row(size_t i)
136  {
137    return iterator(vec_.begin()+columns_*(i+1));
138  }
139
140
141  MatrixWeighted::const_iterator MatrixWeighted::end_row(size_t i) const
142  {
143    return const_iterator(vec_.begin()+columns_*(i+1));
144  }
145
146
147  void MatrixWeighted::resize(size_t rows, size_t columns)
148  {
149    columns_ = columns;
150    vec_.resize(rows*columns);
151  }
152
153 
154  size_t MatrixWeighted::rows(void) const
155  {
156    if (vec_.size())
157      return vec_.size()/columns_;
158    return 0;
159  }
160
161
162  DataWeight& MatrixWeighted::operator()(size_t row, size_t column)
163  {
164    assert(column<columns_);
165    assert(row*column < vec_.size());
166    return vec_[row*columns_ + column];
167  }
168
169
170  const DataWeight& MatrixWeighted::operator()(size_t row, size_t column) const
171  {
172    assert(column<columns_);
173    assert(row*column < vec_.size());
174    return vec_[row*columns_ + column];
175  }
176
177}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.