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