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 |
1367
|
32 #include <cassert> |
449
|
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; |
1470
|
229 static T *bar = &foo; |
254
|
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 |
1360
|
331 // Two dimensional array class. |
237
|
332 |
|
333 template <class T> |
|
334 Array2<T>::Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
335 { |
|
336 d1 = n; |
|
337 d2 = m; |
|
338 } |
|
339 |
|
340 template <class T> |
|
341 Array2<T>::Array2 (void) : Array<T> () |
|
342 { |
|
343 d1 = 0; |
|
344 d2 = 0; |
|
345 } |
|
346 |
|
347 template <class T> |
|
348 Array2<T>::Array2 (int n, int m) : Array<T> (n*m) |
|
349 { |
|
350 d1 = n; |
|
351 d2 = m; |
|
352 } |
|
353 |
|
354 template <class T> |
|
355 Array2<T>::Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
356 { |
|
357 d1 = n; |
|
358 d2 = m; |
|
359 } |
|
360 |
|
361 template <class T> |
|
362 Array2<T>::Array2 (const Array2<T>& a) : Array<T> (a) |
|
363 { |
|
364 d1 = a.d1; |
|
365 d2 = a.d2; |
|
366 } |
|
367 |
|
368 template <class T> |
|
369 Array2<T>::Array2 (const DiagArray<T>& a) |
|
370 : Array<T> (a.rows () * a.cols (), T (0)) |
|
371 { |
|
372 for (int i = 0; i < a.length (); i++) |
|
373 elem (i, i) = a.elem (i, i); |
|
374 } |
|
375 |
|
376 template <class T> |
|
377 Array2<T>& |
|
378 Array2<T>::operator = (const Array2<T>& a) |
|
379 { |
659
|
380 if (this != &a) |
|
381 { |
|
382 Array<T>::operator = (a); |
|
383 d1 = a.d1; |
|
384 d2 = a.d2; |
|
385 } |
|
386 |
237
|
387 return *this; |
|
388 } |
|
389 |
|
390 template <class T> |
|
391 int |
|
392 Array2<T>::dim1 (void) const |
|
393 { |
|
394 return d1; |
|
395 } |
|
396 |
|
397 template <class T> |
|
398 int |
|
399 Array2<T>::dim2 (void) const |
|
400 { |
|
401 return d2; |
|
402 } |
|
403 |
|
404 template <class T> |
|
405 int |
|
406 Array2<T>::rows (void) const |
|
407 { |
|
408 return d1; |
|
409 } |
|
410 |
|
411 template <class T> |
|
412 int |
|
413 Array2<T>::cols (void) const |
|
414 { |
|
415 return d2; |
|
416 } |
|
417 |
|
418 template <class T> |
|
419 int |
|
420 Array2<T>::columns (void) const |
|
421 { |
|
422 return d2; |
|
423 } |
|
424 |
|
425 template <class T> |
|
426 T& |
|
427 Array2<T>::elem (int i, int j) |
|
428 { |
|
429 return Array<T>::elem (d1*j+i); |
|
430 } |
|
431 |
|
432 template <class T> |
|
433 T& |
|
434 Array2<T>::checkelem (int i, int j) |
|
435 { |
254
|
436 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
437 { |
|
438 (*current_liboctave_error_handler) ("range error"); |
|
439 static T foo; |
|
440 return foo; |
|
441 } |
|
442 return Array<T>::elem (d1*j+i); |
237
|
443 } |
|
444 |
|
445 template <class T> |
|
446 T& |
|
447 Array2<T>::operator () (int i, int j) |
|
448 { |
254
|
449 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
450 { |
|
451 (*current_liboctave_error_handler) ("range error"); |
|
452 static T foo; |
|
453 return foo; |
|
454 } |
|
455 return Array<T>::elem (d1*j+i); |
237
|
456 } |
|
457 |
|
458 template <class T> |
|
459 T& |
|
460 Array2<T>::xelem (int i, int j) |
|
461 { |
|
462 return Array<T>::xelem (d1*j+i); |
|
463 } |
|
464 |
|
465 template <class T> |
|
466 T |
|
467 Array2<T>::elem (int i, int j) const |
|
468 { |
|
469 return Array<T>::elem (d1*j+i); |
|
470 } |
|
471 |
|
472 template <class T> |
|
473 T |
|
474 Array2<T>::checkelem (int i, int j) const |
|
475 { |
254
|
476 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
477 { |
|
478 (*current_liboctave_error_handler) ("range error"); |
|
479 T foo; |
1470
|
480 static T *bar = &foo; |
254
|
481 return foo; |
|
482 } |
|
483 return Array<T>::elem (d1*j+i); |
237
|
484 } |
|
485 |
|
486 template <class T> |
|
487 T |
|
488 Array2<T>::operator () (int i, int j) const |
|
489 { |
254
|
490 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
491 { |
|
492 (*current_liboctave_error_handler) ("range error"); |
|
493 T foo; |
1470
|
494 static T *bar = &foo; |
254
|
495 return foo; |
|
496 } |
|
497 return Array<T>::elem (d1*j+i); |
237
|
498 } |
|
499 |
|
500 template <class T> |
|
501 void |
|
502 Array2<T>::resize (int r, int c) |
|
503 { |
|
504 if (r < 0 || c < 0) |
|
505 { |
|
506 (*current_liboctave_error_handler) |
|
507 ("can't resize to negative dimension"); |
|
508 return; |
|
509 } |
|
510 |
|
511 if (r == dim1 () && c == dim2 ()) |
|
512 return; |
|
513 |
|
514 ArrayRep<T> *old_rep = rep; |
|
515 const T *old_data = data (); |
|
516 int old_d1 = dim1 (); |
|
517 int old_d2 = dim2 (); |
|
518 int old_len = length (); |
|
519 |
|
520 rep = new ArrayRep<T> (r*c); |
|
521 rep->count = 1; |
|
522 |
|
523 d1 = r; |
|
524 d2 = c; |
|
525 |
|
526 if (old_data && old_len > 0) |
|
527 { |
|
528 int min_r = old_d1 < r ? old_d1 : r; |
|
529 int min_c = old_d2 < c ? old_d2 : c; |
|
530 |
|
531 for (int j = 0; j < min_c; j++) |
|
532 for (int i = 0; i < min_r; i++) |
|
533 xelem (i, j) = old_data[old_d1*j+i]; |
|
534 } |
|
535 |
|
536 if (--old_rep->count <= 0) |
|
537 delete old_rep; |
|
538 } |
|
539 |
|
540 template <class T> |
|
541 void |
|
542 Array2<T>::resize (int r, int c, const T& val) |
|
543 { |
|
544 if (r < 0 || c < 0) |
|
545 { |
|
546 (*current_liboctave_error_handler) |
|
547 ("can't resize to negative dimension"); |
|
548 return; |
|
549 } |
|
550 |
|
551 if (r == dim1 () && c == dim2 ()) |
|
552 return; |
|
553 |
|
554 ArrayRep<T> *old_rep = rep; |
|
555 const T *old_data = data (); |
|
556 int old_d1 = dim1 (); |
|
557 int old_d2 = dim2 (); |
|
558 int old_len = length (); |
|
559 |
|
560 rep = new ArrayRep<T> (r*c); |
|
561 rep->count = 1; |
|
562 |
|
563 d1 = r; |
|
564 d2 = c; |
|
565 |
|
566 int min_r = old_d1 < r ? old_d1 : r; |
|
567 int min_c = old_d2 < c ? old_d2 : c; |
|
568 |
|
569 if (old_data && old_len > 0) |
|
570 { |
1321
|
571 for (int j = 0; j < min_c; j++) |
|
572 for (int i = 0; i < min_r; i++) |
237
|
573 xelem (i, j) = old_data[old_d1*j+i]; |
|
574 } |
|
575 |
1321
|
576 for (int j = 0; j < min_c; j++) |
|
577 for (int i = min_r; i < r; i++) |
237
|
578 xelem (i, j) = val; |
|
579 |
1321
|
580 for (int j = min_c; j < c; j++) |
|
581 for (int i = 0; i < r; i++) |
237
|
582 xelem (i, j) = val; |
|
583 |
|
584 if (--old_rep->count <= 0) |
|
585 delete old_rep; |
|
586 } |
|
587 |
1360
|
588 // Three dimensional array class. |
237
|
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 { |
659
|
629 if (this != &a) |
|
630 { |
|
631 Array<T>::operator = (a); |
|
632 d1 = a.d1; |
|
633 d2 = a.d2; |
|
634 d3 = a.d3; |
|
635 } |
|
636 |
237
|
637 return *this; |
|
638 } |
|
639 |
|
640 template <class T> |
|
641 int |
|
642 Array3<T>::dim3 (void) const |
|
643 { |
|
644 return d3; |
|
645 } |
|
646 |
|
647 template <class T> |
|
648 T& |
|
649 Array3<T>::elem (int i, int j, int k) |
|
650 { |
|
651 return Array2<T>::elem (i, d2*k+j); |
|
652 } |
|
653 |
|
654 template <class T> |
|
655 T& |
|
656 Array3<T>::checkelem (int i, int j, int k) |
|
657 { |
254
|
658 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
659 { |
|
660 (*current_liboctave_error_handler) ("range error"); |
|
661 static T foo; |
|
662 return foo; |
|
663 } |
|
664 return Array2<T>::elem (i, d1*k+j); |
237
|
665 } |
|
666 |
|
667 template <class T> |
|
668 T& |
|
669 Array3<T>::operator () (int i, int j, int k) |
|
670 { |
254
|
671 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
672 { |
|
673 (*current_liboctave_error_handler) ("range error"); |
|
674 static T foo; |
|
675 return foo; |
|
676 } |
|
677 return Array2<T>::elem (i, d2*k+j); |
237
|
678 } |
|
679 |
|
680 template <class T> |
|
681 T& |
|
682 Array3<T>::xelem (int i, int j, int k) |
|
683 { |
|
684 return Array2<T>::xelem (i, d2*k+j); |
|
685 } |
|
686 |
|
687 template <class T> |
|
688 T |
|
689 Array3<T>::elem (int i, int j, int k) const |
|
690 { |
|
691 return Array2<T>::elem (i, d2*k+j); |
|
692 } |
|
693 |
|
694 template <class T> |
|
695 T |
|
696 Array3<T>::checkelem (int i, int j, int k) const |
|
697 { |
254
|
698 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
699 { |
|
700 (*current_liboctave_error_handler) ("range error"); |
|
701 T foo; |
1470
|
702 static T *bar = &foo; |
254
|
703 return foo; |
|
704 } |
|
705 return Array2<T>::elem (i, d1*k+j); |
237
|
706 } |
|
707 |
|
708 template <class T> |
|
709 T |
|
710 Array3<T>::operator () (int i, int j, int k) const |
|
711 { |
254
|
712 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
713 { |
|
714 (*current_liboctave_error_handler) ("range error"); |
|
715 T foo; |
1470
|
716 static T *bar = &foo; |
254
|
717 return foo; |
|
718 } |
|
719 return Array2<T>::elem (i, d2*k+j); |
237
|
720 } |
|
721 |
|
722 template <class T> |
|
723 void |
|
724 Array3<T>::resize (int n, int m, int k) |
|
725 { |
1360
|
726 assert (0); // XXX FIXME XXX |
237
|
727 } |
|
728 |
|
729 template <class T> |
|
730 void |
|
731 Array3<T>::resize (int n, int m, int k, const T& val) |
|
732 { |
1360
|
733 assert (0); // XXX FIXME XXX |
237
|
734 } |
|
735 |
1360
|
736 // A two-dimensional array with diagonal elements only. |
237
|
737 |
|
738 template <class T> |
|
739 DiagArray<T>::DiagArray (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
|
740 { |
|
741 nr = r; |
|
742 nc = c; |
|
743 } |
|
744 |
|
745 template <class T> |
|
746 DiagArray<T>::DiagArray (void) : Array<T> () |
|
747 { |
|
748 nr = 0; |
|
749 nc = 0; |
|
750 } |
|
751 |
|
752 template <class T> |
|
753 DiagArray<T>::DiagArray (int n) : Array<T> (n) |
|
754 { |
|
755 nr = n; |
|
756 nc = n; |
|
757 } |
|
758 |
|
759 template <class T> |
|
760 DiagArray<T>::DiagArray (int n, const T& val) : Array<T> (n, val) |
|
761 { |
248
|
762 nr = nc = n; |
237
|
763 } |
|
764 |
|
765 template <class T> |
|
766 DiagArray<T>::DiagArray (int r, int c) : Array<T> (r < c ? r : c) |
|
767 { |
|
768 nr = r; |
|
769 nc = c; |
|
770 } |
|
771 |
|
772 template <class T> |
|
773 DiagArray<T>::DiagArray (int r, int c, const T& val) |
|
774 : Array<T> (r < c ? r : c, val) |
|
775 { |
|
776 nr = r; |
|
777 nc = c; |
|
778 } |
|
779 |
|
780 template <class T> |
|
781 DiagArray<T>::DiagArray (const Array<T>& a) : Array<T> (a) |
|
782 { |
|
783 nr = nc = a.length (); |
|
784 } |
|
785 |
|
786 template <class T> |
|
787 DiagArray<T>::DiagArray (const DiagArray<T>& a) : Array<T> (a) |
|
788 { |
|
789 nr = a.nr; |
|
790 nc = a.nc; |
|
791 } |
|
792 |
|
793 template <class T> |
|
794 DiagArray<T>& |
|
795 DiagArray<T>::operator = (const DiagArray<T>& a) |
|
796 { |
659
|
797 if (this != &a) |
|
798 { |
|
799 Array<T>::operator = (a); |
|
800 nr = a.nr; |
|
801 nc = a.nc; |
|
802 } |
|
803 |
237
|
804 return *this; |
|
805 } |
|
806 |
|
807 template <class T> |
|
808 int |
|
809 DiagArray<T>::dim1 (void) const |
|
810 { |
|
811 return nr; |
|
812 } |
|
813 |
|
814 template <class T> |
|
815 int |
|
816 DiagArray<T>::dim2 (void) const |
|
817 { |
|
818 return nc; |
|
819 } |
|
820 |
|
821 template <class T> |
|
822 int |
|
823 DiagArray<T>::rows (void) const |
|
824 { |
|
825 return nr; |
|
826 } |
|
827 |
|
828 template <class T> |
|
829 int |
|
830 DiagArray<T>::cols (void) const |
|
831 { |
|
832 return nc; |
|
833 } |
|
834 |
|
835 template <class T> |
|
836 int |
|
837 DiagArray<T>::columns (void) const |
|
838 { |
|
839 return nc; |
|
840 } |
|
841 |
880
|
842 #if 1 |
344
|
843 template <class T> |
|
844 T& |
|
845 DiagArray<T>::elem (int r, int c) |
|
846 { |
|
847 static T foo (0); |
|
848 return (r == c) ? Array<T>::elem (r) : foo; |
|
849 } |
|
850 |
|
851 template <class T> |
|
852 T& |
|
853 DiagArray<T>::checkelem (int r, int c) |
|
854 { |
|
855 static T foo (0); |
|
856 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
857 { |
|
858 (*current_liboctave_error_handler) ("range error"); |
|
859 return foo; |
|
860 } |
|
861 return (r == c) ? Array<T>::elem (r) : foo; |
|
862 } |
|
863 |
|
864 template <class T> |
|
865 T& |
|
866 DiagArray<T>::operator () (int r, int c) |
|
867 { |
|
868 static T foo (0); |
|
869 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
870 { |
|
871 (*current_liboctave_error_handler) ("range error"); |
|
872 return foo; |
|
873 } |
|
874 return (r == c) ? Array<T>::elem (r) : foo; |
|
875 } |
347
|
876 #endif |
344
|
877 |
237
|
878 template <class T> |
|
879 T& |
|
880 DiagArray<T>::xelem (int r, int c) |
|
881 { |
|
882 static T foo (0); |
|
883 return (r == c) ? Array<T>::xelem (r) : foo; |
|
884 } |
|
885 |
|
886 template <class T> |
|
887 T |
|
888 DiagArray<T>::elem (int r, int c) const |
|
889 { |
|
890 return (r == c) ? Array<T>::elem (r) : T (0); |
|
891 } |
|
892 |
|
893 template <class T> |
|
894 T |
|
895 DiagArray<T>::checkelem (int r, int c) const |
|
896 { |
254
|
897 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
898 { |
|
899 (*current_liboctave_error_handler) ("range error"); |
|
900 T foo; |
1470
|
901 static T *bar = &foo; |
254
|
902 return foo; |
|
903 } |
|
904 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
905 } |
|
906 |
|
907 template <class T> |
|
908 T |
|
909 DiagArray<T>::operator () (int r, int c) const |
|
910 { |
254
|
911 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
912 { |
|
913 (*current_liboctave_error_handler) ("range error"); |
|
914 T foo; |
1470
|
915 static T *bar = &foo; |
254
|
916 return foo; |
|
917 } |
|
918 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
919 } |
|
920 |
|
921 template <class T> |
|
922 void |
|
923 DiagArray<T>::resize (int r, int c) |
|
924 { |
|
925 if (r < 0 || c < 0) |
|
926 { |
|
927 (*current_liboctave_error_handler) |
|
928 ("can't resize to negative dimensions"); |
|
929 return; |
|
930 } |
|
931 |
|
932 if (r == dim1 () && c == dim2 ()) |
|
933 return; |
|
934 |
|
935 ArrayRep<T> *old_rep = rep; |
|
936 const T *old_data = data (); |
|
937 int old_len = length (); |
|
938 |
|
939 int new_len = r < c ? r : c; |
|
940 |
|
941 rep = new ArrayRep<T> (new_len); |
|
942 rep->count = 1; |
|
943 |
|
944 nr = r; |
|
945 nc = c; |
|
946 |
|
947 if (old_data && old_len > 0) |
|
948 { |
|
949 int min_len = old_len < new_len ? old_len : new_len; |
|
950 |
|
951 for (int i = 0; i < min_len; i++) |
|
952 xelem (i, i) = old_data[i]; |
|
953 } |
|
954 |
|
955 if (--old_rep->count <= 0) |
|
956 delete old_rep; |
|
957 } |
|
958 |
|
959 template <class T> |
|
960 void |
|
961 DiagArray<T>::resize (int r, int c, const T& val) |
|
962 { |
|
963 if (r < 0 || c < 0) |
|
964 { |
|
965 (*current_liboctave_error_handler) |
|
966 ("can't resize to negative dimensions"); |
|
967 return; |
|
968 } |
|
969 |
|
970 if (r == dim1 () && c == dim2 ()) |
|
971 return; |
|
972 |
|
973 ArrayRep<T> *old_rep = rep; |
|
974 const T *old_data = data (); |
|
975 int old_len = length (); |
|
976 |
|
977 int new_len = r < c ? r : c; |
|
978 |
|
979 rep = new ArrayRep<T> (new_len); |
|
980 rep->count = 1; |
|
981 |
|
982 nr = r; |
|
983 nc = c; |
|
984 |
|
985 int min_len = old_len < new_len ? old_len : new_len; |
|
986 |
|
987 if (old_data && old_len > 0) |
|
988 { |
|
989 for (int i = 0; i < min_len; i++) |
|
990 xelem (i, i) = old_data[i]; |
|
991 } |
|
992 |
|
993 for (int i = min_len; i < new_len; i++) |
|
994 xelem (i, i) = val; |
|
995 |
|
996 if (--old_rep->count <= 0) |
|
997 delete old_rep; |
|
998 } |
|
999 |
|
1000 /* |
|
1001 ;;; Local Variables: *** |
|
1002 ;;; mode: C++ *** |
|
1003 ;;; page-delimiter: "^/\\*" *** |
|
1004 ;;; End: *** |
|
1005 */ |