]> git.donarmstrong.com Git - lilypond.git/blob - flower/full-storage.cc
release: 0.0.46.jcn1
[lilypond.git] / flower / full-storage.cc
1 /*
2   full-storage.cc -- implement Full_storage
3
4   source file of the Flower Library
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include "full-storage.hh"
10
11 void
12 Full_storage::operator=(Full_storage const &fs)
13 {
14     resize(fs.height_i_, fs.width_i_);
15     OK();
16     fs.OK();
17     for (int i=0; i<height_i_; i++)
18         for (int j=0; j<width_i_; j++)
19             els_p_p_[i][j]= fs.els_p_p_[i][j];
20 }
21
22 void
23 Full_storage::OK() const
24 {
25 #ifndef NDEBUG
26     //    static Real dummy;            
27     assert(max_height_i_ >= height_i_ && max_width_i_ >= width_i_);
28     assert(height_i_ >= 0 && width_i_ >= 0);
29     assert(els_p_p_||!max_height_i_);
30 #endif
31 }
32 void
33 Full_storage::resize_cols(int newh)
34 {
35     if (newh <= max_height_i_) {
36         height_i_=newh;
37         return;
38     }
39    
40     Real ** newa=new Real*[newh];
41     int j=0;
42     for (; j < height_i_; j++)
43         newa[j] = els_p_p_[j];
44     for (; j < newh; j++)
45         newa[j] = new Real[max_width_i_];
46     delete[] els_p_p_;
47     els_p_p_=newa;
48
49     height_i_ = max_height_i_ = newh;
50 }
51
52 void
53 Full_storage::resize_rows(int neww)
54 {
55     if (neww <= max_width_i_) {
56         width_i_=neww;
57         return;
58     }
59     for (int i=0; i < max_height_i_ ; i++) {
60         Real* newa = new Real[neww];
61         for (int k=0; k < width_i_; k++)
62             newa[k] = els_p_p_[i][k];
63
64         delete[] els_p_p_[i];
65         els_p_p_[i] = newa;
66     }
67     width_i_ = max_width_i_ = neww;     
68 }
69
70 Full_storage::~Full_storage() {
71     for (int i=0; i < max_height_i_; i++)
72         delete [] els_p_p_[i];
73     delete[] els_p_p_;
74 }
75
76 void
77 Full_storage::resize(int rows, int cols)
78 {
79     OK();
80     resize_cols(rows);
81     resize_rows(cols);
82
83 }
84
85
86 bool
87 Full_storage::mult_ok(int i, int j) const
88 {
89     return valid(i,j);
90 }
91
92 bool
93 Full_storage::trans_ok(int i, int j) const
94 {
95        return valid(i,j);
96
97
98
99 void
100 Full_storage::trans_next(int &i, int &j) const
101 {
102     assert(trans_ok(i,j));
103     i++;
104     if (i >= height_i_) {
105         i=0;
106         j ++;
107     }
108 }
109
110 void
111 Full_storage::mult_next(int &i, int &j) const
112 {
113     assert(mult_ok(i,j));
114     j++;
115     if (j >= width_i_) {
116         j=0;
117         i++;
118     }
119 }
120
121 void
122 Full_storage::delete_column(int k)
123 {
124     assert(0 <= k &&k<width_i_);    
125     for (int i=0; i< height_i_ ; i++)
126         for (int j=k+1; j <width_i_; j++)
127             els_p_p_[i][j-1]=els_p_p_[i][j];
128     width_i_--;
129 }
130 void
131 Full_storage::delete_row(int k)
132 {
133     assert(0 <= k &&k<height_i_);
134     for (int i=k+1; i < height_i_ ; i++)
135         for (int j=0; j < width_i_; j++)
136             els_p_p_[i-1][j]=els_p_p_[i][j];
137     height_i_--;
138 }
139
140
141 void
142 Full_storage::insert_row(int k)
143 {
144     assert(0 <= k&& k <=height_i_);
145     resize_cols(height_i_+1);
146     for (int i=height_i_-1; i > k ; i--)
147         for (int j=0; j <width_i_; j++)
148             els_p_p_[i][j]=els_p_p_[i-1][j];
149
150 }
151
152
153 Array<Real>
154 Full_storage::row(int n) const
155 {
156     Array<Real> r;
157     for (int j = 0; j < width_i_; j++)
158         r.push(els_p_p_[n][j]);
159     return r;
160 }
161
162 Array<Real>
163 Full_storage::column(int n) const
164 {
165     
166     Array<Real> r;
167     for (int i = 0; i<height_i_; i++)
168         r.push(els_p_p_[i][n]);
169     return r;
170 }
171
172
173 Full_storage::Full_storage(Full_storage&s)
174 {
175     init();
176     (*this) = s;
177 }
178 Matrix_storage*
179 Full_storage::clone()
180 {
181     return new Full_storage(*this);
182 }
183
184
185 Matrix_storage *
186 Matrix_storage::get_full(int n, int m)
187 {
188     return new Full_storage(n,m);
189 }
190
191 bool
192 Full_storage::try_right_multiply(Matrix_storage * dest, Matrix_storage const * right)
193 {
194     if (dest->name() != Full_storage::static_name() ||
195         right->name() != Full_storage::static_name())
196         return false;
197
198     Full_storage *d_l = (Full_storage*)dest;
199     Full_storage *r_l = (Full_storage*)right;
200     
201     d_l->set_size(height_i_, r_l->width_i_);
202     for (int i=0; i < d_l->height_i_; i++)
203         for (int j = 0; j < d_l->width_i_; j++) {
204             Real &r(d_l->els_p_p_[i][j]);
205             r=0.0;
206             for (int k = 0; k < width_i_; k++)
207                 r += els_p_p_[i][k] * r_l->els_p_p_[k][j];
208             
209         }
210     return true;
211     
212     
213 }
214 IMPLEMENT_STATIC_NAME(Full_storage);
215 IMPLEMENT_STATIC_NAME(Matrix_storage);