]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/matrix.hh
Run `make grand-replace'.
[lilypond.git] / flower / include / matrix.hh
1 /*
2   matrix.hh -- declare and implement 2d arrays
3
4   source file of the Flower Library
5
6   (c) 2006--2008 Joe Neeman <joeneeman@gmail.com>
7 */
8
9 #ifndef MATRIX_HH
10 #define MATRIX_HH
11
12 #include "std-vector.hh"
13
14 template<class T, class A=std::allocator<T> >
15 class Matrix
16 {
17 public:
18   Matrix<T, A> ()
19   {
20     rank_ = 0;
21   }
22
23   Matrix<T, A> (vsize rows, vsize columns, T const &t)
24   : data_(rows * columns, t)
25   {
26     rank_ = rows;
27   }
28
29   const T &at (vsize row, vsize col) const
30   {
31     assert (row < rank_ && col * rank_ + row < data_.size ());
32
33     return data_[col * rank_ + row];
34   }
35
36   T &at (vsize row, vsize col)
37   {
38     assert (row < rank_ && col * rank_ + row < data_.size ());
39
40     return data_[col * rank_ + row];
41   }
42
43   void resize (vsize rows, vsize columns, T const &t)
44   {
45     if (rows == rank_)
46       data_.resize (rows * columns, t);
47     else
48       {
49         vector<T,A> new_data;
50         new_data.resize (rows * columns, t);
51         vsize cur_cols = rank_ ? data_.size () / rank_: 0;
52
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;
58       }
59   }
60
61 private:
62   vector<T, A> data_;
63   vsize rank_;
64 };
65
66 #endif /* MATRIX_HH */