2974
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2974
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
4153
|
31 #include "quit.h" |
|
32 |
2974
|
33 #include "error.h" |
|
34 #include "gripes.h" |
|
35 #include "oct-obj.h" |
|
36 #include "ov-mapper.h" |
|
37 #include "ov.h" |
|
38 |
3219
|
39 DEFINE_OCTAVE_ALLOCATOR (octave_mapper); |
2974
|
40 |
3219
|
41 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_mapper, |
|
42 "built-in mapper function"); |
2974
|
43 |
|
44 static bool |
|
45 any_element_less_than (const Matrix& a, double val) |
|
46 { |
|
47 int nr = a.rows (); |
|
48 int nc = a.columns (); |
|
49 |
|
50 for (int j = 0; j < nc; j++) |
|
51 for (int i = 0; i < nr; i++) |
4153
|
52 { |
|
53 OCTAVE_QUIT; |
|
54 |
|
55 if (a (i, j) < val) |
|
56 return true; |
|
57 } |
2974
|
58 |
|
59 return false; |
|
60 } |
|
61 |
|
62 static bool |
|
63 any_element_greater_than (const Matrix& a, double val) |
|
64 { |
|
65 int nr = a.rows (); |
|
66 int nc = a.columns (); |
|
67 |
|
68 for (int j = 0; j < nc; j++) |
|
69 for (int i = 0; i < nr; i++) |
4153
|
70 { |
|
71 OCTAVE_QUIT; |
|
72 |
|
73 if (a (i, j) > val) |
|
74 return true; |
|
75 } |
2974
|
76 |
|
77 return false; |
|
78 } |
|
79 |
3962
|
80 // In most cases, we could use the map member function from the Matrix |
|
81 // classes, but as currently implemented, they don't allow us to |
|
82 // detect errors and abort properly. So use these macros to do the |
|
83 // looping here instead. |
|
84 |
|
85 #define MAPPER_LOOP_2(T, F, M, CONV, R) \ |
|
86 do \ |
|
87 { \ |
|
88 int nr = M.rows (); \ |
|
89 int nc = M.cols (); \ |
|
90 \ |
|
91 T result (nr, nc); \ |
|
92 \ |
|
93 for (int j = 0; j < nc; j++) \ |
|
94 { \ |
|
95 for (int i = 0; i < nr; i++) \ |
|
96 { \ |
4153
|
97 OCTAVE_QUIT; \ |
|
98 \ |
3962
|
99 result (i, j) = CONV (F (M (i, j))); \ |
|
100 \ |
|
101 if (error_state) \ |
|
102 return retval; \ |
|
103 } \ |
|
104 } \ |
|
105 retval = R; \ |
|
106 } \ |
|
107 while (0) |
|
108 |
|
109 #define MAPPER_LOOP_1(T, F, M, CONV) \ |
|
110 MAPPER_LOOP_2 (T, F, M, CONV, result) |
|
111 |
|
112 #define MAPPER_LOOP(T, F, M) \ |
|
113 MAPPER_LOOP_1 (T, F, M, ) |
|
114 |
2974
|
115 octave_value |
|
116 octave_mapper::apply (const octave_value& arg) const |
|
117 { |
|
118 octave_value retval; |
|
119 |
4123
|
120 // XXX FIXME XXX -- is_real_type can return true for strings if |
|
121 // implicit_str_to_num_ok is nonzero. Should it really work that |
|
122 // way? |
|
123 |
|
124 if (arg.is_real_type () && ! (arg.is_string () && ch_map_fcn)) |
4104
|
125 { |
|
126 if (arg.is_scalar_type ()) |
|
127 { |
|
128 double d = arg.double_value (); |
|
129 |
|
130 if (can_ret_cmplx_for_real && (d < lower_limit || d > upper_limit)) |
|
131 { |
|
132 if (c_c_map_fcn) |
|
133 retval = c_c_map_fcn (Complex (d)); |
|
134 else |
|
135 error ("%s: unable to handle real arguments", |
|
136 name().c_str ()); |
|
137 } |
|
138 else if (d_d_map_fcn) |
|
139 retval = d_d_map_fcn (d); |
|
140 else if (d_b_map_fcn) |
|
141 retval = d_b_map_fcn (d); |
|
142 else |
|
143 error ("%s: unable to handle real arguments", |
|
144 name().c_str ()); |
|
145 } |
|
146 else |
|
147 { |
|
148 Matrix m = arg.matrix_value (); |
|
149 |
|
150 if (error_state) |
|
151 return retval; |
|
152 |
|
153 if (can_ret_cmplx_for_real |
|
154 && (any_element_less_than (m, lower_limit) |
|
155 || any_element_greater_than (m, upper_limit))) |
|
156 { |
|
157 if (c_c_map_fcn) |
|
158 MAPPER_LOOP (ComplexMatrix, c_c_map_fcn, m); |
|
159 else |
|
160 error ("%s: unable to handle real arguments", |
|
161 name().c_str ()); |
|
162 } |
|
163 else if (d_d_map_fcn) |
|
164 MAPPER_LOOP (Matrix, d_d_map_fcn, m); |
|
165 else if (d_b_map_fcn) |
|
166 MAPPER_LOOP (boolMatrix, d_b_map_fcn, m); |
|
167 else |
|
168 error ("%s: unable to handle real arguments", |
|
169 name().c_str ()); |
|
170 } |
|
171 } |
|
172 else if (arg.is_complex_type ()) |
|
173 { |
|
174 if (arg.is_scalar_type ()) |
|
175 { |
|
176 Complex c = arg.complex_value (); |
|
177 |
|
178 if (d_c_map_fcn) |
|
179 retval = d_c_map_fcn (c); |
|
180 else if (c_c_map_fcn) |
|
181 retval = c_c_map_fcn (c); |
|
182 else if (c_b_map_fcn) |
|
183 retval = c_b_map_fcn (c); |
|
184 else |
|
185 error ("%s: unable to handle complex arguments", |
|
186 name().c_str ()); |
|
187 } |
|
188 else |
|
189 { |
|
190 ComplexMatrix cm = arg.complex_matrix_value (); |
|
191 |
|
192 if (error_state) |
|
193 return retval; |
|
194 |
|
195 if (d_c_map_fcn) |
|
196 MAPPER_LOOP (Matrix, d_c_map_fcn, cm); |
|
197 else if (c_c_map_fcn) |
|
198 MAPPER_LOOP (ComplexMatrix, c_c_map_fcn, cm); |
|
199 else if (c_b_map_fcn) |
|
200 MAPPER_LOOP (boolMatrix, c_b_map_fcn, cm); |
|
201 else |
|
202 error ("%s: unable to handle complex arguments", |
|
203 name().c_str ()); |
|
204 } |
|
205 } |
|
206 else if (ch_map_fcn) |
2974
|
207 { |
|
208 // XXX FIXME XXX -- this could be done in a better way... |
|
209 |
|
210 octave_value tmp = arg.convert_to_str (); |
|
211 |
|
212 if (! error_state) |
|
213 { |
|
214 charMatrix chm = tmp.char_matrix_value (); |
|
215 |
|
216 if (! error_state) |
|
217 { |
4100
|
218 switch (ch_map_flag) |
2974
|
219 { |
|
220 case 0: |
3962
|
221 MAPPER_LOOP_1 (boolMatrix, ch_map_fcn, chm, bool); |
2974
|
222 break; |
|
223 |
|
224 case 1: |
3962
|
225 MAPPER_LOOP (Matrix, ch_map_fcn, chm); |
2974
|
226 break; |
|
227 |
|
228 case 2: |
3962
|
229 MAPPER_LOOP_2 (charMatrix, ch_map_fcn, chm, , |
|
230 octave_value (result, true)); |
2974
|
231 break; |
|
232 |
|
233 default: |
|
234 panic_impossible (); |
|
235 break; |
|
236 } |
|
237 } |
|
238 } |
|
239 } |
|
240 else |
4104
|
241 gripe_wrong_type_arg ("mapper", arg); |
2974
|
242 |
|
243 return retval; |
|
244 } |
|
245 |
|
246 octave_value_list |
4247
|
247 octave_mapper::subsref (const std::string& type, |
4219
|
248 const std::list<octave_value_list>& idx, |
3933
|
249 int nargout) |
|
250 { |
|
251 octave_value_list retval; |
|
252 |
|
253 switch (type[0]) |
|
254 { |
|
255 case '(': |
|
256 retval = do_multi_index_op (nargout, idx.front ()); |
|
257 break; |
|
258 |
|
259 case '{': |
|
260 case '.': |
|
261 { |
|
262 std::string nm = type_name (); |
|
263 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
264 } |
|
265 break; |
|
266 |
|
267 default: |
|
268 panic_impossible (); |
|
269 } |
|
270 |
|
271 return retval; |
|
272 |
|
273 // XXX FIXME XXX |
|
274 // return retval.next_subsref (type, idx); |
|
275 } |
|
276 |
|
277 octave_value_list |
3544
|
278 octave_mapper::do_multi_index_op (int, const octave_value_list& args) |
2974
|
279 { |
|
280 octave_value retval; |
|
281 |
|
282 if (error_state) |
|
283 return retval; |
|
284 |
|
285 int nargin = args.length (); |
|
286 |
|
287 if (nargin > 1) |
|
288 ::error ("%s: too many arguments", name().c_str ()); |
|
289 else if (nargin < 1) |
|
290 ::error ("%s: too few arguments", name().c_str ()); |
|
291 else |
|
292 { |
|
293 if (args(0).is_defined ()) |
|
294 retval = apply (args(0)); |
|
295 else |
|
296 ::error ("%s: argument undefined", name().c_str ()); |
|
297 } |
|
298 |
|
299 return retval; |
|
300 } |
|
301 |
|
302 /* |
|
303 ;;; Local Variables: *** |
|
304 ;;; mode: C++ *** |
|
305 ;;; End: *** |
|
306 */ |