1 /* 2 This file is part of LilyPond, the GNU music typesetter. 3 4 Copyright (C) 2006--2020 Joe Neeman <joeneeman@gmail.com> 5 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. 10 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. 15 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/>. 18 */ 19 20 #ifndef MATRIX_HH 21 #define MATRIX_HH 22 23 #include "std-vector.hh" 24 25 template<class T, class A = std::allocator<T> > 26 class Matrix 27 { 28 public: 29 Matrix<T, A> () 30 { 31 rank_ = 0; 32 } 33 34 Matrix<T, A> (vsize rows, vsize columns, T const &t) 35 : data_ (rows * columns, t) 36 { 37 rank_ = rows; 38 } 39 at(vsize row,vsize col) const40 const T &at (vsize row, vsize col) const 41 { 42 assert (row < rank_ && col * rank_ + row < data_.size ()); 43 44 return data_[col * rank_ + row]; 45 } 46 at(vsize row,vsize col)47 T &at (vsize row, vsize col) 48 { 49 assert (row < rank_ && col * rank_ + row < data_.size ()); 50 51 return data_[col * rank_ + row]; 52 } 53 resize(vsize rows,vsize columns,T const & t)54 void resize (vsize rows, vsize columns, T const &t) 55 { 56 if (rows == rank_) 57 data_.resize (rows * columns, t); 58 else 59 { 60 std::vector<T, A> new_data; 61 new_data.resize (rows * columns, t); 62 vsize cur_cols = rank_ ? data_.size () / rank_ : 0; 63 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; 69 } 70 } 71 72 private: 73 std::vector<T, A> data_; 74 vsize rank_; 75 }; 76 77 #endif /* MATRIX_HH */ 78