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