228
|
1 // Template array classes -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1993, 1994, 1995 John W. Eaton |
228
|
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. |
228
|
21 |
|
22 */ |
|
23 |
382
|
24 #if !defined (octave_Array_h) |
|
25 #define octave_Array_h 1 |
|
26 |
1296
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
1560
|
31 #define HEAVYWEIGHT_INDEXING 1 |
|
32 |
1366
|
33 #include <cassert> |
228
|
34 |
448
|
35 #include "lo-error.h" |
354
|
36 |
1560
|
37 class idx_vector; |
|
38 |
238
|
39 // Classes we declare. |
228
|
40 |
238
|
41 template <class T> class ArrayRep; |
|
42 template <class T> class Array; |
|
43 template <class T> class Array2; |
|
44 template <class T> class Array3; |
1574
|
45 |
|
46 #ifndef NO_DIAG_ARRAY |
238
|
47 template <class T> class DiagArray; |
1574
|
48 #endif |
238
|
49 |
1359
|
50 // The real representation of all arrays. |
228
|
51 |
|
52 template <class T> |
|
53 class ArrayRep |
|
54 { |
1359
|
55 // Rethink resize()? |
|
56 |
228
|
57 friend class Array<T>; |
238
|
58 friend class Array2<T>; |
|
59 friend class Array3<T>; |
1574
|
60 |
|
61 #ifndef NO_DIAG_ARRAY |
238
|
62 friend class DiagArray<T>; |
1574
|
63 #endif |
238
|
64 |
1550
|
65 private: |
|
66 |
1560
|
67 T *data; |
1550
|
68 int count; |
|
69 int len; |
1560
|
70 |
238
|
71 protected: |
|
72 |
1550
|
73 ArrayRep (T *d, int l) |
|
74 { |
1560
|
75 data = d; |
1550
|
76 len = l; |
|
77 } |
228
|
78 |
|
79 public: |
|
80 |
1550
|
81 ArrayRep (void) |
|
82 { |
1560
|
83 data = 0; |
1550
|
84 len = 0; |
|
85 } |
|
86 |
238
|
87 ArrayRep (int n); |
1550
|
88 |
238
|
89 ArrayRep (const ArrayRep<T>& a); |
228
|
90 |
|
91 ~ArrayRep (void); |
238
|
92 |
1550
|
93 int length (void) const { return len; } |
238
|
94 |
228
|
95 T& elem (int n); |
238
|
96 |
228
|
97 T elem (int n) const; |
|
98 }; |
|
99 |
1359
|
100 // One dimensional array class. Handles the reference counting for |
|
101 // all the derived classes. |
238
|
102 |
228
|
103 template <class T> |
|
104 class Array |
|
105 { |
1619
|
106 private: |
|
107 |
|
108 #ifdef HEAVYWEIGHT_INDEXING |
|
109 idx_vector *idx; |
|
110 int max_indices; |
|
111 int idx_count; |
|
112 #endif |
|
113 |
238
|
114 protected: |
|
115 |
|
116 ArrayRep<T> *rep; |
|
117 |
1550
|
118 Array (T *d, int l) |
|
119 { |
|
120 rep = new ArrayRep<T> (d, l); |
|
121 rep->count = 1; |
1619
|
122 |
|
123 #ifdef HEAVYWEIGHT_INDEXING |
|
124 idx = 0; |
|
125 max_indices = 1; |
|
126 idx_count = 0; |
|
127 #endif |
1550
|
128 } |
238
|
129 |
228
|
130 public: |
238
|
131 |
1550
|
132 Array (void) |
|
133 { |
1560
|
134 rep = new ArrayRep<T> (); |
1550
|
135 rep->count = 1; |
1619
|
136 |
|
137 #ifdef HEAVYWEIGHT_INDEXING |
|
138 idx = 0; |
|
139 max_indices = 1; |
|
140 idx_count = 0; |
|
141 #endif |
1550
|
142 } |
|
143 |
|
144 Array (int n) |
|
145 { |
|
146 rep = new ArrayRep<T> (n); |
|
147 rep->count = 1; |
1619
|
148 |
|
149 #ifdef HEAVYWEIGHT_INDEXING |
|
150 idx = 0; |
|
151 max_indices = 1; |
|
152 idx_count = 0; |
|
153 #endif |
1550
|
154 } |
|
155 |
238
|
156 Array (int n, const T& val); |
|
157 |
1550
|
158 Array (const Array<T>& a) |
|
159 { |
|
160 rep = a.rep; |
|
161 rep->count++; |
1619
|
162 |
|
163 #ifdef HEAVYWEIGHT_INDEXING |
|
164 max_indices = a.max_indices; |
|
165 idx_count = 0; |
|
166 idx = 0; |
|
167 #endif |
1550
|
168 } |
228
|
169 |
1619
|
170 ~Array (void); |
228
|
171 |
|
172 Array<T>& operator = (const Array<T>& a); |
238
|
173 |
1550
|
174 int capacity (void) const { return rep->length (); } |
|
175 int length (void) const { return rep->length (); } |
228
|
176 |
1550
|
177 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 |
228
|
188 T& checkelem (int n); |
1550
|
189 T& operator () (int n) { return checkelem (n); } |
228
|
190 |
1359
|
191 // No checking. |
|
192 |
1550
|
193 T& xelem (int n) { return rep->elem (n); } |
238
|
194 |
228
|
195 T elem (int n) const; |
|
196 T checkelem (int n) const; |
|
197 T operator () (int n) const; |
|
198 |
238
|
199 void resize (int n); |
|
200 void resize (int n, const T& val); |
|
201 |
1550
|
202 const T *data (void) const { return rep->data; } |
228
|
203 |
238
|
204 T *fortran_vec (void); |
1560
|
205 |
|
206 #ifdef HEAVYWEIGHT_INDEXING |
1619
|
207 void set_max_indices (int mi) { max_indices = mi; } |
1560
|
208 |
1619
|
209 void clear_index (void); |
1560
|
210 |
1619
|
211 void set_index (const idx_vector& i); |
1560
|
212 |
1619
|
213 int index_count (void) const { return idx_count; } |
1560
|
214 |
1619
|
215 idx_vector *get_idx (void) const { return idx; } |
1560
|
216 |
|
217 void maybe_delete_elements (idx_vector& i); |
|
218 |
|
219 Array<T> value (void); |
|
220 #endif |
228
|
221 }; |
|
222 |
1560
|
223 template <class LT, class RT> |
|
224 int assign (Array<LT>& lhs, const Array<RT>& rhs); |
|
225 |
1359
|
226 // Two dimensional array class. |
238
|
227 |
228
|
228 template <class T> |
|
229 class Array2 : public Array<T> |
|
230 { |
238
|
231 protected: |
|
232 |
1550
|
233 Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
234 { |
|
235 d1 = n; |
|
236 d2 = m; |
1560
|
237 set_max_indices (2); |
1550
|
238 } |
238
|
239 |
228
|
240 public: |
|
241 |
1560
|
242 // These really need to be protected (and they will be in the |
|
243 // future, so don't depend on them being here!), but they can't be |
|
244 // until template friends work correctly in g++. |
|
245 |
|
246 int d1; |
|
247 int d2; |
|
248 |
1550
|
249 Array2 (void) : Array<T> () |
|
250 { |
|
251 d1 = 0; |
|
252 d2 = 0; |
1560
|
253 set_max_indices (2); |
1550
|
254 } |
|
255 |
|
256 Array2 (int n, int m) : Array<T> (n*m) |
|
257 { |
|
258 d1 = n; |
|
259 d2 = m; |
1560
|
260 set_max_indices (2); |
1550
|
261 } |
|
262 |
|
263 Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
264 { |
|
265 d1 = n; |
|
266 d2 = m; |
1560
|
267 set_max_indices (2); |
1550
|
268 } |
|
269 |
|
270 Array2 (const Array2<T>& a) : Array<T> (a) |
|
271 { |
|
272 d1 = a.d1; |
|
273 d2 = a.d2; |
1560
|
274 set_max_indices (2); |
1550
|
275 } |
|
276 |
1560
|
277 Array2 (const Array<T>& a, int n, int m) : Array<T> (a) |
|
278 { |
|
279 d1 = n; |
|
280 d2 = m; |
|
281 set_max_indices (2); |
|
282 } |
|
283 |
1574
|
284 #ifndef NO_DIAG_ARRAY |
1560
|
285 Array2 (const DiagArray<T>& a) : Array<T> (a.rows () * a.cols (), T (0)) |
1550
|
286 { |
|
287 for (int i = 0; i < a.length (); i++) |
|
288 elem (i, i) = a.elem (i, i); |
1560
|
289 |
|
290 set_max_indices (2); |
1550
|
291 } |
1574
|
292 #endif |
228
|
293 |
1230
|
294 ~Array2 (void) { } |
|
295 |
1550
|
296 Array2<T>& operator = (const Array2<T>& a) |
|
297 { |
|
298 if (this != &a) |
|
299 { |
|
300 Array<T>::operator = (a); |
|
301 d1 = a.d1; |
|
302 d2 = a.d2; |
|
303 } |
238
|
304 |
1550
|
305 return *this; |
|
306 } |
|
307 |
|
308 int dim1 (void) const { return d1; } |
|
309 int dim2 (void) const { return d2; } |
238
|
310 |
1550
|
311 int rows (void) const { return d1; } |
|
312 int cols (void) const { return d2; } |
|
313 int columns (void) const { return d2; } |
|
314 |
|
315 T& elem (int i, int j) { return Array<T>::elem (d1*j+i); } |
238
|
316 T& checkelem (int i, int j); |
1550
|
317 T& operator () (int i, int j) { return checkelem (i, j); } |
238
|
318 |
1359
|
319 // No checking. |
|
320 |
1550
|
321 T& xelem (int i, int j) { return Array<T>::xelem (d1*j+i); } |
238
|
322 |
228
|
323 T elem (int i, int j) const; |
|
324 T checkelem (int i, int j) const; |
|
325 T operator () (int i, int j) const; |
|
326 |
238
|
327 void resize (int n, int m); |
|
328 void resize (int n, int m, const T& val); |
1560
|
329 |
1561
|
330 Array2<T>& insert (const Array2<T>& a, int r, int c); |
|
331 |
1560
|
332 #ifdef HEAVYWEIGHT_INDEXING |
|
333 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
334 |
|
335 Array2<T> value (void); |
|
336 #endif |
228
|
337 }; |
|
338 |
1560
|
339 template <class LT, class RT> |
|
340 int assign (Array2<LT>& lhs, const Array2<RT>& rhs); |
|
341 |
1359
|
342 // Three dimensional array class. |
238
|
343 |
228
|
344 template <class T> |
|
345 class Array3 : public Array2<T> |
|
346 { |
238
|
347 protected: |
|
348 |
|
349 int d3; |
|
350 |
1550
|
351 Array3 (T *d, int n, int m, int k) : Array2<T> (d, n, m*k) |
|
352 { |
|
353 d2 = m; |
|
354 d3 = k; |
1560
|
355 set_max_indices (3); |
1550
|
356 } |
238
|
357 |
228
|
358 public: |
|
359 |
1550
|
360 Array3 (void) : Array2<T> () |
|
361 { |
|
362 d2 = 0; |
|
363 d3 = 0; |
1560
|
364 set_max_indices (3); |
1550
|
365 } |
|
366 |
|
367 Array3 (int n, int m, int k) : Array2<T> (n, m*k) |
|
368 { |
|
369 d2 = m; |
|
370 d3 = k; |
1560
|
371 set_max_indices (3); |
1550
|
372 } |
|
373 |
|
374 Array3 (int n, int m, int k, const T& val) : Array2<T> (n, m*k, val) |
|
375 { |
|
376 d2 = m; |
|
377 d3 = k; |
1560
|
378 set_max_indices (3); |
1550
|
379 } |
|
380 |
|
381 Array3 (const Array3<T>& a) : Array2<T> (a) |
|
382 { |
|
383 d2 = a.d2; |
|
384 d3 = a.d3; |
1560
|
385 set_max_indices (3); |
1550
|
386 } |
228
|
387 |
1230
|
388 ~Array3 (void) { } |
|
389 |
1550
|
390 Array3<T>& operator = (const Array3<T>& a) |
|
391 { |
|
392 if (this != &a) |
|
393 { |
|
394 Array<T>::operator = (a); |
|
395 d1 = a.d1; |
|
396 d2 = a.d2; |
|
397 d3 = a.d3; |
|
398 } |
228
|
399 |
1550
|
400 return *this; |
|
401 } |
|
402 |
|
403 int dim3 (void) const { return d3; } |
|
404 |
|
405 T& elem (int i, int j, int k) { return Array2<T>::elem (i, d2*k+j); } |
238
|
406 T& checkelem (int i, int j, int k); |
1550
|
407 T& operator () (int i, int j, int k) { return checkelem (i, j, k); } |
238
|
408 |
1359
|
409 // No checking. |
|
410 |
1550
|
411 T& xelem (int i, int j, int k) { return Array2<T>::xelem (i, d2*k+j); } |
238
|
412 |
228
|
413 T elem (int i, int j, int k) const; |
238
|
414 T checkelem (int i, int j, int k) const; |
|
415 T operator () (int i, int j, int k) const; |
228
|
416 |
238
|
417 void resize (int n, int m, int k); |
|
418 void resize (int n, int m, int k, const T& val); |
1560
|
419 |
|
420 #ifdef HEAVYWEIGHT_INDEXING |
|
421 void maybe_delete_elements (idx_vector& i, idx_vector& j, idx_vector& k); |
|
422 |
|
423 Array3<T> value (void); |
|
424 #endif |
228
|
425 }; |
|
426 |
1560
|
427 template <class LT, class RT> |
|
428 int assign (Array3<LT>& lhs, const Array3<RT>& rhs); |
|
429 |
1359
|
430 // A two-dimensional array with diagonal elements only. |
|
431 // |
|
432 // Idea and example code for Proxy class and functions from: |
|
433 // |
|
434 // From: kanze@us-es.sel.de (James Kanze) |
|
435 // Subject: Re: How to overload [] to do READ/WRITE differently ? |
|
436 // Message-ID: <KANZE.93Nov29151407@slsvhdt.us-es.sel.de> |
|
437 // Sender: news@us-es.sel.de |
|
438 // Date: 29 Nov 1993 14:14:07 GMT |
|
439 // -- |
|
440 // James Kanze email: kanze@us-es.sel.de |
|
441 // GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France |
238
|
442 |
1574
|
443 #ifndef NO_DIAG_ARRAY |
228
|
444 template <class T> |
|
445 class DiagArray : public Array<T> |
|
446 { |
366
|
447 private: |
1560
|
448 T get (int i) { return Array<T>::elem (i); } |
|
449 void set (const T& val, int i) { Array<T>::elem (i) = val; } |
366
|
450 |
880
|
451 #if 0 |
645
|
452 #if ! (defined (_AIX) && defined (__GNUG__) && __GNUC__ > 1 && __GNUC_MINOR__ < 6) |
319
|
453 class Proxy |
|
454 { |
|
455 public: |
|
456 |
1560
|
457 Proxy (DiagArray<T> *ref, int r, int c) |
645
|
458 : i (r), j (c), object (ref) { } |
319
|
459 |
1560
|
460 const Proxy& operator = (const T& val) const |
|
461 { |
|
462 if (i == j) |
|
463 { |
|
464 if (object) |
|
465 object->set (val, i); |
|
466 } |
|
467 else |
|
468 (*current_liboctave_error_handler) ("invalid assignment to off-diagonal in diagonal array"); |
319
|
469 |
1560
|
470 return *this; |
|
471 } |
319
|
472 |
1560
|
473 operator T () const |
|
474 { |
|
475 if (object && i == j) |
|
476 return object->get (i); |
|
477 else |
|
478 { |
|
479 static T foo (0); |
|
480 return foo; |
|
481 } |
|
482 } |
319
|
483 |
|
484 private: |
|
485 |
1359
|
486 // XXX FIXME XXX -- this is declared private to keep the user from |
|
487 // taking the address of a Proxy. Maybe it should be implemented |
|
488 // by means of a companion function in the DiagArray class. |
319
|
489 |
1560
|
490 T *operator& () const { assert (0); return (T *) 0; } |
319
|
491 |
|
492 int i; |
|
493 int j; |
|
494 |
|
495 DiagArray<T> *object; |
|
496 |
|
497 }; |
|
498 |
|
499 friend class Proxy; |
344
|
500 #endif |
880
|
501 #endif |
319
|
502 |
238
|
503 protected: |
|
504 |
|
505 int nr; |
|
506 int nc; |
|
507 |
1550
|
508 DiagArray (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
|
509 { |
|
510 nr = r; |
|
511 nc = c; |
1560
|
512 set_max_indices (2); |
1550
|
513 } |
238
|
514 |
228
|
515 public: |
238
|
516 |
1550
|
517 DiagArray (void) : Array<T> () |
|
518 { |
|
519 nr = 0; |
|
520 nc = 0; |
1560
|
521 set_max_indices (2); |
1550
|
522 } |
|
523 |
|
524 DiagArray (int n) : Array<T> (n) |
1560
|
525 { |
|
526 nr = n; |
|
527 nc = n; |
|
528 set_max_indices (2); |
|
529 } |
1550
|
530 |
|
531 DiagArray (int n, const T& val) : Array<T> (n, val) |
1560
|
532 { |
|
533 nr = n; |
|
534 nc = n; |
|
535 set_max_indices (2); |
|
536 } |
1550
|
537 |
|
538 DiagArray (int r, int c) : Array<T> (r < c ? r : c) |
1560
|
539 { |
|
540 nr = r; |
|
541 nc = c; |
|
542 set_max_indices (2); |
|
543 } |
1550
|
544 |
|
545 DiagArray (int r, int c, const T& val) : Array<T> (r < c ? r : c, val) |
1560
|
546 { |
|
547 nr = r; |
|
548 nc = c; |
|
549 set_max_indices (2); |
|
550 } |
1550
|
551 |
|
552 DiagArray (const Array<T>& a) : Array<T> (a) |
1560
|
553 { |
|
554 nr = nc = a.length (); |
|
555 set_max_indices (2); |
|
556 } |
1550
|
557 |
|
558 DiagArray (const DiagArray<T>& a) : Array<T> (a) |
1560
|
559 { |
|
560 nr = a.nr; |
|
561 nc = a.nc; |
|
562 set_max_indices (2); |
|
563 } |
228
|
564 |
1230
|
565 ~DiagArray (void) { } |
|
566 |
1550
|
567 DiagArray<T>& operator = (const DiagArray<T>& a) |
|
568 { |
1560
|
569 if (this != &a) |
|
570 { |
|
571 Array<T>::operator = (a); |
|
572 nr = a.nr; |
|
573 nc = a.nc; |
|
574 } |
|
575 |
|
576 return *this; |
1550
|
577 } |
228
|
578 |
1550
|
579 int dim1 (void) const { return nr; } |
|
580 int dim2 (void) const { return nc; } |
|
581 |
|
582 int rows (void) const { return nr; } |
|
583 int cols (void) const { return nc; } |
|
584 int columns (void) const { return nc; } |
228
|
585 |
880
|
586 #if 0 |
1560
|
587 Proxy elem (int r, int c) |
|
588 { |
|
589 return Proxy (this, r, c); |
|
590 } |
319
|
591 |
1560
|
592 Proxy checkelem (int r, int c) |
|
593 { |
|
594 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
595 { |
|
596 (*current_liboctave_error_handler) ("range error"); |
|
597 return Proxy (0, r, c); |
|
598 } |
|
599 else |
|
600 return Proxy (this, r, c); |
|
601 } |
319
|
602 |
1560
|
603 Proxy operator () (int r, int c) |
|
604 { |
|
605 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
606 { |
|
607 (*current_liboctave_error_handler) ("range error"); |
|
608 return Proxy (0, r, c); |
|
609 } |
|
610 else |
|
611 return Proxy (this, r, c); |
319
|
612 } |
880
|
613 #else |
|
614 T& elem (int r, int c); |
|
615 T& checkelem (int r, int c); |
|
616 T& operator () (int r, int c); |
344
|
617 #endif |
228
|
618 |
1359
|
619 // No checking. |
|
620 |
238
|
621 T& xelem (int r, int c); |
|
622 |
228
|
623 T elem (int r, int c) const; |
|
624 T checkelem (int r, int c) const; |
|
625 T operator () (int r, int c) const; |
|
626 |
238
|
627 void resize (int n, int m); |
|
628 void resize (int n, int m, const T& val); |
1560
|
629 |
|
630 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
228
|
631 }; |
1574
|
632 #endif |
228
|
633 |
|
634 #endif |
|
635 |
|
636 /* |
|
637 ;;; Local Variables: *** |
|
638 ;;; mode: C++ *** |
|
639 ;;; page-delimiter: "^/\\*" *** |
|
640 ;;; End: *** |
|
641 */ |