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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
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 |
448
|
31 #include <assert.h> |
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 |
|
43 /* |
|
44 * The real representation of all arrays. |
|
45 */ |
228
|
46 |
|
47 template <class T> |
|
48 class ArrayRep |
|
49 { |
238
|
50 // Rethink resize()? |
228
|
51 friend class Array<T>; |
238
|
52 friend class Array2<T>; |
|
53 friend class Array3<T>; |
|
54 friend class DiagArray<T>; |
|
55 |
|
56 protected: |
|
57 |
|
58 ArrayRep (T *d, int l); |
228
|
59 |
|
60 public: |
|
61 |
238
|
62 ArrayRep (void); |
|
63 ArrayRep (int n); |
|
64 ArrayRep (const ArrayRep<T>& a); |
228
|
65 |
|
66 ~ArrayRep (void); |
238
|
67 |
228
|
68 int length (void) const; |
238
|
69 |
228
|
70 T& elem (int n); |
238
|
71 |
228
|
72 T elem (int n) const; |
238
|
73 |
|
74 void resize (int n); |
|
75 |
228
|
76 private: |
238
|
77 |
228
|
78 T *data; |
|
79 int len; |
|
80 int count; |
|
81 }; |
|
82 |
238
|
83 /* |
|
84 * One dimensional array class. Handles the reference counting for |
|
85 * all the derived classes. |
|
86 */ |
|
87 |
228
|
88 template <class T> |
|
89 class Array |
|
90 { |
238
|
91 protected: |
|
92 |
|
93 ArrayRep<T> *rep; |
|
94 |
|
95 Array (T *d, int l); |
|
96 |
228
|
97 public: |
238
|
98 |
228
|
99 Array (void); |
238
|
100 Array (int n); |
|
101 Array (int n, const T& val); |
|
102 |
228
|
103 Array (const Array<T>& a); |
|
104 |
|
105 ~Array (void); |
|
106 |
|
107 Array<T>& operator = (const Array<T>& a); |
238
|
108 |
|
109 int capacity (void) const; |
228
|
110 int length (void) const; |
|
111 |
|
112 T& elem (int n); |
|
113 T& checkelem (int n); |
|
114 T& operator () (int n); |
|
115 |
238
|
116 // No checking. |
|
117 T& xelem (int n); |
|
118 |
228
|
119 T elem (int n) const; |
|
120 T checkelem (int n) const; |
|
121 T operator () (int n) const; |
|
122 |
238
|
123 void resize (int n); |
|
124 void resize (int n, const T& val); |
|
125 |
|
126 const T *data (void) const; |
228
|
127 |
238
|
128 T *fortran_vec (void); |
228
|
129 }; |
|
130 |
238
|
131 /* |
|
132 * Two dimensional array class. |
|
133 */ |
|
134 |
228
|
135 template <class T> |
|
136 class Array2 : public Array<T> |
|
137 { |
238
|
138 protected: |
|
139 |
|
140 int d1; |
|
141 int d2; |
|
142 |
|
143 Array2 (T *d, int n, int m); |
|
144 |
228
|
145 public: |
|
146 |
|
147 Array2 (void); |
|
148 Array2 (int n, int m); |
238
|
149 Array2 (int n, int m, const T& val); |
228
|
150 Array2 (const Array2<T>& a); |
238
|
151 Array2 (const DiagArray<T>& a); |
228
|
152 |
1230
|
153 ~Array2 (void) { } |
|
154 |
228
|
155 Array2<T>& operator = (const Array2<T>& a); |
|
156 |
|
157 int dim1 (void) const; |
|
158 int dim2 (void) const; |
238
|
159 |
|
160 int rows (void) const; |
|
161 int cols (void) const; |
|
162 int columns (void) const; |
|
163 |
228
|
164 T& elem (int i, int j); |
238
|
165 T& checkelem (int i, int j); |
228
|
166 T& operator () (int i, int j); |
238
|
167 |
|
168 // No checking. |
|
169 T& xelem (int i, int j); |
|
170 |
228
|
171 T elem (int i, int j) const; |
|
172 T checkelem (int i, int j) const; |
|
173 T operator () (int i, int j) const; |
|
174 |
238
|
175 void resize (int n, int m); |
|
176 void resize (int n, int m, const T& val); |
228
|
177 }; |
|
178 |
238
|
179 /* |
|
180 * Three dimensional array class. |
|
181 */ |
|
182 |
228
|
183 template <class T> |
|
184 class Array3 : public Array2<T> |
|
185 { |
238
|
186 protected: |
|
187 |
|
188 int d3; |
|
189 |
|
190 Array3 (T *d, int n, int m, int k); |
|
191 |
228
|
192 public: |
|
193 |
|
194 Array3 (void); |
|
195 Array3 (int n, int m, int k); |
238
|
196 Array3 (int n, int m, int k, const T& val); |
228
|
197 Array3 (const Array3<T>& a); |
|
198 |
1230
|
199 ~Array3 (void) { } |
|
200 |
228
|
201 Array3<T>& operator = (const Array3<T>& a); |
|
202 |
|
203 int dim3 (void) const; |
|
204 |
|
205 T& elem (int i, int j, int k); |
238
|
206 T& checkelem (int i, int j, int k); |
|
207 T& operator () (int i, int j, int k); |
|
208 |
|
209 // No checking. |
|
210 T& xelem (int i, int j, int k); |
|
211 |
228
|
212 T elem (int i, int j, int k) const; |
238
|
213 T checkelem (int i, int j, int k) const; |
|
214 T operator () (int i, int j, int k) const; |
228
|
215 |
238
|
216 void resize (int n, int m, int k); |
|
217 void resize (int n, int m, int k, const T& val); |
228
|
218 }; |
|
219 |
238
|
220 /* |
|
221 * A two-dimensional array with diagonal elements only. |
319
|
222 * |
|
223 * Idea and example code for Proxy class and functions from: |
|
224 * |
|
225 * From: kanze@us-es.sel.de (James Kanze) |
|
226 * Subject: Re: How to overload [] to do READ/WRITE differently ? |
|
227 * Message-ID: <KANZE.93Nov29151407@slsvhdt.us-es.sel.de> |
|
228 * Sender: news@us-es.sel.de |
|
229 * Date: 29 Nov 1993 14:14:07 GMT |
|
230 * -- |
|
231 * James Kanze email: kanze@us-es.sel.de |
|
232 * GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France |
238
|
233 */ |
|
234 |
228
|
235 template <class T> |
|
236 class DiagArray : public Array<T> |
|
237 { |
366
|
238 private: |
|
239 inline T get (int i) { return Array<T>::elem (i); } |
|
240 inline void set (const T& val, int i) { Array<T>::elem (i) = val; } |
|
241 |
880
|
242 #if 0 |
645
|
243 #if ! (defined (_AIX) && defined (__GNUG__) && __GNUC__ > 1 && __GNUC_MINOR__ < 6) |
319
|
244 class Proxy |
|
245 { |
|
246 public: |
|
247 |
355
|
248 inline Proxy (DiagArray<T> *ref, int r, int c) |
645
|
249 : i (r), j (c), object (ref) { } |
319
|
250 |
355
|
251 inline const Proxy& operator = (const T& val) const |
319
|
252 { |
|
253 if (i == j) |
|
254 { |
|
255 if (object) |
|
256 object->set (val, i); |
|
257 } |
|
258 else |
|
259 (*current_liboctave_error_handler) |
|
260 ("assignment to off-diagonal element attempted for diagonal array"); |
|
261 |
|
262 return *this; |
|
263 } |
|
264 |
355
|
265 inline operator T () const |
319
|
266 { |
448
|
267 if (object && i == j) |
319
|
268 return object->get (i); |
|
269 else |
|
270 { |
|
271 static T foo (0); |
|
272 return foo; |
|
273 } |
|
274 } |
|
275 |
|
276 private: |
|
277 |
|
278 // XXX FIXME XXX -- this is declared private to keep the user from |
|
279 // taking the address of a Proxy. Maybe it should be implemented by |
|
280 // means of a companion function in the DiagArray class. |
|
281 |
376
|
282 inline T *operator& () const { assert (0); return (T *) 0; } |
319
|
283 |
|
284 int i; |
|
285 int j; |
|
286 |
|
287 DiagArray<T> *object; |
|
288 |
|
289 }; |
|
290 |
|
291 friend class Proxy; |
344
|
292 #endif |
880
|
293 #endif |
319
|
294 |
238
|
295 protected: |
|
296 |
|
297 int nr; |
|
298 int nc; |
|
299 |
|
300 DiagArray (T *d, int r, int c); |
|
301 |
228
|
302 public: |
238
|
303 |
228
|
304 DiagArray (void); |
238
|
305 DiagArray (int n); |
|
306 DiagArray (int n, const T& val); |
228
|
307 DiagArray (int r, int c); |
238
|
308 DiagArray (int r, int c, const T& val); |
228
|
309 DiagArray (const Array<T>& a); |
|
310 DiagArray (const DiagArray<T>& a); |
|
311 |
1230
|
312 ~DiagArray (void) { } |
|
313 |
228
|
314 DiagArray<T>& operator = (const DiagArray<T>& a); |
|
315 |
238
|
316 int dim1 (void) const; |
|
317 int dim2 (void) const; |
|
318 |
228
|
319 int rows (void) const; |
|
320 int cols (void) const; |
|
321 int columns (void) const; |
|
322 |
880
|
323 #if 0 |
352
|
324 inline Proxy elem (int r, int c) |
319
|
325 { |
|
326 return Proxy (this, r, c); |
|
327 } |
|
328 |
352
|
329 inline Proxy checkelem (int r, int c) |
319
|
330 { |
|
331 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
332 { |
|
333 (*current_liboctave_error_handler) ("range error"); |
|
334 return Proxy (0, r, c); |
|
335 } |
|
336 else |
|
337 return Proxy (this, r, c); |
|
338 } |
|
339 |
352
|
340 inline Proxy operator () (int r, int c) |
319
|
341 { |
|
342 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
343 { |
|
344 (*current_liboctave_error_handler) ("range error"); |
|
345 return Proxy (0, r, c); |
|
346 } |
|
347 else |
|
348 return Proxy (this, r, c); |
|
349 } |
880
|
350 #else |
|
351 T& elem (int r, int c); |
|
352 T& checkelem (int r, int c); |
|
353 T& operator () (int r, int c); |
344
|
354 #endif |
228
|
355 |
238
|
356 // No checking. |
|
357 T& xelem (int r, int c); |
|
358 |
228
|
359 T elem (int r, int c) const; |
|
360 T checkelem (int r, int c) const; |
|
361 T operator () (int r, int c) const; |
|
362 |
238
|
363 void resize (int n, int m); |
|
364 void resize (int n, int m, const T& val); |
228
|
365 }; |
|
366 |
|
367 #endif |
|
368 |
|
369 /* |
|
370 ;;; Local Variables: *** |
|
371 ;;; mode: C++ *** |
|
372 ;;; page-delimiter: "^/\\*" *** |
|
373 ;;; End: *** |
|
374 */ |