237
|
1 // Template array classes -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 John W. Eaton |
237
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
237
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
237
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
237
|
30 #endif |
|
31 |
1367
|
32 #include <cassert> |
449
|
33 |
1560
|
34 #include <iostream.h> |
|
35 |
237
|
36 #include "Array.h" |
|
37 |
1560
|
38 #if defined (HEAVYWEIGHT_INDEXING) |
|
39 #include "idx-vector.h" |
|
40 #include "Array-idx.h" |
|
41 #endif |
|
42 |
|
43 #include "lo-error.h" |
|
44 |
1360
|
45 // One dimensional array class. Handles the reference counting for |
|
46 // all the derived classes. |
237
|
47 |
|
48 template <class T> |
|
49 Array<T>::Array (int n, const T& val) |
|
50 { |
1735
|
51 rep = new ArrayRep (n); |
1619
|
52 |
237
|
53 for (int i = 0; i < n; i++) |
|
54 rep->data[i] = val; |
1619
|
55 |
|
56 #ifdef HEAVYWEIGHT_INDEXING |
|
57 max_indices = 1; |
|
58 idx_count = 0; |
|
59 idx = 0; |
|
60 #endif |
|
61 } |
|
62 |
|
63 template <class T> |
|
64 Array<T>::~Array (void) |
|
65 { |
|
66 if (--rep->count <= 0) |
|
67 delete rep; |
|
68 |
|
69 #ifdef HEAVYWEIGHT_INDEXING |
|
70 delete [] idx; |
|
71 #endif |
237
|
72 } |
|
73 |
|
74 template <class T> |
|
75 Array<T>& |
|
76 Array<T>::operator = (const Array<T>& a) |
|
77 { |
1700
|
78 if (this != &a && rep != a.rep) |
659
|
79 { |
|
80 if (--rep->count <= 0) |
|
81 delete rep; |
237
|
82 |
659
|
83 rep = a.rep; |
|
84 rep->count++; |
|
85 } |
1619
|
86 |
|
87 #ifdef HEAVYWEIGHT_INDEXING |
|
88 max_indices = 1; |
|
89 idx_count = 0; |
|
90 idx = 0; |
|
91 #endif |
|
92 |
237
|
93 return *this; |
|
94 } |
|
95 |
|
96 template <class T> |
|
97 T& |
|
98 Array<T>::checkelem (int n) |
|
99 { |
|
100 if (n < 0 || n >= rep->length ()) |
|
101 { |
|
102 (*current_liboctave_error_handler) ("range error"); |
254
|
103 static T foo; |
237
|
104 return foo; |
|
105 } |
|
106 return elem (n); |
|
107 } |
|
108 |
|
109 template <class T> |
|
110 T |
|
111 Array<T>::elem (int n) const |
|
112 { |
|
113 return rep->elem (n); |
|
114 } |
|
115 |
|
116 template <class T> |
|
117 T |
|
118 Array<T>::checkelem (int n) const |
|
119 { |
|
120 if (n < 0 || n >= rep->length ()) |
|
121 { |
|
122 (*current_liboctave_error_handler) ("range error"); |
254
|
123 T foo; |
1470
|
124 static T *bar = &foo; |
254
|
125 return foo; |
237
|
126 } |
|
127 return elem (n); |
|
128 } |
|
129 |
|
130 template <class T> |
|
131 T |
|
132 Array<T>::operator () (int n) const |
|
133 { |
|
134 return checkelem (n); |
|
135 } |
|
136 |
|
137 template <class T> |
|
138 void |
|
139 Array<T>::resize (int n) |
|
140 { |
|
141 if (n < 0) |
|
142 { |
1560
|
143 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
144 return; |
|
145 } |
|
146 |
|
147 if (n == length ()) |
|
148 return; |
|
149 |
1735
|
150 ArrayRep *old_rep = rep; |
237
|
151 const T *old_data = data (); |
|
152 int old_len = length (); |
|
153 |
1735
|
154 rep = new ArrayRep (n); |
237
|
155 |
|
156 if (old_data && old_len > 0) |
|
157 { |
|
158 int min_len = old_len < n ? old_len : n; |
|
159 |
|
160 for (int i = 0; i < min_len; i++) |
|
161 xelem (i) = old_data[i]; |
|
162 } |
|
163 |
|
164 if (--old_rep->count <= 0) |
|
165 delete old_rep; |
|
166 } |
|
167 |
|
168 template <class T> |
|
169 void |
|
170 Array<T>::resize (int n, const T& val) |
|
171 { |
|
172 if (n < 0) |
|
173 { |
1560
|
174 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
175 return; |
|
176 } |
|
177 |
|
178 if (n == length ()) |
|
179 return; |
|
180 |
1735
|
181 ArrayRep *old_rep = rep; |
237
|
182 const T *old_data = data (); |
|
183 int old_len = length (); |
|
184 |
1735
|
185 rep = new ArrayRep (n); |
237
|
186 |
|
187 int min_len = old_len < n ? old_len : n; |
|
188 |
|
189 if (old_data && old_len > 0) |
|
190 { |
|
191 for (int i = 0; i < min_len; i++) |
|
192 xelem (i) = old_data[i]; |
|
193 } |
|
194 |
|
195 for (int i = old_len; i < n; i++) |
|
196 xelem (i) = val; |
|
197 |
|
198 if (--old_rep->count <= 0) |
|
199 delete old_rep; |
|
200 } |
|
201 |
|
202 template <class T> |
|
203 T * |
|
204 Array<T>::fortran_vec (void) |
|
205 { |
|
206 if (rep->count > 1) |
|
207 { |
|
208 --rep->count; |
1735
|
209 rep = new ArrayRep (*rep); |
237
|
210 } |
|
211 return rep->data; |
|
212 } |
|
213 |
1360
|
214 // Two dimensional array class. |
237
|
215 |
|
216 template <class T> |
|
217 T& |
|
218 Array2<T>::checkelem (int i, int j) |
|
219 { |
254
|
220 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
221 { |
|
222 (*current_liboctave_error_handler) ("range error"); |
|
223 static T foo; |
|
224 return foo; |
|
225 } |
|
226 return Array<T>::elem (d1*j+i); |
237
|
227 } |
|
228 |
|
229 template <class T> |
|
230 T |
|
231 Array2<T>::elem (int i, int j) const |
|
232 { |
|
233 return Array<T>::elem (d1*j+i); |
|
234 } |
|
235 |
|
236 template <class T> |
|
237 T |
|
238 Array2<T>::checkelem (int i, int j) const |
|
239 { |
254
|
240 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
241 { |
|
242 (*current_liboctave_error_handler) ("range error"); |
|
243 T foo; |
1470
|
244 static T *bar = &foo; |
254
|
245 return foo; |
|
246 } |
|
247 return Array<T>::elem (d1*j+i); |
237
|
248 } |
|
249 |
|
250 template <class T> |
|
251 T |
|
252 Array2<T>::operator () (int i, int j) const |
|
253 { |
254
|
254 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
255 { |
|
256 (*current_liboctave_error_handler) ("range error"); |
|
257 T foo; |
1470
|
258 static T *bar = &foo; |
254
|
259 return foo; |
|
260 } |
|
261 return Array<T>::elem (d1*j+i); |
237
|
262 } |
|
263 |
|
264 template <class T> |
|
265 void |
|
266 Array2<T>::resize (int r, int c) |
|
267 { |
|
268 if (r < 0 || c < 0) |
|
269 { |
1560
|
270 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
271 return; |
|
272 } |
|
273 |
|
274 if (r == dim1 () && c == dim2 ()) |
|
275 return; |
|
276 |
1735
|
277 ArrayRep *old_rep = rep; |
237
|
278 const T *old_data = data (); |
1560
|
279 |
237
|
280 int old_d1 = dim1 (); |
|
281 int old_d2 = dim2 (); |
|
282 int old_len = length (); |
|
283 |
1735
|
284 rep = new ArrayRep (r*c); |
237
|
285 |
|
286 d1 = r; |
|
287 d2 = c; |
|
288 |
|
289 if (old_data && old_len > 0) |
|
290 { |
|
291 int min_r = old_d1 < r ? old_d1 : r; |
|
292 int min_c = old_d2 < c ? old_d2 : c; |
|
293 |
|
294 for (int j = 0; j < min_c; j++) |
|
295 for (int i = 0; i < min_r; i++) |
|
296 xelem (i, j) = old_data[old_d1*j+i]; |
|
297 } |
|
298 |
|
299 if (--old_rep->count <= 0) |
|
300 delete old_rep; |
|
301 } |
|
302 |
|
303 template <class T> |
|
304 void |
|
305 Array2<T>::resize (int r, int c, const T& val) |
|
306 { |
|
307 if (r < 0 || c < 0) |
|
308 { |
1560
|
309 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
310 return; |
|
311 } |
|
312 |
|
313 if (r == dim1 () && c == dim2 ()) |
|
314 return; |
|
315 |
1735
|
316 ArrayRep *old_rep = rep; |
237
|
317 const T *old_data = data (); |
|
318 int old_d1 = dim1 (); |
|
319 int old_d2 = dim2 (); |
|
320 int old_len = length (); |
|
321 |
1735
|
322 rep = new ArrayRep (r*c); |
237
|
323 |
|
324 d1 = r; |
|
325 d2 = c; |
|
326 |
|
327 int min_r = old_d1 < r ? old_d1 : r; |
|
328 int min_c = old_d2 < c ? old_d2 : c; |
|
329 |
|
330 if (old_data && old_len > 0) |
|
331 { |
1321
|
332 for (int j = 0; j < min_c; j++) |
|
333 for (int i = 0; i < min_r; i++) |
237
|
334 xelem (i, j) = old_data[old_d1*j+i]; |
|
335 } |
|
336 |
1321
|
337 for (int j = 0; j < min_c; j++) |
|
338 for (int i = min_r; i < r; i++) |
237
|
339 xelem (i, j) = val; |
|
340 |
1321
|
341 for (int j = min_c; j < c; j++) |
|
342 for (int i = 0; i < r; i++) |
237
|
343 xelem (i, j) = val; |
|
344 |
|
345 if (--old_rep->count <= 0) |
|
346 delete old_rep; |
|
347 } |
|
348 |
1561
|
349 template <class T> |
|
350 Array2<T>& |
|
351 Array2<T>::insert (const Array2<T>& a, int r, int c) |
|
352 { |
|
353 int a_rows = a.rows (); |
|
354 int a_cols = a.cols (); |
1698
|
355 |
|
356 if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) |
1561
|
357 { |
|
358 (*current_liboctave_error_handler) ("range error for insert"); |
|
359 return *this; |
|
360 } |
|
361 |
|
362 for (int j = 0; j < a_cols; j++) |
|
363 for (int i = 0; i < a_rows; i++) |
|
364 elem (r+i, c+j) = a.elem (i, j); |
|
365 |
|
366 return *this; |
|
367 } |
|
368 |
1360
|
369 // Three dimensional array class. |
237
|
370 |
|
371 template <class T> |
|
372 T& |
|
373 Array3<T>::checkelem (int i, int j, int k) |
|
374 { |
254
|
375 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
376 { |
|
377 (*current_liboctave_error_handler) ("range error"); |
|
378 static T foo; |
|
379 return foo; |
|
380 } |
|
381 return Array2<T>::elem (i, d1*k+j); |
237
|
382 } |
|
383 |
|
384 template <class T> |
|
385 T |
|
386 Array3<T>::elem (int i, int j, int k) const |
|
387 { |
|
388 return Array2<T>::elem (i, d2*k+j); |
|
389 } |
|
390 |
|
391 template <class T> |
|
392 T |
|
393 Array3<T>::checkelem (int i, int j, int k) const |
|
394 { |
254
|
395 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
396 { |
|
397 (*current_liboctave_error_handler) ("range error"); |
|
398 T foo; |
1470
|
399 static T *bar = &foo; |
254
|
400 return foo; |
|
401 } |
|
402 return Array2<T>::elem (i, d1*k+j); |
237
|
403 } |
|
404 |
|
405 template <class T> |
|
406 T |
|
407 Array3<T>::operator () (int i, int j, int k) const |
|
408 { |
254
|
409 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
410 { |
|
411 (*current_liboctave_error_handler) ("range error"); |
|
412 T foo; |
1470
|
413 static T *bar = &foo; |
254
|
414 return foo; |
|
415 } |
|
416 return Array2<T>::elem (i, d2*k+j); |
237
|
417 } |
|
418 |
|
419 template <class T> |
|
420 void |
|
421 Array3<T>::resize (int n, int m, int k) |
|
422 { |
1360
|
423 assert (0); // XXX FIXME XXX |
237
|
424 } |
|
425 |
|
426 template <class T> |
|
427 void |
|
428 Array3<T>::resize (int n, int m, int k, const T& val) |
|
429 { |
1360
|
430 assert (0); // XXX FIXME XXX |
237
|
431 } |
|
432 |
1360
|
433 // A two-dimensional array with diagonal elements only. |
237
|
434 |
1574
|
435 #ifndef NO_DIAG_ARRAY |
880
|
436 #if 1 |
344
|
437 template <class T> |
|
438 T& |
|
439 DiagArray<T>::elem (int r, int c) |
|
440 { |
|
441 static T foo (0); |
|
442 return (r == c) ? Array<T>::elem (r) : foo; |
|
443 } |
|
444 |
|
445 template <class T> |
|
446 T& |
|
447 DiagArray<T>::checkelem (int r, int c) |
|
448 { |
|
449 static T foo (0); |
|
450 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
451 { |
|
452 (*current_liboctave_error_handler) ("range error"); |
|
453 return foo; |
|
454 } |
|
455 return (r == c) ? Array<T>::elem (r) : foo; |
|
456 } |
|
457 |
|
458 template <class T> |
|
459 T& |
|
460 DiagArray<T>::operator () (int r, int c) |
|
461 { |
|
462 static T foo (0); |
|
463 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
464 { |
|
465 (*current_liboctave_error_handler) ("range error"); |
|
466 return foo; |
|
467 } |
|
468 return (r == c) ? Array<T>::elem (r) : foo; |
|
469 } |
347
|
470 #endif |
344
|
471 |
237
|
472 template <class T> |
|
473 T& |
|
474 DiagArray<T>::xelem (int r, int c) |
|
475 { |
|
476 static T foo (0); |
|
477 return (r == c) ? Array<T>::xelem (r) : foo; |
|
478 } |
|
479 |
|
480 template <class T> |
|
481 T |
|
482 DiagArray<T>::elem (int r, int c) const |
|
483 { |
|
484 return (r == c) ? Array<T>::elem (r) : T (0); |
|
485 } |
|
486 |
|
487 template <class T> |
|
488 T |
|
489 DiagArray<T>::checkelem (int r, int c) const |
|
490 { |
254
|
491 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
492 { |
|
493 (*current_liboctave_error_handler) ("range error"); |
|
494 T foo; |
1470
|
495 static T *bar = &foo; |
254
|
496 return foo; |
|
497 } |
|
498 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
499 } |
|
500 |
|
501 template <class T> |
|
502 T |
|
503 DiagArray<T>::operator () (int r, int c) const |
|
504 { |
254
|
505 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
506 { |
|
507 (*current_liboctave_error_handler) ("range error"); |
|
508 T foo; |
1470
|
509 static T *bar = &foo; |
254
|
510 return foo; |
|
511 } |
|
512 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
513 } |
|
514 |
|
515 template <class T> |
|
516 void |
|
517 DiagArray<T>::resize (int r, int c) |
|
518 { |
|
519 if (r < 0 || c < 0) |
|
520 { |
1560
|
521 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
237
|
522 return; |
|
523 } |
|
524 |
|
525 if (r == dim1 () && c == dim2 ()) |
|
526 return; |
|
527 |
1735
|
528 ArrayRep *old_rep = rep; |
237
|
529 const T *old_data = data (); |
|
530 int old_len = length (); |
|
531 |
|
532 int new_len = r < c ? r : c; |
|
533 |
1735
|
534 rep = new ArrayRep (new_len); |
237
|
535 |
|
536 nr = r; |
|
537 nc = c; |
|
538 |
|
539 if (old_data && old_len > 0) |
|
540 { |
|
541 int min_len = old_len < new_len ? old_len : new_len; |
|
542 |
|
543 for (int i = 0; i < min_len; i++) |
|
544 xelem (i, i) = old_data[i]; |
|
545 } |
|
546 |
|
547 if (--old_rep->count <= 0) |
|
548 delete old_rep; |
|
549 } |
|
550 |
|
551 template <class T> |
|
552 void |
|
553 DiagArray<T>::resize (int r, int c, const T& val) |
|
554 { |
|
555 if (r < 0 || c < 0) |
|
556 { |
1560
|
557 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
237
|
558 return; |
|
559 } |
|
560 |
|
561 if (r == dim1 () && c == dim2 ()) |
|
562 return; |
|
563 |
1735
|
564 ArrayRep *old_rep = rep; |
237
|
565 const T *old_data = data (); |
|
566 int old_len = length (); |
|
567 |
|
568 int new_len = r < c ? r : c; |
|
569 |
1735
|
570 rep = new ArrayRep (new_len); |
237
|
571 |
|
572 nr = r; |
|
573 nc = c; |
|
574 |
|
575 int min_len = old_len < new_len ? old_len : new_len; |
|
576 |
|
577 if (old_data && old_len > 0) |
|
578 { |
|
579 for (int i = 0; i < min_len; i++) |
|
580 xelem (i, i) = old_data[i]; |
|
581 } |
|
582 |
|
583 for (int i = min_len; i < new_len; i++) |
|
584 xelem (i, i) = val; |
|
585 |
|
586 if (--old_rep->count <= 0) |
|
587 delete old_rep; |
|
588 } |
1574
|
589 #endif |
237
|
590 |
|
591 /* |
|
592 ;;; Local Variables: *** |
|
593 ;;; mode: C++ *** |
|
594 ;;; page-delimiter: "^/\\*" *** |
|
595 ;;; End: *** |
|
596 */ |