lilypond-0.0.35
[lilypond.git] / flower / smat.cc
blobd89e3bc285e9c2482af7ae844dad4252a8eb0b19
1 #include "smat.hh"
3 void
4 Full_storage::operator=(Full_storage const &fs)
6 resize(fs.h, fs.w);
7 OK();
8 fs.OK();
9 for (int i=0; i<h; i++)
10 for (int j=0; j<w; j++)
11 els[i][j]= fs.els[i][j];
14 void
15 Full_storage::OK() const
17 #ifndef NDEBUG
18 // static Real dummy;
19 assert(maxh >= h && maxw >= w);
20 assert(h >= 0 && w >= 0);
21 assert(els||!maxh);
22 if (maxh>0) { // access outer elts.
23 Real *r = els[maxh -1];
24 #if 0
25 if (maxw>0) {
26 assert(r);
27 Real s = r[maxw -1]; // accessing unitialised memory.
28 s = sin(s);
30 #endif
32 #endif
34 void
35 Full_storage::resize_cols(int newh)
37 if (newh <= maxh) {
38 h=newh;
39 return;
42 Real ** newa=new Real*[newh];
43 int j=0;
44 for (; j < h; j++)
45 newa[j] = els[j];
46 for (; j < newh; j++)
47 newa[j] = new Real[maxw];
48 delete[] els;
49 els=newa;
51 h = maxh = newh;
54 void
55 Full_storage::resize_rows(int neww)
57 if (neww <= maxw) {
58 w=neww;
59 return;
61 for (int i=0; i < maxh ; i++) {
62 Real* newa=new Real[neww];
63 for (int k=0; k < w; k++)
64 newa[k] = els[i][k];
66 delete[] els[i];
67 els[i] = newa;
69 w = maxw = neww;
72 Full_storage::~Full_storage() {
73 for (int i=0; i < maxh; i++)
74 delete [] els[i];
75 delete[] els;
78 void
79 Full_storage::resize(int rows, int cols)
81 OK();
82 resize_cols(rows);
83 resize_rows(cols);
88 bool
89 Full_storage::mult_ok(int i, int j) const
91 return valid(i,j);
94 bool
95 Full_storage::trans_ok(int i, int j) const
97 return valid(i,j);
101 void
102 Full_storage::trans_next(int &i, int &j) const
104 assert(trans_ok(i,j));
105 i++;
106 if (i >= h) {
107 i=0;
108 j ++;
112 void
113 Full_storage::mult_next(int &i, int &j) const
115 assert(mult_ok(i,j));
116 j++;
117 if (j >= w) {
118 j=0;
119 i++;
123 void
124 Full_storage::delete_column(int k)
126 assert(0 <= k &&k<w);
127 for (int i=0; i< h ; i++)
128 for (int j=k+1; j <w; j++)
129 els[i][j-1]=els[i][j];
130 w--;
132 void
133 Full_storage::delete_row(int k)
135 assert(0 <= k &&k<h);
136 for (int i=k+1; i < h ; i++)
137 for (int j=0; j < w; j++)
138 els[i-1][j]=els[i][j];
139 h--;
143 void
144 Full_storage::insert_row(int k)
146 assert(0 <= k&& k <=h);
147 resize_cols(h+1);
148 for (int i=h-1; i > k ; i--)
149 for (int j=0; j <w; j++)
150 els[i][j]=els[i-1][j];
155 Array<Real>
156 Full_storage::row(int n) const
158 Array<Real> r;
159 for (int j = 0; j < w; j++)
160 r.push(els[n][j]);
161 return r;
164 Array<Real>
165 Full_storage::column(int n) const
168 Array<Real> r;
169 for (int i = 0; i<h; i++)
170 r.push(els[i][n]);
171 return r;
175 Full_storage::Full_storage(Full_storage&s)
177 init();
178 (*this) = s;
180 virtual_smat*
181 Full_storage::clone()
183 return new Full_storage(*this);
187 virtual_smat *
188 virtual_smat::get_full(int n, int m)
190 return new Full_storage(n,m);