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 |
4548
|
80 dim_vector_rep (int n, const dim_vector_rep *dv) |
|
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++) |
|
94 dims[i] = 0; |
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 |
4548
|
114 private: |
|
115 |
|
116 // No assignment! |
|
117 |
|
118 dim_vector_rep& operator = (const dim_vector_rep& dv); |
|
119 }; |
|
120 |
|
121 dim_vector_rep *rep; |
|
122 |
|
123 void make_unique (void) |
|
124 { |
|
125 if (rep->count > 1) |
|
126 { |
|
127 --rep->count; |
|
128 rep = new dim_vector_rep (*rep); |
|
129 } |
|
130 } |
|
131 |
|
132 private: |
|
133 |
|
134 dim_vector_rep *nil_rep (void) const |
|
135 { |
|
136 static dim_vector_rep *nr = new dim_vector_rep (); |
|
137 |
|
138 return nr; |
|
139 } |
|
140 |
|
141 public: |
|
142 |
|
143 explicit dim_vector (void) |
|
144 : rep (nil_rep ()) { rep->count++; } |
|
145 |
|
146 explicit dim_vector (int n) |
|
147 : rep (new dim_vector_rep (n)) { } |
|
148 |
|
149 explicit dim_vector (int r, int c) |
|
150 : rep (new dim_vector_rep (r, c)) { } |
|
151 |
|
152 explicit dim_vector (int r, int c, int p) |
|
153 : rep (new dim_vector_rep (r, c, p)) { } |
|
154 |
|
155 dim_vector (const dim_vector& dv) |
|
156 : rep (dv.rep) { rep->count++; } |
|
157 |
|
158 dim_vector& operator = (const dim_vector& dv) |
|
159 { |
|
160 if (&dv != this) |
|
161 { |
|
162 if (--rep->count <= 0) |
|
163 delete rep; |
|
164 |
|
165 rep = dv.rep; |
|
166 rep->count++; |
|
167 } |
|
168 |
|
169 return *this; |
|
170 } |
|
171 |
|
172 ~dim_vector (void) |
|
173 { |
|
174 if (--rep->count <= 0) |
|
175 delete rep; |
|
176 } |
|
177 |
|
178 int length (void) const { return rep->length (); } |
|
179 |
|
180 int& elem (int i) { make_unique (); return rep->elem (i); } |
|
181 |
|
182 int elem (int i) const { return rep->elem (i); } |
4513
|
183 |
|
184 int& operator () (int i) { return elem (i); } |
|
185 |
|
186 int operator () (int i) const { return elem (i); } |
|
187 |
|
188 void resize (int n) |
4548
|
189 { |
|
190 int len = length (); |
4513
|
191 |
4548
|
192 if (n != len) |
|
193 { |
|
194 dim_vector_rep *old_rep = rep; |
4513
|
195 |
4548
|
196 rep = new dim_vector_rep (n, old_rep); |
4513
|
197 |
4548
|
198 if (--old_rep->count <= 0) |
|
199 delete old_rep; |
|
200 } |
|
201 } |
4513
|
202 |
|
203 |
4559
|
204 std::string str (char sep = 'x') const |
4548
|
205 { |
|
206 OSSTREAM buf; |
4543
|
207 |
4548
|
208 for (int i = 0; i < length (); i++) |
|
209 { |
|
210 buf << elem (i); |
4543
|
211 |
4548
|
212 if (i < length () - 1) |
4559
|
213 buf << sep; |
4548
|
214 } |
4543
|
215 |
4548
|
216 buf << OSSTREAM_ENDS; |
4543
|
217 |
4548
|
218 std::string retval = OSSTREAM_STR (buf); |
4543
|
219 |
4548
|
220 OSSTREAM_FREEZE (buf); |
4543
|
221 |
4548
|
222 return retval; |
|
223 } |
4543
|
224 |
|
225 bool all_zero (void) const |
4548
|
226 { |
|
227 bool retval = true; |
4543
|
228 |
4548
|
229 for (int i = 0; i < length (); i++) |
|
230 { |
|
231 if (elem (i) != 0) |
|
232 { |
|
233 retval = false; |
|
234 break; |
|
235 } |
|
236 } |
4543
|
237 |
4548
|
238 return retval; |
|
239 } |
4559
|
240 |
|
241 bool any_zero (void) const |
|
242 { |
|
243 bool retval = false; |
|
244 |
|
245 for (int i = 0; i < length (); i++) |
|
246 { |
|
247 if (elem (i) == 0) |
|
248 { |
|
249 retval = true; |
|
250 break; |
|
251 } |
|
252 } |
|
253 |
|
254 return retval; |
|
255 } |
4567
|
256 |
4635
|
257 int |
|
258 num_ones (void) const |
|
259 { |
|
260 int retval = 0; |
|
261 |
|
262 for (int i = 0; i < length (); i++) |
|
263 if (elem (i) == 1) |
|
264 retval++; |
|
265 |
|
266 return retval; |
|
267 } |
|
268 |
4567
|
269 // This is the number of elements that a matrix with this dimension |
|
270 // vector would have, NOT the number of dimensions (elements in the |
|
271 // dimension vector). |
|
272 |
|
273 int numel (void) const |
|
274 { |
|
275 int n_dims = length (); |
|
276 |
|
277 int retval = n_dims > 0 ? elem (0) : 0; |
|
278 |
|
279 for (int i = 1; i < n_dims; i++) |
|
280 retval *= elem (i); |
|
281 |
|
282 return retval; |
|
283 } |
4513
|
284 }; |
|
285 |
4543
|
286 static inline bool |
|
287 operator == (const dim_vector& a, const dim_vector& b) |
|
288 { |
|
289 bool retval = true; |
|
290 |
|
291 int a_len = a.length (); |
|
292 int b_len = b.length (); |
|
293 |
|
294 if (a_len != b_len) |
|
295 retval = false; |
|
296 else |
|
297 { |
|
298 for (int i = 0; i < a_len; i++) |
|
299 { |
|
300 if (a(i) != b(i)) |
|
301 { |
|
302 retval = false; |
|
303 break; |
|
304 } |
|
305 } |
|
306 } |
|
307 |
|
308 return retval; |
|
309 } |
|
310 |
|
311 static inline bool |
|
312 operator != (const dim_vector& a, const dim_vector& b) |
|
313 { |
|
314 return ! operator == (a, b); |
|
315 } |
|
316 |
4513
|
317 #endif |
|
318 |
|
319 /* |
|
320 ;;; Local Variables: *** |
|
321 ;;; mode: C++ *** |
|
322 ;;; End: *** |
|
323 */ |