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 |
4548
|
88 for (int i = 0; i < dv_ndims; i++) |
|
89 dims[i] = dv->dims[i]; |
4513
|
90 |
4548
|
91 for (int i = dv_ndims; i < n; i++) |
|
92 dims[i] = 0; |
4513
|
93 } |
|
94 } |
|
95 |
4548
|
96 ~dim_vector_rep (void) { delete [] dims; } |
4513
|
97 |
4548
|
98 int length (void) const { return ndims; } |
4513
|
99 |
4548
|
100 int& elem (int i) |
4513
|
101 { |
4548
|
102 assert (i >= 0 && i < ndims); |
|
103 return dims[i]; |
|
104 } |
4513
|
105 |
4548
|
106 int elem (int i) const |
|
107 { |
|
108 assert (i >= 0 && i < ndims); |
4513
|
109 return dims[i]; |
|
110 } |
|
111 |
4548
|
112 private: |
|
113 |
|
114 // No assignment! |
|
115 |
|
116 dim_vector_rep& operator = (const dim_vector_rep& dv); |
|
117 }; |
|
118 |
|
119 dim_vector_rep *rep; |
|
120 |
|
121 void make_unique (void) |
|
122 { |
|
123 if (rep->count > 1) |
|
124 { |
|
125 --rep->count; |
|
126 rep = new dim_vector_rep (*rep); |
|
127 } |
|
128 } |
|
129 |
|
130 private: |
|
131 |
|
132 dim_vector_rep *nil_rep (void) const |
|
133 { |
|
134 static dim_vector_rep *nr = new dim_vector_rep (); |
|
135 |
|
136 return nr; |
|
137 } |
|
138 |
|
139 public: |
|
140 |
|
141 explicit dim_vector (void) |
|
142 : rep (nil_rep ()) { rep->count++; } |
|
143 |
|
144 explicit dim_vector (int n) |
|
145 : rep (new dim_vector_rep (n)) { } |
|
146 |
|
147 explicit dim_vector (int r, int c) |
|
148 : rep (new dim_vector_rep (r, c)) { } |
|
149 |
|
150 explicit dim_vector (int r, int c, int p) |
|
151 : rep (new dim_vector_rep (r, c, p)) { } |
|
152 |
|
153 dim_vector (const dim_vector& dv) |
|
154 : rep (dv.rep) { rep->count++; } |
|
155 |
|
156 dim_vector& operator = (const dim_vector& dv) |
|
157 { |
|
158 if (&dv != this) |
|
159 { |
|
160 if (--rep->count <= 0) |
|
161 delete rep; |
|
162 |
|
163 rep = dv.rep; |
|
164 rep->count++; |
|
165 } |
|
166 |
|
167 return *this; |
|
168 } |
|
169 |
|
170 ~dim_vector (void) |
|
171 { |
|
172 if (--rep->count <= 0) |
|
173 delete rep; |
|
174 } |
|
175 |
|
176 int length (void) const { return rep->length (); } |
|
177 |
|
178 int& elem (int i) { make_unique (); return rep->elem (i); } |
|
179 |
|
180 int elem (int i) const { return rep->elem (i); } |
4513
|
181 |
|
182 int& operator () (int i) { return elem (i); } |
|
183 |
|
184 int operator () (int i) const { return elem (i); } |
|
185 |
|
186 void resize (int n) |
4548
|
187 { |
|
188 int len = length (); |
4513
|
189 |
4548
|
190 if (n != len) |
|
191 { |
|
192 dim_vector_rep *old_rep = rep; |
4513
|
193 |
4548
|
194 rep = new dim_vector_rep (n, old_rep); |
4513
|
195 |
4548
|
196 if (--old_rep->count <= 0) |
|
197 delete old_rep; |
|
198 } |
|
199 } |
4513
|
200 |
|
201 |
4559
|
202 std::string str (char sep = 'x') const |
4548
|
203 { |
|
204 OSSTREAM buf; |
4543
|
205 |
4548
|
206 for (int i = 0; i < length (); i++) |
|
207 { |
|
208 buf << elem (i); |
4543
|
209 |
4548
|
210 if (i < length () - 1) |
4559
|
211 buf << sep; |
4548
|
212 } |
4543
|
213 |
4548
|
214 buf << OSSTREAM_ENDS; |
4543
|
215 |
4548
|
216 std::string retval = OSSTREAM_STR (buf); |
4543
|
217 |
4548
|
218 OSSTREAM_FREEZE (buf); |
4543
|
219 |
4548
|
220 return retval; |
|
221 } |
4543
|
222 |
|
223 bool all_zero (void) const |
4548
|
224 { |
|
225 bool retval = true; |
4543
|
226 |
4548
|
227 for (int i = 0; i < length (); i++) |
|
228 { |
|
229 if (elem (i) != 0) |
|
230 { |
|
231 retval = false; |
|
232 break; |
|
233 } |
|
234 } |
4543
|
235 |
4548
|
236 return retval; |
|
237 } |
4559
|
238 |
|
239 bool any_zero (void) const |
|
240 { |
|
241 bool retval = false; |
|
242 |
|
243 for (int i = 0; i < length (); i++) |
|
244 { |
|
245 if (elem (i) == 0) |
|
246 { |
|
247 retval = true; |
|
248 break; |
|
249 } |
|
250 } |
|
251 |
|
252 return retval; |
|
253 } |
4513
|
254 }; |
|
255 |
4543
|
256 static inline bool |
|
257 operator == (const dim_vector& a, const dim_vector& b) |
|
258 { |
|
259 bool retval = true; |
|
260 |
|
261 int a_len = a.length (); |
|
262 int b_len = b.length (); |
|
263 |
|
264 if (a_len != b_len) |
|
265 retval = false; |
|
266 else |
|
267 { |
|
268 for (int i = 0; i < a_len; i++) |
|
269 { |
|
270 if (a(i) != b(i)) |
|
271 { |
|
272 retval = false; |
|
273 break; |
|
274 } |
|
275 } |
|
276 } |
|
277 |
|
278 return retval; |
|
279 } |
|
280 |
|
281 static inline bool |
|
282 operator != (const dim_vector& a, const dim_vector& b) |
|
283 { |
|
284 return ! operator == (a, b); |
|
285 } |
|
286 |
4513
|
287 #endif |
|
288 |
|
289 /* |
|
290 ;;; Local Variables: *** |
|
291 ;;; mode: C++ *** |
|
292 ;;; End: *** |
|
293 */ |