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 |
1560
|
34 #include <iostream.h> |
|
35 |
237
|
36 #include "Array.h" |
|
37 |
1560
|
38 #if defined (HEAVYWEIGHT_INDEXING) |
|
39 #include "idx-vector.h" |
|
40 #include "Array-idx.h" |
|
41 #endif |
|
42 |
|
43 #include "lo-error.h" |
|
44 |
1360
|
45 // The real representation of all arrays. |
237
|
46 |
|
47 template <class T> |
|
48 ArrayRep<T>::ArrayRep (int n) |
|
49 { |
|
50 len = n; |
|
51 data = new T [len]; |
1560
|
52 |
|
53 #ifdef HEAVYWEIGHT_INDEXING |
|
54 idx = 0; |
|
55 max_indices = 0; |
|
56 idx_count = 0; |
|
57 #endif |
237
|
58 } |
|
59 |
|
60 template <class T> |
|
61 ArrayRep<T>::ArrayRep (const ArrayRep<T>& a) |
|
62 { |
|
63 len = a.len; |
|
64 count = a.count; |
1560
|
65 |
237
|
66 data = new T [len]; |
|
67 for (int i = 0; i < len; i++) |
|
68 data[i] = a.data[i]; |
1560
|
69 |
|
70 #ifdef HEAVYWEIGHT_INDEXING |
|
71 max_indices = a.max_indices; |
|
72 idx_count = a.idx_count; |
|
73 if (a.idx) |
|
74 { |
|
75 idx_vector *idx = new idx_vector [max_indices]; |
|
76 for (int i = 0; i < max_indices; i++) |
|
77 idx[i] = a.idx[i]; |
|
78 } |
|
79 else |
|
80 idx = 0; |
|
81 #endif |
237
|
82 } |
|
83 |
|
84 template <class T> |
|
85 ArrayRep<T>::~ArrayRep (void) |
|
86 { |
|
87 delete [] data; |
1560
|
88 delete [] idx; |
237
|
89 } |
|
90 |
|
91 template <class T> |
|
92 T& |
|
93 ArrayRep<T>::elem (int n) |
|
94 { |
|
95 return data[n]; |
|
96 } |
|
97 |
|
98 template <class T> |
|
99 T |
|
100 ArrayRep<T>::elem (int n) const |
|
101 { |
|
102 return data[n]; |
|
103 } |
|
104 |
1360
|
105 // One dimensional array class. Handles the reference counting for |
|
106 // all the derived classes. |
237
|
107 |
|
108 template <class T> |
|
109 Array<T>::Array (int n, const T& val) |
|
110 { |
|
111 rep = new ArrayRep<T> (n); |
|
112 rep->count = 1; |
|
113 for (int i = 0; i < n; i++) |
|
114 rep->data[i] = val; |
|
115 } |
|
116 |
|
117 template <class T> |
|
118 Array<T>& |
|
119 Array<T>::operator = (const Array<T>& a) |
|
120 { |
659
|
121 if (this != &a) |
|
122 { |
|
123 if (--rep->count <= 0) |
|
124 delete rep; |
237
|
125 |
659
|
126 rep = a.rep; |
|
127 rep->count++; |
|
128 } |
237
|
129 return *this; |
|
130 } |
|
131 |
|
132 template <class T> |
|
133 T& |
|
134 Array<T>::checkelem (int n) |
|
135 { |
|
136 if (n < 0 || n >= rep->length ()) |
|
137 { |
|
138 (*current_liboctave_error_handler) ("range error"); |
254
|
139 static T foo; |
237
|
140 return foo; |
|
141 } |
|
142 return elem (n); |
|
143 } |
|
144 |
|
145 template <class T> |
|
146 T |
|
147 Array<T>::elem (int n) const |
|
148 { |
|
149 return rep->elem (n); |
|
150 } |
|
151 |
|
152 template <class T> |
|
153 T |
|
154 Array<T>::checkelem (int n) const |
|
155 { |
|
156 if (n < 0 || n >= rep->length ()) |
|
157 { |
|
158 (*current_liboctave_error_handler) ("range error"); |
254
|
159 T foo; |
1470
|
160 static T *bar = &foo; |
254
|
161 return foo; |
237
|
162 } |
|
163 return elem (n); |
|
164 } |
|
165 |
|
166 template <class T> |
|
167 T |
|
168 Array<T>::operator () (int n) const |
|
169 { |
|
170 return checkelem (n); |
|
171 } |
|
172 |
|
173 template <class T> |
|
174 void |
|
175 Array<T>::resize (int n) |
|
176 { |
|
177 if (n < 0) |
|
178 { |
1560
|
179 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
180 return; |
|
181 } |
|
182 |
|
183 if (n == length ()) |
|
184 return; |
|
185 |
|
186 ArrayRep<T> *old_rep = rep; |
|
187 const T *old_data = data (); |
|
188 int old_len = length (); |
|
189 |
|
190 rep = new ArrayRep<T> (n); |
|
191 rep->count = 1; |
|
192 |
1560
|
193 SET_MAX_INDICES (1); |
|
194 |
237
|
195 if (old_data && old_len > 0) |
|
196 { |
|
197 int min_len = old_len < n ? old_len : n; |
|
198 |
|
199 for (int i = 0; i < min_len; i++) |
|
200 xelem (i) = old_data[i]; |
|
201 } |
|
202 |
|
203 if (--old_rep->count <= 0) |
|
204 delete old_rep; |
|
205 } |
|
206 |
|
207 template <class T> |
|
208 void |
|
209 Array<T>::resize (int n, const T& val) |
|
210 { |
|
211 if (n < 0) |
|
212 { |
1560
|
213 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
214 return; |
|
215 } |
|
216 |
|
217 if (n == length ()) |
|
218 return; |
|
219 |
|
220 ArrayRep<T> *old_rep = rep; |
|
221 const T *old_data = data (); |
|
222 int old_len = length (); |
|
223 |
|
224 rep = new ArrayRep<T> (n); |
|
225 rep->count = 1; |
|
226 |
1560
|
227 SET_MAX_INDICES (1); |
|
228 |
237
|
229 int min_len = old_len < n ? old_len : n; |
|
230 |
|
231 if (old_data && old_len > 0) |
|
232 { |
|
233 for (int i = 0; i < min_len; i++) |
|
234 xelem (i) = old_data[i]; |
|
235 } |
|
236 |
|
237 for (int i = old_len; i < n; i++) |
|
238 xelem (i) = val; |
|
239 |
|
240 if (--old_rep->count <= 0) |
|
241 delete old_rep; |
|
242 } |
|
243 |
|
244 template <class T> |
|
245 T * |
|
246 Array<T>::fortran_vec (void) |
|
247 { |
|
248 if (rep->count > 1) |
|
249 { |
|
250 --rep->count; |
|
251 rep = new ArrayRep<T> (*rep); |
|
252 rep->count = 1; |
|
253 } |
|
254 return rep->data; |
|
255 } |
|
256 |
1360
|
257 // Two dimensional array class. |
237
|
258 |
|
259 template <class T> |
|
260 T& |
|
261 Array2<T>::checkelem (int i, int j) |
|
262 { |
254
|
263 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
264 { |
|
265 (*current_liboctave_error_handler) ("range error"); |
|
266 static T foo; |
|
267 return foo; |
|
268 } |
|
269 return Array<T>::elem (d1*j+i); |
237
|
270 } |
|
271 |
|
272 template <class T> |
|
273 T |
|
274 Array2<T>::elem (int i, int j) const |
|
275 { |
|
276 return Array<T>::elem (d1*j+i); |
|
277 } |
|
278 |
|
279 template <class T> |
|
280 T |
|
281 Array2<T>::checkelem (int i, int j) const |
|
282 { |
254
|
283 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
284 { |
|
285 (*current_liboctave_error_handler) ("range error"); |
|
286 T foo; |
1470
|
287 static T *bar = &foo; |
254
|
288 return foo; |
|
289 } |
|
290 return Array<T>::elem (d1*j+i); |
237
|
291 } |
|
292 |
|
293 template <class T> |
|
294 T |
|
295 Array2<T>::operator () (int i, int j) const |
|
296 { |
254
|
297 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
298 { |
|
299 (*current_liboctave_error_handler) ("range error"); |
|
300 T foo; |
1470
|
301 static T *bar = &foo; |
254
|
302 return foo; |
|
303 } |
|
304 return Array<T>::elem (d1*j+i); |
237
|
305 } |
|
306 |
|
307 template <class T> |
|
308 void |
|
309 Array2<T>::resize (int r, int c) |
|
310 { |
|
311 if (r < 0 || c < 0) |
|
312 { |
1560
|
313 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
314 return; |
|
315 } |
|
316 |
|
317 if (r == dim1 () && c == dim2 ()) |
|
318 return; |
|
319 |
|
320 ArrayRep<T> *old_rep = rep; |
|
321 const T *old_data = data (); |
1560
|
322 |
237
|
323 int old_d1 = dim1 (); |
|
324 int old_d2 = dim2 (); |
|
325 int old_len = length (); |
|
326 |
|
327 rep = new ArrayRep<T> (r*c); |
|
328 rep->count = 1; |
|
329 |
1560
|
330 SET_MAX_INDICES (2); |
|
331 |
237
|
332 d1 = r; |
|
333 d2 = c; |
|
334 |
|
335 if (old_data && old_len > 0) |
|
336 { |
|
337 int min_r = old_d1 < r ? old_d1 : r; |
|
338 int min_c = old_d2 < c ? old_d2 : c; |
|
339 |
|
340 for (int j = 0; j < min_c; j++) |
|
341 for (int i = 0; i < min_r; i++) |
|
342 xelem (i, j) = old_data[old_d1*j+i]; |
|
343 } |
|
344 |
|
345 if (--old_rep->count <= 0) |
|
346 delete old_rep; |
|
347 } |
|
348 |
|
349 template <class T> |
|
350 void |
|
351 Array2<T>::resize (int r, int c, const T& val) |
|
352 { |
|
353 if (r < 0 || c < 0) |
|
354 { |
1560
|
355 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
356 return; |
|
357 } |
|
358 |
|
359 if (r == dim1 () && c == dim2 ()) |
|
360 return; |
|
361 |
|
362 ArrayRep<T> *old_rep = rep; |
|
363 const T *old_data = data (); |
|
364 int old_d1 = dim1 (); |
|
365 int old_d2 = dim2 (); |
|
366 int old_len = length (); |
|
367 |
|
368 rep = new ArrayRep<T> (r*c); |
|
369 rep->count = 1; |
|
370 |
1560
|
371 SET_MAX_INDICES (2); |
|
372 |
237
|
373 d1 = r; |
|
374 d2 = c; |
|
375 |
|
376 int min_r = old_d1 < r ? old_d1 : r; |
|
377 int min_c = old_d2 < c ? old_d2 : c; |
|
378 |
|
379 if (old_data && old_len > 0) |
|
380 { |
1321
|
381 for (int j = 0; j < min_c; j++) |
|
382 for (int i = 0; i < min_r; i++) |
237
|
383 xelem (i, j) = old_data[old_d1*j+i]; |
|
384 } |
|
385 |
1321
|
386 for (int j = 0; j < min_c; j++) |
|
387 for (int i = min_r; i < r; i++) |
237
|
388 xelem (i, j) = val; |
|
389 |
1321
|
390 for (int j = min_c; j < c; j++) |
|
391 for (int i = 0; i < r; i++) |
237
|
392 xelem (i, j) = val; |
|
393 |
|
394 if (--old_rep->count <= 0) |
|
395 delete old_rep; |
|
396 } |
|
397 |
1561
|
398 template <class T> |
|
399 Array2<T>& |
|
400 Array2<T>::insert (const Array2<T>& a, int r, int c) |
|
401 { |
|
402 int a_rows = a.rows (); |
|
403 int a_cols = a.cols (); |
|
404 if (r < 0 || r + a_rows - 1 > rows () |
|
405 || c < 0 || c + a_cols - 1 > cols ()) |
|
406 { |
|
407 (*current_liboctave_error_handler) ("range error for insert"); |
|
408 return *this; |
|
409 } |
|
410 |
|
411 for (int j = 0; j < a_cols; j++) |
|
412 for (int i = 0; i < a_rows; i++) |
|
413 elem (r+i, c+j) = a.elem (i, j); |
|
414 |
|
415 return *this; |
|
416 } |
|
417 |
1360
|
418 // Three dimensional array class. |
237
|
419 |
|
420 template <class T> |
|
421 T& |
|
422 Array3<T>::checkelem (int i, int j, int k) |
|
423 { |
254
|
424 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
425 { |
|
426 (*current_liboctave_error_handler) ("range error"); |
|
427 static T foo; |
|
428 return foo; |
|
429 } |
|
430 return Array2<T>::elem (i, d1*k+j); |
237
|
431 } |
|
432 |
|
433 template <class T> |
|
434 T |
|
435 Array3<T>::elem (int i, int j, int k) const |
|
436 { |
|
437 return Array2<T>::elem (i, d2*k+j); |
|
438 } |
|
439 |
|
440 template <class T> |
|
441 T |
|
442 Array3<T>::checkelem (int i, int j, int k) const |
|
443 { |
254
|
444 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
445 { |
|
446 (*current_liboctave_error_handler) ("range error"); |
|
447 T foo; |
1470
|
448 static T *bar = &foo; |
254
|
449 return foo; |
|
450 } |
|
451 return Array2<T>::elem (i, d1*k+j); |
237
|
452 } |
|
453 |
|
454 template <class T> |
|
455 T |
|
456 Array3<T>::operator () (int i, int j, int k) const |
|
457 { |
254
|
458 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
459 { |
|
460 (*current_liboctave_error_handler) ("range error"); |
|
461 T foo; |
1470
|
462 static T *bar = &foo; |
254
|
463 return foo; |
|
464 } |
|
465 return Array2<T>::elem (i, d2*k+j); |
237
|
466 } |
|
467 |
|
468 template <class T> |
|
469 void |
|
470 Array3<T>::resize (int n, int m, int k) |
|
471 { |
1360
|
472 assert (0); // XXX FIXME XXX |
237
|
473 } |
|
474 |
|
475 template <class T> |
|
476 void |
|
477 Array3<T>::resize (int n, int m, int k, const T& val) |
|
478 { |
1360
|
479 assert (0); // XXX FIXME XXX |
237
|
480 } |
|
481 |
1360
|
482 // A two-dimensional array with diagonal elements only. |
237
|
483 |
880
|
484 #if 1 |
344
|
485 template <class T> |
|
486 T& |
|
487 DiagArray<T>::elem (int r, int c) |
|
488 { |
|
489 static T foo (0); |
|
490 return (r == c) ? Array<T>::elem (r) : foo; |
|
491 } |
|
492 |
|
493 template <class T> |
|
494 T& |
|
495 DiagArray<T>::checkelem (int r, int c) |
|
496 { |
|
497 static T foo (0); |
|
498 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
499 { |
|
500 (*current_liboctave_error_handler) ("range error"); |
|
501 return foo; |
|
502 } |
|
503 return (r == c) ? Array<T>::elem (r) : foo; |
|
504 } |
|
505 |
|
506 template <class T> |
|
507 T& |
|
508 DiagArray<T>::operator () (int r, int c) |
|
509 { |
|
510 static T foo (0); |
|
511 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
512 { |
|
513 (*current_liboctave_error_handler) ("range error"); |
|
514 return foo; |
|
515 } |
|
516 return (r == c) ? Array<T>::elem (r) : foo; |
|
517 } |
347
|
518 #endif |
344
|
519 |
237
|
520 template <class T> |
|
521 T& |
|
522 DiagArray<T>::xelem (int r, int c) |
|
523 { |
|
524 static T foo (0); |
|
525 return (r == c) ? Array<T>::xelem (r) : foo; |
|
526 } |
|
527 |
|
528 template <class T> |
|
529 T |
|
530 DiagArray<T>::elem (int r, int c) const |
|
531 { |
|
532 return (r == c) ? Array<T>::elem (r) : T (0); |
|
533 } |
|
534 |
|
535 template <class T> |
|
536 T |
|
537 DiagArray<T>::checkelem (int r, int c) const |
|
538 { |
254
|
539 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
540 { |
|
541 (*current_liboctave_error_handler) ("range error"); |
|
542 T foo; |
1470
|
543 static T *bar = &foo; |
254
|
544 return foo; |
|
545 } |
|
546 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
547 } |
|
548 |
|
549 template <class T> |
|
550 T |
|
551 DiagArray<T>::operator () (int r, int c) const |
|
552 { |
254
|
553 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
554 { |
|
555 (*current_liboctave_error_handler) ("range error"); |
|
556 T foo; |
1470
|
557 static T *bar = &foo; |
254
|
558 return foo; |
|
559 } |
|
560 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
561 } |
|
562 |
|
563 template <class T> |
|
564 void |
|
565 DiagArray<T>::resize (int r, int c) |
|
566 { |
|
567 if (r < 0 || c < 0) |
|
568 { |
1560
|
569 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
237
|
570 return; |
|
571 } |
|
572 |
|
573 if (r == dim1 () && c == dim2 ()) |
|
574 return; |
|
575 |
|
576 ArrayRep<T> *old_rep = rep; |
|
577 const T *old_data = data (); |
|
578 int old_len = length (); |
|
579 |
|
580 int new_len = r < c ? r : c; |
|
581 |
|
582 rep = new ArrayRep<T> (new_len); |
|
583 rep->count = 1; |
|
584 |
1560
|
585 SET_MAX_INDICES (2); |
|
586 |
237
|
587 nr = r; |
|
588 nc = c; |
|
589 |
|
590 if (old_data && old_len > 0) |
|
591 { |
|
592 int min_len = old_len < new_len ? old_len : new_len; |
|
593 |
|
594 for (int i = 0; i < min_len; i++) |
|
595 xelem (i, i) = old_data[i]; |
|
596 } |
|
597 |
|
598 if (--old_rep->count <= 0) |
|
599 delete old_rep; |
|
600 } |
|
601 |
|
602 template <class T> |
|
603 void |
|
604 DiagArray<T>::resize (int r, int c, const T& val) |
|
605 { |
|
606 if (r < 0 || c < 0) |
|
607 { |
1560
|
608 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
237
|
609 return; |
|
610 } |
|
611 |
|
612 if (r == dim1 () && c == dim2 ()) |
|
613 return; |
|
614 |
|
615 ArrayRep<T> *old_rep = rep; |
|
616 const T *old_data = data (); |
|
617 int old_len = length (); |
|
618 |
|
619 int new_len = r < c ? r : c; |
|
620 |
|
621 rep = new ArrayRep<T> (new_len); |
|
622 rep->count = 1; |
|
623 |
1560
|
624 SET_MAX_INDICES (2); |
|
625 |
237
|
626 nr = r; |
|
627 nc = c; |
|
628 |
|
629 int min_len = old_len < new_len ? old_len : new_len; |
|
630 |
|
631 if (old_data && old_len > 0) |
|
632 { |
|
633 for (int i = 0; i < min_len; i++) |
|
634 xelem (i, i) = old_data[i]; |
|
635 } |
|
636 |
|
637 for (int i = min_len; i < new_len; i++) |
|
638 xelem (i, i) = val; |
|
639 |
|
640 if (--old_rep->count <= 0) |
|
641 delete old_rep; |
|
642 } |
|
643 |
|
644 /* |
|
645 ;;; Local Variables: *** |
|
646 ;;; mode: C++ *** |
|
647 ;;; page-delimiter: "^/\\*" *** |
|
648 ;;; End: *** |
|
649 */ |