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