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