3665
|
1 // Template array classes |
|
2 /* |
|
3 |
|
4 Copyright (C) 2000 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
3665
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <cassert> |
|
33 |
|
34 #include <iostream> |
|
35 |
|
36 #include "ArrayN.h" |
|
37 |
|
38 #if defined (HEAVYWEIGHT_INDEXING) |
|
39 #include "idx-vector.h" |
|
40 #include "ArrayN-idx.h" |
|
41 #endif |
|
42 |
|
43 #include "lo-error.h" |
|
44 |
|
45 // N-dimensional array class. |
|
46 |
|
47 template <class T> |
|
48 int |
4273
|
49 ArrayN<T>::compute_index (const Array<int>& ra_idx) const |
3665
|
50 { |
|
51 int retval = -1; |
|
52 |
|
53 int n = dimensions.length (); |
|
54 |
4273
|
55 if (n > 0 && n == ra_idx.length ()) |
3665
|
56 { |
4273
|
57 retval = ra_idx(--n); |
3665
|
58 |
|
59 while (--n >= 0) |
|
60 { |
|
61 retval *= dimensions(n); |
4273
|
62 retval += ra_idx(n); |
3665
|
63 } |
|
64 } |
|
65 else |
|
66 (*current_liboctave_error_handler) |
4273
|
67 ("ArrayN<T>::compute_index: invalid ra_idxing operation"); |
3665
|
68 |
|
69 return retval; |
|
70 } |
|
71 |
4142
|
72 // A guess (should be quite conservative). |
|
73 #define MALLOC_OVERHEAD 1024 |
|
74 |
3665
|
75 template <class T> |
|
76 int |
4273
|
77 ArrayN<T>::get_size (const Array<int>& ra_idx) |
3665
|
78 { |
|
79 // XXX KLUGE XXX |
|
80 |
|
81 // If an allocation of an array with r * c elements of type T |
|
82 // would cause an overflow in the allocator when computing the |
|
83 // size of the allocation, then return a value which, although |
|
84 // not equivalent to the actual request, should be too large for |
|
85 // most current hardware, but not so large to cause the |
|
86 // allocator to barf on computing retval * sizeof (T). |
|
87 |
|
88 static int nl; |
|
89 static double dl |
|
90 = frexp (static_cast<double> |
|
91 (INT_MAX - MALLOC_OVERHEAD) / sizeof (T), &nl); |
|
92 |
|
93 // This value should be an integer. If we return this value and |
|
94 // things work the way we expect, we should be paying a visit to |
|
95 // new_handler in no time flat. |
|
96 static int max_items = static_cast<int> (ldexp (dl, nl)); |
|
97 |
|
98 int retval = max_items; |
|
99 |
4273
|
100 int n = ra_idx.length (); |
3665
|
101 |
|
102 int nt = 0; |
|
103 double dt = 1; |
|
104 |
|
105 for (int i = 0; i < n; i++) |
|
106 { |
4273
|
107 int nra_idx; |
|
108 double dra_idx = frexp (static_cast<double> (ra_idx(i)), &nra_idx); |
3665
|
109 |
4273
|
110 nt += nra_idx; |
|
111 dt *= dra_idx; |
3665
|
112 } |
|
113 |
|
114 if (dt <= 0.5) |
|
115 { |
|
116 nt--; |
|
117 dt *= 2; |
|
118 |
|
119 if (dt <= 0.5) |
|
120 nt--; |
|
121 } |
|
122 |
|
123 if (nt < nl || (nt == nl && dt < dl)) |
|
124 { |
|
125 retval = 1; |
|
126 |
|
127 for (int i = 0; i < n; i++) |
4273
|
128 retval *= ra_idx(i); |
3665
|
129 } |
|
130 |
|
131 return retval; |
|
132 } |
|
133 |
4142
|
134 #undef MALLOC_OVERHEAD |
|
135 |
3665
|
136 template <class T> |
|
137 T |
4273
|
138 ArrayN<T>::range_error (const char *fcn, const Array<int>& ra_idx) const |
3665
|
139 { |
|
140 // XXX FIXME XXX -- report index values too! |
|
141 |
3928
|
142 (*current_liboctave_error_handler) ("range error in ArrayN"); |
3665
|
143 |
|
144 return T (); |
|
145 } |
|
146 |
|
147 template <class T> |
|
148 T& |
4273
|
149 ArrayN<T>::range_error (const char *fcn, const Array<int>& ra_idx) |
3665
|
150 { |
|
151 // XXX FIXME XXX -- report index values too! |
|
152 |
3928
|
153 (*current_liboctave_error_handler) ("range error in ArrayN"); |
3665
|
154 |
|
155 static T foo; |
|
156 return foo; |
|
157 } |
|
158 |
|
159 static inline bool |
4273
|
160 index_in_bounds (const Array<int>& ra_idx, const Array<int>& dimensions) |
3665
|
161 { |
|
162 bool retval = true; |
|
163 |
4273
|
164 int n = ra_idx.length (); |
3665
|
165 |
|
166 if (n == dimensions.length ()) |
|
167 { |
|
168 for (int i = 0; i < n; i++) |
|
169 { |
4273
|
170 if (ra_idx(i) < 0 || ra_idx(i) >= dimensions (i)) |
3665
|
171 { |
|
172 retval = false; |
|
173 break; |
|
174 } |
|
175 } |
|
176 } |
|
177 else |
|
178 retval = false; |
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
|
183 static inline void |
4273
|
184 increment_index (Array<int>& ra_idx, const Array<int>& dimensions) |
3665
|
185 { |
4273
|
186 ra_idx(0)++; |
3665
|
187 |
4273
|
188 int n = ra_idx.length () - 1; |
3665
|
189 |
|
190 for (int i = 0; i < n; i++) |
|
191 { |
4273
|
192 if (ra_idx(i) < dimensions(i)) |
3665
|
193 break; |
|
194 else |
|
195 { |
4273
|
196 ra_idx(i) = 0; |
|
197 ra_idx(i+1)++; |
3665
|
198 } |
|
199 } |
|
200 } |
|
201 |
|
202 template <class T> |
|
203 void |
|
204 ArrayN<T>::resize (const Array<int>& dims) |
|
205 { |
|
206 int n = dims.length (); |
|
207 |
|
208 for (int i = 0; i < n; i++) |
|
209 { |
|
210 if (dims(i) < 0) |
|
211 { |
|
212 (*current_liboctave_error_handler) |
|
213 ("can't resize to negative dimension"); |
|
214 return; |
|
215 } |
|
216 } |
|
217 |
|
218 bool no_change = true; |
|
219 |
|
220 for (int i = 0; i < n; i++) |
|
221 { |
|
222 if (dims(i) != dimensions(i)) |
|
223 { |
|
224 no_change = false; |
|
225 break; |
|
226 } |
|
227 } |
|
228 |
|
229 if (no_change) |
|
230 return; |
|
231 |
4473
|
232 int old_len = length (); |
|
233 |
4323
|
234 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
3665
|
235 const T *old_data = data (); |
|
236 |
4323
|
237 Array<T>::rep = new typename Array<T>::ArrayRep (get_size (dims)); |
3665
|
238 |
|
239 Array<int> old_dimensions = dimensions; |
|
240 |
|
241 dimensions = dims; |
|
242 |
4273
|
243 Array<int> ra_idx (dimensions.length (), 0); |
3665
|
244 |
|
245 for (int i = 0; i < old_len; i++) |
|
246 { |
4273
|
247 if (index_in_bounds (ra_idx, dimensions)) |
|
248 xelem (ra_idx) = old_data[i]; |
3665
|
249 |
4273
|
250 increment_index (ra_idx, dimensions); |
3665
|
251 } |
|
252 |
|
253 if (--old_rep->count <= 0) |
|
254 delete old_rep; |
|
255 } |
|
256 |
|
257 template <class T> |
|
258 void |
|
259 ArrayN<T>::resize (const Array<int>& dims, const T& val) |
|
260 { |
|
261 int n = dims.length (); |
|
262 |
|
263 for (int i = 0; i < n; i++) |
|
264 { |
|
265 if (dims(i) < 0) |
|
266 { |
|
267 (*current_liboctave_error_handler) |
|
268 ("can't resize to negative dimension"); |
|
269 return; |
|
270 } |
|
271 } |
|
272 |
|
273 bool no_change = true; |
|
274 |
|
275 for (int i = 0; i < n; i++) |
|
276 { |
|
277 if (dims(i) != dimensions(i)) |
|
278 { |
|
279 no_change = false; |
|
280 break; |
|
281 } |
|
282 } |
|
283 |
|
284 if (no_change) |
|
285 return; |
|
286 |
4323
|
287 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
3665
|
288 const T *old_data = data (); |
|
289 |
4473
|
290 int old_len = length (); |
|
291 |
3665
|
292 int len = get_size (dims); |
|
293 |
4323
|
294 Array<T>::rep = new typename Array<T>::ArrayRep (len); |
3665
|
295 |
|
296 Array<int> old_dimensions = dimensions; |
|
297 |
|
298 dimensions = dims; |
|
299 |
4273
|
300 Array<int> ra_idx (dimensions.length (), 0); |
3665
|
301 |
|
302 for (int i = 0; i < len; i++) |
4323
|
303 Array<T>::rep->elem (i) = val; |
3665
|
304 |
|
305 for (int i = 0; i < old_len; i++) |
|
306 { |
4273
|
307 if (index_in_bounds (ra_idx, dimensions)) |
|
308 xelem (ra_idx) = old_data[i]; |
3665
|
309 |
4273
|
310 increment_index (ra_idx, dimensions); |
3665
|
311 } |
|
312 |
|
313 if (--old_rep->count <= 0) |
|
314 delete old_rep; |
|
315 } |
|
316 |
|
317 template <class T> |
|
318 ArrayN<T>& |
4273
|
319 ArrayN<T>::insert (const ArrayN<T>& a, const Array<int>& ra_idx) |
3665
|
320 { |
4273
|
321 int n = ra_idx.length (); |
3665
|
322 |
|
323 if (n == dimensions.length ()) |
|
324 { |
|
325 Array<int> a_dims = a.dims (); |
|
326 |
|
327 for (int i = 0; i < n; i++) |
|
328 { |
4273
|
329 if (ra_idx(i) < 0 || ra_idx(i) + a_dims(i) > dimensions(i)) |
3665
|
330 { |
|
331 (*current_liboctave_error_handler) |
|
332 ("ArrayN<T>::insert: range error for insert"); |
|
333 return *this; |
|
334 } |
|
335 } |
|
336 |
|
337 #if 0 |
|
338 // XXX FIXME XXX -- need to copy elements |
|
339 |
|
340 for (int j = 0; j < a_cols; j++) |
|
341 for (int i = 0; i < a_rows; i++) |
|
342 elem (r+i, c+j) = a.elem (i, j); |
|
343 #endif |
|
344 |
|
345 } |
|
346 else |
|
347 (*current_liboctave_error_handler) |
|
348 ("ArrayN<T>::insert: invalid indexing operation"); |
|
349 |
|
350 return *this; |
|
351 } |
|
352 |
|
353 template <class T> |
|
354 std::ostream& |
|
355 operator << (std::ostream& os, const ArrayN<T>& a) |
|
356 { |
4473
|
357 Array<int> a_dims = a.dimensions; |
|
358 |
|
359 int n_dims = a_dims.length (); |
|
360 |
|
361 os << n_dims << "-dimensional array"; |
|
362 |
|
363 if (n_dims) |
|
364 { |
|
365 os << " ("; |
|
366 |
|
367 for (int i = 0; i < n_dims - 1; i++) |
|
368 os << a_dims(i) << "x"; |
|
369 |
|
370 os << a_dims(n_dims-1) << ")"; |
|
371 } |
|
372 |
|
373 os <<"\n\n"; |
3665
|
374 |
4473
|
375 if (n_dims) |
|
376 { |
|
377 os << "data:"; |
|
378 |
|
379 Array<int> ra_idx (n_dims,0); |
|
380 |
|
381 // Number of times the first 2d-array is to be displayed. |
3665
|
382 |
4473
|
383 int m = 1; |
|
384 for (int i = 2; i < n_dims; i++) |
|
385 m *= a_dims(i); |
|
386 |
|
387 if (m == 1) |
|
388 { |
|
389 int rows = 0; |
|
390 int cols = 0; |
|
391 |
|
392 switch (n_dims) |
|
393 { |
|
394 case 2: |
|
395 rows = a_dims(0); |
|
396 cols = a_dims(1); |
3665
|
397 |
4473
|
398 for (int j = 0; j < rows; j++) |
|
399 { |
|
400 ra_idx(0) = j; |
|
401 for (int k = 0; k < cols; k++) |
|
402 { |
|
403 ra_idx(1) = k; |
|
404 os << " " << a.elem(ra_idx); |
|
405 } |
|
406 os << "\n"; |
|
407 } |
|
408 break; |
|
409 |
|
410 case 1: |
|
411 rows = a_dims(0); |
|
412 |
|
413 for (int k = 0; k < rows; k++) |
|
414 { |
|
415 ra_idx(0) = k; |
|
416 os << " " << a.elem(ra_idx); |
|
417 } |
|
418 break; |
|
419 |
|
420 default: |
|
421 (*current_liboctave_error_handler) |
|
422 ("std::operator <<: problems with dimensions (= 0)!"); |
|
423 } |
3665
|
424 |
4473
|
425 os << "\n"; |
|
426 } |
|
427 else |
|
428 { |
|
429 int rows = a_dims(0); |
|
430 int cols = a_dims(1); |
|
431 |
|
432 for (int i = 0; i < m; i++) |
|
433 { |
|
434 os << "\n(:,:,"; |
|
435 |
|
436 for (int j = 2; j < n_dims - 1; j++) |
|
437 os << ra_idx(j) + 1 << ","; |
|
438 |
|
439 os << ra_idx(n_dims - 1) + 1 << ") = \n"; |
3665
|
440 |
4473
|
441 for (int j = 0; j < rows; j++) |
|
442 { |
|
443 ra_idx(0) = j; |
|
444 |
|
445 for (int k = 0; k < cols; k++) |
|
446 { |
|
447 ra_idx(1) = k; |
|
448 os << " " << a.elem(ra_idx); |
|
449 } |
3665
|
450 |
4473
|
451 os << "\n"; |
|
452 } |
|
453 |
|
454 os << "\n"; |
|
455 |
|
456 if (i != m - 1) |
|
457 increment_index (ra_idx, a_dims, 2); |
|
458 } |
|
459 } |
|
460 } |
3665
|
461 |
|
462 return os; |
|
463 } |
|
464 |
|
465 /* |
|
466 ;;; Local Variables: *** |
|
467 ;;; mode: C++ *** |
|
468 ;;; End: *** |
|
469 */ |