Fix InstrumentSwitch grob definition.
[lilypond.git] / flower / include / matrix.hh
blobd9b32c2ee47569e9c09470e271707c68f06e438d
1 /*
2 matrix.hh -- declare and implement 2d arrays
4 source file of the Flower Library
6 (c) 2006--2007 Joe Neeman <joeneeman@gmail.com>
7 */
9 #ifndef MATRIX_HH
10 #define MATRIX_HH
12 #include "std-vector.hh"
14 template<class T, class A=std::allocator<T> >
15 class Matrix
17 public:
18 Matrix<T, A> ()
20 rank_ = 0;
23 Matrix<T, A> (vsize rows, vsize columns, T const &t)
24 : data_(rows * columns, t)
26 rank_ = rows;
29 const T &at (vsize row, vsize col) const
31 assert (row < rank_ && col * rank_ + row < data_.size ());
33 return data_[col * rank_ + row];
36 T &at (vsize row, vsize col)
38 assert (row < rank_ && col * rank_ + row < data_.size ());
40 return data_[col * rank_ + row];
43 void resize (vsize rows, vsize columns, T const &t)
45 if (rows == rank_)
46 data_.resize (rows * columns, t);
47 else
49 vector<T,A> new_data;
50 new_data.resize (rows * columns, t);
51 vsize cur_cols = rank_ ? data_.size () / rank_: 0;
53 for (vsize i = 0; i < cur_cols; i++)
54 for (vsize j = 0; j < rank_; j++)
55 new_data[i*rows + j] = data_[i*rank_ + j];
56 rank_ = rows;
57 data_ = new_data;
61 private:
62 vector<T, A> data_;
63 vsize rank_;
66 #endif /* MATRIX_HH */