1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1988
|
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_DiagArray2_h) |
|
25 #define octave_DiagArray2_h 1 |
|
26 |
4066
|
27 #if defined (__GNUG__) && ! defined (NO_PRAGMA_INTERFACE_IMPLEMENTATION) |
1988
|
28 #pragma interface |
|
29 #endif |
|
30 |
|
31 #include <cassert> |
|
32 #include <cstdlib> |
|
33 |
|
34 #include "Array2.h" |
|
35 #include "lo-error.h" |
|
36 |
|
37 class idx_vector; |
|
38 |
|
39 // A two-dimensional array with diagonal elements only. |
|
40 // |
|
41 // Idea and example code for Proxy class and functions from: |
|
42 // |
|
43 // From: kanze@us-es.sel.de (James Kanze) |
|
44 // Subject: Re: How to overload [] to do READ/WRITE differently ? |
|
45 // Message-ID: <KANZE.93Nov29151407@slsvhdt.us-es.sel.de> |
|
46 // Sender: news@us-es.sel.de |
|
47 // Date: 29 Nov 1993 14:14:07 GMT |
|
48 // -- |
|
49 // James Kanze email: kanze@us-es.sel.de |
|
50 // GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France |
|
51 |
|
52 template <class T> |
3585
|
53 class |
|
54 DiagArray2 : public Array<T> |
1988
|
55 { |
|
56 private: |
|
57 |
|
58 T get (int i) { return Array<T>::xelem (i); } |
|
59 |
|
60 void set (const T& val, int i) { Array<T>::xelem (i) = val; } |
|
61 |
|
62 class Proxy |
|
63 { |
|
64 public: |
|
65 |
|
66 Proxy (DiagArray2<T> *ref, int r, int c) |
|
67 : i (r), j (c), object (ref) { } |
|
68 |
|
69 const Proxy& operator = (const T& val) const |
|
70 { |
|
71 if (i == j) |
|
72 { |
|
73 if (object) |
|
74 object->set (val, i); |
|
75 } |
|
76 else |
|
77 (*current_liboctave_error_handler) |
|
78 ("invalid assignment to off-diagonal in diagonal array"); |
|
79 |
|
80 return *this; |
|
81 } |
|
82 |
|
83 operator T () const |
|
84 { |
|
85 if (object && i == j) |
|
86 return object->get (i); |
|
87 else |
|
88 { |
|
89 static T foo (0); |
|
90 return foo; |
|
91 } |
|
92 } |
|
93 |
|
94 private: |
|
95 |
|
96 // XXX FIXME XXX -- this is declared private to keep the user from |
|
97 // taking the address of a Proxy. Maybe it should be implemented |
|
98 // by means of a companion function in the DiagArray2 class. |
|
99 |
|
100 T *operator& () const { assert (0); return (T *) 0; } |
|
101 |
|
102 int i; |
|
103 int j; |
|
104 |
|
105 DiagArray2<T> *object; |
|
106 |
|
107 }; |
|
108 |
|
109 friend class Proxy; |
|
110 |
|
111 protected: |
|
112 |
|
113 int nr; |
|
114 int nc; |
|
115 |
|
116 DiagArray2 (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
|
117 { |
|
118 nr = r; |
|
119 nc = c; |
|
120 set_max_indices (2); |
|
121 } |
|
122 |
|
123 public: |
|
124 |
|
125 DiagArray2 (void) : Array<T> () |
|
126 { |
|
127 nr = 0; |
|
128 nc = 0; |
|
129 set_max_indices (2); |
|
130 } |
|
131 |
|
132 DiagArray2 (int r, int c) : Array<T> (r < c ? r : c) |
|
133 { |
|
134 nr = r; |
|
135 nc = c; |
|
136 set_max_indices (2); |
|
137 } |
|
138 |
|
139 DiagArray2 (int r, int c, const T& val) : Array<T> (r < c ? r : c, val) |
|
140 { |
|
141 nr = r; |
|
142 nc = c; |
|
143 set_max_indices (2); |
|
144 } |
|
145 |
|
146 DiagArray2 (const Array<T>& a) : Array<T> (a) |
|
147 { |
|
148 nr = nc = a.length (); |
|
149 set_max_indices (2); |
|
150 } |
|
151 |
|
152 DiagArray2 (const DiagArray2<T>& a) : Array<T> (a) |
|
153 { |
|
154 nr = a.nr; |
|
155 nc = a.nc; |
|
156 set_max_indices (2); |
|
157 } |
|
158 |
|
159 ~DiagArray2 (void) { } |
|
160 |
|
161 DiagArray2<T>& operator = (const DiagArray2<T>& a) |
|
162 { |
|
163 if (this != &a) |
|
164 { |
|
165 Array<T>::operator = (a); |
|
166 nr = a.nr; |
|
167 nc = a.nc; |
|
168 } |
|
169 |
|
170 return *this; |
|
171 } |
|
172 |
|
173 #if 0 |
|
174 operator Array2<T> () const |
|
175 { |
|
176 Array2<T> retval (nr, nc, T (0)); |
|
177 |
|
178 int len = nr < nc ? nr : nc; |
|
179 |
|
180 for (int i = 0; i < len; i++) |
|
181 retval.xelem (i, i) = xelem (i, i); |
|
182 |
|
183 return retval; |
|
184 } |
|
185 #endif |
|
186 |
|
187 int dim1 (void) const { return nr; } |
|
188 int dim2 (void) const { return nc; } |
|
189 |
|
190 int rows (void) const { return nr; } |
|
191 int cols (void) const { return nc; } |
|
192 int columns (void) const { return nc; } |
|
193 |
|
194 #if 1 |
|
195 Proxy elem (int r, int c) |
|
196 { |
|
197 return Proxy (this, r, c); |
|
198 } |
|
199 |
|
200 Proxy checkelem (int r, int c) |
|
201 { |
|
202 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
203 { |
3928
|
204 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
205 return Proxy (0, r, c); |
|
206 } |
|
207 else |
|
208 return Proxy (this, r, c); |
|
209 } |
|
210 |
|
211 Proxy operator () (int r, int c) |
|
212 { |
|
213 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
214 { |
3928
|
215 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
216 return Proxy (0, r, c); |
|
217 } |
|
218 else |
|
219 return Proxy (this, r, c); |
|
220 } |
|
221 #else |
|
222 T& elem (int r, int c); |
|
223 T& checkelem (int r, int c); |
|
224 T& operator () (int r, int c); |
|
225 #endif |
|
226 |
|
227 T elem (int r, int c) const; |
|
228 T checkelem (int r, int c) const; |
|
229 T operator () (int r, int c) const; |
|
230 |
|
231 // No checking. |
|
232 |
|
233 T& xelem (int r, int c); |
|
234 T xelem (int r, int c) const; |
|
235 |
|
236 void resize (int n, int m); |
|
237 void resize (int n, int m, const T& val); |
|
238 |
|
239 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
240 }; |
|
241 |
|
242 #endif |
|
243 |
|
244 /* |
|
245 ;;; Local Variables: *** |
|
246 ;;; mode: C++ *** |
|
247 ;;; End: *** |
|
248 */ |