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