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 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
4153
|
27 #include "quit.h" |
|
28 |
2974
|
29 #include "error.h" |
|
30 #include "gripes.h" |
|
31 #include "oct-obj.h" |
|
32 #include "ov-mapper.h" |
|
33 #include "ov.h" |
4748
|
34 #include "toplev.h" |
|
35 #include "unwind-prot.h" |
2974
|
36 |
3219
|
37 DEFINE_OCTAVE_ALLOCATOR (octave_mapper); |
2974
|
38 |
3219
|
39 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_mapper, |
4612
|
40 "built-in mapper function", |
3219
|
41 "built-in mapper function"); |
2974
|
42 |
|
43 static bool |
4643
|
44 any_element_less_than (const NDArray& a, double val) |
2974
|
45 { |
4643
|
46 int len = a.length (); |
2974
|
47 |
4643
|
48 for (int i = 0; i < len; i++) |
|
49 { |
|
50 OCTAVE_QUIT; |
4153
|
51 |
4643
|
52 if (a(i) < val) |
|
53 return true; |
|
54 } |
2974
|
55 |
|
56 return false; |
|
57 } |
|
58 |
|
59 static bool |
4643
|
60 any_element_greater_than (const NDArray& a, double val) |
2974
|
61 { |
4643
|
62 int len = a.length (); |
2974
|
63 |
4643
|
64 for (int i = 0; i < len; i++) |
|
65 { |
|
66 OCTAVE_QUIT; |
4153
|
67 |
4643
|
68 if (a(i) > val) |
|
69 return true; |
|
70 } |
2974
|
71 |
|
72 return false; |
|
73 } |
|
74 |
4643
|
75 // In most cases, we could use the map member function from the NDArray |
3962
|
76 // classes, but as currently implemented, they don't allow us to |
|
77 // detect errors and abort properly. So use these macros to do the |
|
78 // looping here instead. |
|
79 |
|
80 #define MAPPER_LOOP_2(T, F, M, CONV, R) \ |
|
81 do \ |
|
82 { \ |
4643
|
83 int len = M.length (); \ |
3962
|
84 \ |
4643
|
85 T result (M.dims ()); \ |
3962
|
86 \ |
4643
|
87 for (int i = 0; i < len; i++) \ |
3962
|
88 { \ |
4643
|
89 OCTAVE_QUIT; \ |
|
90 \ |
|
91 result(i) = CONV (F (M(i))); \ |
4153
|
92 \ |
4643
|
93 if (error_state) \ |
|
94 return retval; \ |
|
95 } \ |
3962
|
96 \ |
|
97 retval = R; \ |
|
98 } \ |
|
99 while (0) |
|
100 |
|
101 #define MAPPER_LOOP_1(T, F, M, CONV) \ |
|
102 MAPPER_LOOP_2 (T, F, M, CONV, result) |
|
103 |
|
104 #define MAPPER_LOOP(T, F, M) \ |
|
105 MAPPER_LOOP_1 (T, F, M, ) |
|
106 |
2974
|
107 octave_value |
|
108 octave_mapper::apply (const octave_value& arg) const |
|
109 { |
|
110 octave_value retval; |
|
111 |
4452
|
112 // XXX FIXME XXX -- is_real_type can return true. Should it really |
|
113 // work that way? |
4123
|
114 |
5124
|
115 if (arg.is_real_type () |
|
116 && (c_c_map_fcn || d_d_map_fcn || d_b_map_fcn) |
|
117 && ! (arg.is_string () && ch_map_fcn)) |
4104
|
118 { |
|
119 if (arg.is_scalar_type ()) |
|
120 { |
|
121 double d = arg.double_value (); |
|
122 |
|
123 if (can_ret_cmplx_for_real && (d < lower_limit || d > upper_limit)) |
|
124 { |
|
125 if (c_c_map_fcn) |
|
126 retval = c_c_map_fcn (Complex (d)); |
|
127 else |
|
128 error ("%s: unable to handle real arguments", |
|
129 name().c_str ()); |
|
130 } |
|
131 else if (d_d_map_fcn) |
|
132 retval = d_d_map_fcn (d); |
|
133 else if (d_b_map_fcn) |
|
134 retval = d_b_map_fcn (d); |
|
135 else |
|
136 error ("%s: unable to handle real arguments", |
|
137 name().c_str ()); |
|
138 } |
|
139 else |
|
140 { |
4643
|
141 NDArray m = arg.array_value (); |
4104
|
142 |
|
143 if (error_state) |
|
144 return retval; |
|
145 |
|
146 if (can_ret_cmplx_for_real |
|
147 && (any_element_less_than (m, lower_limit) |
|
148 || any_element_greater_than (m, upper_limit))) |
|
149 { |
|
150 if (c_c_map_fcn) |
4643
|
151 MAPPER_LOOP (ComplexNDArray, c_c_map_fcn, m); |
4104
|
152 else |
|
153 error ("%s: unable to handle real arguments", |
|
154 name().c_str ()); |
|
155 } |
|
156 else if (d_d_map_fcn) |
4643
|
157 MAPPER_LOOP (NDArray, d_d_map_fcn, m); |
4104
|
158 else if (d_b_map_fcn) |
4643
|
159 MAPPER_LOOP (boolNDArray, d_b_map_fcn, m); |
4104
|
160 else |
|
161 error ("%s: unable to handle real arguments", |
|
162 name().c_str ()); |
|
163 } |
|
164 } |
|
165 else if (arg.is_complex_type ()) |
|
166 { |
|
167 if (arg.is_scalar_type ()) |
|
168 { |
|
169 Complex c = arg.complex_value (); |
|
170 |
|
171 if (d_c_map_fcn) |
|
172 retval = d_c_map_fcn (c); |
|
173 else if (c_c_map_fcn) |
|
174 retval = c_c_map_fcn (c); |
|
175 else if (c_b_map_fcn) |
|
176 retval = c_b_map_fcn (c); |
|
177 else |
|
178 error ("%s: unable to handle complex arguments", |
|
179 name().c_str ()); |
|
180 } |
|
181 else |
|
182 { |
4643
|
183 ComplexNDArray cm = arg.complex_array_value (); |
4104
|
184 |
|
185 if (error_state) |
|
186 return retval; |
|
187 |
|
188 if (d_c_map_fcn) |
4643
|
189 MAPPER_LOOP (NDArray, d_c_map_fcn, cm); |
4104
|
190 else if (c_c_map_fcn) |
4643
|
191 MAPPER_LOOP (ComplexNDArray, c_c_map_fcn, cm); |
4104
|
192 else if (c_b_map_fcn) |
4643
|
193 MAPPER_LOOP (boolNDArray, c_b_map_fcn, cm); |
4104
|
194 else |
|
195 error ("%s: unable to handle complex arguments", |
|
196 name().c_str ()); |
|
197 } |
|
198 } |
|
199 else if (ch_map_fcn) |
2974
|
200 { |
|
201 // XXX FIXME XXX -- this could be done in a better way... |
|
202 |
|
203 octave_value tmp = arg.convert_to_str (); |
|
204 |
|
205 if (! error_state) |
|
206 { |
4643
|
207 charNDArray chm = tmp.char_array_value (); |
2974
|
208 |
|
209 if (! error_state) |
|
210 { |
4100
|
211 switch (ch_map_flag) |
2974
|
212 { |
|
213 case 0: |
4643
|
214 MAPPER_LOOP_1 (boolNDArray, ch_map_fcn, chm, bool); |
2974
|
215 break; |
|
216 |
|
217 case 1: |
4643
|
218 MAPPER_LOOP (NDArray, ch_map_fcn, chm); |
2974
|
219 break; |
|
220 |
|
221 case 2: |
4643
|
222 MAPPER_LOOP_2 (charNDArray, ch_map_fcn, chm, , |
3962
|
223 octave_value (result, true)); |
2974
|
224 break; |
|
225 |
|
226 default: |
|
227 panic_impossible (); |
|
228 break; |
|
229 } |
|
230 } |
|
231 } |
|
232 } |
|
233 else |
4104
|
234 gripe_wrong_type_arg ("mapper", arg); |
2974
|
235 |
|
236 return retval; |
|
237 } |
|
238 |
|
239 octave_value_list |
4247
|
240 octave_mapper::subsref (const std::string& type, |
4219
|
241 const std::list<octave_value_list>& idx, |
3933
|
242 int nargout) |
|
243 { |
|
244 octave_value_list retval; |
|
245 |
|
246 switch (type[0]) |
|
247 { |
|
248 case '(': |
|
249 retval = do_multi_index_op (nargout, idx.front ()); |
|
250 break; |
|
251 |
|
252 case '{': |
|
253 case '.': |
|
254 { |
|
255 std::string nm = type_name (); |
|
256 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
257 } |
|
258 break; |
|
259 |
|
260 default: |
|
261 panic_impossible (); |
|
262 } |
|
263 |
|
264 return retval; |
|
265 |
4994
|
266 // XXX FIXME XXX -- perhaps there should be an |
|
267 // octave_value_list::next_subsref member function? See also |
|
268 // and octave_builtin::subsref. |
|
269 |
|
270 if (idx.size () > 1) |
|
271 retval = retval(0).next_subsref (nargout, type, idx); |
3933
|
272 } |
|
273 |
|
274 octave_value_list |
3544
|
275 octave_mapper::do_multi_index_op (int, const octave_value_list& args) |
2974
|
276 { |
|
277 octave_value retval; |
|
278 |
|
279 if (error_state) |
|
280 return retval; |
|
281 |
|
282 int nargin = args.length (); |
|
283 |
|
284 if (nargin > 1) |
|
285 ::error ("%s: too many arguments", name().c_str ()); |
|
286 else if (nargin < 1) |
|
287 ::error ("%s: too few arguments", name().c_str ()); |
|
288 else |
|
289 { |
|
290 if (args(0).is_defined ()) |
4748
|
291 { |
4987
|
292 unwind_protect::begin_frame ("mapper_func_eval"); |
|
293 |
4748
|
294 unwind_protect_ptr (curr_function); |
4975
|
295 unwind_protect_ptr (curr_caller_function); |
|
296 |
|
297 curr_caller_function = curr_function; |
4748
|
298 curr_function = this; |
|
299 |
|
300 retval = apply (args(0)); |
|
301 |
4987
|
302 unwind_protect::run_frame ("mapper_func_eval"); |
4748
|
303 } |
2974
|
304 else |
|
305 ::error ("%s: argument undefined", name().c_str ()); |
|
306 } |
|
307 |
|
308 return retval; |
|
309 } |
|
310 |
|
311 /* |
|
312 ;;; Local Variables: *** |
|
313 ;;; mode: C++ *** |
|
314 ;;; End: *** |
|
315 */ |