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 |
1366
|
31 #include <cassert> |
228
|
32 |
448
|
33 #include "lo-error.h" |
354
|
34 |
238
|
35 // Classes we declare. |
228
|
36 |
238
|
37 template <class T> class ArrayRep; |
|
38 template <class T> class Array; |
|
39 template <class T> class Array2; |
|
40 template <class T> class Array3; |
|
41 template <class T> class DiagArray; |
|
42 |
1359
|
43 // The real representation of all arrays. |
228
|
44 |
|
45 template <class T> |
|
46 class ArrayRep |
|
47 { |
1359
|
48 // Rethink resize()? |
|
49 |
228
|
50 friend class Array<T>; |
238
|
51 friend class Array2<T>; |
|
52 friend class Array3<T>; |
|
53 friend class DiagArray<T>; |
|
54 |
1550
|
55 private: |
|
56 |
|
57 int count; |
|
58 int len; |
|
59 T *data; |
|
60 |
238
|
61 protected: |
|
62 |
1550
|
63 ArrayRep (T *d, int l) |
|
64 { |
|
65 len = l; |
|
66 data = d; |
|
67 } |
228
|
68 |
|
69 public: |
|
70 |
1550
|
71 ArrayRep (void) |
|
72 { |
|
73 len = 0; |
|
74 data = 0; |
|
75 } |
|
76 |
238
|
77 ArrayRep (int n); |
1550
|
78 |
238
|
79 ArrayRep (const ArrayRep<T>& a); |
228
|
80 |
|
81 ~ArrayRep (void); |
238
|
82 |
1550
|
83 int length (void) const { return len; } |
238
|
84 |
228
|
85 T& elem (int n); |
238
|
86 |
228
|
87 T elem (int n) const; |
238
|
88 |
|
89 void resize (int n); |
228
|
90 }; |
|
91 |
1359
|
92 // One dimensional array class. Handles the reference counting for |
|
93 // all the derived classes. |
238
|
94 |
228
|
95 template <class T> |
|
96 class Array |
|
97 { |
238
|
98 protected: |
|
99 |
|
100 ArrayRep<T> *rep; |
|
101 |
1550
|
102 Array (T *d, int l) |
|
103 { |
|
104 rep = new ArrayRep<T> (d, l); |
|
105 rep->count = 1; |
|
106 } |
238
|
107 |
228
|
108 public: |
238
|
109 |
1550
|
110 Array (void) |
|
111 { |
|
112 rep = new ArrayRep<T>; |
|
113 rep->count = 1; |
|
114 } |
|
115 |
|
116 Array (int n) |
|
117 { |
|
118 rep = new ArrayRep<T> (n); |
|
119 rep->count = 1; |
|
120 } |
|
121 |
238
|
122 Array (int n, const T& val); |
|
123 |
1550
|
124 Array (const Array<T>& a) |
|
125 { |
|
126 rep = a.rep; |
|
127 rep->count++; |
|
128 } |
228
|
129 |
1550
|
130 ~Array (void) |
|
131 { |
|
132 if (--rep->count <= 0) |
|
133 delete rep; |
|
134 } |
228
|
135 |
|
136 Array<T>& operator = (const Array<T>& a); |
238
|
137 |
1550
|
138 int capacity (void) const { return rep->length (); } |
|
139 int length (void) const { return rep->length (); } |
228
|
140 |
1550
|
141 T& elem (int n) |
|
142 { |
|
143 if (rep->count > 1) |
|
144 { |
|
145 --rep->count; |
|
146 rep = new ArrayRep<T> (*rep); |
|
147 rep->count = 1; |
|
148 } |
|
149 return rep->elem (n); |
|
150 } |
|
151 |
228
|
152 T& checkelem (int n); |
1550
|
153 T& operator () (int n) { return checkelem (n); } |
228
|
154 |
1359
|
155 // No checking. |
|
156 |
1550
|
157 T& xelem (int n) { return rep->elem (n); } |
238
|
158 |
228
|
159 T elem (int n) const; |
|
160 T checkelem (int n) const; |
|
161 T operator () (int n) const; |
|
162 |
238
|
163 void resize (int n); |
|
164 void resize (int n, const T& val); |
|
165 |
1550
|
166 const T *data (void) const { return rep->data; } |
228
|
167 |
238
|
168 T *fortran_vec (void); |
228
|
169 }; |
|
170 |
1359
|
171 // Two dimensional array class. |
238
|
172 |
228
|
173 template <class T> |
|
174 class Array2 : public Array<T> |
|
175 { |
238
|
176 protected: |
|
177 |
|
178 int d1; |
|
179 int d2; |
|
180 |
1550
|
181 Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
182 { |
|
183 d1 = n; |
|
184 d2 = m; |
|
185 } |
238
|
186 |
228
|
187 public: |
|
188 |
1550
|
189 Array2 (void) : Array<T> () |
|
190 { |
|
191 d1 = 0; |
|
192 d2 = 0; |
|
193 } |
|
194 |
|
195 Array2 (int n, int m) : Array<T> (n*m) |
|
196 { |
|
197 d1 = n; |
|
198 d2 = m; |
|
199 } |
|
200 |
|
201 Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
202 { |
|
203 d1 = n; |
|
204 d2 = m; |
|
205 } |
|
206 |
|
207 Array2 (const Array2<T>& a) : Array<T> (a) |
|
208 { |
|
209 d1 = a.d1; |
|
210 d2 = a.d2; |
|
211 } |
|
212 |
|
213 Array2 (const DiagArray<T>& a) : Array<T> (a.rows () * a.cols (), T (0)) |
|
214 { |
|
215 for (int i = 0; i < a.length (); i++) |
|
216 elem (i, i) = a.elem (i, i); |
|
217 } |
228
|
218 |
1230
|
219 ~Array2 (void) { } |
|
220 |
1550
|
221 Array2<T>& operator = (const Array2<T>& a) |
|
222 { |
|
223 if (this != &a) |
|
224 { |
|
225 Array<T>::operator = (a); |
|
226 d1 = a.d1; |
|
227 d2 = a.d2; |
|
228 } |
238
|
229 |
1550
|
230 return *this; |
|
231 } |
|
232 |
|
233 int dim1 (void) const { return d1; } |
|
234 int dim2 (void) const { return d2; } |
238
|
235 |
1550
|
236 int rows (void) const { return d1; } |
|
237 int cols (void) const { return d2; } |
|
238 int columns (void) const { return d2; } |
|
239 |
|
240 T& elem (int i, int j) { return Array<T>::elem (d1*j+i); } |
238
|
241 T& checkelem (int i, int j); |
1550
|
242 T& operator () (int i, int j) { return checkelem (i, j); } |
238
|
243 |
1359
|
244 // No checking. |
|
245 |
1550
|
246 T& xelem (int i, int j) { return Array<T>::xelem (d1*j+i); } |
238
|
247 |
228
|
248 T elem (int i, int j) const; |
|
249 T checkelem (int i, int j) const; |
|
250 T operator () (int i, int j) const; |
|
251 |
238
|
252 void resize (int n, int m); |
|
253 void resize (int n, int m, const T& val); |
228
|
254 }; |
|
255 |
1359
|
256 // Three dimensional array class. |
238
|
257 |
228
|
258 template <class T> |
|
259 class Array3 : public Array2<T> |
|
260 { |
238
|
261 protected: |
|
262 |
|
263 int d3; |
|
264 |
1550
|
265 Array3 (T *d, int n, int m, int k) : Array2<T> (d, n, m*k) |
|
266 { |
|
267 d2 = m; |
|
268 d3 = k; |
|
269 } |
238
|
270 |
228
|
271 public: |
|
272 |
1550
|
273 Array3 (void) : Array2<T> () |
|
274 { |
|
275 d2 = 0; |
|
276 d3 = 0; |
|
277 } |
|
278 |
|
279 Array3 (int n, int m, int k) : Array2<T> (n, m*k) |
|
280 { |
|
281 d2 = m; |
|
282 d3 = k; |
|
283 } |
|
284 |
|
285 Array3 (int n, int m, int k, const T& val) : Array2<T> (n, m*k, val) |
|
286 { |
|
287 d2 = m; |
|
288 d3 = k; |
|
289 } |
|
290 |
|
291 Array3 (const Array3<T>& a) : Array2<T> (a) |
|
292 { |
|
293 d2 = a.d2; |
|
294 d3 = a.d3; |
|
295 } |
228
|
296 |
1230
|
297 ~Array3 (void) { } |
|
298 |
1550
|
299 Array3<T>& operator = (const Array3<T>& a) |
|
300 { |
|
301 if (this != &a) |
|
302 { |
|
303 Array<T>::operator = (a); |
|
304 d1 = a.d1; |
|
305 d2 = a.d2; |
|
306 d3 = a.d3; |
|
307 } |
228
|
308 |
1550
|
309 return *this; |
|
310 } |
|
311 |
|
312 int dim3 (void) const { return d3; } |
|
313 |
|
314 T& elem (int i, int j, int k) { return Array2<T>::elem (i, d2*k+j); } |
238
|
315 T& checkelem (int i, int j, int k); |
1550
|
316 T& operator () (int i, int j, int k) { return checkelem (i, j, k); } |
238
|
317 |
1359
|
318 // No checking. |
|
319 |
1550
|
320 T& xelem (int i, int j, int k) { return Array2<T>::xelem (i, d2*k+j); } |
238
|
321 |
228
|
322 T elem (int i, int j, int k) const; |
238
|
323 T checkelem (int i, int j, int k) const; |
|
324 T operator () (int i, int j, int k) const; |
228
|
325 |
238
|
326 void resize (int n, int m, int k); |
|
327 void resize (int n, int m, int k, const T& val); |
228
|
328 }; |
|
329 |
1359
|
330 // A two-dimensional array with diagonal elements only. |
|
331 // |
|
332 // Idea and example code for Proxy class and functions from: |
|
333 // |
|
334 // From: kanze@us-es.sel.de (James Kanze) |
|
335 // Subject: Re: How to overload [] to do READ/WRITE differently ? |
|
336 // Message-ID: <KANZE.93Nov29151407@slsvhdt.us-es.sel.de> |
|
337 // Sender: news@us-es.sel.de |
|
338 // Date: 29 Nov 1993 14:14:07 GMT |
|
339 // -- |
|
340 // James Kanze email: kanze@us-es.sel.de |
|
341 // GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France |
238
|
342 |
228
|
343 template <class T> |
|
344 class DiagArray : public Array<T> |
|
345 { |
366
|
346 private: |
|
347 inline T get (int i) { return Array<T>::elem (i); } |
|
348 inline void set (const T& val, int i) { Array<T>::elem (i) = val; } |
|
349 |
880
|
350 #if 0 |
645
|
351 #if ! (defined (_AIX) && defined (__GNUG__) && __GNUC__ > 1 && __GNUC_MINOR__ < 6) |
319
|
352 class Proxy |
|
353 { |
|
354 public: |
|
355 |
355
|
356 inline Proxy (DiagArray<T> *ref, int r, int c) |
645
|
357 : i (r), j (c), object (ref) { } |
319
|
358 |
355
|
359 inline const Proxy& operator = (const T& val) const |
319
|
360 { |
|
361 if (i == j) |
|
362 { |
|
363 if (object) |
|
364 object->set (val, i); |
|
365 } |
|
366 else |
|
367 (*current_liboctave_error_handler) |
|
368 ("assignment to off-diagonal element attempted for diagonal array"); |
|
369 |
|
370 return *this; |
|
371 } |
|
372 |
355
|
373 inline operator T () const |
319
|
374 { |
448
|
375 if (object && i == j) |
319
|
376 return object->get (i); |
|
377 else |
|
378 { |
|
379 static T foo (0); |
|
380 return foo; |
|
381 } |
|
382 } |
|
383 |
|
384 private: |
|
385 |
1359
|
386 // XXX FIXME XXX -- this is declared private to keep the user from |
|
387 // taking the address of a Proxy. Maybe it should be implemented |
|
388 // by means of a companion function in the DiagArray class. |
319
|
389 |
376
|
390 inline T *operator& () const { assert (0); return (T *) 0; } |
319
|
391 |
|
392 int i; |
|
393 int j; |
|
394 |
|
395 DiagArray<T> *object; |
|
396 |
|
397 }; |
|
398 |
|
399 friend class Proxy; |
344
|
400 #endif |
880
|
401 #endif |
319
|
402 |
238
|
403 protected: |
|
404 |
|
405 int nr; |
|
406 int nc; |
|
407 |
1550
|
408 DiagArray (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
|
409 { |
|
410 nr = r; |
|
411 nc = c; |
|
412 } |
238
|
413 |
228
|
414 public: |
238
|
415 |
1550
|
416 DiagArray (void) : Array<T> () |
|
417 { |
|
418 nr = 0; |
|
419 nc = 0; |
|
420 } |
|
421 |
|
422 DiagArray (int n) : Array<T> (n) |
|
423 { |
|
424 nr = n; |
|
425 nc = n; |
|
426 } |
|
427 |
|
428 DiagArray (int n, const T& val) : Array<T> (n, val) |
|
429 { |
|
430 nr = n; |
|
431 nc = n; |
|
432 } |
|
433 |
|
434 DiagArray (int r, int c) : Array<T> (r < c ? r : c) |
|
435 { |
|
436 nr = r; |
|
437 nc = c; |
|
438 } |
|
439 |
|
440 DiagArray (int r, int c, const T& val) : Array<T> (r < c ? r : c, val) |
|
441 { |
|
442 nr = r; |
|
443 nc = c; |
|
444 } |
|
445 |
|
446 DiagArray (const Array<T>& a) : Array<T> (a) |
|
447 { |
|
448 nr = nc = a.length (); |
|
449 } |
|
450 |
|
451 DiagArray (const DiagArray<T>& a) : Array<T> (a) |
|
452 { |
|
453 nr = a.nr; |
|
454 nc = a.nc; |
|
455 } |
228
|
456 |
1230
|
457 ~DiagArray (void) { } |
|
458 |
1550
|
459 DiagArray<T>& operator = (const DiagArray<T>& a) |
|
460 { |
|
461 if (this != &a) |
|
462 { |
|
463 Array<T>::operator = (a); |
|
464 nr = a.nr; |
|
465 nc = a.nc; |
|
466 } |
228
|
467 |
1550
|
468 return *this; |
|
469 } |
238
|
470 |
1550
|
471 int dim1 (void) const { return nr; } |
|
472 int dim2 (void) const { return nc; } |
|
473 |
|
474 int rows (void) const { return nr; } |
|
475 int cols (void) const { return nc; } |
|
476 int columns (void) const { return nc; } |
228
|
477 |
880
|
478 #if 0 |
352
|
479 inline Proxy elem (int r, int c) |
319
|
480 { |
|
481 return Proxy (this, r, c); |
|
482 } |
|
483 |
352
|
484 inline Proxy checkelem (int r, int c) |
319
|
485 { |
|
486 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
487 { |
|
488 (*current_liboctave_error_handler) ("range error"); |
|
489 return Proxy (0, r, c); |
|
490 } |
|
491 else |
|
492 return Proxy (this, r, c); |
|
493 } |
|
494 |
352
|
495 inline Proxy operator () (int r, int c) |
319
|
496 { |
|
497 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
498 { |
|
499 (*current_liboctave_error_handler) ("range error"); |
|
500 return Proxy (0, r, c); |
|
501 } |
|
502 else |
|
503 return Proxy (this, r, c); |
|
504 } |
880
|
505 #else |
|
506 T& elem (int r, int c); |
|
507 T& checkelem (int r, int c); |
|
508 T& operator () (int r, int c); |
344
|
509 #endif |
228
|
510 |
1359
|
511 // No checking. |
|
512 |
238
|
513 T& xelem (int r, int c); |
|
514 |
228
|
515 T elem (int r, int c) const; |
|
516 T checkelem (int r, int c) const; |
|
517 T operator () (int r, int c) const; |
|
518 |
238
|
519 void resize (int n, int m); |
|
520 void resize (int n, int m, const T& val); |
228
|
521 }; |
|
522 |
|
523 #endif |
|
524 |
|
525 /* |
|
526 ;;; Local Variables: *** |
|
527 ;;; mode: C++ *** |
|
528 ;;; page-delimiter: "^/\\*" *** |
|
529 ;;; End: *** |
|
530 */ |