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 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1988
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <cassert> |
|
33 |
3503
|
34 #include <iostream> |
1988
|
35 |
|
36 #include "DiagArray2.h" |
|
37 |
|
38 #include "lo-error.h" |
|
39 |
|
40 // A two-dimensional array with diagonal elements only. |
|
41 |
|
42 #if 0 |
|
43 template <class T> |
|
44 T& |
|
45 DiagArray2<T>::elem (int r, int c) |
|
46 { |
|
47 static T foo (0); |
|
48 return (r == c) ? Array<T>::xelem (r) : foo; |
|
49 } |
|
50 |
|
51 template <class T> |
|
52 T& |
|
53 DiagArray2<T>::checkelem (int r, int c) |
|
54 { |
|
55 static T foo (0); |
4513
|
56 if (r < 0 || c < 0 || r >= dim1 () || c >= dim2 ()) |
1988
|
57 { |
3928
|
58 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
59 return foo; |
|
60 } |
|
61 return (r == c) ? Array<T>::xelem (r) : foo; |
|
62 } |
|
63 |
|
64 template <class T> |
|
65 T& |
|
66 DiagArray2<T>::operator () (int r, int c) |
|
67 { |
|
68 static T foo (0); |
4513
|
69 if (r < 0 || c < 0 || r >= dim1 () || c >= dim2 ()) |
1988
|
70 { |
3928
|
71 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
72 return foo; |
|
73 } |
|
74 return (r == c) ? Array<T>::xelem (r) : foo; |
|
75 } |
|
76 #endif |
|
77 |
|
78 template <class T> |
|
79 T |
|
80 DiagArray2<T>::elem (int r, int c) const |
|
81 { |
|
82 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
83 } |
|
84 |
|
85 template <class T> |
|
86 T |
|
87 DiagArray2<T>::checkelem (int r, int c) const |
|
88 { |
4513
|
89 if (r < 0 || c < 0 || r >= dim1 () || c >= dim2 ()) |
1988
|
90 { |
3928
|
91 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
3333
|
92 return T (); |
1988
|
93 } |
|
94 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
95 } |
|
96 |
|
97 template <class T> |
|
98 T |
|
99 DiagArray2<T>::operator () (int r, int c) const |
|
100 { |
4513
|
101 if (r < 0 || c < 0 || r >= dim1 () || c >= dim2 ()) |
1988
|
102 { |
3928
|
103 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
3333
|
104 return T (); |
1988
|
105 } |
|
106 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
107 } |
|
108 |
|
109 template <class T> |
|
110 T& |
|
111 DiagArray2<T>::xelem (int r, int c) |
|
112 { |
|
113 static T foo (0); |
|
114 return (r == c) ? Array<T>::xelem (r) : foo; |
|
115 } |
|
116 |
|
117 template <class T> |
|
118 T |
|
119 DiagArray2<T>::xelem (int r, int c) const |
|
120 { |
|
121 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
122 } |
|
123 |
|
124 template <class T> |
|
125 void |
|
126 DiagArray2<T>::resize (int r, int c) |
|
127 { |
|
128 if (r < 0 || c < 0) |
|
129 { |
|
130 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
131 return; |
|
132 } |
|
133 |
|
134 if (r == dim1 () && c == dim2 ()) |
|
135 return; |
|
136 |
4323
|
137 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
1988
|
138 const T *old_data = data (); |
|
139 int old_len = length (); |
|
140 |
|
141 int new_len = r < c ? r : c; |
|
142 |
4323
|
143 Array<T>::rep = new typename Array<T>::ArrayRep (new_len); |
1988
|
144 |
4513
|
145 dimensions = dim_vector (r, c); |
1988
|
146 |
|
147 if (old_data && old_len > 0) |
|
148 { |
|
149 int min_len = old_len < new_len ? old_len : new_len; |
|
150 |
|
151 for (int i = 0; i < min_len; i++) |
|
152 xelem (i, i) = old_data[i]; |
|
153 } |
|
154 |
|
155 if (--old_rep->count <= 0) |
|
156 delete old_rep; |
|
157 } |
|
158 |
|
159 template <class T> |
|
160 void |
|
161 DiagArray2<T>::resize (int r, int c, const T& val) |
|
162 { |
|
163 if (r < 0 || c < 0) |
|
164 { |
|
165 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
166 return; |
|
167 } |
|
168 |
|
169 if (r == dim1 () && c == dim2 ()) |
|
170 return; |
|
171 |
4323
|
172 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
1988
|
173 const T *old_data = data (); |
|
174 int old_len = length (); |
|
175 |
|
176 int new_len = r < c ? r : c; |
|
177 |
4323
|
178 Array<T>::rep = new typename Array<T>::ArrayRep (new_len); |
1988
|
179 |
4513
|
180 dimensions = dim_vector (r, c); |
1988
|
181 |
|
182 int min_len = old_len < new_len ? old_len : new_len; |
|
183 |
|
184 if (old_data && old_len > 0) |
|
185 { |
|
186 for (int i = 0; i < min_len; i++) |
|
187 xelem (i, i) = old_data[i]; |
|
188 } |
|
189 |
|
190 for (int i = min_len; i < new_len; i++) |
|
191 xelem (i, i) = val; |
|
192 |
|
193 if (--old_rep->count <= 0) |
|
194 delete old_rep; |
|
195 } |
|
196 |
|
197 /* |
|
198 ;;; Local Variables: *** |
|
199 ;;; mode: C++ *** |
|
200 ;;; End: *** |
|
201 */ |