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