237
|
1 // Template array classes -*- C++ -*- |
|
2 /* |
|
3 |
309
|
4 Copyright (C) 1993, 1994 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #include <assert.h> |
|
29 |
|
30 #if defined (__GNUG__) && defined (USE_EXTERNAL_TEMPLATES) |
355
|
31 #pragma implementation |
237
|
32 #endif |
|
33 |
|
34 #include "Array.h" |
|
35 |
|
36 /* |
|
37 * The real representation of all arrays. |
|
38 */ |
|
39 |
|
40 template <class T> |
|
41 ArrayRep<T>::ArrayRep (T *d, int l) |
|
42 { |
|
43 data = d; |
|
44 len = l; |
|
45 } |
|
46 |
|
47 template <class T> |
|
48 ArrayRep<T>::ArrayRep (void) |
|
49 { |
|
50 len = 0; |
|
51 data = (T *) 0; |
|
52 } |
|
53 |
|
54 template <class T> |
|
55 ArrayRep<T>::ArrayRep (int n) |
|
56 { |
|
57 len = n; |
|
58 data = new T [len]; |
|
59 } |
|
60 |
|
61 template <class T> |
|
62 ArrayRep<T>::ArrayRep (const ArrayRep<T>& a) |
|
63 { |
|
64 len = a.len; |
|
65 count = a.count; |
|
66 data = new T [len]; |
|
67 for (int i = 0; i < len; i++) |
|
68 data[i] = a.data[i]; |
|
69 } |
|
70 |
|
71 template <class T> |
|
72 ArrayRep<T>::~ArrayRep (void) |
|
73 { |
|
74 delete [] data; |
|
75 data = (T *) 0; |
|
76 } |
|
77 |
|
78 template <class T> |
|
79 int |
|
80 ArrayRep<T>::length (void) const |
|
81 { |
|
82 return len; |
|
83 } |
|
84 |
|
85 template <class T> |
|
86 T& |
|
87 ArrayRep<T>::elem (int n) |
|
88 { |
|
89 return data[n]; |
|
90 } |
|
91 |
|
92 template <class T> |
|
93 T |
|
94 ArrayRep<T>::elem (int n) const |
|
95 { |
|
96 return data[n]; |
|
97 } |
|
98 |
|
99 /* |
|
100 * One dimensional array class. Handles the reference counting for |
|
101 * all the derived classes. |
|
102 */ |
|
103 |
|
104 template <class T> |
|
105 Array<T>::Array (T *d, int l) |
|
106 { |
|
107 rep = new ArrayRep<T> (d, l); |
|
108 rep->count = 1; |
|
109 } |
|
110 |
|
111 template <class T> |
|
112 Array<T>::Array (void) |
|
113 { |
|
114 rep = new ArrayRep<T>; |
|
115 rep->count = 1; |
|
116 } |
|
117 |
|
118 template <class T> |
|
119 Array<T>::Array (int n) |
|
120 { |
|
121 rep = new ArrayRep<T> (n); |
|
122 rep->count = 1; |
|
123 } |
|
124 |
|
125 template <class T> |
|
126 Array<T>::Array (int n, const T& val) |
|
127 { |
|
128 rep = new ArrayRep<T> (n); |
|
129 rep->count = 1; |
|
130 for (int i = 0; i < n; i++) |
|
131 rep->data[i] = val; |
|
132 } |
|
133 |
|
134 template <class T> |
|
135 Array<T>::Array (const Array<T>& a) |
|
136 { |
|
137 rep = a.rep; |
|
138 rep->count++; |
|
139 } |
|
140 |
|
141 template <class T> |
|
142 Array<T>::~Array (void) |
|
143 { |
|
144 if (--rep->count <= 0) |
|
145 delete rep; |
|
146 } |
|
147 |
|
148 template <class T> |
|
149 Array<T>& |
|
150 Array<T>::operator = (const Array<T>& a) |
|
151 { |
|
152 if (--rep->count <= 0) |
|
153 delete rep; |
|
154 |
|
155 rep = a.rep; |
|
156 rep->count++; |
|
157 return *this; |
|
158 } |
|
159 |
|
160 template <class T> |
|
161 int |
|
162 Array<T>::capacity (void) const |
|
163 { |
|
164 return rep->length (); |
|
165 } |
|
166 |
|
167 template <class T> |
|
168 int |
|
169 Array<T>::length (void) const |
|
170 { |
|
171 return rep->length (); |
|
172 } |
|
173 |
|
174 template <class T> |
|
175 T& |
|
176 Array<T>::elem (int n) |
|
177 { |
|
178 if (rep->count > 1) |
|
179 { |
|
180 --rep->count; |
|
181 rep = new ArrayRep<T> (*rep); |
|
182 rep->count = 1; |
|
183 } |
|
184 return rep->elem (n); |
|
185 } |
|
186 |
|
187 template <class T> |
|
188 T& |
|
189 Array<T>::checkelem (int n) |
|
190 { |
|
191 if (n < 0 || n >= rep->length ()) |
|
192 { |
|
193 (*current_liboctave_error_handler) ("range error"); |
254
|
194 static T foo; |
237
|
195 return foo; |
|
196 } |
|
197 return elem (n); |
|
198 } |
|
199 |
|
200 template <class T> |
|
201 T& |
|
202 Array<T>::operator () (int n) |
|
203 { |
|
204 return checkelem (n); |
|
205 } |
|
206 |
|
207 template <class T> |
|
208 T& |
|
209 Array<T>::xelem (int n) |
|
210 { |
|
211 return rep->elem (n); |
|
212 } |
|
213 |
|
214 template <class T> |
|
215 T |
|
216 Array<T>::elem (int n) const |
|
217 { |
|
218 return rep->elem (n); |
|
219 } |
|
220 |
|
221 template <class T> |
|
222 T |
|
223 Array<T>::checkelem (int n) const |
|
224 { |
|
225 if (n < 0 || n >= rep->length ()) |
|
226 { |
|
227 (*current_liboctave_error_handler) ("range error"); |
254
|
228 T foo; |
|
229 return foo; |
237
|
230 } |
|
231 return elem (n); |
|
232 } |
|
233 |
|
234 template <class T> |
|
235 T |
|
236 Array<T>::operator () (int n) const |
|
237 { |
|
238 return checkelem (n); |
|
239 } |
|
240 |
|
241 template <class T> |
|
242 void |
|
243 Array<T>::resize (int n) |
|
244 { |
|
245 if (n < 0) |
|
246 { |
|
247 (*current_liboctave_error_handler) |
|
248 ("can't resize to negative dimension"); |
|
249 return; |
|
250 } |
|
251 |
|
252 if (n == length ()) |
|
253 return; |
|
254 |
|
255 ArrayRep<T> *old_rep = rep; |
|
256 const T *old_data = data (); |
|
257 int old_len = length (); |
|
258 |
|
259 rep = new ArrayRep<T> (n); |
|
260 rep->count = 1; |
|
261 |
|
262 if (old_data && old_len > 0) |
|
263 { |
|
264 int min_len = old_len < n ? old_len : n; |
|
265 |
|
266 for (int i = 0; i < min_len; i++) |
|
267 xelem (i) = old_data[i]; |
|
268 } |
|
269 |
|
270 if (--old_rep->count <= 0) |
|
271 delete old_rep; |
|
272 } |
|
273 |
|
274 template <class T> |
|
275 void |
|
276 Array<T>::resize (int n, const T& val) |
|
277 { |
|
278 if (n < 0) |
|
279 { |
|
280 (*current_liboctave_error_handler) |
|
281 ("can't resize to negative dimension"); |
|
282 return; |
|
283 } |
|
284 |
|
285 if (n == length ()) |
|
286 return; |
|
287 |
|
288 ArrayRep<T> *old_rep = rep; |
|
289 const T *old_data = data (); |
|
290 int old_len = length (); |
|
291 |
|
292 rep = new ArrayRep<T> (n); |
|
293 rep->count = 1; |
|
294 |
|
295 int min_len = old_len < n ? old_len : n; |
|
296 |
|
297 if (old_data && old_len > 0) |
|
298 { |
|
299 for (int i = 0; i < min_len; i++) |
|
300 xelem (i) = old_data[i]; |
|
301 } |
|
302 |
|
303 for (int i = old_len; i < n; i++) |
|
304 xelem (i) = val; |
|
305 |
|
306 if (--old_rep->count <= 0) |
|
307 delete old_rep; |
|
308 } |
|
309 |
|
310 template <class T> |
|
311 const T * |
|
312 Array<T>::data (void) const |
|
313 { |
|
314 return rep->data; |
|
315 } |
|
316 |
|
317 template <class T> |
|
318 T * |
|
319 Array<T>::fortran_vec (void) |
|
320 { |
|
321 if (rep->count > 1) |
|
322 { |
|
323 --rep->count; |
|
324 rep = new ArrayRep<T> (*rep); |
|
325 rep->count = 1; |
|
326 } |
|
327 return rep->data; |
|
328 } |
|
329 |
|
330 /* |
|
331 * Two dimensional array class. |
|
332 */ |
|
333 |
|
334 template <class T> |
|
335 Array2<T>::Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
336 { |
|
337 d1 = n; |
|
338 d2 = m; |
|
339 } |
|
340 |
|
341 template <class T> |
|
342 Array2<T>::Array2 (void) : Array<T> () |
|
343 { |
|
344 d1 = 0; |
|
345 d2 = 0; |
|
346 } |
|
347 |
|
348 template <class T> |
|
349 Array2<T>::Array2 (int n, int m) : Array<T> (n*m) |
|
350 { |
|
351 d1 = n; |
|
352 d2 = m; |
|
353 } |
|
354 |
|
355 template <class T> |
|
356 Array2<T>::Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
357 { |
|
358 d1 = n; |
|
359 d2 = m; |
|
360 } |
|
361 |
|
362 template <class T> |
|
363 Array2<T>::Array2 (const Array2<T>& a) : Array<T> (a) |
|
364 { |
|
365 d1 = a.d1; |
|
366 d2 = a.d2; |
|
367 } |
|
368 |
|
369 template <class T> |
|
370 Array2<T>::Array2 (const DiagArray<T>& a) |
|
371 : Array<T> (a.rows () * a.cols (), T (0)) |
|
372 { |
|
373 for (int i = 0; i < a.length (); i++) |
|
374 elem (i, i) = a.elem (i, i); |
|
375 } |
|
376 |
|
377 template <class T> |
|
378 Array2<T>& |
|
379 Array2<T>::operator = (const Array2<T>& a) |
|
380 { |
|
381 Array<T>::operator = (a); |
|
382 d1 = a.d1; |
|
383 d2 = a.d2; |
|
384 return *this; |
|
385 } |
|
386 |
|
387 template <class T> |
|
388 int |
|
389 Array2<T>::dim1 (void) const |
|
390 { |
|
391 return d1; |
|
392 } |
|
393 |
|
394 template <class T> |
|
395 int |
|
396 Array2<T>::dim2 (void) const |
|
397 { |
|
398 return d2; |
|
399 } |
|
400 |
|
401 template <class T> |
|
402 int |
|
403 Array2<T>::rows (void) const |
|
404 { |
|
405 return d1; |
|
406 } |
|
407 |
|
408 template <class T> |
|
409 int |
|
410 Array2<T>::cols (void) const |
|
411 { |
|
412 return d2; |
|
413 } |
|
414 |
|
415 template <class T> |
|
416 int |
|
417 Array2<T>::columns (void) const |
|
418 { |
|
419 return d2; |
|
420 } |
|
421 |
|
422 template <class T> |
|
423 T& |
|
424 Array2<T>::elem (int i, int j) |
|
425 { |
|
426 return Array<T>::elem (d1*j+i); |
|
427 } |
|
428 |
|
429 template <class T> |
|
430 T& |
|
431 Array2<T>::checkelem (int i, int j) |
|
432 { |
254
|
433 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
434 { |
|
435 (*current_liboctave_error_handler) ("range error"); |
|
436 static T foo; |
|
437 return foo; |
|
438 } |
|
439 return Array<T>::elem (d1*j+i); |
237
|
440 } |
|
441 |
|
442 template <class T> |
|
443 T& |
|
444 Array2<T>::operator () (int i, int j) |
|
445 { |
254
|
446 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
447 { |
|
448 (*current_liboctave_error_handler) ("range error"); |
|
449 static T foo; |
|
450 return foo; |
|
451 } |
|
452 return Array<T>::elem (d1*j+i); |
237
|
453 } |
|
454 |
|
455 template <class T> |
|
456 T& |
|
457 Array2<T>::xelem (int i, int j) |
|
458 { |
|
459 return Array<T>::xelem (d1*j+i); |
|
460 } |
|
461 |
|
462 template <class T> |
|
463 T |
|
464 Array2<T>::elem (int i, int j) const |
|
465 { |
|
466 return Array<T>::elem (d1*j+i); |
|
467 } |
|
468 |
|
469 template <class T> |
|
470 T |
|
471 Array2<T>::checkelem (int i, int j) const |
|
472 { |
254
|
473 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
474 { |
|
475 (*current_liboctave_error_handler) ("range error"); |
|
476 T foo; |
|
477 return foo; |
|
478 } |
|
479 return Array<T>::elem (d1*j+i); |
237
|
480 } |
|
481 |
|
482 template <class T> |
|
483 T |
|
484 Array2<T>::operator () (int i, int j) const |
|
485 { |
254
|
486 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
487 { |
|
488 (*current_liboctave_error_handler) ("range error"); |
|
489 T foo; |
|
490 return foo; |
|
491 } |
|
492 return Array<T>::elem (d1*j+i); |
237
|
493 } |
|
494 |
|
495 template <class T> |
|
496 void |
|
497 Array2<T>::resize (int r, int c) |
|
498 { |
|
499 if (r < 0 || c < 0) |
|
500 { |
|
501 (*current_liboctave_error_handler) |
|
502 ("can't resize to negative dimension"); |
|
503 return; |
|
504 } |
|
505 |
|
506 if (r == dim1 () && c == dim2 ()) |
|
507 return; |
|
508 |
|
509 ArrayRep<T> *old_rep = rep; |
|
510 const T *old_data = data (); |
|
511 int old_d1 = dim1 (); |
|
512 int old_d2 = dim2 (); |
|
513 int old_len = length (); |
|
514 |
|
515 rep = new ArrayRep<T> (r*c); |
|
516 rep->count = 1; |
|
517 |
|
518 d1 = r; |
|
519 d2 = c; |
|
520 |
|
521 if (old_data && old_len > 0) |
|
522 { |
|
523 int min_r = old_d1 < r ? old_d1 : r; |
|
524 int min_c = old_d2 < c ? old_d2 : c; |
|
525 |
|
526 for (int j = 0; j < min_c; j++) |
|
527 for (int i = 0; i < min_r; i++) |
|
528 xelem (i, j) = old_data[old_d1*j+i]; |
|
529 } |
|
530 |
|
531 if (--old_rep->count <= 0) |
|
532 delete old_rep; |
|
533 } |
|
534 |
|
535 template <class T> |
|
536 void |
|
537 Array2<T>::resize (int r, int c, const T& val) |
|
538 { |
|
539 if (r < 0 || c < 0) |
|
540 { |
|
541 (*current_liboctave_error_handler) |
|
542 ("can't resize to negative dimension"); |
|
543 return; |
|
544 } |
|
545 |
|
546 if (r == dim1 () && c == dim2 ()) |
|
547 return; |
|
548 |
|
549 ArrayRep<T> *old_rep = rep; |
|
550 const T *old_data = data (); |
|
551 int old_d1 = dim1 (); |
|
552 int old_d2 = dim2 (); |
|
553 int old_len = length (); |
|
554 |
|
555 rep = new ArrayRep<T> (r*c); |
|
556 rep->count = 1; |
|
557 |
|
558 d1 = r; |
|
559 d2 = c; |
|
560 |
|
561 int min_r = old_d1 < r ? old_d1 : r; |
|
562 int min_c = old_d2 < c ? old_d2 : c; |
|
563 |
|
564 int i, j; |
|
565 |
|
566 if (old_data && old_len > 0) |
|
567 { |
|
568 for (j = 0; j < min_c; j++) |
|
569 for (i = 0; i < min_r; i++) |
|
570 xelem (i, j) = old_data[old_d1*j+i]; |
|
571 } |
|
572 |
|
573 for (j = 0; j < min_c; j++) |
|
574 for (i = min_r; i < r; i++) |
|
575 xelem (i, j) = val; |
|
576 |
|
577 for (j = min_c; j < c; j++) |
|
578 for (i = 0; i < r; i++) |
|
579 xelem (i, j) = val; |
|
580 |
|
581 if (--old_rep->count <= 0) |
|
582 delete old_rep; |
|
583 } |
|
584 |
|
585 /* |
|
586 * Three dimensional array class. |
|
587 */ |
|
588 |
|
589 template <class T> |
|
590 Array3<T>::Array3 (T *d, int n, int m, int k) : Array2<T> (d, n, m*k) |
|
591 { |
|
592 d2 = m; |
|
593 d3 = k; |
|
594 } |
|
595 |
|
596 template <class T> |
|
597 Array3<T>::Array3 (void) : Array2<T> () |
|
598 { |
|
599 d2 = 0; |
|
600 d3 = 0; |
|
601 } |
|
602 |
|
603 template <class T> |
|
604 Array3<T>::Array3 (int n, int m, int k) : Array2<T> (n, m*k) |
|
605 { |
|
606 d2 = m; |
|
607 d3 = k; |
|
608 } |
|
609 |
|
610 template <class T> |
|
611 Array3<T>::Array3 (int n, int m, int k, const T& val) : Array2<T> (n, m*k, val) |
|
612 { |
|
613 d2 = m; |
|
614 d3 = k; |
|
615 } |
|
616 |
|
617 template <class T> |
|
618 Array3<T>::Array3 (const Array3<T>& a) : Array2<T> (a) |
|
619 { |
|
620 d2 = a.d2; |
|
621 d3 = a.d3; |
|
622 } |
|
623 |
|
624 template <class T> |
|
625 Array3<T>& |
|
626 Array3<T>::operator = (const Array3<T>& a) |
|
627 { |
|
628 Array<T>::operator = (a); |
|
629 d1 = a.d1; |
|
630 d2 = a.d2; |
|
631 d3 = a.d3; |
|
632 return *this; |
|
633 } |
|
634 |
|
635 template <class T> |
|
636 int |
|
637 Array3<T>::dim3 (void) const |
|
638 { |
|
639 return d3; |
|
640 } |
|
641 |
|
642 template <class T> |
|
643 T& |
|
644 Array3<T>::elem (int i, int j, int k) |
|
645 { |
|
646 return Array2<T>::elem (i, d2*k+j); |
|
647 } |
|
648 |
|
649 template <class T> |
|
650 T& |
|
651 Array3<T>::checkelem (int i, int j, int k) |
|
652 { |
254
|
653 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
654 { |
|
655 (*current_liboctave_error_handler) ("range error"); |
|
656 static T foo; |
|
657 return foo; |
|
658 } |
|
659 return Array2<T>::elem (i, d1*k+j); |
237
|
660 } |
|
661 |
|
662 template <class T> |
|
663 T& |
|
664 Array3<T>::operator () (int i, int j, int k) |
|
665 { |
254
|
666 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
667 { |
|
668 (*current_liboctave_error_handler) ("range error"); |
|
669 static T foo; |
|
670 return foo; |
|
671 } |
|
672 return Array2<T>::elem (i, d2*k+j); |
237
|
673 } |
|
674 |
|
675 template <class T> |
|
676 T& |
|
677 Array3<T>::xelem (int i, int j, int k) |
|
678 { |
|
679 return Array2<T>::xelem (i, d2*k+j); |
|
680 } |
|
681 |
|
682 template <class T> |
|
683 T |
|
684 Array3<T>::elem (int i, int j, int k) const |
|
685 { |
|
686 return Array2<T>::elem (i, d2*k+j); |
|
687 } |
|
688 |
|
689 template <class T> |
|
690 T |
|
691 Array3<T>::checkelem (int i, int j, int k) const |
|
692 { |
254
|
693 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
694 { |
|
695 (*current_liboctave_error_handler) ("range error"); |
|
696 T foo; |
|
697 return foo; |
|
698 } |
|
699 return Array2<T>::elem (i, d1*k+j); |
237
|
700 } |
|
701 |
|
702 template <class T> |
|
703 T |
|
704 Array3<T>::operator () (int i, int j, int k) const |
|
705 { |
254
|
706 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
707 { |
|
708 (*current_liboctave_error_handler) ("range error"); |
|
709 T foo; |
|
710 return foo; |
|
711 } |
|
712 return Array2<T>::elem (i, d2*k+j); |
237
|
713 } |
|
714 |
|
715 template <class T> |
|
716 void |
|
717 Array3<T>::resize (int n, int m, int k) |
|
718 { |
|
719 assert (0); /* XXX FIXME XXX */ |
|
720 } |
|
721 |
|
722 template <class T> |
|
723 void |
|
724 Array3<T>::resize (int n, int m, int k, const T& val) |
|
725 { |
|
726 assert (0); /* XXX FIXME XXX */ |
|
727 } |
|
728 |
|
729 /* |
|
730 * A two-dimensional array with diagonal elements only. |
|
731 */ |
|
732 |
|
733 template <class T> |
|
734 DiagArray<T>::DiagArray (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
|
735 { |
|
736 nr = r; |
|
737 nc = c; |
|
738 } |
|
739 |
|
740 template <class T> |
|
741 DiagArray<T>::DiagArray (void) : Array<T> () |
|
742 { |
|
743 nr = 0; |
|
744 nc = 0; |
|
745 } |
|
746 |
|
747 template <class T> |
|
748 DiagArray<T>::DiagArray (int n) : Array<T> (n) |
|
749 { |
|
750 nr = n; |
|
751 nc = n; |
|
752 } |
|
753 |
|
754 template <class T> |
|
755 DiagArray<T>::DiagArray (int n, const T& val) : Array<T> (n, val) |
|
756 { |
248
|
757 nr = nc = n; |
237
|
758 } |
|
759 |
|
760 template <class T> |
|
761 DiagArray<T>::DiagArray (int r, int c) : Array<T> (r < c ? r : c) |
|
762 { |
|
763 nr = r; |
|
764 nc = c; |
|
765 } |
|
766 |
|
767 template <class T> |
|
768 DiagArray<T>::DiagArray (int r, int c, const T& val) |
|
769 : Array<T> (r < c ? r : c, val) |
|
770 { |
|
771 nr = r; |
|
772 nc = c; |
|
773 } |
|
774 |
|
775 template <class T> |
|
776 DiagArray<T>::DiagArray (const Array<T>& a) : Array<T> (a) |
|
777 { |
|
778 nr = nc = a.length (); |
|
779 } |
|
780 |
|
781 template <class T> |
|
782 DiagArray<T>::DiagArray (const DiagArray<T>& a) : Array<T> (a) |
|
783 { |
|
784 nr = a.nr; |
|
785 nc = a.nc; |
|
786 } |
|
787 |
|
788 template <class T> |
|
789 DiagArray<T>& |
|
790 DiagArray<T>::operator = (const DiagArray<T>& a) |
|
791 { |
|
792 Array<T>::operator = (a); |
|
793 nr = a.nr; |
|
794 nc = a.nc; |
|
795 return *this; |
|
796 } |
|
797 |
|
798 template <class T> |
|
799 int |
|
800 DiagArray<T>::dim1 (void) const |
|
801 { |
|
802 return nr; |
|
803 } |
|
804 |
|
805 template <class T> |
|
806 int |
|
807 DiagArray<T>::dim2 (void) const |
|
808 { |
|
809 return nc; |
|
810 } |
|
811 |
|
812 template <class T> |
|
813 int |
|
814 DiagArray<T>::rows (void) const |
|
815 { |
|
816 return nr; |
|
817 } |
|
818 |
|
819 template <class T> |
|
820 int |
|
821 DiagArray<T>::cols (void) const |
|
822 { |
|
823 return nc; |
|
824 } |
|
825 |
|
826 template <class T> |
|
827 int |
|
828 DiagArray<T>::columns (void) const |
|
829 { |
|
830 return nc; |
|
831 } |
|
832 |
347
|
833 #if defined (_AIX) |
344
|
834 template <class T> |
|
835 T& |
|
836 DiagArray<T>::elem (int r, int c) |
|
837 { |
|
838 static T foo (0); |
|
839 return (r == c) ? Array<T>::elem (r) : foo; |
|
840 } |
|
841 |
|
842 template <class T> |
|
843 T& |
|
844 DiagArray<T>::checkelem (int r, int c) |
|
845 { |
|
846 static T foo (0); |
|
847 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
848 { |
|
849 (*current_liboctave_error_handler) ("range error"); |
|
850 return foo; |
|
851 } |
|
852 return (r == c) ? Array<T>::elem (r) : foo; |
|
853 } |
|
854 |
|
855 template <class T> |
|
856 T& |
|
857 DiagArray<T>::operator () (int r, int c) |
|
858 { |
|
859 static T foo (0); |
|
860 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
861 { |
|
862 (*current_liboctave_error_handler) ("range error"); |
|
863 return foo; |
|
864 } |
|
865 return (r == c) ? Array<T>::elem (r) : foo; |
|
866 } |
347
|
867 #endif |
344
|
868 |
237
|
869 template <class T> |
|
870 T& |
|
871 DiagArray<T>::xelem (int r, int c) |
|
872 { |
|
873 static T foo (0); |
|
874 return (r == c) ? Array<T>::xelem (r) : foo; |
|
875 } |
|
876 |
|
877 template <class T> |
|
878 T |
|
879 DiagArray<T>::elem (int r, int c) const |
|
880 { |
|
881 return (r == c) ? Array<T>::elem (r) : T (0); |
|
882 } |
|
883 |
|
884 template <class T> |
|
885 T |
|
886 DiagArray<T>::checkelem (int r, int c) const |
|
887 { |
254
|
888 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
889 { |
|
890 (*current_liboctave_error_handler) ("range error"); |
|
891 T foo; |
|
892 return foo; |
|
893 } |
|
894 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
895 } |
|
896 |
|
897 template <class T> |
|
898 T |
|
899 DiagArray<T>::operator () (int r, int c) const |
|
900 { |
254
|
901 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
902 { |
|
903 (*current_liboctave_error_handler) ("range error"); |
|
904 T foo; |
|
905 return foo; |
|
906 } |
|
907 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
908 } |
|
909 |
|
910 template <class T> |
|
911 void |
|
912 DiagArray<T>::resize (int r, int c) |
|
913 { |
|
914 if (r < 0 || c < 0) |
|
915 { |
|
916 (*current_liboctave_error_handler) |
|
917 ("can't resize to negative dimensions"); |
|
918 return; |
|
919 } |
|
920 |
|
921 if (r == dim1 () && c == dim2 ()) |
|
922 return; |
|
923 |
|
924 ArrayRep<T> *old_rep = rep; |
|
925 const T *old_data = data (); |
|
926 int old_len = length (); |
|
927 |
|
928 int new_len = r < c ? r : c; |
|
929 |
|
930 rep = new ArrayRep<T> (new_len); |
|
931 rep->count = 1; |
|
932 |
|
933 nr = r; |
|
934 nc = c; |
|
935 |
|
936 if (old_data && old_len > 0) |
|
937 { |
|
938 int min_len = old_len < new_len ? old_len : new_len; |
|
939 |
|
940 for (int i = 0; i < min_len; i++) |
|
941 xelem (i, i) = old_data[i]; |
|
942 } |
|
943 |
|
944 if (--old_rep->count <= 0) |
|
945 delete old_rep; |
|
946 } |
|
947 |
|
948 template <class T> |
|
949 void |
|
950 DiagArray<T>::resize (int r, int c, const T& val) |
|
951 { |
|
952 if (r < 0 || c < 0) |
|
953 { |
|
954 (*current_liboctave_error_handler) |
|
955 ("can't resize to negative dimensions"); |
|
956 return; |
|
957 } |
|
958 |
|
959 if (r == dim1 () && c == dim2 ()) |
|
960 return; |
|
961 |
|
962 ArrayRep<T> *old_rep = rep; |
|
963 const T *old_data = data (); |
|
964 int old_len = length (); |
|
965 |
|
966 int new_len = r < c ? r : c; |
|
967 |
|
968 rep = new ArrayRep<T> (new_len); |
|
969 rep->count = 1; |
|
970 |
|
971 nr = r; |
|
972 nc = c; |
|
973 |
|
974 int min_len = old_len < new_len ? old_len : new_len; |
|
975 |
|
976 if (old_data && old_len > 0) |
|
977 { |
|
978 for (int i = 0; i < min_len; i++) |
|
979 xelem (i, i) = old_data[i]; |
|
980 } |
|
981 |
|
982 for (int i = min_len; i < new_len; i++) |
|
983 xelem (i, i) = val; |
|
984 |
|
985 if (--old_rep->count <= 0) |
|
986 delete old_rep; |
|
987 } |
|
988 |
309
|
989 #if defined (__GNUG__) && defined (USE_EXTERNAL_TEMPLATES) |
|
990 #if defined (OCTAVE_SOURCE) |
237
|
991 |
|
992 typedef Array<double> array_type_double; |
|
993 typedef Array2<double> array2_type_double; |
|
994 typedef DiagArray<double> diag_array_type_double; |
|
995 |
|
996 #include <Complex.h> |
|
997 typedef Array<Complex> array_type_complex; |
|
998 typedef Array2<Complex> array2_type_complex; |
|
999 typedef DiagArray<Complex> diag_array_type_complex; |
|
1000 |
309
|
1001 #elif defined (USER_TYPEDEFS) |
|
1002 |
|
1003 // Users can generate their own .o files with their own types, as many |
|
1004 // times as they like. USER_TYPEDEFS should be defined to be the name |
|
1005 // of an include file that contains typdefs for the desired types. |
|
1006 // |
|
1007 // For example, if my-types.h contains typedefs for the Array types |
|
1008 // you are interested in, you might compile this file with the command |
|
1009 // |
386
|
1010 // g++ -fexternal-templates -DUSE_EXTERNAL_TEMPLATES \ |
309
|
1011 // -DUSER_TYPEDEFS=\"my-types.h\" |
|
1012 |
|
1013 #include USER_TYPEDEFS |
|
1014 |
237
|
1015 #endif |
|
1016 #endif |
|
1017 |
|
1018 /* |
|
1019 ;;; Local Variables: *** |
|
1020 ;;; mode: C++ *** |
|
1021 ;;; page-delimiter: "^/\\*" *** |
|
1022 ;;; End: *** |
|
1023 */ |