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 |
1574
|
484 #ifndef NO_DIAG_ARRAY |
880
|
485 #if 1 |
344
|
486 template <class T> |
|
487 T& |
|
488 DiagArray<T>::elem (int r, int c) |
|
489 { |
|
490 static T foo (0); |
|
491 return (r == c) ? Array<T>::elem (r) : foo; |
|
492 } |
|
493 |
|
494 template <class T> |
|
495 T& |
|
496 DiagArray<T>::checkelem (int r, int c) |
|
497 { |
|
498 static T foo (0); |
|
499 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
500 { |
|
501 (*current_liboctave_error_handler) ("range error"); |
|
502 return foo; |
|
503 } |
|
504 return (r == c) ? Array<T>::elem (r) : foo; |
|
505 } |
|
506 |
|
507 template <class T> |
|
508 T& |
|
509 DiagArray<T>::operator () (int r, int c) |
|
510 { |
|
511 static T foo (0); |
|
512 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
513 { |
|
514 (*current_liboctave_error_handler) ("range error"); |
|
515 return foo; |
|
516 } |
|
517 return (r == c) ? Array<T>::elem (r) : foo; |
|
518 } |
347
|
519 #endif |
344
|
520 |
237
|
521 template <class T> |
|
522 T& |
|
523 DiagArray<T>::xelem (int r, int c) |
|
524 { |
|
525 static T foo (0); |
|
526 return (r == c) ? Array<T>::xelem (r) : foo; |
|
527 } |
|
528 |
|
529 template <class T> |
|
530 T |
|
531 DiagArray<T>::elem (int r, int c) const |
|
532 { |
|
533 return (r == c) ? Array<T>::elem (r) : T (0); |
|
534 } |
|
535 |
|
536 template <class T> |
|
537 T |
|
538 DiagArray<T>::checkelem (int r, int c) const |
|
539 { |
254
|
540 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
541 { |
|
542 (*current_liboctave_error_handler) ("range error"); |
|
543 T foo; |
1470
|
544 static T *bar = &foo; |
254
|
545 return foo; |
|
546 } |
|
547 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
548 } |
|
549 |
|
550 template <class T> |
|
551 T |
|
552 DiagArray<T>::operator () (int r, int c) const |
|
553 { |
254
|
554 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
555 { |
|
556 (*current_liboctave_error_handler) ("range error"); |
|
557 T foo; |
1470
|
558 static T *bar = &foo; |
254
|
559 return foo; |
|
560 } |
|
561 return (r == c) ? Array<T>::elem (r) : T (0); |
237
|
562 } |
|
563 |
|
564 template <class T> |
|
565 void |
|
566 DiagArray<T>::resize (int r, int c) |
|
567 { |
|
568 if (r < 0 || c < 0) |
|
569 { |
1560
|
570 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
237
|
571 return; |
|
572 } |
|
573 |
|
574 if (r == dim1 () && c == dim2 ()) |
|
575 return; |
|
576 |
|
577 ArrayRep<T> *old_rep = rep; |
|
578 const T *old_data = data (); |
|
579 int old_len = length (); |
|
580 |
|
581 int new_len = r < c ? r : c; |
|
582 |
|
583 rep = new ArrayRep<T> (new_len); |
|
584 rep->count = 1; |
|
585 |
1560
|
586 SET_MAX_INDICES (2); |
|
587 |
237
|
588 nr = r; |
|
589 nc = c; |
|
590 |
|
591 if (old_data && old_len > 0) |
|
592 { |
|
593 int min_len = old_len < new_len ? old_len : new_len; |
|
594 |
|
595 for (int i = 0; i < min_len; i++) |
|
596 xelem (i, i) = old_data[i]; |
|
597 } |
|
598 |
|
599 if (--old_rep->count <= 0) |
|
600 delete old_rep; |
|
601 } |
|
602 |
|
603 template <class T> |
|
604 void |
|
605 DiagArray<T>::resize (int r, int c, const T& val) |
|
606 { |
|
607 if (r < 0 || c < 0) |
|
608 { |
1560
|
609 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
237
|
610 return; |
|
611 } |
|
612 |
|
613 if (r == dim1 () && c == dim2 ()) |
|
614 return; |
|
615 |
|
616 ArrayRep<T> *old_rep = rep; |
|
617 const T *old_data = data (); |
|
618 int old_len = length (); |
|
619 |
|
620 int new_len = r < c ? r : c; |
|
621 |
|
622 rep = new ArrayRep<T> (new_len); |
|
623 rep->count = 1; |
|
624 |
1560
|
625 SET_MAX_INDICES (2); |
|
626 |
237
|
627 nr = r; |
|
628 nc = c; |
|
629 |
|
630 int min_len = old_len < new_len ? old_len : new_len; |
|
631 |
|
632 if (old_data && old_len > 0) |
|
633 { |
|
634 for (int i = 0; i < min_len; i++) |
|
635 xelem (i, i) = old_data[i]; |
|
636 } |
|
637 |
|
638 for (int i = min_len; i < new_len; i++) |
|
639 xelem (i, i) = val; |
|
640 |
|
641 if (--old_rep->count <= 0) |
|
642 delete old_rep; |
|
643 } |
1574
|
644 #endif |
237
|
645 |
|
646 /* |
|
647 ;;; Local Variables: *** |
|
648 ;;; mode: C++ *** |
|
649 ;;; page-delimiter: "^/\\*" *** |
|
650 ;;; End: *** |
|
651 */ |