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