228
|
1 // Template array classes -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
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 |
|
24 #if !defined (_Array_h) |
|
25 #define _Array_h 1 |
|
26 |
238
|
27 #if defined (__GNUG__) && defined (USE_EXTERNAL_TEMPLATES) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
|
31 // Classes we declare. |
228
|
32 |
238
|
33 template <class T> class ArrayRep; |
|
34 template <class T> class Array; |
|
35 template <class T> class Array2; |
|
36 template <class T> class Array3; |
|
37 template <class T> class DiagArray; |
|
38 |
|
39 /* |
|
40 * The real representation of all arrays. |
|
41 */ |
228
|
42 |
|
43 template <class T> |
|
44 class ArrayRep |
|
45 { |
238
|
46 // Rethink resize()? |
228
|
47 friend class Array<T>; |
238
|
48 friend class Array2<T>; |
|
49 friend class Array3<T>; |
|
50 friend class DiagArray<T>; |
|
51 |
|
52 protected: |
|
53 |
|
54 ArrayRep (T *d, int l); |
228
|
55 |
|
56 public: |
|
57 |
238
|
58 ArrayRep (void); |
|
59 ArrayRep (int n); |
|
60 ArrayRep (const ArrayRep<T>& a); |
228
|
61 |
|
62 ~ArrayRep (void); |
238
|
63 |
228
|
64 int length (void) const; |
238
|
65 |
228
|
66 T& elem (int n); |
238
|
67 |
228
|
68 T elem (int n) const; |
238
|
69 |
|
70 void resize (int n); |
|
71 |
228
|
72 private: |
238
|
73 |
228
|
74 T *data; |
|
75 int len; |
|
76 int count; |
|
77 }; |
|
78 |
238
|
79 /* |
|
80 * One dimensional array class. Handles the reference counting for |
|
81 * all the derived classes. |
|
82 */ |
|
83 |
228
|
84 template <class T> |
|
85 class Array |
|
86 { |
238
|
87 protected: |
|
88 |
|
89 ArrayRep<T> *rep; |
|
90 |
|
91 Array (T *d, int l); |
|
92 |
228
|
93 public: |
238
|
94 |
228
|
95 Array (void); |
238
|
96 Array (int n); |
|
97 Array (int n, const T& val); |
|
98 |
228
|
99 Array (const Array<T>& a); |
|
100 |
|
101 ~Array (void); |
|
102 |
|
103 Array<T>& operator = (const Array<T>& a); |
238
|
104 |
|
105 int capacity (void) const; |
228
|
106 int length (void) const; |
|
107 |
|
108 T& elem (int n); |
|
109 T& checkelem (int n); |
|
110 T& operator () (int n); |
|
111 |
238
|
112 // No checking. |
|
113 T& xelem (int n); |
|
114 |
228
|
115 T elem (int n) const; |
|
116 T checkelem (int n) const; |
|
117 T operator () (int n) const; |
|
118 |
238
|
119 void resize (int n); |
|
120 void resize (int n, const T& val); |
|
121 |
|
122 const T *data (void) const; |
228
|
123 |
238
|
124 T *fortran_vec (void); |
228
|
125 }; |
|
126 |
238
|
127 /* |
|
128 * Two dimensional array class. |
|
129 */ |
|
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 |
|
149 Array2<T>& operator = (const Array2<T>& a); |
|
150 |
|
151 int dim1 (void) const; |
|
152 int dim2 (void) const; |
238
|
153 |
|
154 int rows (void) const; |
|
155 int cols (void) const; |
|
156 int columns (void) const; |
|
157 |
228
|
158 T& elem (int i, int j); |
238
|
159 T& checkelem (int i, int j); |
228
|
160 T& operator () (int i, int j); |
238
|
161 |
|
162 // No checking. |
|
163 T& xelem (int i, int j); |
|
164 |
228
|
165 T elem (int i, int j) const; |
|
166 T checkelem (int i, int j) const; |
|
167 T operator () (int i, int j) const; |
|
168 |
238
|
169 void resize (int n, int m); |
|
170 void resize (int n, int m, const T& val); |
228
|
171 }; |
|
172 |
238
|
173 /* |
|
174 * Three dimensional array class. |
|
175 */ |
|
176 |
228
|
177 template <class T> |
|
178 class Array3 : public Array2<T> |
|
179 { |
238
|
180 protected: |
|
181 |
|
182 int d3; |
|
183 |
|
184 Array3 (T *d, int n, int m, int k); |
|
185 |
228
|
186 public: |
|
187 |
|
188 Array3 (void); |
|
189 Array3 (int n, int m, int k); |
238
|
190 Array3 (int n, int m, int k, const T& val); |
228
|
191 Array3 (const Array3<T>& a); |
|
192 |
|
193 Array3<T>& operator = (const Array3<T>& a); |
|
194 |
|
195 int dim3 (void) const; |
|
196 |
|
197 T& elem (int i, int j, int k); |
238
|
198 T& checkelem (int i, int j, int k); |
|
199 T& operator () (int i, int j, int k); |
|
200 |
|
201 // No checking. |
|
202 T& xelem (int i, int j, int k); |
|
203 |
228
|
204 T elem (int i, int j, int k) const; |
238
|
205 T checkelem (int i, int j, int k) const; |
|
206 T operator () (int i, int j, int k) const; |
228
|
207 |
238
|
208 void resize (int n, int m, int k); |
|
209 void resize (int n, int m, int k, const T& val); |
228
|
210 }; |
|
211 |
238
|
212 /* |
|
213 * A two-dimensional array with diagonal elements only. |
|
214 */ |
|
215 |
228
|
216 template <class T> |
|
217 class DiagArray : public Array<T> |
|
218 { |
238
|
219 protected: |
|
220 |
|
221 int nr; |
|
222 int nc; |
|
223 |
|
224 DiagArray (T *d, int r, int c); |
|
225 |
228
|
226 public: |
238
|
227 |
228
|
228 DiagArray (void); |
238
|
229 DiagArray (int n); |
|
230 DiagArray (int n, const T& val); |
228
|
231 DiagArray (int r, int c); |
238
|
232 DiagArray (int r, int c, const T& val); |
228
|
233 DiagArray (const Array<T>& a); |
|
234 DiagArray (const DiagArray<T>& a); |
|
235 |
|
236 DiagArray<T>& operator = (const DiagArray<T>& a); |
|
237 |
238
|
238 int dim1 (void) const; |
|
239 int dim2 (void) const; |
|
240 |
228
|
241 int rows (void) const; |
|
242 int cols (void) const; |
|
243 int columns (void) const; |
|
244 |
|
245 T& elem (int r, int c); |
|
246 T& checkelem (int r, int c); |
|
247 T& operator () (int r, int c); |
|
248 |
238
|
249 // No checking. |
|
250 T& xelem (int r, int c); |
|
251 |
228
|
252 T elem (int r, int c) const; |
|
253 T checkelem (int r, int c) const; |
|
254 T operator () (int r, int c) const; |
|
255 |
238
|
256 void resize (int n, int m); |
|
257 void resize (int n, int m, const T& val); |
228
|
258 }; |
|
259 |
238
|
260 #if defined (__GNUG__) && ! defined (USE_EXTERNAL_TEMPLATES) |
228
|
261 #include "Array.cc" |
|
262 #endif |
|
263 |
|
264 #endif |
|
265 |
|
266 /* |
|
267 ;;; Local Variables: *** |
|
268 ;;; mode: C++ *** |
|
269 ;;; page-delimiter: "^/\\*" *** |
|
270 ;;; End: *** |
|
271 */ |