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) |
|
31 #pragma implementation |
|
32 #endif |
|
33 |
|
34 #include "Array.h" |
|
35 #include "lo-error.h" |
|
36 |
|
37 /* |
|
38 * The real representation of all arrays. |
|
39 */ |
|
40 |
|
41 template <class T> |
|
42 ArrayRep<T>::ArrayRep (T *d, int l) |
|
43 { |
|
44 data = d; |
|
45 len = l; |
|
46 } |
|
47 |
|
48 template <class T> |
|
49 ArrayRep<T>::ArrayRep (void) |
|
50 { |
|
51 len = 0; |
|
52 data = (T *) 0; |
|
53 } |
|
54 |
|
55 template <class T> |
|
56 ArrayRep<T>::ArrayRep (int n) |
|
57 { |
|
58 len = n; |
|
59 data = new T [len]; |
|
60 } |
|
61 |
|
62 template <class T> |
|
63 ArrayRep<T>::ArrayRep (const ArrayRep<T>& a) |
|
64 { |
|
65 len = a.len; |
|
66 count = a.count; |
|
67 data = new T [len]; |
|
68 for (int i = 0; i < len; i++) |
|
69 data[i] = a.data[i]; |
|
70 } |
|
71 |
|
72 template <class T> |
|
73 ArrayRep<T>::~ArrayRep (void) |
|
74 { |
|
75 delete [] data; |
|
76 data = (T *) 0; |
|
77 } |
|
78 |
|
79 template <class T> |
|
80 int |
|
81 ArrayRep<T>::length (void) const |
|
82 { |
|
83 return len; |
|
84 } |
|
85 |
|
86 template <class T> |
|
87 T& |
|
88 ArrayRep<T>::elem (int n) |
|
89 { |
|
90 return data[n]; |
|
91 } |
|
92 |
|
93 template <class T> |
|
94 T |
|
95 ArrayRep<T>::elem (int n) const |
|
96 { |
|
97 return data[n]; |
|
98 } |
|
99 |
|
100 /* |
|
101 * One dimensional array class. Handles the reference counting for |
|
102 * all the derived classes. |
|
103 */ |
|
104 |
|
105 template <class T> |
|
106 Array<T>::Array (T *d, int l) |
|
107 { |
|
108 rep = new ArrayRep<T> (d, l); |
|
109 rep->count = 1; |
|
110 } |
|
111 |
|
112 template <class T> |
|
113 Array<T>::Array (void) |
|
114 { |
|
115 rep = new ArrayRep<T>; |
|
116 rep->count = 1; |
|
117 } |
|
118 |
|
119 template <class T> |
|
120 Array<T>::Array (int n) |
|
121 { |
|
122 rep = new ArrayRep<T> (n); |
|
123 rep->count = 1; |
|
124 } |
|
125 |
|
126 template <class T> |
|
127 Array<T>::Array (int n, const T& val) |
|
128 { |
|
129 rep = new ArrayRep<T> (n); |
|
130 rep->count = 1; |
|
131 for (int i = 0; i < n; i++) |
|
132 rep->data[i] = val; |
|
133 } |
|
134 |
|
135 template <class T> |
|
136 Array<T>::Array (const Array<T>& a) |
|
137 { |
|
138 rep = a.rep; |
|
139 rep->count++; |
|
140 } |
|
141 |
|
142 template <class T> |
|
143 Array<T>::~Array (void) |
|
144 { |
|
145 if (--rep->count <= 0) |
|
146 delete rep; |
|
147 } |
|
148 |
|
149 template <class T> |
|
150 Array<T>& |
|
151 Array<T>::operator = (const Array<T>& a) |
|
152 { |
|
153 if (--rep->count <= 0) |
|
154 delete rep; |
|
155 |
|
156 rep = a.rep; |
|
157 rep->count++; |
|
158 return *this; |
|
159 } |
|
160 |
|
161 template <class T> |
|
162 int |
|
163 Array<T>::capacity (void) const |
|
164 { |
|
165 return rep->length (); |
|
166 } |
|
167 |
|
168 template <class T> |
|
169 int |
|
170 Array<T>::length (void) const |
|
171 { |
|
172 return rep->length (); |
|
173 } |
|
174 |
|
175 template <class T> |
|
176 T& |
|
177 Array<T>::elem (int n) |
|
178 { |
|
179 if (rep->count > 1) |
|
180 { |
|
181 --rep->count; |
|
182 rep = new ArrayRep<T> (*rep); |
|
183 rep->count = 1; |
|
184 } |
|
185 return rep->elem (n); |
|
186 } |
|
187 |
|
188 template <class T> |
|
189 T& |
|
190 Array<T>::checkelem (int n) |
|
191 { |
|
192 if (n < 0 || n >= rep->length ()) |
|
193 { |
|
194 (*current_liboctave_error_handler) ("range error"); |
254
|
195 static T foo; |
237
|
196 return foo; |
|
197 } |
|
198 return elem (n); |
|
199 } |
|
200 |
|
201 template <class T> |
|
202 T& |
|
203 Array<T>::operator () (int n) |
|
204 { |
|
205 return checkelem (n); |
|
206 } |
|
207 |
|
208 template <class T> |
|
209 T& |
|
210 Array<T>::xelem (int n) |
|
211 { |
|
212 return rep->elem (n); |
|
213 } |
|
214 |
|
215 template <class T> |
|
216 T |
|
217 Array<T>::elem (int n) const |
|
218 { |
|
219 return rep->elem (n); |
|
220 } |
|
221 |
|
222 template <class T> |
|
223 T |
|
224 Array<T>::checkelem (int n) const |
|
225 { |
|
226 if (n < 0 || n >= rep->length ()) |
|
227 { |
|
228 (*current_liboctave_error_handler) ("range error"); |
254
|
229 T foo; |
|
230 return foo; |
237
|
231 } |
|
232 return elem (n); |
|
233 } |
|
234 |
|
235 template <class T> |
|
236 T |
|
237 Array<T>::operator () (int n) const |
|
238 { |
|
239 return checkelem (n); |
|
240 } |
|
241 |
|
242 template <class T> |
|
243 void |
|
244 Array<T>::resize (int n) |
|
245 { |
|
246 if (n < 0) |
|
247 { |
|
248 (*current_liboctave_error_handler) |
|
249 ("can't resize to negative dimension"); |
|
250 return; |
|
251 } |
|
252 |
|
253 if (n == length ()) |
|
254 return; |
|
255 |
|
256 ArrayRep<T> *old_rep = rep; |
|
257 const T *old_data = data (); |
|
258 int old_len = length (); |
|
259 |
|
260 rep = new ArrayRep<T> (n); |
|
261 rep->count = 1; |
|
262 |
|
263 if (old_data && old_len > 0) |
|
264 { |
|
265 int min_len = old_len < n ? old_len : n; |
|
266 |
|
267 for (int i = 0; i < min_len; i++) |
|
268 xelem (i) = old_data[i]; |
|
269 } |
|
270 |
|
271 if (--old_rep->count <= 0) |
|
272 delete old_rep; |
|
273 } |
|
274 |
|
275 template <class T> |
|
276 void |
|
277 Array<T>::resize (int n, const T& val) |
|
278 { |
|
279 if (n < 0) |
|
280 { |
|
281 (*current_liboctave_error_handler) |
|
282 ("can't resize to negative dimension"); |
|
283 return; |
|
284 } |
|
285 |
|
286 if (n == length ()) |
|
287 return; |
|
288 |
|
289 ArrayRep<T> *old_rep = rep; |
|
290 const T *old_data = data (); |
|
291 int old_len = length (); |
|
292 |
|
293 rep = new ArrayRep<T> (n); |
|
294 rep->count = 1; |
|
295 |
|
296 int min_len = old_len < n ? old_len : n; |
|
297 |
|
298 if (old_data && old_len > 0) |
|
299 { |
|
300 for (int i = 0; i < min_len; i++) |
|
301 xelem (i) = old_data[i]; |
|
302 } |
|
303 |
|
304 for (int i = old_len; i < n; i++) |
|
305 xelem (i) = val; |
|
306 |
|
307 if (--old_rep->count <= 0) |
|
308 delete old_rep; |
|
309 } |
|
310 |
|
311 template <class T> |
|
312 const T * |
|
313 Array<T>::data (void) const |
|
314 { |
|
315 return rep->data; |
|
316 } |
|
317 |
|
318 template <class T> |
|
319 T * |
|
320 Array<T>::fortran_vec (void) |
|
321 { |
|
322 if (rep->count > 1) |
|
323 { |
|
324 --rep->count; |
|
325 rep = new ArrayRep<T> (*rep); |
|
326 rep->count = 1; |
|
327 } |
|
328 return rep->data; |
|
329 } |
|
330 |
|
331 /* |
|
332 * Two dimensional array class. |
|
333 */ |
|
334 |
|
335 template <class T> |
|
336 Array2<T>::Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
337 { |
|
338 d1 = n; |
|
339 d2 = m; |
|
340 } |
|
341 |
|
342 template <class T> |
|
343 Array2<T>::Array2 (void) : Array<T> () |
|
344 { |
|
345 d1 = 0; |
|
346 d2 = 0; |
|
347 } |
|
348 |
|
349 template <class T> |
|
350 Array2<T>::Array2 (int n, int m) : Array<T> (n*m) |
|
351 { |
|
352 d1 = n; |
|
353 d2 = m; |
|
354 } |
|
355 |
|
356 template <class T> |
|
357 Array2<T>::Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
358 { |
|
359 d1 = n; |
|
360 d2 = m; |
|
361 } |
|
362 |
|
363 template <class T> |
|
364 Array2<T>::Array2 (const Array2<T>& a) : Array<T> (a) |
|
365 { |
|
366 d1 = a.d1; |
|
367 d2 = a.d2; |
|
368 } |
|
369 |
|
370 template <class T> |
|
371 Array2<T>::Array2 (const DiagArray<T>& a) |
|
372 : Array<T> (a.rows () * a.cols (), T (0)) |
|
373 { |
|
374 for (int i = 0; i < a.length (); i++) |
|
375 elem (i, i) = a.elem (i, i); |
|
376 } |
|
377 |
|
378 template <class T> |
|
379 Array2<T>& |
|
380 Array2<T>::operator = (const Array2<T>& a) |
|
381 { |
|
382 Array<T>::operator = (a); |
|
383 d1 = a.d1; |
|
384 d2 = a.d2; |
|
385 return *this; |
|
386 } |
|
387 |
|
388 template <class T> |
|
389 int |
|
390 Array2<T>::dim1 (void) const |
|
391 { |
|
392 return d1; |
|
393 } |
|
394 |
|
395 template <class T> |
|
396 int |
|
397 Array2<T>::dim2 (void) const |
|
398 { |
|
399 return d2; |
|
400 } |
|
401 |
|
402 template <class T> |
|
403 int |
|
404 Array2<T>::rows (void) const |
|
405 { |
|
406 return d1; |
|
407 } |
|
408 |
|
409 template <class T> |
|
410 int |
|
411 Array2<T>::cols (void) const |
|
412 { |
|
413 return d2; |
|
414 } |
|
415 |
|
416 template <class T> |
|
417 int |
|
418 Array2<T>::columns (void) const |
|
419 { |
|
420 return d2; |
|
421 } |
|
422 |
|
423 template <class T> |
|
424 T& |
|
425 Array2<T>::elem (int i, int j) |
|
426 { |
|
427 return Array<T>::elem (d1*j+i); |
|
428 } |
|
429 |
|
430 template <class T> |
|
431 T& |
|
432 Array2<T>::checkelem (int i, int j) |
|
433 { |
254
|
434 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
435 { |
|
436 (*current_liboctave_error_handler) ("range error"); |
|
437 static T foo; |
|
438 return foo; |
|
439 } |
|
440 return Array<T>::elem (d1*j+i); |
237
|
441 } |
|
442 |
|
443 template <class T> |
|
444 T& |
|
445 Array2<T>::operator () (int i, int j) |
|
446 { |
254
|
447 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
448 { |
|
449 (*current_liboctave_error_handler) ("range error"); |
|
450 static T foo; |
|
451 return foo; |
|
452 } |
|
453 return Array<T>::elem (d1*j+i); |
237
|
454 } |
|
455 |
|
456 template <class T> |
|
457 T& |
|
458 Array2<T>::xelem (int i, int j) |
|
459 { |
|
460 return Array<T>::xelem (d1*j+i); |
|
461 } |
|
462 |
|
463 template <class T> |
|
464 T |
|
465 Array2<T>::elem (int i, int j) const |
|
466 { |
|
467 return Array<T>::elem (d1*j+i); |
|
468 } |
|
469 |
|
470 template <class T> |
|
471 T |
|
472 Array2<T>::checkelem (int i, int j) const |
|
473 { |
254
|
474 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
475 { |
|
476 (*current_liboctave_error_handler) ("range error"); |
|
477 T foo; |
|
478 return foo; |
|
479 } |
|
480 return Array<T>::elem (d1*j+i); |
237
|
481 } |
|
482 |
|
483 template <class T> |
|
484 T |
|
485 Array2<T>::operator () (int i, int j) const |
|
486 { |
254
|
487 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
488 { |
|
489 (*current_liboctave_error_handler) ("range error"); |
|
490 T foo; |
|
491 return foo; |
|
492 } |
|
493 return Array<T>::elem (d1*j+i); |
237
|
494 } |
|
495 |
|
496 template <class T> |
|
497 void |
|
498 Array2<T>::resize (int r, int c) |
|
499 { |
|
500 if (r < 0 || c < 0) |
|
501 { |
|
502 (*current_liboctave_error_handler) |
|
503 ("can't resize to negative dimension"); |
|
504 return; |
|
505 } |
|
506 |
|
507 if (r == dim1 () && c == dim2 ()) |
|
508 return; |
|
509 |
|
510 ArrayRep<T> *old_rep = rep; |
|
511 const T *old_data = data (); |
|
512 int old_d1 = dim1 (); |
|
513 int old_d2 = dim2 (); |
|
514 int old_len = length (); |
|
515 |
|
516 rep = new ArrayRep<T> (r*c); |
|
517 rep->count = 1; |
|
518 |
|
519 d1 = r; |
|
520 d2 = c; |
|
521 |
|
522 if (old_data && old_len > 0) |
|
523 { |
|
524 int min_r = old_d1 < r ? old_d1 : r; |
|
525 int min_c = old_d2 < c ? old_d2 : c; |
|
526 |
|
527 for (int j = 0; j < min_c; j++) |
|
528 for (int i = 0; i < min_r; i++) |
|
529 xelem (i, j) = old_data[old_d1*j+i]; |
|
530 } |
|
531 |
|
532 if (--old_rep->count <= 0) |
|
533 delete old_rep; |
|
534 } |
|
535 |
|
536 template <class T> |
|
537 void |
|
538 Array2<T>::resize (int r, int c, const T& val) |
|
539 { |
|
540 if (r < 0 || c < 0) |
|
541 { |
|
542 (*current_liboctave_error_handler) |
|
543 ("can't resize to negative dimension"); |
|
544 return; |
|
545 } |
|
546 |
|
547 if (r == dim1 () && c == dim2 ()) |
|
548 return; |
|
549 |
|
550 ArrayRep<T> *old_rep = rep; |
|
551 const T *old_data = data (); |
|
552 int old_d1 = dim1 (); |
|
553 int old_d2 = dim2 (); |
|
554 int old_len = length (); |
|
555 |
|
556 rep = new ArrayRep<T> (r*c); |
|
557 rep->count = 1; |
|
558 |
|
559 d1 = r; |
|
560 d2 = c; |
|
561 |
|
562 int min_r = old_d1 < r ? old_d1 : r; |
|
563 int min_c = old_d2 < c ? old_d2 : c; |
|
564 |
|
565 int i, j; |
|
566 |
|
567 if (old_data && old_len > 0) |
|
568 { |
|
569 for (j = 0; j < min_c; j++) |
|
570 for (i = 0; i < min_r; i++) |
|
571 xelem (i, j) = old_data[old_d1*j+i]; |
|
572 } |
|
573 |
|
574 for (j = 0; j < min_c; j++) |
|
575 for (i = min_r; i < r; i++) |
|
576 xelem (i, j) = val; |
|
577 |
|
578 for (j = min_c; j < c; j++) |
|
579 for (i = 0; i < r; i++) |
|
580 xelem (i, j) = val; |
|
581 |
|
582 if (--old_rep->count <= 0) |
|
583 delete old_rep; |
|
584 } |
|
585 |
|
586 /* |
|
587 * Three dimensional array class. |
|
588 */ |
|
589 |
|
590 template <class T> |
|
591 Array3<T>::Array3 (T *d, int n, int m, int k) : Array2<T> (d, n, m*k) |
|
592 { |
|
593 d2 = m; |
|
594 d3 = k; |
|
595 } |
|
596 |
|
597 template <class T> |
|
598 Array3<T>::Array3 (void) : Array2<T> () |
|
599 { |
|
600 d2 = 0; |
|
601 d3 = 0; |
|
602 } |
|
603 |
|
604 template <class T> |
|
605 Array3<T>::Array3 (int n, int m, int k) : Array2<T> (n, m*k) |
|
606 { |
|
607 d2 = m; |
|
608 d3 = k; |
|
609 } |
|
610 |
|
611 template <class T> |
|
612 Array3<T>::Array3 (int n, int m, int k, const T& val) : Array2<T> (n, m*k, val) |
|
613 { |
|
614 d2 = m; |
|
615 d3 = k; |
|
616 } |
|
617 |
|
618 template <class T> |
|
619 Array3<T>::Array3 (const Array3<T>& a) : Array2<T> (a) |
|
620 { |
|
621 d2 = a.d2; |
|
622 d3 = a.d3; |
|
623 } |
|
624 |
|
625 template <class T> |
|
626 Array3<T>& |
|
627 Array3<T>::operator = (const Array3<T>& a) |
|
628 { |
|
629 Array<T>::operator = (a); |
|
630 d1 = a.d1; |
|
631 d2 = a.d2; |
|
632 d3 = a.d3; |
|
633 return *this; |
|
634 } |
|
635 |
|
636 template <class T> |
|
637 int |
|
638 Array3<T>::dim3 (void) const |
|
639 { |
|
640 return d3; |
|
641 } |
|
642 |
|
643 template <class T> |
|
644 T& |
|
645 Array3<T>::elem (int i, int j, int k) |
|
646 { |
|
647 return Array2<T>::elem (i, d2*k+j); |
|
648 } |
|
649 |
|
650 template <class T> |
|
651 T& |
|
652 Array3<T>::checkelem (int i, int j, int k) |
|
653 { |
254
|
654 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
655 { |
|
656 (*current_liboctave_error_handler) ("range error"); |
|
657 static T foo; |
|
658 return foo; |
|
659 } |
|
660 return Array2<T>::elem (i, d1*k+j); |
237
|
661 } |
|
662 |
|
663 template <class T> |
|
664 T& |
|
665 Array3<T>::operator () (int i, int j, int k) |
|
666 { |
254
|
667 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
668 { |
|
669 (*current_liboctave_error_handler) ("range error"); |
|
670 static T foo; |
|
671 return foo; |
|
672 } |
|
673 return Array2<T>::elem (i, d2*k+j); |
237
|
674 } |
|
675 |
|
676 template <class T> |
|
677 T& |
|
678 Array3<T>::xelem (int i, int j, int k) |
|
679 { |
|
680 return Array2<T>::xelem (i, d2*k+j); |
|
681 } |
|
682 |
|
683 template <class T> |
|
684 T |
|
685 Array3<T>::elem (int i, int j, int k) const |
|
686 { |
|
687 return Array2<T>::elem (i, d2*k+j); |
|
688 } |
|
689 |
|
690 template <class T> |
|
691 T |
|
692 Array3<T>::checkelem (int i, int j, int k) const |
|
693 { |
254
|
694 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
695 { |
|
696 (*current_liboctave_error_handler) ("range error"); |
|
697 T foo; |
|
698 return foo; |
|
699 } |
|
700 return Array2<T>::elem (i, d1*k+j); |
237
|
701 } |
|
702 |
|
703 template <class T> |
|
704 T |
|
705 Array3<T>::operator () (int i, int j, int k) const |
|
706 { |
254
|
707 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
708 { |
|
709 (*current_liboctave_error_handler) ("range error"); |
|
710 T foo; |
|
711 return foo; |
|
712 } |
|
713 return Array2<T>::elem (i, d2*k+j); |
237
|
714 } |
|
715 |
|
716 template <class T> |
|
717 void |
|
718 Array3<T>::resize (int n, int m, int k) |
|
719 { |
|
720 assert (0); /* XXX FIXME XXX */ |
|
721 } |
|
722 |
|
723 template <class T> |
|
724 void |
|
725 Array3<T>::resize (int n, int m, int k, const T& val) |
|
726 { |
|
727 assert (0); /* XXX FIXME XXX */ |
|
728 } |
|
729 |
|
730 /* |
|
731 * A two-dimensional array with diagonal elements only. |
|
732 */ |
|
733 |
|
734 template <class T> |
|
735 DiagArray<T>::DiagArray (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
|
736 { |
|
737 nr = r; |
|
738 nc = c; |
|
739 } |
|
740 |
|
741 template <class T> |
|
742 DiagArray<T>::DiagArray (void) : Array<T> () |
|
743 { |
|
744 nr = 0; |
|
745 nc = 0; |
|
746 } |
|
747 |
|
748 template <class T> |
|
749 DiagArray<T>::DiagArray (int n) : Array<T> (n) |
|
750 { |
|
751 nr = n; |
|
752 nc = n; |
|
753 } |
|
754 |
|
755 template <class T> |
|
756 DiagArray<T>::DiagArray (int n, const T& val) : Array<T> (n, val) |
|
757 { |
248
|
758 nr = nc = n; |
237
|
759 } |
|
760 |
|
761 template <class T> |
|
762 DiagArray<T>::DiagArray (int r, int c) : Array<T> (r < c ? r : c) |
|
763 { |
|
764 nr = r; |
|
765 nc = c; |
|
766 } |
|
767 |
|
768 template <class T> |
|
769 DiagArray<T>::DiagArray (int r, int c, const T& val) |
|
770 : Array<T> (r < c ? r : c, val) |
|
771 { |
|
772 nr = r; |
|
773 nc = c; |
|
774 } |
|
775 |
|
776 template <class T> |
|
777 DiagArray<T>::DiagArray (const Array<T>& a) : Array<T> (a) |
|
778 { |
|
779 nr = nc = a.length (); |
|
780 } |
|
781 |
|
782 template <class T> |
|
783 DiagArray<T>::DiagArray (const DiagArray<T>& a) : Array<T> (a) |
|
784 { |
|
785 nr = a.nr; |
|
786 nc = a.nc; |
|
787 } |
|
788 |
|
789 template <class T> |
|
790 DiagArray<T>& |
|
791 DiagArray<T>::operator = (const DiagArray<T>& a) |
|
792 { |
|
793 Array<T>::operator = (a); |
|
794 nr = a.nr; |
|
795 nc = a.nc; |
|
796 return *this; |
|
797 } |
|
798 |
|
799 template <class T> |
|
800 int |
|
801 DiagArray<T>::dim1 (void) const |
|
802 { |
|
803 return nr; |
|
804 } |
|
805 |
|
806 template <class T> |
|
807 int |
|
808 DiagArray<T>::dim2 (void) const |
|
809 { |
|
810 return nc; |
|
811 } |
|
812 |
|
813 template <class T> |
|
814 int |
|
815 DiagArray<T>::rows (void) const |
|
816 { |
|
817 return nr; |
|
818 } |
|
819 |
|
820 template <class T> |
|
821 int |
|
822 DiagArray<T>::cols (void) const |
|
823 { |
|
824 return nc; |
|
825 } |
|
826 |
|
827 template <class T> |
|
828 int |
|
829 DiagArray<T>::columns (void) const |
|
830 { |
|
831 return nc; |
|
832 } |
|
833 |
344
|
834 #if defind (_AIX) |
|
835 template <class T> |
|
836 T& |
|
837 DiagArray<T>::elem (int r, int c) |
|
838 { |
|
839 static T foo (0); |
|
840 return (r == c) ? Array<T>::elem (r) : foo; |
|
841 } |
|
842 |
|
843 template <class T> |
|
844 T& |
|
845 DiagArray<T>::checkelem (int r, int c) |
|
846 { |
|
847 static T foo (0); |
|
848 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
849 { |
|
850 (*current_liboctave_error_handler) ("range error"); |
|
851 return foo; |
|
852 } |
|
853 return (r == c) ? Array<T>::elem (r) : foo; |
|
854 } |
|
855 |
|
856 template <class T> |
|
857 T& |
|
858 DiagArray<T>::operator () (int r, int c) |
|
859 { |
|
860 static T foo (0); |
|
861 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
862 { |
|
863 (*current_liboctave_error_handler) ("range error"); |
|
864 return foo; |
|
865 } |
|
866 return (r == c) ? Array<T>::elem (r) : foo; |
|
867 } |
|
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 // |
|
1010 // g++ -fexternal-templates -DUSER_EXTERNAL_TEMPLATES \ |
|
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 */ |