4513
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4513
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_dim_vector_h) |
|
25 #define octave_dim_vector_h 1 |
|
26 |
|
27 #include <cassert> |
5765
|
28 #include <sstream> |
4543
|
29 #include <string> |
|
30 |
5275
|
31 #include "oct-types.h" |
4513
|
32 |
|
33 class |
|
34 dim_vector |
|
35 { |
4548
|
36 protected: |
4513
|
37 |
4548
|
38 class dim_vector_rep |
|
39 { |
|
40 public: |
4513
|
41 |
5275
|
42 octave_idx_type *dims; |
4548
|
43 int ndims; |
|
44 int count; |
4513
|
45 |
4548
|
46 dim_vector_rep (void) : dims (0), ndims (0), count (1) { } |
|
47 |
5275
|
48 dim_vector_rep (octave_idx_type n) : dims (new octave_idx_type [1]), ndims (1), count (1) |
4548
|
49 { |
|
50 dims[0] = n; |
|
51 } |
4513
|
52 |
5275
|
53 dim_vector_rep (octave_idx_type r, octave_idx_type c) : dims (new octave_idx_type [2]), ndims (2), count (1) |
4548
|
54 { |
|
55 dims[0] = r; |
|
56 dims[1] = c; |
|
57 } |
4513
|
58 |
5275
|
59 dim_vector_rep (octave_idx_type r, octave_idx_type c, octave_idx_type p) |
|
60 : dims (new octave_idx_type [3]), ndims (3), count (1) |
4513
|
61 { |
4548
|
62 dims[0] = r; |
|
63 dims[1] = c; |
|
64 dims[2] = p; |
|
65 } |
|
66 |
|
67 dim_vector_rep (const dim_vector_rep& dv) |
5275
|
68 : dims (dv.ndims > 0 ? new octave_idx_type [dv.ndims] : 0), |
4548
|
69 ndims (dv.ndims > 0 ? dv.ndims : 0), count (1) |
|
70 { |
|
71 if (dims) |
4513
|
72 { |
|
73 for (int i = 0; i < ndims; i++) |
|
74 dims[i] = dv.dims[i]; |
|
75 } |
|
76 } |
|
77 |
5275
|
78 dim_vector_rep (octave_idx_type n, const dim_vector_rep *dv, int fill_value = 0) |
|
79 : dims ((dv && n > 0) ? new octave_idx_type [n] : 0), |
4548
|
80 ndims (n > 0 ? n : 0), count (1) |
4513
|
81 { |
4548
|
82 if (dims) |
4513
|
83 { |
4548
|
84 int dv_ndims = dv ? dv->ndims : 0; |
4513
|
85 |
4565
|
86 int min_len = n < dv_ndims ? n : dv_ndims; |
|
87 |
|
88 for (int i = 0; i < min_len; i++) |
4548
|
89 dims[i] = dv->dims[i]; |
4513
|
90 |
4548
|
91 for (int i = dv_ndims; i < n; i++) |
4887
|
92 dims[i] = fill_value; |
4513
|
93 } |
|
94 } |
|
95 |
4548
|
96 ~dim_vector_rep (void) { delete [] dims; } |
4513
|
97 |
4548
|
98 int length (void) const { return ndims; } |
4513
|
99 |
5275
|
100 octave_idx_type& elem (int i) |
4513
|
101 { |
4548
|
102 assert (i >= 0 && i < ndims); |
|
103 return dims[i]; |
|
104 } |
4513
|
105 |
5275
|
106 octave_idx_type elem (int i) const |
4548
|
107 { |
|
108 assert (i >= 0 && i < ndims); |
4513
|
109 return dims[i]; |
|
110 } |
|
111 |
4673
|
112 void chop_trailing_singletons (void) |
|
113 { |
|
114 for (int i = ndims - 1; i > 1; i--) |
|
115 { |
|
116 if (dims[i] == 1) |
|
117 ndims--; |
|
118 else |
|
119 break; |
|
120 } |
|
121 } |
|
122 |
4548
|
123 private: |
|
124 |
|
125 // No assignment! |
|
126 |
|
127 dim_vector_rep& operator = (const dim_vector_rep& dv); |
|
128 }; |
|
129 |
|
130 dim_vector_rep *rep; |
|
131 |
|
132 void make_unique (void) |
|
133 { |
|
134 if (rep->count > 1) |
|
135 { |
|
136 --rep->count; |
|
137 rep = new dim_vector_rep (*rep); |
|
138 } |
|
139 } |
|
140 |
|
141 private: |
|
142 |
|
143 dim_vector_rep *nil_rep (void) const |
|
144 { |
|
145 static dim_vector_rep *nr = new dim_vector_rep (); |
|
146 |
|
147 return nr; |
|
148 } |
|
149 |
|
150 public: |
|
151 |
|
152 explicit dim_vector (void) |
|
153 : rep (nil_rep ()) { rep->count++; } |
|
154 |
5275
|
155 explicit dim_vector (octave_idx_type n) |
4548
|
156 : rep (new dim_vector_rep (n)) { } |
|
157 |
5275
|
158 explicit dim_vector (octave_idx_type r, octave_idx_type c) |
4548
|
159 : rep (new dim_vector_rep (r, c)) { } |
|
160 |
5275
|
161 explicit dim_vector (octave_idx_type r, octave_idx_type c, octave_idx_type p) |
4548
|
162 : rep (new dim_vector_rep (r, c, p)) { } |
|
163 |
|
164 dim_vector (const dim_vector& dv) |
|
165 : rep (dv.rep) { rep->count++; } |
|
166 |
|
167 dim_vector& operator = (const dim_vector& dv) |
|
168 { |
|
169 if (&dv != this) |
|
170 { |
|
171 if (--rep->count <= 0) |
|
172 delete rep; |
|
173 |
|
174 rep = dv.rep; |
|
175 rep->count++; |
|
176 } |
|
177 |
|
178 return *this; |
|
179 } |
|
180 |
|
181 ~dim_vector (void) |
|
182 { |
|
183 if (--rep->count <= 0) |
|
184 delete rep; |
|
185 } |
|
186 |
|
187 int length (void) const { return rep->length (); } |
|
188 |
5275
|
189 octave_idx_type& elem (int i) { make_unique (); return rep->elem (i); } |
4548
|
190 |
5275
|
191 octave_idx_type elem (int i) const { return rep->elem (i); } |
4513
|
192 |
5275
|
193 octave_idx_type& operator () (int i) { return elem (i); } |
4513
|
194 |
5275
|
195 octave_idx_type operator () (int i) const { return elem (i); } |
4513
|
196 |
4887
|
197 void resize (int n, int fill_value = 0) |
4548
|
198 { |
|
199 int len = length (); |
4513
|
200 |
4548
|
201 if (n != len) |
|
202 { |
|
203 dim_vector_rep *old_rep = rep; |
4513
|
204 |
4887
|
205 rep = new dim_vector_rep (n, old_rep, fill_value); |
4513
|
206 |
4548
|
207 if (--old_rep->count <= 0) |
|
208 delete old_rep; |
|
209 } |
|
210 } |
4513
|
211 |
4559
|
212 std::string str (char sep = 'x') const |
4548
|
213 { |
5765
|
214 std::ostringstream buf; |
4543
|
215 |
4548
|
216 for (int i = 0; i < length (); i++) |
|
217 { |
|
218 buf << elem (i); |
4543
|
219 |
4548
|
220 if (i < length () - 1) |
4559
|
221 buf << sep; |
4548
|
222 } |
4543
|
223 |
5765
|
224 std::string retval = buf.str (); |
4543
|
225 |
4548
|
226 return retval; |
|
227 } |
4543
|
228 |
|
229 bool all_zero (void) const |
4548
|
230 { |
|
231 bool retval = true; |
4543
|
232 |
4548
|
233 for (int i = 0; i < length (); i++) |
|
234 { |
|
235 if (elem (i) != 0) |
|
236 { |
|
237 retval = false; |
|
238 break; |
|
239 } |
|
240 } |
4543
|
241 |
4548
|
242 return retval; |
|
243 } |
4559
|
244 |
|
245 bool any_zero (void) const |
|
246 { |
|
247 bool retval = false; |
|
248 |
|
249 for (int i = 0; i < length (); i++) |
|
250 { |
|
251 if (elem (i) == 0) |
|
252 { |
|
253 retval = true; |
|
254 break; |
|
255 } |
|
256 } |
|
257 |
|
258 return retval; |
|
259 } |
4567
|
260 |
4635
|
261 int |
|
262 num_ones (void) const |
|
263 { |
|
264 int retval = 0; |
|
265 |
|
266 for (int i = 0; i < length (); i++) |
|
267 if (elem (i) == 1) |
|
268 retval++; |
|
269 |
|
270 return retval; |
|
271 } |
|
272 |
4655
|
273 bool |
|
274 all_ones (void) const |
|
275 { |
|
276 return (num_ones () == length ()); |
|
277 } |
|
278 |
4567
|
279 // This is the number of elements that a matrix with this dimension |
|
280 // vector would have, NOT the number of dimensions (elements in the |
|
281 // dimension vector). |
|
282 |
5275
|
283 octave_idx_type numel (void) const |
4567
|
284 { |
|
285 int n_dims = length (); |
|
286 |
5275
|
287 octave_idx_type retval = n_dims > 0 ? elem (0) : 0; |
4567
|
288 |
|
289 for (int i = 1; i < n_dims; i++) |
|
290 retval *= elem (i); |
|
291 |
|
292 return retval; |
|
293 } |
4673
|
294 |
|
295 void chop_trailing_singletons (void) |
|
296 { |
|
297 make_unique (); |
|
298 rep->chop_trailing_singletons (); |
|
299 } |
4735
|
300 |
|
301 dim_vector squeeze (void) const |
|
302 { |
|
303 dim_vector new_dims = *this; |
|
304 |
|
305 bool dims_changed = 1; |
|
306 |
|
307 int k = 0; |
|
308 |
|
309 for (int i = 0; i < length (); i++) |
|
310 { |
|
311 if (elem (i) == 1) |
|
312 dims_changed = true; |
|
313 else |
|
314 new_dims(k++) = elem (i); |
|
315 } |
|
316 |
|
317 if (dims_changed) |
|
318 { |
|
319 if (k == 0) |
|
320 new_dims = dim_vector (1, 1); |
|
321 else if (k == 1) |
|
322 { |
|
323 // There is one non-singleton dimension, so we need |
|
324 // to decide the correct orientation. |
|
325 |
|
326 if (elem (0) == 1) |
|
327 { |
|
328 // The original dimension vector had a leading |
|
329 // singleton dimension. |
|
330 |
5275
|
331 octave_idx_type tmp = new_dims(0); |
4735
|
332 |
|
333 new_dims.resize (2); |
|
334 |
|
335 new_dims(0) = 1; |
|
336 new_dims(1) = tmp; |
|
337 } |
|
338 else |
|
339 { |
|
340 // The first element of the original dimension vector |
|
341 // was not a singleton dimension. |
|
342 |
|
343 new_dims.resize (2); |
|
344 |
|
345 new_dims(1) = 1; |
|
346 } |
|
347 } |
|
348 else |
|
349 new_dims.resize(k); |
|
350 } |
|
351 |
|
352 return new_dims; |
|
353 } |
4915
|
354 |
|
355 bool concat (const dim_vector& dvb, int dim = 0) |
|
356 { |
|
357 if (all_zero ()) |
|
358 { |
|
359 *this = dvb; |
|
360 return true; |
|
361 } |
|
362 |
|
363 if (dvb.all_zero ()) |
|
364 return true; |
|
365 |
|
366 int na = length (); |
|
367 int nb = dvb.length (); |
|
368 |
|
369 // Find the max and min value of na and nb |
|
370 int n_max = na > nb ? na : nb; |
|
371 int n_min = na < nb ? na : nb; |
|
372 |
|
373 // The elements of the dimension vectors can only differ |
|
374 // if the dim variable differs from the actual dimension |
|
375 // they differ. |
|
376 |
|
377 for (int i = 0; i < n_min; i++) |
|
378 { |
|
379 if (elem(i) != dvb(i) && dim != i) |
|
380 return false; |
|
381 } |
|
382 |
|
383 // Ditto. |
|
384 for (int i = n_min; i < n_max; i++) |
|
385 { |
|
386 if (na > n_min) |
|
387 { |
|
388 if (elem(i) != 1 && dim != i) |
|
389 return false; |
|
390 } |
|
391 else |
|
392 { |
|
393 if (dvb(i) != 1 && dim != i) |
|
394 return false; |
|
395 } |
|
396 } |
|
397 |
|
398 // If we want to add the dimension vectors at a dimension |
|
399 // larger than both, then we need to set n_max to this number |
|
400 // so that we resize *this to the right dimension. |
|
401 |
|
402 n_max = n_max > (dim + 1) ? n_max : (dim + 1); |
|
403 |
|
404 // Resize *this to the appropriate dimensions. |
|
405 |
|
406 if (n_max > na) |
|
407 { |
|
408 dim_vector_rep *old_rep = rep; |
|
409 |
|
410 rep = new dim_vector_rep (n_max, old_rep, 1); |
|
411 |
|
412 if (--old_rep->count <= 0) |
|
413 delete old_rep; |
|
414 } |
|
415 |
|
416 // Larger or equal since dim has been decremented by one. |
|
417 |
|
418 if (dim >= nb) |
4940
|
419 elem (dim)++; |
4915
|
420 else |
|
421 elem (dim) += dvb(dim); |
|
422 |
|
423 return true; |
|
424 } |
4513
|
425 }; |
|
426 |
4543
|
427 static inline bool |
|
428 operator == (const dim_vector& a, const dim_vector& b) |
|
429 { |
|
430 bool retval = true; |
|
431 |
|
432 int a_len = a.length (); |
|
433 int b_len = b.length (); |
|
434 |
|
435 if (a_len != b_len) |
|
436 retval = false; |
|
437 else |
|
438 { |
|
439 for (int i = 0; i < a_len; i++) |
|
440 { |
|
441 if (a(i) != b(i)) |
|
442 { |
|
443 retval = false; |
|
444 break; |
|
445 } |
|
446 } |
|
447 } |
|
448 |
|
449 return retval; |
|
450 } |
|
451 |
|
452 static inline bool |
|
453 operator != (const dim_vector& a, const dim_vector& b) |
|
454 { |
|
455 return ! operator == (a, b); |
|
456 } |
|
457 |
4513
|
458 #endif |
|
459 |
|
460 /* |
|
461 ;;; Local Variables: *** |
|
462 ;;; mode: C++ *** |
|
463 ;;; End: *** |
|
464 */ |