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 |
|
27 #include <cassert> |
|
28 #include <cstdlib> |
|
29 |
4645
|
30 #include "Array.h" |
1988
|
31 #include "lo-error.h" |
|
32 |
|
33 class idx_vector; |
|
34 |
|
35 // A two-dimensional array with diagonal elements only. |
|
36 // |
|
37 // Idea and example code for Proxy class and functions from: |
|
38 // |
|
39 // From: kanze@us-es.sel.de (James Kanze) |
|
40 // Subject: Re: How to overload [] to do READ/WRITE differently ? |
|
41 // Message-ID: <KANZE.93Nov29151407@slsvhdt.us-es.sel.de> |
|
42 // Sender: news@us-es.sel.de |
|
43 // Date: 29 Nov 1993 14:14:07 GMT |
|
44 // -- |
|
45 // James Kanze email: kanze@us-es.sel.de |
|
46 // GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France |
|
47 |
|
48 template <class T> |
3585
|
49 class |
|
50 DiagArray2 : public Array<T> |
1988
|
51 { |
|
52 private: |
|
53 |
|
54 T get (int i) { return Array<T>::xelem (i); } |
|
55 |
|
56 void set (const T& val, int i) { Array<T>::xelem (i) = val; } |
|
57 |
|
58 class Proxy |
|
59 { |
|
60 public: |
|
61 |
|
62 Proxy (DiagArray2<T> *ref, int r, int c) |
|
63 : i (r), j (c), object (ref) { } |
|
64 |
|
65 const Proxy& operator = (const T& val) const |
|
66 { |
|
67 if (i == j) |
|
68 { |
|
69 if (object) |
|
70 object->set (val, i); |
|
71 } |
|
72 else |
|
73 (*current_liboctave_error_handler) |
|
74 ("invalid assignment to off-diagonal in diagonal array"); |
|
75 |
|
76 return *this; |
|
77 } |
|
78 |
|
79 operator T () const |
|
80 { |
|
81 if (object && i == j) |
|
82 return object->get (i); |
|
83 else |
|
84 { |
|
85 static T foo (0); |
|
86 return foo; |
|
87 } |
|
88 } |
|
89 |
|
90 private: |
|
91 |
|
92 // XXX FIXME XXX -- this is declared private to keep the user from |
|
93 // taking the address of a Proxy. Maybe it should be implemented |
|
94 // by means of a companion function in the DiagArray2 class. |
|
95 |
|
96 T *operator& () const { assert (0); return (T *) 0; } |
|
97 |
|
98 int i; |
|
99 int j; |
|
100 |
|
101 DiagArray2<T> *object; |
|
102 |
|
103 }; |
|
104 |
|
105 friend class Proxy; |
|
106 |
|
107 protected: |
|
108 |
|
109 DiagArray2 (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
4645
|
110 { Array<T>::dimensions = dim_vector (r, c); } |
1988
|
111 |
|
112 public: |
|
113 |
4513
|
114 DiagArray2 (void) : Array<T> (dim_vector (0, 0)) { } |
1988
|
115 |
|
116 DiagArray2 (int r, int c) : Array<T> (r < c ? r : c) |
4645
|
117 { this->dimensions = dim_vector (r, c); } |
1988
|
118 |
4513
|
119 DiagArray2 (int r, int c, const T& val) : Array<T> (r < c ? r : c) |
1988
|
120 { |
4645
|
121 this->dimensions = dim_vector (r, c); |
4513
|
122 |
|
123 fill (val); |
1988
|
124 } |
|
125 |
|
126 DiagArray2 (const Array<T>& a) : Array<T> (a) |
4645
|
127 { this->dimensions = dim_vector (a.length (), a.length ()); } |
1988
|
128 |
|
129 DiagArray2 (const DiagArray2<T>& a) : Array<T> (a) |
4645
|
130 { this->dimensions = a.dims (); } |
1988
|
131 |
|
132 ~DiagArray2 (void) { } |
|
133 |
|
134 DiagArray2<T>& operator = (const DiagArray2<T>& a) |
|
135 { |
|
136 if (this != &a) |
4645
|
137 Array<T>::operator = (a); |
1988
|
138 |
|
139 return *this; |
|
140 } |
|
141 |
|
142 Proxy elem (int r, int c) |
|
143 { |
|
144 return Proxy (this, r, c); |
|
145 } |
|
146 |
|
147 Proxy checkelem (int r, int c) |
|
148 { |
4645
|
149 if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ()) |
1988
|
150 { |
3928
|
151 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
152 return Proxy (0, r, c); |
|
153 } |
|
154 else |
|
155 return Proxy (this, r, c); |
|
156 } |
|
157 |
|
158 Proxy operator () (int r, int c) |
|
159 { |
4645
|
160 if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ()) |
1988
|
161 { |
3928
|
162 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
163 return Proxy (0, r, c); |
|
164 } |
|
165 else |
|
166 return Proxy (this, r, c); |
|
167 } |
|
168 |
|
169 T elem (int r, int c) const; |
|
170 T checkelem (int r, int c) const; |
|
171 T operator () (int r, int c) const; |
|
172 |
|
173 // No checking. |
|
174 |
|
175 T& xelem (int r, int c); |
|
176 T xelem (int r, int c) const; |
|
177 |
|
178 void resize (int n, int m); |
|
179 void resize (int n, int m, const T& val); |
|
180 |
|
181 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
182 }; |
|
183 |
|
184 #endif |
|
185 |
|
186 /* |
|
187 ;;; Local Variables: *** |
|
188 ;;; mode: C++ *** |
|
189 ;;; End: *** |
|
190 */ |