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