]> git.donarmstrong.com Git - lilypond.git/blob - flower/full-storage.cc
223796ba35337d095a06b43fe6580650b0409ec5
[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) 1996,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
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
33 void
34 Full_storage::resize_cols(int newh)
35 {
36     if (newh <= max_height_i_) {
37         height_i_=newh;
38         return;
39     }
40    
41     Real ** newa=new Real*[newh];
42     int j=0;
43     for (; j < height_i_; j++)
44         newa[j] = els_p_p_[j];
45     for (; j < newh; j++)
46         newa[j] = new Real[max_width_i_];
47     delete[] els_p_p_;
48     els_p_p_=newa;
49
50     height_i_ = max_height_i_ = newh;
51 }
52
53
54 Full_storage::Full_storage(Matrix_storage*m)
55 {
56     set_size(m->rows(), m->cols());
57     if ( !m->is_type_b ( Full_storage::static_name()))
58         for (int i=0; i<height_i_; i++)
59             for (int j=0; j<width_i_; j++)
60                 els_p_p_[i][j]=0.0;
61     for (int i,j=0; m->mult_ok(i,j); m->mult_next(i,j))
62         els_p_p_[i][j] = m->elem(i,j);
63 }
64
65 void
66 Full_storage::resize_rows(int neww)
67 {
68     if (neww <= max_width_i_) {
69         width_i_=neww;
70         return;
71     }
72     for (int i=0; i < max_height_i_ ; i++) {
73         Real* newa = new Real[neww];
74         for (int k=0; k < width_i_; k++)
75             newa[k] = els_p_p_[i][k];
76
77         delete[] els_p_p_[i];
78         els_p_p_[i] = newa;
79     }
80     width_i_ = max_width_i_ = neww;     
81 }
82
83 Full_storage::~Full_storage() {
84     for (int i=0; i < max_height_i_; i++)
85         delete [] els_p_p_[i];
86     delete[] els_p_p_;
87 }
88
89 void
90 Full_storage::resize(int rows, int cols)
91 {
92     OK();
93     resize_cols(rows);
94     resize_rows(cols);
95 }
96
97
98 bool
99 Full_storage::mult_ok(int i, int ) const
100 {
101     return i < height_i_;
102 }
103
104 bool
105 Full_storage::trans_ok(int , int j) const
106 {
107        return j < width_i_;
108
109
110
111 void
112 Full_storage::trans_next(int &i, int &j) const
113 {
114     assert(trans_ok(i,j));
115     i++;
116     if (i >= height_i_) {
117         i=0;
118         j ++;
119     }
120 }
121
122 void
123 Full_storage::mult_next(int &i, int &j) const
124 {
125     assert(mult_ok(i,j));
126     j++;
127     if (j >= width_i_) {
128         j=0;
129         i++;
130     }
131 }
132
133 void
134 Full_storage::delete_column(int k)
135 {
136     assert(0 <= k &&k<width_i_);    
137     for (int i=0; i< height_i_ ; i++)
138         for (int j=k+1; j <width_i_; j++)
139             els_p_p_[i][j-1]=els_p_p_[i][j];
140     width_i_--;
141 }
142 void
143 Full_storage::delete_row(int k)
144 {
145     assert(0 <= k &&k<height_i_);
146     for (int i=k+1; i < height_i_ ; i++)
147         for (int j=0; j < width_i_; j++)
148             els_p_p_[i-1][j]=els_p_p_[i][j];
149     height_i_--;
150 }
151
152
153 void
154 Full_storage::insert_row(int k)
155 {
156     assert(0 <= k&& k <=height_i_);
157     resize_cols(height_i_+1);
158     for (int i=height_i_-1; i > k ; i--)
159         for (int j=0; j <width_i_; j++)
160             els_p_p_[i][j]=els_p_p_[i-1][j];
161
162 }
163
164 int
165 Full_storage::dim()const
166 {
167     assert (rows()==cols());
168     return rows();
169 }
170
171 Full_storage::Full_storage(Full_storage const&s)
172 {
173     init();
174     (*this) = s;
175 }
176
177 bool
178 Full_storage::try_right_multiply(Matrix_storage * dest, Matrix_storage const * right)const
179 {
180     if (dest->name() != Full_storage::static_name() ||
181         right->name() != Full_storage::static_name())
182         return false;
183
184     Full_storage *d_l = (Full_storage*)dest;
185     Full_storage *r_l = (Full_storage*)right;
186     
187     d_l->set_size(height_i_, r_l->width_i_);
188     for (int i=0; i < d_l->height_i_; i++)
189         for (int j = 0; j < d_l->width_i_; j++) {
190             Real &r(d_l->els_p_p_[i][j]);
191             r=0.0;
192             for (int k = 0; k < width_i_; k++)
193                 r += els_p_p_[i][k] * r_l->els_p_p_[k][j];
194             
195         }
196     return true;
197     
198     
199 }
200 IMPLEMENT_IS_TYPE_B1(Full_storage,Matrix_storage);