Mercurial > hg > octave-nkf
comparison liboctave/Array.h @ 11507:c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 13 Jan 2011 06:01:08 -0500 |
parents | 9478b216752e |
children | fd0a3ac60b0e |
comparison
equal
deleted
inserted
replaced
11506:964b7fd379f1 | 11507:c3ad80f4ce36 |
---|---|
123 typedef bool (*compare_fcn_type) (typename ref_param<T>::type, | 123 typedef bool (*compare_fcn_type) (typename ref_param<T>::type, |
124 typename ref_param<T>::type); | 124 typename ref_param<T>::type); |
125 | 125 |
126 protected: | 126 protected: |
127 | 127 |
128 dim_vector dimensions; | |
129 | |
128 typename Array<T>::ArrayRep *rep; | 130 typename Array<T>::ArrayRep *rep; |
129 | |
130 dim_vector dimensions; | |
131 | 131 |
132 // Rationale: | 132 // Rationale: |
133 // slice_data is a pointer to rep->data, denoting together with slice_len the | 133 // slice_data is a pointer to rep->data, denoting together with slice_len the |
134 // actual portion of the data referenced by this Array<T> object. This allows | 134 // actual portion of the data referenced by this Array<T> object. This allows |
135 // to make shallow copies not only of a whole array, but also of contiguous | 135 // to make shallow copies not only of a whole array, but also of contiguous |
140 octave_idx_type slice_len; | 140 octave_idx_type slice_len; |
141 | 141 |
142 // slice constructor | 142 // slice constructor |
143 Array (const Array<T>& a, const dim_vector& dv, | 143 Array (const Array<T>& a, const dim_vector& dv, |
144 octave_idx_type l, octave_idx_type u) | 144 octave_idx_type l, octave_idx_type u) |
145 : rep(a.rep), dimensions (dv) | 145 : dimensions (dv), rep(a.rep), slice_data (a.slice_data+l), slice_len (u-l) |
146 { | 146 { |
147 rep->count++; | 147 rep->count++; |
148 slice_data = a.slice_data + l; | |
149 slice_len = u - l; | |
150 dimensions.chop_trailing_singletons (); | 148 dimensions.chop_trailing_singletons (); |
151 } | 149 } |
152 | 150 |
153 private: | 151 private: |
154 | 152 |
163 public: | 161 public: |
164 | 162 |
165 // Empty ctor (0x0). | 163 // Empty ctor (0x0). |
166 | 164 |
167 Array (void) | 165 Array (void) |
168 : rep (nil_rep ()), dimensions () | 166 : dimensions (), rep (nil_rep ()), slice_data (rep->data), |
167 slice_len (rep->len) | |
169 { | 168 { |
170 rep->count++; | 169 rep->count++; |
171 slice_data = rep->data; | |
172 slice_len = rep->len; | |
173 } | 170 } |
174 | 171 |
175 // Obsolete 1D ctor (there are no 1D arrays). | 172 // Obsolete 1D ctor (there are no 1D arrays). |
176 explicit Array (octave_idx_type n) GCC_ATTR_DEPRECATED | 173 explicit Array (octave_idx_type n) GCC_ATTR_DEPRECATED |
177 : rep (new typename Array<T>::ArrayRep (n)), dimensions (n, 1) | 174 : dimensions (n, 1), rep (new typename Array<T>::ArrayRep (n)), |
178 { | 175 slice_data (rep->data), slice_len (rep->len) |
179 slice_data = rep->data; | 176 { } |
180 slice_len = rep->len; | |
181 } | |
182 | 177 |
183 // 2D uninitialized ctor. | 178 // 2D uninitialized ctor. |
184 explicit Array (octave_idx_type m, octave_idx_type n) | 179 explicit Array (octave_idx_type m, octave_idx_type n) |
185 : rep (), dimensions (m, n) | 180 : dimensions (m, n), |
186 { | 181 rep (new typename Array<T>::ArrayRep (dimensions.safe_numel ())), |
187 rep = new typename Array<T>::ArrayRep (dimensions.safe_numel ()); | 182 slice_data (rep->data), slice_len (rep->len) |
188 slice_data = rep->data; | 183 { } |
189 slice_len = rep->len; | |
190 } | |
191 | 184 |
192 // 2D initialized ctor. | 185 // 2D initialized ctor. |
193 explicit Array (octave_idx_type m, octave_idx_type n, const T& val) | 186 explicit Array (octave_idx_type m, octave_idx_type n, const T& val) |
194 : rep (), dimensions (m, n) | 187 : dimensions (m, n), |
188 rep (new typename Array<T>::ArrayRep (dimensions.safe_numel ())), | |
189 slice_data (rep->data), slice_len (rep->len) | |
195 { | 190 { |
196 rep = new typename Array<T>::ArrayRep (dimensions.safe_numel ()); | |
197 slice_data = rep->data; | |
198 slice_len = rep->len; | |
199 fill (val); | 191 fill (val); |
200 } | 192 } |
201 | 193 |
202 // nD uninitialized ctor. | 194 // nD uninitialized ctor. |
203 explicit Array (const dim_vector& dv) | 195 explicit Array (const dim_vector& dv) |
204 : rep (new typename Array<T>::ArrayRep (dv.safe_numel ())), | 196 : dimensions (dv), |
205 dimensions (dv) | 197 rep (new typename Array<T>::ArrayRep (dv.safe_numel ())), |
198 slice_data (rep->data), slice_len (rep->len) | |
206 { | 199 { |
207 slice_data = rep->data; | |
208 slice_len = rep->len; | |
209 dimensions.chop_trailing_singletons (); | 200 dimensions.chop_trailing_singletons (); |
210 } | 201 } |
211 | 202 |
212 // nD initialized ctor. | 203 // nD initialized ctor. |
213 explicit Array (const dim_vector& dv, const T& val) | 204 explicit Array (const dim_vector& dv, const T& val) |
214 : rep (new typename Array<T>::ArrayRep (dv.safe_numel ())), | 205 : dimensions (dv), |
215 dimensions (dv) | 206 rep (new typename Array<T>::ArrayRep (dv.safe_numel ())), |
216 { | 207 slice_data (rep->data), slice_len (rep->len) |
217 slice_data = rep->data; | 208 { |
218 slice_len = rep->len; | |
219 fill (val); | 209 fill (val); |
220 dimensions.chop_trailing_singletons (); | 210 dimensions.chop_trailing_singletons (); |
221 } | 211 } |
222 | 212 |
223 // Reshape constructor. | 213 // Reshape constructor. |
226 Array (const Array<T>& a, octave_idx_type nr, octave_idx_type nc); | 216 Array (const Array<T>& a, octave_idx_type nr, octave_idx_type nc); |
227 | 217 |
228 // Type conversion case. | 218 // Type conversion case. |
229 template <class U> | 219 template <class U> |
230 Array (const Array<U>& a) | 220 Array (const Array<U>& a) |
231 : rep (new typename Array<T>::ArrayRep (a.data (), a.length ())), | 221 : dimensions (a.dims ()), |
232 dimensions (a.dims ()) | 222 rep (new typename Array<T>::ArrayRep (a.data (), a.length ())), |
233 { | 223 slice_data (rep->data), slice_len (rep->len) |
234 slice_data = rep->data; | 224 { } |
235 slice_len = rep->len; | |
236 } | |
237 | 225 |
238 // No type conversion case. | 226 // No type conversion case. |
239 Array (const Array<T>& a) | 227 Array (const Array<T>& a) |
240 : rep (a.rep), dimensions (a.dimensions) | 228 : dimensions (a.dimensions), rep (a.rep), slice_data (a.slice_data), |
229 slice_len (a.slice_len) | |
241 { | 230 { |
242 rep->count++; | 231 rep->count++; |
243 slice_data = a.slice_data; | |
244 slice_len = a.slice_len; | |
245 } | 232 } |
246 | 233 |
247 public: | 234 public: |
248 | 235 |
249 ~Array (void) | 236 ~Array (void) |