237
|
1 // Template array classes -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1993, 1994, 1995 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 |
449
|
32 #include <assert.h> |
|
33 |
237
|
34 #include "Array.h" |
|
35 |
1360
|
36 // The real representation of all arrays. |
237
|
37 |
|
38 template <class T> |
|
39 ArrayRep<T>::ArrayRep (T *d, int l) |
|
40 { |
|
41 data = d; |
|
42 len = l; |
|
43 } |
|
44 |
|
45 template <class T> |
|
46 ArrayRep<T>::ArrayRep (void) |
|
47 { |
|
48 len = 0; |
|
49 data = (T *) 0; |
|
50 } |
|
51 |
|
52 template <class T> |
|
53 ArrayRep<T>::ArrayRep (int n) |
|
54 { |
|
55 len = n; |
|
56 data = new T [len]; |
|
57 } |
|
58 |
|
59 template <class T> |
|
60 ArrayRep<T>::ArrayRep (const ArrayRep<T>& a) |
|
61 { |
|
62 len = a.len; |
|
63 count = a.count; |
|
64 data = new T [len]; |
|
65 for (int i = 0; i < len; i++) |
|
66 data[i] = a.data[i]; |
|
67 } |
|
68 |
|
69 template <class T> |
|
70 ArrayRep<T>::~ArrayRep (void) |
|
71 { |
|
72 delete [] data; |
|
73 data = (T *) 0; |
|
74 } |
|
75 |
|
76 template <class T> |
|
77 int |
|
78 ArrayRep<T>::length (void) const |
|
79 { |
|
80 return len; |
|
81 } |
|
82 |
|
83 template <class T> |
|
84 T& |
|
85 ArrayRep<T>::elem (int n) |
|
86 { |
|
87 return data[n]; |
|
88 } |
|
89 |
|
90 template <class T> |
|
91 T |
|
92 ArrayRep<T>::elem (int n) const |
|
93 { |
|
94 return data[n]; |
|
95 } |
|
96 |
1360
|
97 // One dimensional array class. Handles the reference counting for |
|
98 // all the derived classes. |
237
|
99 |
|
100 template <class T> |
|
101 Array<T>::Array (T *d, int l) |
|
102 { |
|
103 rep = new ArrayRep<T> (d, l); |
|
104 rep->count = 1; |
|
105 } |
|
106 |
|
107 template <class T> |
|
108 Array<T>::Array (void) |
|
109 { |
|
110 rep = new ArrayRep<T>; |
|
111 rep->count = 1; |
|
112 } |
|
113 |
|
114 template <class T> |
|
115 Array<T>::Array (int n) |
|
116 { |
|
117 rep = new ArrayRep<T> (n); |
|
118 rep->count = 1; |
|
119 } |
|
120 |
|
121 template <class T> |
|
122 Array<T>::Array (int n, const T& val) |
|
123 { |
|
124 rep = new ArrayRep<T> (n); |
|
125 rep->count = 1; |
|
126 for (int i = 0; i < n; i++) |
|
127 rep->data[i] = val; |
|
128 } |
|
129 |
|
130 template <class T> |
|
131 Array<T>::Array (const Array<T>& a) |
|
132 { |
|
133 rep = a.rep; |
|
134 rep->count++; |
|
135 } |
|
136 |
|
137 template <class T> |
|
138 Array<T>::~Array (void) |
|
139 { |
|
140 if (--rep->count <= 0) |
|
141 delete rep; |
|
142 } |
|
143 |
|
144 template <class T> |
|
145 Array<T>& |
|
146 Array<T>::operator = (const Array<T>& a) |
|
147 { |
659
|
148 if (this != &a) |
|
149 { |
|
150 if (--rep->count <= 0) |
|
151 delete rep; |
237
|
152 |
659
|
153 rep = a.rep; |
|
154 rep->count++; |
|
155 } |
|
156 |
237
|
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 |
1360
|
330 // Two dimensional array class. |
237
|
331 |
|
332 template <class T> |
|
333 Array2<T>::Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
334 { |
|
335 d1 = n; |
|
336 d2 = m; |
|
337 } |
|
338 |
|
339 template <class T> |
|
340 Array2<T>::Array2 (void) : Array<T> () |
|
341 { |
|
342 d1 = 0; |
|
343 d2 = 0; |
|
344 } |
|
345 |
|
346 template <class T> |
|
347 Array2<T>::Array2 (int n, int m) : Array<T> (n*m) |
|
348 { |
|
349 d1 = n; |
|
350 d2 = m; |
|
351 } |
|
352 |
|
353 template <class T> |
|
354 Array2<T>::Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
355 { |
|
356 d1 = n; |
|
357 d2 = m; |
|
358 } |
|
359 |
|
360 template <class T> |
|
361 Array2<T>::Array2 (const Array2<T>& a) : Array<T> (a) |
|
362 { |
|
363 d1 = a.d1; |
|
364 d2 = a.d2; |
|
365 } |
|
366 |
|
367 template <class T> |
|
368 Array2<T>::Array2 (const DiagArray<T>& a) |
|
369 : Array<T> (a.rows () * a.cols (), T (0)) |
|
370 { |
|
371 for (int i = 0; i < a.length (); i++) |
|
372 elem (i, i) = a.elem (i, i); |
|
373 } |
|
374 |
|
375 template <class T> |
|
376 Array2<T>& |
|
377 Array2<T>::operator = (const Array2<T>& a) |
|
378 { |
659
|
379 if (this != &a) |
|
380 { |
|
381 Array<T>::operator = (a); |
|
382 d1 = a.d1; |
|
383 d2 = a.d2; |
|
384 } |
|
385 |
237
|
386 return *this; |
|
387 } |
|
388 |
|
389 template <class T> |
|
390 int |
|
391 Array2<T>::dim1 (void) const |
|
392 { |
|
393 return d1; |
|
394 } |
|
395 |
|
396 template <class T> |
|
397 int |
|
398 Array2<T>::dim2 (void) const |
|
399 { |
|
400 return d2; |
|
401 } |
|
402 |
|
403 template <class T> |
|
404 int |
|
405 Array2<T>::rows (void) const |
|
406 { |
|
407 return d1; |
|
408 } |
|
409 |
|
410 template <class T> |
|
411 int |
|
412 Array2<T>::cols (void) const |
|
413 { |
|
414 return d2; |
|
415 } |
|
416 |
|
417 template <class T> |
|
418 int |
|
419 Array2<T>::columns (void) const |
|
420 { |
|
421 return d2; |
|
422 } |
|
423 |
|
424 template <class T> |
|
425 T& |
|
426 Array2<T>::elem (int i, int j) |
|
427 { |
|
428 return Array<T>::elem (d1*j+i); |
|
429 } |
|
430 |
|
431 template <class T> |
|
432 T& |
|
433 Array2<T>::checkelem (int i, int j) |
|
434 { |
254
|
435 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
436 { |
|
437 (*current_liboctave_error_handler) ("range error"); |
|
438 static T foo; |
|
439 return foo; |
|
440 } |
|
441 return Array<T>::elem (d1*j+i); |
237
|
442 } |
|
443 |
|
444 template <class T> |
|
445 T& |
|
446 Array2<T>::operator () (int i, int j) |
|
447 { |
254
|
448 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
449 { |
|
450 (*current_liboctave_error_handler) ("range error"); |
|
451 static T foo; |
|
452 return foo; |
|
453 } |
|
454 return Array<T>::elem (d1*j+i); |
237
|
455 } |
|
456 |
|
457 template <class T> |
|
458 T& |
|
459 Array2<T>::xelem (int i, int j) |
|
460 { |
|
461 return Array<T>::xelem (d1*j+i); |
|
462 } |
|
463 |
|
464 template <class T> |
|
465 T |
|
466 Array2<T>::elem (int i, int j) const |
|
467 { |
|
468 return Array<T>::elem (d1*j+i); |
|
469 } |
|
470 |
|
471 template <class T> |
|
472 T |
|
473 Array2<T>::checkelem (int i, int j) const |
|
474 { |
254
|
475 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
476 { |
|
477 (*current_liboctave_error_handler) ("range error"); |
|
478 T foo; |
|
479 return foo; |
|
480 } |
|
481 return Array<T>::elem (d1*j+i); |
237
|
482 } |
|
483 |
|
484 template <class T> |
|
485 T |
|
486 Array2<T>::operator () (int i, int j) const |
|
487 { |
254
|
488 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
489 { |
|
490 (*current_liboctave_error_handler) ("range error"); |
|
491 T foo; |
|
492 return foo; |
|
493 } |
|
494 return Array<T>::elem (d1*j+i); |
237
|
495 } |
|
496 |
|
497 template <class T> |
|
498 void |
|
499 Array2<T>::resize (int r, int c) |
|
500 { |
|
501 if (r < 0 || c < 0) |
|
502 { |
|
503 (*current_liboctave_error_handler) |
|
504 ("can't resize to negative dimension"); |
|
505 return; |
|
506 } |
|
507 |
|
508 if (r == dim1 () && c == dim2 ()) |
|
509 return; |
|
510 |
|
511 ArrayRep<T> *old_rep = rep; |
|
512 const T *old_data = data (); |
|
513 int old_d1 = dim1 (); |
|
514 int old_d2 = dim2 (); |
|
515 int old_len = length (); |
|
516 |
|
517 rep = new ArrayRep<T> (r*c); |
|
518 rep->count = 1; |
|
519 |
|
520 d1 = r; |
|
521 d2 = c; |
|
522 |
|
523 if (old_data && old_len > 0) |
|
524 { |
|
525 int min_r = old_d1 < r ? old_d1 : r; |
|
526 int min_c = old_d2 < c ? old_d2 : c; |
|
527 |
|
528 for (int j = 0; j < min_c; j++) |
|
529 for (int i = 0; i < min_r; i++) |
|
530 xelem (i, j) = old_data[old_d1*j+i]; |
|
531 } |
|
532 |
|
533 if (--old_rep->count <= 0) |
|
534 delete old_rep; |
|
535 } |
|
536 |
|
537 template <class T> |
|
538 void |
|
539 Array2<T>::resize (int r, int c, const T& val) |
|
540 { |
|
541 if (r < 0 || c < 0) |
|
542 { |
|
543 (*current_liboctave_error_handler) |
|
544 ("can't resize to negative dimension"); |
|
545 return; |
|
546 } |
|
547 |
|
548 if (r == dim1 () && c == dim2 ()) |
|
549 return; |
|
550 |
|
551 ArrayRep<T> *old_rep = rep; |
|
552 const T *old_data = data (); |
|
553 int old_d1 = dim1 (); |
|
554 int old_d2 = dim2 (); |
|
555 int old_len = length (); |
|
556 |
|
557 rep = new ArrayRep<T> (r*c); |
|
558 rep->count = 1; |
|
559 |
|
560 d1 = r; |
|
561 d2 = c; |
|
562 |
|
563 int min_r = old_d1 < r ? old_d1 : r; |
|
564 int min_c = old_d2 < c ? old_d2 : c; |
|
565 |
|
566 if (old_data && old_len > 0) |
|
567 { |
1321
|
568 for (int j = 0; j < min_c; j++) |
|
569 for (int i = 0; i < min_r; i++) |
237
|
570 xelem (i, j) = old_data[old_d1*j+i]; |
|
571 } |
|
572 |
1321
|
573 for (int j = 0; j < min_c; j++) |
|
574 for (int i = min_r; i < r; i++) |
237
|
575 xelem (i, j) = val; |
|
576 |
1321
|
577 for (int j = min_c; j < c; j++) |
|
578 for (int i = 0; i < r; i++) |
237
|
579 xelem (i, j) = val; |
|
580 |
|
581 if (--old_rep->count <= 0) |
|
582 delete old_rep; |
|
583 } |
|
584 |
1360
|
585 // Three dimensional array class. |
237
|
586 |
|
587 template <class T> |
|
588 Array3<T>::Array3 (T *d, int n, int m, int k) : Array2<T> (d, n, m*k) |
|
589 { |
|
590 d2 = m; |
|
591 d3 = k; |
|
592 } |
|
593 |
|
594 template <class T> |
|
595 Array3<T>::Array3 (void) : Array2<T> () |
|
596 { |
|
597 d2 = 0; |
|
598 d3 = 0; |
|
599 } |
|
600 |
|
601 template <class T> |
|
602 Array3<T>::Array3 (int n, int m, int k) : Array2<T> (n, m*k) |
|
603 { |
|
604 d2 = m; |
|
605 d3 = k; |
|
606 } |
|
607 |
|
608 template <class T> |
|
609 Array3<T>::Array3 (int n, int m, int k, const T& val) : Array2<T> (n, m*k, val) |
|
610 { |
|
611 d2 = m; |
|
612 d3 = k; |
|
613 } |
|
614 |
|
615 template <class T> |
|
616 Array3<T>::Array3 (const Array3<T>& a) : Array2<T> (a) |
|
617 { |
|
618 d2 = a.d2; |
|
619 d3 = a.d3; |
|
620 } |
|
621 |
|
622 template <class T> |
|
623 Array3<T>& |
|
624 Array3<T>::operator = (const Array3<T>& a) |
|
625 { |
659
|
626 if (this != &a) |
|
627 { |
|
628 Array<T>::operator = (a); |
|
629 d1 = a.d1; |
|
630 d2 = a.d2; |
|
631 d3 = a.d3; |
|
632 } |
|
633 |
237
|
634 return *this; |
|
635 } |
|
636 |
|
637 template <class T> |
|
638 int |
|
639 Array3<T>::dim3 (void) const |
|
640 { |
|
641 return d3; |
|
642 } |
|
643 |
|
644 template <class T> |
|
645 T& |
|
646 Array3<T>::elem (int i, int j, int k) |
|
647 { |
|
648 return Array2<T>::elem (i, d2*k+j); |
|
649 } |
|
650 |
|
651 template <class T> |
|
652 T& |
|
653 Array3<T>::checkelem (int i, int j, int k) |
|
654 { |
254
|
655 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
656 { |
|
657 (*current_liboctave_error_handler) ("range error"); |
|
658 static T foo; |
|
659 return foo; |
|
660 } |
|
661 return Array2<T>::elem (i, d1*k+j); |
237
|
662 } |
|
663 |
|
664 template <class T> |
|
665 T& |
|
666 Array3<T>::operator () (int i, int j, int k) |
|
667 { |
254
|
668 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
669 { |
|
670 (*current_liboctave_error_handler) ("range error"); |
|
671 static T foo; |
|
672 return foo; |
|
673 } |
|
674 return Array2<T>::elem (i, d2*k+j); |
237
|
675 } |
|
676 |
|
677 template <class T> |
|
678 T& |
|
679 Array3<T>::xelem (int i, int j, int k) |
|
680 { |
|
681 return Array2<T>::xelem (i, d2*k+j); |
|
682 } |
|
683 |
|
684 template <class T> |
|
685 T |
|
686 Array3<T>::elem (int i, int j, int k) const |
|
687 { |
|
688 return Array2<T>::elem (i, d2*k+j); |
|
689 } |
|
690 |
|
691 template <class T> |
|
692 T |
|
693 Array3<T>::checkelem (int i, int j, int k) const |
|
694 { |
254
|
695 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
696 { |
|
697 (*current_liboctave_error_handler) ("range error"); |
|
698 T foo; |
|
699 return foo; |
|
700 } |
|
701 return Array2<T>::elem (i, d1*k+j); |
237
|
702 } |
|
703 |
|
704 template <class T> |
|
705 T |
|
706 Array3<T>::operator () (int i, int j, int k) const |
|
707 { |
254
|
708 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
709 { |
|
710 (*current_liboctave_error_handler) ("range error"); |
|
711 T foo; |
|
712 return foo; |
|
713 } |
|
714 return Array2<T>::elem (i, d2*k+j); |
237
|
715 } |
|
716 |
|
717 template <class T> |
|
718 void |
|
719 Array3<T>::resize (int n, int m, int k) |
|
720 { |
1360
|
721 assert (0); // XXX FIXME XXX |
237
|
722 } |
|
723 |
|
724 template <class T> |
|
725 void |
|
726 Array3<T>::resize (int n, int m, int k, const T& val) |
|
727 { |
1360
|
728 assert (0); // XXX FIXME XXX |
237
|
729 } |
|
730 |
1360
|
731 // A two-dimensional array with diagonal elements only. |
237
|
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 { |
659
|
792 if (this != &a) |
|
793 { |
|
794 Array<T>::operator = (a); |
|
795 nr = a.nr; |
|
796 nc = a.nc; |
|
797 } |
|
798 |
237
|
799 return *this; |
|
800 } |
|
801 |
|
802 template <class T> |
|
803 int |
|
804 DiagArray<T>::dim1 (void) const |
|
805 { |
|
806 return nr; |
|
807 } |
|
808 |
|
809 template <class T> |
|
810 int |
|
811 DiagArray<T>::dim2 (void) const |
|
812 { |
|
813 return nc; |
|
814 } |
|
815 |
|
816 template <class T> |
|
817 int |
|
818 DiagArray<T>::rows (void) const |
|
819 { |
|
820 return nr; |
|
821 } |
|
822 |
|
823 template <class T> |
|
824 int |
|
825 DiagArray<T>::cols (void) const |
|
826 { |
|
827 return nc; |
|
828 } |
|
829 |
|
830 template <class T> |
|
831 int |
|
832 DiagArray<T>::columns (void) const |
|
833 { |
|
834 return nc; |
|
835 } |
|
836 |
880
|
837 #if 1 |
344
|
838 template <class T> |
|
839 T& |
|
840 DiagArray<T>::elem (int r, int c) |
|
841 { |
|
842 static T foo (0); |
|
843 return (r == c) ? Array<T>::elem (r) : foo; |
|
844 } |
|
845 |
|
846 template <class T> |
|
847 T& |
|
848 DiagArray<T>::checkelem (int r, int c) |
|
849 { |
|
850 static T foo (0); |
|
851 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
852 { |
|
853 (*current_liboctave_error_handler) ("range error"); |
|
854 return foo; |
|
855 } |
|
856 return (r == c) ? Array<T>::elem (r) : foo; |
|
857 } |
|
858 |
|
859 template <class T> |
|
860 T& |
|
861 DiagArray<T>::operator () (int r, int c) |
|
862 { |
|
863 static T foo (0); |
|
864 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
865 { |
|
866 (*current_liboctave_error_handler) ("range error"); |
|
867 return foo; |
|
868 } |
|
869 return (r == c) ? Array<T>::elem (r) : foo; |
|
870 } |
347
|
871 #endif |
344
|
872 |
237
|
873 template <class T> |
|
874 T& |
|
875 DiagArray<T>::xelem (int r, int c) |
|
876 { |
|
877 static T foo (0); |
|
878 return (r == c) ? Array<T>::xelem (r) : foo; |
|
879 } |
|
880 |
|
881 template <class T> |
|
882 T |
|
883 DiagArray<T>::elem (int r, int c) const |
|
884 { |
|
885 return (r == c) ? Array<T>::elem (r) : T (0); |
|
886 } |
|
887 |
|
888 template <class T> |
|
889 T |
|
890 DiagArray<T>::checkelem (int r, int c) const |
|
891 { |
254
|
892 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
893 { |
|
894 (*current_liboctave_error_handler) ("range error"); |
|
895 T foo; |
|
896 return foo; |
|
897 } |
|
898 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
899 } |
|
900 |
|
901 template <class T> |
|
902 T |
|
903 DiagArray<T>::operator () (int r, int c) const |
|
904 { |
254
|
905 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
906 { |
|
907 (*current_liboctave_error_handler) ("range error"); |
|
908 T foo; |
|
909 return foo; |
|
910 } |
|
911 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
912 } |
|
913 |
|
914 template <class T> |
|
915 void |
|
916 DiagArray<T>::resize (int r, int c) |
|
917 { |
|
918 if (r < 0 || c < 0) |
|
919 { |
|
920 (*current_liboctave_error_handler) |
|
921 ("can't resize to negative dimensions"); |
|
922 return; |
|
923 } |
|
924 |
|
925 if (r == dim1 () && c == dim2 ()) |
|
926 return; |
|
927 |
|
928 ArrayRep<T> *old_rep = rep; |
|
929 const T *old_data = data (); |
|
930 int old_len = length (); |
|
931 |
|
932 int new_len = r < c ? r : c; |
|
933 |
|
934 rep = new ArrayRep<T> (new_len); |
|
935 rep->count = 1; |
|
936 |
|
937 nr = r; |
|
938 nc = c; |
|
939 |
|
940 if (old_data && old_len > 0) |
|
941 { |
|
942 int min_len = old_len < new_len ? old_len : new_len; |
|
943 |
|
944 for (int i = 0; i < min_len; i++) |
|
945 xelem (i, i) = old_data[i]; |
|
946 } |
|
947 |
|
948 if (--old_rep->count <= 0) |
|
949 delete old_rep; |
|
950 } |
|
951 |
|
952 template <class T> |
|
953 void |
|
954 DiagArray<T>::resize (int r, int c, const T& val) |
|
955 { |
|
956 if (r < 0 || c < 0) |
|
957 { |
|
958 (*current_liboctave_error_handler) |
|
959 ("can't resize to negative dimensions"); |
|
960 return; |
|
961 } |
|
962 |
|
963 if (r == dim1 () && c == dim2 ()) |
|
964 return; |
|
965 |
|
966 ArrayRep<T> *old_rep = rep; |
|
967 const T *old_data = data (); |
|
968 int old_len = length (); |
|
969 |
|
970 int new_len = r < c ? r : c; |
|
971 |
|
972 rep = new ArrayRep<T> (new_len); |
|
973 rep->count = 1; |
|
974 |
|
975 nr = r; |
|
976 nc = c; |
|
977 |
|
978 int min_len = old_len < new_len ? old_len : new_len; |
|
979 |
|
980 if (old_data && old_len > 0) |
|
981 { |
|
982 for (int i = 0; i < min_len; i++) |
|
983 xelem (i, i) = old_data[i]; |
|
984 } |
|
985 |
|
986 for (int i = min_len; i < new_len; i++) |
|
987 xelem (i, i) = val; |
|
988 |
|
989 if (--old_rep->count <= 0) |
|
990 delete old_rep; |
|
991 } |
|
992 |
|
993 /* |
|
994 ;;; Local Variables: *** |
|
995 ;;; mode: C++ *** |
|
996 ;;; page-delimiter: "^/\\*" *** |
|
997 ;;; End: *** |
|
998 */ |