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 |
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 |
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 |
|
55 protected: |
|
56 |
|
57 ArrayRep (T *d, int l); |
228
|
58 |
|
59 public: |
|
60 |
238
|
61 ArrayRep (void); |
|
62 ArrayRep (int n); |
|
63 ArrayRep (const ArrayRep<T>& a); |
228
|
64 |
|
65 ~ArrayRep (void); |
238
|
66 |
228
|
67 int length (void) const; |
238
|
68 |
228
|
69 T& elem (int n); |
238
|
70 |
228
|
71 T elem (int n) const; |
238
|
72 |
|
73 void resize (int n); |
|
74 |
228
|
75 private: |
238
|
76 |
228
|
77 T *data; |
|
78 int len; |
|
79 int count; |
|
80 }; |
|
81 |
1359
|
82 // One dimensional array class. Handles the reference counting for |
|
83 // all the derived classes. |
238
|
84 |
228
|
85 template <class T> |
|
86 class Array |
|
87 { |
238
|
88 protected: |
|
89 |
|
90 ArrayRep<T> *rep; |
|
91 |
|
92 Array (T *d, int l); |
|
93 |
228
|
94 public: |
238
|
95 |
228
|
96 Array (void); |
238
|
97 Array (int n); |
|
98 Array (int n, const T& val); |
|
99 |
228
|
100 Array (const Array<T>& a); |
|
101 |
|
102 ~Array (void); |
|
103 |
|
104 Array<T>& operator = (const Array<T>& a); |
238
|
105 |
|
106 int capacity (void) const; |
228
|
107 int length (void) const; |
|
108 |
|
109 T& elem (int n); |
|
110 T& checkelem (int n); |
|
111 T& operator () (int n); |
|
112 |
1359
|
113 // No checking. |
|
114 |
238
|
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 |
1359
|
129 // Two dimensional array class. |
238
|
130 |
228
|
131 template <class T> |
|
132 class Array2 : public Array<T> |
|
133 { |
238
|
134 protected: |
|
135 |
|
136 int d1; |
|
137 int d2; |
|
138 |
|
139 Array2 (T *d, int n, int m); |
|
140 |
228
|
141 public: |
|
142 |
|
143 Array2 (void); |
|
144 Array2 (int n, int m); |
238
|
145 Array2 (int n, int m, const T& val); |
228
|
146 Array2 (const Array2<T>& a); |
238
|
147 Array2 (const DiagArray<T>& a); |
228
|
148 |
1230
|
149 ~Array2 (void) { } |
|
150 |
228
|
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 |
1359
|
164 // No checking. |
|
165 |
238
|
166 T& xelem (int i, int j); |
|
167 |
228
|
168 T elem (int i, int j) const; |
|
169 T checkelem (int i, int j) const; |
|
170 T operator () (int i, int j) const; |
|
171 |
238
|
172 void resize (int n, int m); |
|
173 void resize (int n, int m, const T& val); |
228
|
174 }; |
|
175 |
1359
|
176 // Three dimensional array class. |
238
|
177 |
228
|
178 template <class T> |
|
179 class Array3 : public Array2<T> |
|
180 { |
238
|
181 protected: |
|
182 |
|
183 int d3; |
|
184 |
|
185 Array3 (T *d, int n, int m, int k); |
|
186 |
228
|
187 public: |
|
188 |
|
189 Array3 (void); |
|
190 Array3 (int n, int m, int k); |
238
|
191 Array3 (int n, int m, int k, const T& val); |
228
|
192 Array3 (const Array3<T>& a); |
|
193 |
1230
|
194 ~Array3 (void) { } |
|
195 |
228
|
196 Array3<T>& operator = (const Array3<T>& a); |
|
197 |
|
198 int dim3 (void) const; |
|
199 |
|
200 T& elem (int i, int j, int k); |
238
|
201 T& checkelem (int i, int j, int k); |
|
202 T& operator () (int i, int j, int k); |
|
203 |
1359
|
204 // No checking. |
|
205 |
238
|
206 T& xelem (int i, int j, int k); |
|
207 |
228
|
208 T elem (int i, int j, int k) const; |
238
|
209 T checkelem (int i, int j, int k) const; |
|
210 T operator () (int i, int j, int k) const; |
228
|
211 |
238
|
212 void resize (int n, int m, int k); |
|
213 void resize (int n, int m, int k, const T& val); |
228
|
214 }; |
|
215 |
1359
|
216 // A two-dimensional array with diagonal elements only. |
|
217 // |
|
218 // Idea and example code for Proxy class and functions from: |
|
219 // |
|
220 // From: kanze@us-es.sel.de (James Kanze) |
|
221 // Subject: Re: How to overload [] to do READ/WRITE differently ? |
|
222 // Message-ID: <KANZE.93Nov29151407@slsvhdt.us-es.sel.de> |
|
223 // Sender: news@us-es.sel.de |
|
224 // Date: 29 Nov 1993 14:14:07 GMT |
|
225 // -- |
|
226 // James Kanze email: kanze@us-es.sel.de |
|
227 // GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France |
238
|
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 |
1359
|
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 |
|
274 // by means of a companion function in the DiagArray class. |
319
|
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 |
1230
|
306 ~DiagArray (void) { } |
|
307 |
228
|
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 |
880
|
317 #if 0 |
352
|
318 inline Proxy elem (int r, int c) |
319
|
319 { |
|
320 return Proxy (this, r, c); |
|
321 } |
|
322 |
352
|
323 inline Proxy checkelem (int r, int c) |
319
|
324 { |
|
325 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
326 { |
|
327 (*current_liboctave_error_handler) ("range error"); |
|
328 return Proxy (0, r, c); |
|
329 } |
|
330 else |
|
331 return Proxy (this, r, c); |
|
332 } |
|
333 |
352
|
334 inline Proxy operator () (int r, int c) |
319
|
335 { |
|
336 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
337 { |
|
338 (*current_liboctave_error_handler) ("range error"); |
|
339 return Proxy (0, r, c); |
|
340 } |
|
341 else |
|
342 return Proxy (this, r, c); |
|
343 } |
880
|
344 #else |
|
345 T& elem (int r, int c); |
|
346 T& checkelem (int r, int c); |
|
347 T& operator () (int r, int c); |
344
|
348 #endif |
228
|
349 |
1359
|
350 // No checking. |
|
351 |
238
|
352 T& xelem (int r, int c); |
|
353 |
228
|
354 T elem (int r, int c) const; |
|
355 T checkelem (int r, int c) const; |
|
356 T operator () (int r, int c) const; |
|
357 |
238
|
358 void resize (int n, int m); |
|
359 void resize (int n, int m, const T& val); |
228
|
360 }; |
|
361 |
|
362 #endif |
|
363 |
|
364 /* |
|
365 ;;; Local Variables: *** |
|
366 ;;; mode: C++ *** |
|
367 ;;; page-delimiter: "^/\\*" *** |
|
368 ;;; End: *** |
|
369 */ |