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 |
4192
|
27 #if defined (__GNUG__) && defined (USE_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 DiagArray2 (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
4513
|
114 { dimensions = dim_vector (r, c); } |
1988
|
115 |
|
116 public: |
|
117 |
4513
|
118 DiagArray2 (void) : Array<T> (dim_vector (0, 0)) { } |
1988
|
119 |
|
120 DiagArray2 (int r, int c) : Array<T> (r < c ? r : c) |
4513
|
121 { dimensions = dim_vector (r, c); } |
1988
|
122 |
4513
|
123 DiagArray2 (int r, int c, const T& val) : Array<T> (r < c ? r : c) |
1988
|
124 { |
4513
|
125 dimensions = dim_vector (r, c); |
|
126 |
|
127 fill (val); |
1988
|
128 } |
|
129 |
|
130 DiagArray2 (const Array<T>& a) : Array<T> (a) |
4513
|
131 { dimensions = dim_vector (a.length (), a.length ()); } |
1988
|
132 |
|
133 DiagArray2 (const DiagArray2<T>& a) : Array<T> (a) |
4513
|
134 { dimensions = a.dims (); } |
1988
|
135 |
|
136 ~DiagArray2 (void) { } |
|
137 |
|
138 DiagArray2<T>& operator = (const DiagArray2<T>& a) |
|
139 { |
|
140 if (this != &a) |
|
141 { |
|
142 Array<T>::operator = (a); |
4513
|
143 dimensions = a.dims (); |
1988
|
144 } |
|
145 |
|
146 return *this; |
|
147 } |
|
148 |
|
149 #if 0 |
|
150 operator Array2<T> () const |
|
151 { |
4513
|
152 int nr = dim1 (); |
|
153 int nc = dim2 (); |
|
154 |
1988
|
155 Array2<T> retval (nr, nc, T (0)); |
|
156 |
|
157 int len = nr < nc ? nr : nc; |
|
158 |
|
159 for (int i = 0; i < len; i++) |
|
160 retval.xelem (i, i) = xelem (i, i); |
|
161 |
|
162 return retval; |
|
163 } |
|
164 #endif |
|
165 |
|
166 #if 1 |
|
167 Proxy elem (int r, int c) |
|
168 { |
|
169 return Proxy (this, r, c); |
|
170 } |
|
171 |
|
172 Proxy checkelem (int r, int c) |
|
173 { |
4513
|
174 if (r < 0 || c < 0 || r >= dim1 () || c >= dim2 ()) |
1988
|
175 { |
3928
|
176 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
177 return Proxy (0, r, c); |
|
178 } |
|
179 else |
|
180 return Proxy (this, r, c); |
|
181 } |
|
182 |
|
183 Proxy operator () (int r, int c) |
|
184 { |
4513
|
185 if (r < 0 || c < 0 || r >= dim1 () || c >= dim2 ()) |
1988
|
186 { |
3928
|
187 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
188 return Proxy (0, r, c); |
|
189 } |
|
190 else |
|
191 return Proxy (this, r, c); |
|
192 } |
|
193 #else |
|
194 T& elem (int r, int c); |
|
195 T& checkelem (int r, int c); |
|
196 T& operator () (int r, int c); |
|
197 #endif |
|
198 |
|
199 T elem (int r, int c) const; |
|
200 T checkelem (int r, int c) const; |
|
201 T operator () (int r, int c) const; |
|
202 |
|
203 // No checking. |
|
204 |
|
205 T& xelem (int r, int c); |
|
206 T xelem (int r, int c) const; |
|
207 |
|
208 void resize (int n, int m); |
|
209 void resize (int n, int m, const T& val); |
|
210 |
|
211 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
212 }; |
|
213 |
|
214 #endif |
|
215 |
|
216 /* |
|
217 ;;; Local Variables: *** |
|
218 ;;; mode: C++ *** |
|
219 ;;; End: *** |
|
220 */ |