2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 #if !defined (octave_ops_h) |
|
24 #define octave_ops_h 1 |
|
25 |
4669
|
26 #include "Array-util.h" |
|
27 |
2376
|
28 extern void install_ops (void); |
|
29 |
3203
|
30 #define INSTALL_UNOP(op, t, f) \ |
|
31 octave_value_typeinfo::register_unary_op \ |
|
32 (octave_value::op, t::static_type_id (), oct_unop_ ## f); |
|
33 |
|
34 #define INSTALL_NCUNOP(op, t, f) \ |
|
35 octave_value_typeinfo::register_non_const_unary_op \ |
|
36 (octave_value::op, t::static_type_id (), oct_unop_ ## f); |
|
37 |
2376
|
38 #define INSTALL_BINOP(op, t1, t2, f) \ |
|
39 octave_value_typeinfo::register_binary_op \ |
3203
|
40 (octave_value::op, t1::static_type_id (), t2::static_type_id (), \ |
|
41 oct_binop_ ## f); |
2376
|
42 |
4915
|
43 #define INSTALL_CATOP(t1, t2, f) \ |
|
44 octave_value_typeinfo::register_cat_op \ |
|
45 (t1::static_type_id (), t2::static_type_id (), oct_catop_ ## f); |
|
46 |
2879
|
47 #define INSTALL_ASSIGNOP(op, t1, t2, f) \ |
2376
|
48 octave_value_typeinfo::register_assign_op \ |
3203
|
49 (octave_value::op, t1::static_type_id (), t2::static_type_id (), \ |
|
50 oct_assignop_ ## f); |
2376
|
51 |
3195
|
52 #define INSTALL_ASSIGNANYOP(op, t1, f) \ |
|
53 octave_value_typeinfo::register_assignany_op \ |
3203
|
54 (octave_value::op, t1::static_type_id (), oct_assignop_ ## f); |
3195
|
55 |
2376
|
56 #define INSTALL_ASSIGNCONV(t1, t2, tr) \ |
|
57 octave_value_typeinfo::register_pref_assign_conv \ |
|
58 (t1::static_type_id (), t2::static_type_id (), tr::static_type_id ()); |
|
59 |
4901
|
60 #define INSTALL_CONVOP(t1, t2, f) \ |
|
61 octave_value_typeinfo::register_type_conv_op \ |
|
62 (t1::static_type_id (), t2::static_type_id (), oct_conv_ ## f); |
|
63 |
2376
|
64 #define INSTALL_WIDENOP(t1, t2, f) \ |
|
65 octave_value_typeinfo::register_widening_op \ |
3203
|
66 (t1::static_type_id (), t2::static_type_id (), oct_conv_ ## f); |
2376
|
67 |
|
68 #define BOOL_OP1(xt, xn, get_x, yt, yn, get_y) \ |
|
69 xt xn = get_x; \ |
|
70 yt yn = get_y; |
|
71 |
|
72 #define BOOL_OP2(x) \ |
|
73 int nr = x.rows (); \ |
|
74 int nc = x.columns (); |
|
75 |
|
76 #define BOOL_OP3(test) \ |
2825
|
77 boolMatrix retval (nr, nc); \ |
2376
|
78 for (int j = 0; j < nc; j++) \ |
|
79 for (int i = 0; i < nr; i++) \ |
|
80 retval (i, j) = test; \ |
|
81 return retval; |
|
82 |
2601
|
83 #define SC_MX_BOOL_OP(st, sn, get_s, mt, mn, get_m, test, empty_result) \ |
2376
|
84 do \ |
|
85 { \ |
|
86 BOOL_OP1 (st, sn, get_s, mt, mn, get_m) \ |
|
87 BOOL_OP2 (mn) \ |
2612
|
88 if (nr == 0 || nc == 0) \ |
2601
|
89 return empty_result; \ |
2376
|
90 BOOL_OP3 (test) \ |
|
91 } \ |
|
92 while (0) |
|
93 |
2601
|
94 #define MX_SC_BOOL_OP(mt, mn, get_m, st, sn, get_s, test, empty_result) \ |
2376
|
95 do \ |
|
96 { \ |
|
97 BOOL_OP1 (mt, mn, get_m, st, sn, get_s) \ |
|
98 BOOL_OP2 (mn) \ |
2612
|
99 if (nr == 0 || nc == 0) \ |
2601
|
100 return empty_result; \ |
2376
|
101 BOOL_OP3 (test) \ |
|
102 } \ |
|
103 while (0) |
|
104 |
|
105 #define MX_MX_BOOL_OP(m1t, m1n, get_m1, m2t, m2n, get_m2, test, op, \ |
2613
|
106 one_empty_result, two_empty_result) \ |
2376
|
107 do \ |
|
108 { \ |
|
109 BOOL_OP1 (m1t, m1n, get_m1, m2t, m2n, get_m2) \ |
2601
|
110 int m1_nr = m1n.rows (); \ |
|
111 int m1_nc = m1n.cols (); \ |
|
112 int m2_nr = m2n.rows (); \ |
|
113 int m2_nc = m2n.cols (); \ |
2613
|
114 if (m1_nr == m2_nr && m1_nc == m2_nc) \ |
2376
|
115 { \ |
2613
|
116 if (m1_nr == 0 && m1_nc == 0) \ |
|
117 return two_empty_result; \ |
|
118 else \ |
|
119 { \ |
|
120 BOOL_OP2 (m1n) \ |
|
121 BOOL_OP3 (test) \ |
|
122 } \ |
2376
|
123 } \ |
2613
|
124 else \ |
|
125 { \ |
|
126 if ((m1_nr == 0 && m1_nc == 0) || (m2_nr == 0 && m2_nc == 0)) \ |
|
127 return one_empty_result; \ |
|
128 else \ |
|
129 { \ |
|
130 gripe_nonconformant ("operator " op, m1_nr, m1_nc, \ |
|
131 m2_nr, m2_nc); \ |
2825
|
132 return boolMatrix (); \ |
2613
|
133 } \ |
|
134 } \ |
2376
|
135 } \ |
|
136 while (0) |
|
137 |
3203
|
138 #define CAST_UNOP_ARG(t) \ |
4901
|
139 t v = DYNAMIC_CAST (t, a) |
3203
|
140 |
2376
|
141 #define CAST_BINOP_ARGS(t1, t2) \ |
|
142 t1 v1 = DYNAMIC_CAST (t1, a1); \ |
4901
|
143 t2 v2 = DYNAMIC_CAST (t2, a2) |
2376
|
144 |
|
145 #define CAST_CONV_ARG(t) \ |
4901
|
146 t v = DYNAMIC_CAST (t, a) |
2376
|
147 |
2914
|
148 #define ASSIGNOPDECL(name) \ |
|
149 static octave_value \ |
3203
|
150 oct_assignop_ ## name (octave_value& a1, \ |
|
151 const octave_value_list& idx, \ |
|
152 const octave_value& a2) |
2914
|
153 |
|
154 #define DEFASSIGNOP(name, t1, t2) \ |
|
155 ASSIGNOPDECL (name) |
|
156 |
|
157 #define DEFASSIGNOP_FN(name, t1, t2, f) \ |
|
158 ASSIGNOPDECL (name) \ |
|
159 { \ |
|
160 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
161 \ |
|
162 v1.f (idx, v2.t1 ## _value ()); \ |
|
163 return octave_value (); \ |
|
164 } |
|
165 |
4686
|
166 #define DEFNDASSIGNOP_FN(name, t1, t2, e, f) \ |
|
167 ASSIGNOPDECL (name) \ |
|
168 { \ |
|
169 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
170 \ |
|
171 v1.f (idx, v2.e ## _value ()); \ |
|
172 return octave_value (); \ |
|
173 } |
|
174 |
3195
|
175 #define DEFASSIGNANYOP_FN(name, t1, f) \ |
|
176 ASSIGNOPDECL (name) \ |
|
177 { \ |
|
178 octave_ ## t1& v1 = DYNAMIC_CAST (octave_ ## t1&, a1); \ |
|
179 \ |
|
180 v1.f (idx, a2); \ |
|
181 return octave_value (); \ |
|
182 } |
|
183 |
2914
|
184 #define CONVDECL(name) \ |
|
185 static octave_value * \ |
3203
|
186 oct_conv_ ## name (const octave_value& a) |
|
187 |
|
188 #define CONVDECLX(name) \ |
|
189 static octave_value * \ |
|
190 oct_conv_ ## name (const octave_value&) |
2914
|
191 |
4901
|
192 #define DEFCONV(name, a_dummy, b_dummy) \ |
2914
|
193 CONVDECL (name) |
|
194 |
4901
|
195 #define DEFCONVFNX(name, tfrom, ovtto, tto, e) \ |
|
196 CONVDECL (name) \ |
|
197 { \ |
|
198 CAST_CONV_ARG (const octave_ ## tfrom&); \ |
|
199 \ |
|
200 return new octave_ ## ovtto (tto ## NDArray (v.e ## array_value ())); \ |
|
201 } |
|
202 |
|
203 #define DEFDBLCONVFN(name, ovtfrom, e) \ |
|
204 CONVDECL (name) \ |
|
205 { \ |
|
206 CAST_CONV_ARG (const octave_ ## ovtfrom&); \ |
|
207 \ |
|
208 return new octave_matrix (NDArray (v.e ## _value ())); \ |
|
209 } |
|
210 |
5032
|
211 #define DEFSTRINTCONVFN(name, tto) \ |
|
212 DEFCONVFNX(name, char_matrix_str, tto ## _matrix, tto, char_) |
|
213 |
|
214 #define DEFSTRDBLCONVFN(name) \ |
|
215 DEFCONVFNX(name, char_matrix_str, matrix, , char_) |
|
216 |
4901
|
217 #define DEFCONVFN(name, tfrom, tto) \ |
|
218 DEFCONVFNX (name, tfrom, tto ## _matrix, tto, ) |
|
219 |
|
220 #define DEFCONVFN2(name, tfrom, sm, tto) \ |
|
221 DEFCONVFNX (name, tfrom ## _ ## sm, tto ## _matrix, tto, tfrom ## _) |
|
222 |
3203
|
223 #define UNOPDECL(name, a) \ |
|
224 static octave_value \ |
|
225 oct_unop_ ## name (const octave_value& a) |
|
226 |
|
227 #define DEFUNOPX(name, t) \ |
|
228 UNOPDECL (name, , ) |
|
229 |
|
230 #define DEFUNOP(name, t) \ |
|
231 UNOPDECL (name, a) |
|
232 |
|
233 #define DEFUNOP_OP(name, t, op) \ |
|
234 UNOPDECL (name, a) \ |
|
235 { \ |
|
236 CAST_UNOP_ARG (const octave_ ## t&); \ |
|
237 return octave_value (op v.t ## _value ()); \ |
|
238 } |
|
239 |
4550
|
240 #define DEFNDUNOP_OP(name, t, e, op) \ |
4543
|
241 UNOPDECL (name, a) \ |
|
242 { \ |
|
243 CAST_UNOP_ARG (const octave_ ## t&); \ |
4550
|
244 return octave_value (op v.e ## _value ()); \ |
4543
|
245 } |
|
246 |
3203
|
247 // XXX FIXME XXX -- in some cases, the constructor isn't necessary. |
|
248 |
|
249 #define DEFUNOP_FN(name, t, f) \ |
|
250 UNOPDECL (name, a) \ |
|
251 { \ |
|
252 CAST_UNOP_ARG (const octave_ ## t&); \ |
|
253 return octave_value (f (v.t ## _value ())); \ |
|
254 } |
|
255 |
4550
|
256 #define DEFNDUNOP_FN(name, t, e, f) \ |
4543
|
257 UNOPDECL (name, a) \ |
|
258 { \ |
|
259 CAST_UNOP_ARG (const octave_ ## t&); \ |
4550
|
260 return octave_value (f (v.e ## _value ())); \ |
4543
|
261 } |
|
262 |
3203
|
263 #define DEFNCUNOP_METHOD(name, t, method) \ |
|
264 static void \ |
|
265 oct_unop_ ## name (octave_value& a) \ |
|
266 { \ |
|
267 CAST_UNOP_ARG (octave_ ## t&); \ |
|
268 v.method (); \ |
|
269 } |
|
270 |
2914
|
271 #define BINOPDECL(name, a1, a2) \ |
|
272 static octave_value \ |
3203
|
273 oct_binop_ ## name (const octave_value& a1, const octave_value& a2) |
2914
|
274 |
|
275 #define DEFBINOPX(name, t1, t2) \ |
|
276 BINOPDECL (name, , ) |
|
277 |
|
278 #define DEFBINOP(name, t1, t2) \ |
|
279 BINOPDECL (name, a1, a2) |
|
280 |
|
281 #define DEFBINOP_OP(name, t1, t2, op) \ |
|
282 BINOPDECL (name, a1, a2) \ |
|
283 { \ |
|
284 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
285 return octave_value \ |
|
286 (v1.t1 ## _value () op v2.t2 ## _value ()); \ |
|
287 } |
|
288 |
4543
|
289 #define DEFNDBINOP_OP(name, t1, t2, e1, e2, op) \ |
|
290 BINOPDECL (name, a1, a2) \ |
|
291 { \ |
|
292 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
293 return octave_value \ |
|
294 (v1.e1 ## _value () op v2.e2 ## _value ()); \ |
|
295 } |
|
296 |
2914
|
297 // XXX FIXME XXX -- in some cases, the constructor isn't necessary. |
|
298 |
|
299 #define DEFBINOP_FN(name, t1, t2, f) \ |
|
300 BINOPDECL (name, a1, a2) \ |
|
301 { \ |
|
302 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
303 return octave_value (f (v1.t1 ## _value (), v2.t2 ## _value ())); \ |
|
304 } |
|
305 |
4543
|
306 #define DEFNDBINOP_FN(name, t1, t2, e1, e2, f) \ |
|
307 BINOPDECL (name, a1, a2) \ |
|
308 { \ |
|
309 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
310 return octave_value (f (v1.e1 ## _value (), v2.e2 ## _value ())); \ |
|
311 } |
|
312 |
2914
|
313 #define BINOP_NONCONFORMANT(msg) \ |
|
314 gripe_nonconformant (msg, \ |
|
315 a1.rows (), a1.columns (), \ |
|
316 a2.rows (), a2.columns ()); \ |
|
317 return octave_value () |
|
318 |
4915
|
319 #define CATOPDECL(name, a1, a2) \ |
|
320 static octave_value \ |
5073
|
321 oct_catop_ ## name (octave_value& a1, const octave_value& a2, \ |
4915
|
322 const Array<int>& ra_idx) |
|
323 |
|
324 #define DEFCATOPX(name, t1, t2) \ |
|
325 CATOPDECL (name, , ) |
|
326 |
|
327 #define DEFCATOP(name, t1, t2) \ |
|
328 CATOPDECL (name, a1, a2) |
|
329 |
|
330 // XXX FIXME XXX -- in some cases, the constructor isn't necessary. |
|
331 |
|
332 #define DEFCATOP_FN(name, t1, t2, f) \ |
5075
|
333 CATOPDECL (name, a1, a2) \ |
4915
|
334 { \ |
5073
|
335 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
336 return octave_value (v1.t1 ## _value (). f (v2.t2 ## _value (), ra_idx)); \ |
4915
|
337 } |
|
338 |
5075
|
339 #define DEFNDCATOP_FN(name, t1, t2, e1, e2, f) \ |
|
340 CATOPDECL (name, a1, a2) \ |
4915
|
341 { \ |
5073
|
342 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
343 return octave_value (v1.e1 ## _value (). f (v2.e2 ## _value (), ra_idx)); \ |
4915
|
344 } |
|
345 |
5075
|
346 // For compatibility, the second arg is always converted to the type |
|
347 // of the first. Hmm. |
|
348 |
|
349 #define DEFNDCATOP_FN2(name, t1, t2, tc1, tc2, e1, e2, f) \ |
|
350 CATOPDECL (name, a1, a2) \ |
|
351 { \ |
|
352 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
353 return octave_value (tc1 (v1.e1 ## _value ()) . f (tc2 (v2.e2 ## _value ()), ra_idx)); \ |
|
354 } |
|
355 |
4915
|
356 #define CATOP_NONCONFORMANT(msg) \ |
|
357 gripe_nonconformant (msg, \ |
|
358 a1.rows (), a1.columns (), \ |
|
359 a2.rows (), a2.columns ()); \ |
|
360 return octave_value () |
|
361 |
2376
|
362 #endif |
|
363 |
|
364 /* |
|
365 ;;; Local Variables: *** |
|
366 ;;; mode: C++ *** |
|
367 ;;; End: *** |
|
368 */ |