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