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