Merge commit 'origin' into release/unstable
[lilypond/mpolesky.git] / flower / include / matrix.hh
blobf0676c2d730ba613beaa864d5b713d81e6210849
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2006--2010 Joe Neeman <joeneeman@gmail.com>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef MATRIX_HH
21 #define MATRIX_HH
23 #include "std-vector.hh"
25 template<class T, class A=std::allocator<T> >
26 class Matrix
28 public:
29 Matrix<T, A> ()
31 rank_ = 0;
34 Matrix<T, A> (vsize rows, vsize columns, T const &t)
35 : data_(rows * columns, t)
37 rank_ = rows;
40 const T &at (vsize row, vsize col) const
42 assert (row < rank_ && col * rank_ + row < data_.size ());
44 return data_[col * rank_ + row];
47 T &at (vsize row, vsize col)
49 assert (row < rank_ && col * rank_ + row < data_.size ());
51 return data_[col * rank_ + row];
54 void resize (vsize rows, vsize columns, T const &t)
56 if (rows == rank_)
57 data_.resize (rows * columns, t);
58 else
60 vector<T,A> new_data;
61 new_data.resize (rows * columns, t);
62 vsize cur_cols = rank_ ? data_.size () / rank_: 0;
64 for (vsize i = 0; i < cur_cols; i++)
65 for (vsize j = 0; j < rank_; j++)
66 new_data[i*rows + j] = data_[i*rank_ + j];
67 rank_ = rows;
68 data_ = new_data;
72 private:
73 vector<T, A> data_;
74 vsize rank_;
77 #endif /* MATRIX_HH */