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 |
2879
|
43 #define INSTALL_ASSIGNOP(op, t1, t2, f) \ |
2376
|
44 octave_value_typeinfo::register_assign_op \ |
3203
|
45 (octave_value::op, t1::static_type_id (), t2::static_type_id (), \ |
|
46 oct_assignop_ ## f); |
2376
|
47 |
3195
|
48 #define INSTALL_ASSIGNANYOP(op, t1, f) \ |
|
49 octave_value_typeinfo::register_assignany_op \ |
3203
|
50 (octave_value::op, t1::static_type_id (), oct_assignop_ ## f); |
3195
|
51 |
2376
|
52 #define INSTALL_ASSIGNCONV(t1, t2, tr) \ |
|
53 octave_value_typeinfo::register_pref_assign_conv \ |
|
54 (t1::static_type_id (), t2::static_type_id (), tr::static_type_id ()); |
|
55 |
4901
|
56 #define INSTALL_CONVOP(t1, t2, f) \ |
|
57 octave_value_typeinfo::register_type_conv_op \ |
|
58 (t1::static_type_id (), t2::static_type_id (), oct_conv_ ## f); |
|
59 |
2376
|
60 #define INSTALL_WIDENOP(t1, t2, f) \ |
|
61 octave_value_typeinfo::register_widening_op \ |
3203
|
62 (t1::static_type_id (), t2::static_type_id (), oct_conv_ ## f); |
2376
|
63 |
|
64 #define BOOL_OP1(xt, xn, get_x, yt, yn, get_y) \ |
|
65 xt xn = get_x; \ |
|
66 yt yn = get_y; |
|
67 |
|
68 #define BOOL_OP2(x) \ |
|
69 int nr = x.rows (); \ |
|
70 int nc = x.columns (); |
|
71 |
|
72 #define BOOL_OP3(test) \ |
2825
|
73 boolMatrix retval (nr, nc); \ |
2376
|
74 for (int j = 0; j < nc; j++) \ |
|
75 for (int i = 0; i < nr; i++) \ |
|
76 retval (i, j) = test; \ |
|
77 return retval; |
|
78 |
2601
|
79 #define SC_MX_BOOL_OP(st, sn, get_s, mt, mn, get_m, test, empty_result) \ |
2376
|
80 do \ |
|
81 { \ |
|
82 BOOL_OP1 (st, sn, get_s, mt, mn, get_m) \ |
|
83 BOOL_OP2 (mn) \ |
2612
|
84 if (nr == 0 || nc == 0) \ |
2601
|
85 return empty_result; \ |
2376
|
86 BOOL_OP3 (test) \ |
|
87 } \ |
|
88 while (0) |
|
89 |
2601
|
90 #define MX_SC_BOOL_OP(mt, mn, get_m, st, sn, get_s, test, empty_result) \ |
2376
|
91 do \ |
|
92 { \ |
|
93 BOOL_OP1 (mt, mn, get_m, st, sn, get_s) \ |
|
94 BOOL_OP2 (mn) \ |
2612
|
95 if (nr == 0 || nc == 0) \ |
2601
|
96 return empty_result; \ |
2376
|
97 BOOL_OP3 (test) \ |
|
98 } \ |
|
99 while (0) |
|
100 |
|
101 #define MX_MX_BOOL_OP(m1t, m1n, get_m1, m2t, m2n, get_m2, test, op, \ |
2613
|
102 one_empty_result, two_empty_result) \ |
2376
|
103 do \ |
|
104 { \ |
|
105 BOOL_OP1 (m1t, m1n, get_m1, m2t, m2n, get_m2) \ |
2601
|
106 int m1_nr = m1n.rows (); \ |
|
107 int m1_nc = m1n.cols (); \ |
|
108 int m2_nr = m2n.rows (); \ |
|
109 int m2_nc = m2n.cols (); \ |
2613
|
110 if (m1_nr == m2_nr && m1_nc == m2_nc) \ |
2376
|
111 { \ |
2613
|
112 if (m1_nr == 0 && m1_nc == 0) \ |
|
113 return two_empty_result; \ |
|
114 else \ |
|
115 { \ |
|
116 BOOL_OP2 (m1n) \ |
|
117 BOOL_OP3 (test) \ |
|
118 } \ |
2376
|
119 } \ |
2613
|
120 else \ |
|
121 { \ |
|
122 if ((m1_nr == 0 && m1_nc == 0) || (m2_nr == 0 && m2_nc == 0)) \ |
|
123 return one_empty_result; \ |
|
124 else \ |
|
125 { \ |
|
126 gripe_nonconformant ("operator " op, m1_nr, m1_nc, \ |
|
127 m2_nr, m2_nc); \ |
2825
|
128 return boolMatrix (); \ |
2613
|
129 } \ |
|
130 } \ |
2376
|
131 } \ |
|
132 while (0) |
|
133 |
3203
|
134 #define CAST_UNOP_ARG(t) \ |
4901
|
135 t v = DYNAMIC_CAST (t, a) |
3203
|
136 |
2376
|
137 #define CAST_BINOP_ARGS(t1, t2) \ |
|
138 t1 v1 = DYNAMIC_CAST (t1, a1); \ |
4901
|
139 t2 v2 = DYNAMIC_CAST (t2, a2) |
2376
|
140 |
|
141 #define CAST_CONV_ARG(t) \ |
4901
|
142 t v = DYNAMIC_CAST (t, a) |
2376
|
143 |
2914
|
144 #define ASSIGNOPDECL(name) \ |
|
145 static octave_value \ |
3203
|
146 oct_assignop_ ## name (octave_value& a1, \ |
|
147 const octave_value_list& idx, \ |
|
148 const octave_value& a2) |
2914
|
149 |
|
150 #define DEFASSIGNOP(name, t1, t2) \ |
|
151 ASSIGNOPDECL (name) |
|
152 |
|
153 #define DEFASSIGNOP_FN(name, t1, t2, f) \ |
|
154 ASSIGNOPDECL (name) \ |
|
155 { \ |
|
156 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
157 \ |
|
158 v1.f (idx, v2.t1 ## _value ()); \ |
|
159 return octave_value (); \ |
|
160 } |
|
161 |
4686
|
162 #define DEFNDASSIGNOP_FN(name, t1, t2, e, f) \ |
|
163 ASSIGNOPDECL (name) \ |
|
164 { \ |
|
165 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
166 \ |
|
167 v1.f (idx, v2.e ## _value ()); \ |
|
168 return octave_value (); \ |
|
169 } |
|
170 |
3195
|
171 #define DEFASSIGNANYOP_FN(name, t1, f) \ |
|
172 ASSIGNOPDECL (name) \ |
|
173 { \ |
|
174 octave_ ## t1& v1 = DYNAMIC_CAST (octave_ ## t1&, a1); \ |
|
175 \ |
|
176 v1.f (idx, a2); \ |
|
177 return octave_value (); \ |
|
178 } |
|
179 |
2914
|
180 #define CONVDECL(name) \ |
|
181 static octave_value * \ |
3203
|
182 oct_conv_ ## name (const octave_value& a) |
|
183 |
|
184 #define CONVDECLX(name) \ |
|
185 static octave_value * \ |
|
186 oct_conv_ ## name (const octave_value&) |
2914
|
187 |
4901
|
188 #define DEFCONV(name, a_dummy, b_dummy) \ |
2914
|
189 CONVDECL (name) |
|
190 |
4901
|
191 #define DEFCONVFNX(name, tfrom, ovtto, tto, e) \ |
|
192 CONVDECL (name) \ |
|
193 { \ |
|
194 CAST_CONV_ARG (const octave_ ## tfrom&); \ |
|
195 \ |
|
196 return new octave_ ## ovtto (tto ## NDArray (v.e ## array_value ())); \ |
|
197 } |
|
198 |
|
199 #define DEFDBLCONVFN(name, ovtfrom, e) \ |
|
200 CONVDECL (name) \ |
|
201 { \ |
|
202 CAST_CONV_ARG (const octave_ ## ovtfrom&); \ |
|
203 \ |
|
204 return new octave_matrix (NDArray (v.e ## _value ())); \ |
|
205 } |
|
206 |
|
207 #define DEFCONVFN(name, tfrom, tto) \ |
|
208 DEFCONVFNX (name, tfrom, tto ## _matrix, tto, ) |
|
209 |
|
210 #define DEFCONVFN2(name, tfrom, sm, tto) \ |
|
211 DEFCONVFNX (name, tfrom ## _ ## sm, tto ## _matrix, tto, tfrom ## _) |
|
212 |
3203
|
213 #define UNOPDECL(name, a) \ |
|
214 static octave_value \ |
|
215 oct_unop_ ## name (const octave_value& a) |
|
216 |
|
217 #define DEFUNOPX(name, t) \ |
|
218 UNOPDECL (name, , ) |
|
219 |
|
220 #define DEFUNOP(name, t) \ |
|
221 UNOPDECL (name, a) |
|
222 |
|
223 #define DEFUNOP_OP(name, t, op) \ |
|
224 UNOPDECL (name, a) \ |
|
225 { \ |
|
226 CAST_UNOP_ARG (const octave_ ## t&); \ |
|
227 return octave_value (op v.t ## _value ()); \ |
|
228 } |
|
229 |
4550
|
230 #define DEFNDUNOP_OP(name, t, e, op) \ |
4543
|
231 UNOPDECL (name, a) \ |
|
232 { \ |
|
233 CAST_UNOP_ARG (const octave_ ## t&); \ |
4550
|
234 return octave_value (op v.e ## _value ()); \ |
4543
|
235 } |
|
236 |
3203
|
237 // XXX FIXME XXX -- in some cases, the constructor isn't necessary. |
|
238 |
|
239 #define DEFUNOP_FN(name, t, f) \ |
|
240 UNOPDECL (name, a) \ |
|
241 { \ |
|
242 CAST_UNOP_ARG (const octave_ ## t&); \ |
|
243 return octave_value (f (v.t ## _value ())); \ |
|
244 } |
|
245 |
4550
|
246 #define DEFNDUNOP_FN(name, t, e, f) \ |
4543
|
247 UNOPDECL (name, a) \ |
|
248 { \ |
|
249 CAST_UNOP_ARG (const octave_ ## t&); \ |
4550
|
250 return octave_value (f (v.e ## _value ())); \ |
4543
|
251 } |
|
252 |
3203
|
253 #define DEFNCUNOP_METHOD(name, t, method) \ |
|
254 static void \ |
|
255 oct_unop_ ## name (octave_value& a) \ |
|
256 { \ |
|
257 CAST_UNOP_ARG (octave_ ## t&); \ |
|
258 v.method (); \ |
|
259 } |
|
260 |
2914
|
261 #define BINOPDECL(name, a1, a2) \ |
|
262 static octave_value \ |
3203
|
263 oct_binop_ ## name (const octave_value& a1, const octave_value& a2) |
2914
|
264 |
|
265 #define DEFBINOPX(name, t1, t2) \ |
|
266 BINOPDECL (name, , ) |
|
267 |
|
268 #define DEFBINOP(name, t1, t2) \ |
|
269 BINOPDECL (name, a1, a2) |
|
270 |
|
271 #define DEFBINOP_OP(name, t1, t2, op) \ |
|
272 BINOPDECL (name, a1, a2) \ |
|
273 { \ |
|
274 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
275 return octave_value \ |
|
276 (v1.t1 ## _value () op v2.t2 ## _value ()); \ |
|
277 } |
|
278 |
4543
|
279 #define DEFNDBINOP_OP(name, t1, t2, e1, e2, op) \ |
|
280 BINOPDECL (name, a1, a2) \ |
|
281 { \ |
|
282 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
283 return octave_value \ |
|
284 (v1.e1 ## _value () op v2.e2 ## _value ()); \ |
|
285 } |
|
286 |
2914
|
287 // XXX FIXME XXX -- in some cases, the constructor isn't necessary. |
|
288 |
|
289 #define DEFBINOP_FN(name, t1, t2, f) \ |
|
290 BINOPDECL (name, a1, a2) \ |
|
291 { \ |
|
292 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
293 return octave_value (f (v1.t1 ## _value (), v2.t2 ## _value ())); \ |
|
294 } |
|
295 |
4543
|
296 #define DEFNDBINOP_FN(name, t1, t2, e1, e2, f) \ |
|
297 BINOPDECL (name, a1, a2) \ |
|
298 { \ |
|
299 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
300 return octave_value (f (v1.e1 ## _value (), v2.e2 ## _value ())); \ |
|
301 } |
|
302 |
2914
|
303 #define BINOP_NONCONFORMANT(msg) \ |
|
304 gripe_nonconformant (msg, \ |
|
305 a1.rows (), a1.columns (), \ |
|
306 a2.rows (), a2.columns ()); \ |
|
307 return octave_value () |
|
308 |
2376
|
309 #endif |
|
310 |
|
311 /* |
|
312 ;;; Local Variables: *** |
|
313 ;;; mode: C++ *** |
|
314 ;;; End: *** |
|
315 */ |