Mercurial > hg > octave-nkf
annotate src/ov-fcn-inline.cc @ 8916:a2878ba31a9e
add diag & perm matrix query methods to octave_value
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 06 Mar 2009 12:00:11 +0100 |
parents | 8ba2ee57c594 |
children | eb63fbe60fab |
rev | line source |
---|---|
4933 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
4933 | 4 |
7016 | 5 This file is part of Octave. |
4933 | 6 |
7016 | 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 3 of the License, or (at your | |
10 option) any 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. | |
4933 | 16 |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
4933 | 20 |
21 In addition to the terms of the GPL, you are permitted to link | |
22 this program with any Open Source program, as defined by the | |
23 Open Source Initiative (www.opensource.org) | |
24 | |
25 */ | |
26 | |
27 #ifdef HAVE_CONFIG_H | |
28 #include <config.h> | |
29 #endif | |
30 | |
4988 | 31 #include <istream> |
4933 | 32 #include <iostream> |
5765 | 33 #include <sstream> |
5164 | 34 #include <vector> |
4933 | 35 |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8045
diff
changeset
|
36 #include "oct-locbuf.h" |
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8045
diff
changeset
|
37 |
4933 | 38 #include "defun.h" |
39 #include "error.h" | |
40 #include "gripes.h" | |
41 #include "oct-map.h" | |
42 #include "ov-base.h" | |
43 #include "ov-fcn-inline.h" | |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
44 #include "ov-usr-fcn.h" |
4933 | 45 #include "pr-output.h" |
46 #include "variables.h" | |
47 #include "parse.h" | |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
48 #include "toplev.h" |
4933 | 49 |
4972 | 50 #include "byte-swap.h" |
51 #include "ls-oct-ascii.h" | |
52 #include "ls-hdf5.h" | |
53 #include "ls-utils.h" | |
54 | |
4933 | 55 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_inline); |
56 | |
57 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_inline, | |
58 "inline function", | |
6220 | 59 "function_handle"); |
4933 | 60 |
61 octave_fcn_inline::octave_fcn_inline (const std::string& f, | |
4973 | 62 const string_vector& a, |
4933 | 63 const std::string& n) |
5007 | 64 : octave_fcn_handle (n), iftext (f), ifargs (a) |
4933 | 65 { |
4973 | 66 // Form a string representing the function. |
4933 | 67 |
5765 | 68 std::ostringstream buf; |
4933 | 69 |
5007 | 70 buf << "@("; |
4933 | 71 |
72 for (int i = 0; i < ifargs.length (); i++) | |
73 { | |
74 if (i > 0) | |
75 buf << ", "; | |
76 | |
77 buf << ifargs(i); | |
78 } | |
79 | |
5765 | 80 buf << ") " << iftext; |
4933 | 81 |
5007 | 82 int parse_status; |
5765 | 83 octave_value anon_fcn_handle = eval_string (buf.str (), true, parse_status); |
4933 | 84 |
5007 | 85 if (parse_status == 0) |
4933 | 86 { |
5007 | 87 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); |
4933 | 88 |
5007 | 89 if (fh) |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
90 { |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
91 fcn = fh->fcn_val (); |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
92 |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
93 octave_user_function *uf = fcn.user_function_value (); |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
94 |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
95 if (uf) |
8045
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
96 { |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
97 octave_function *curr_fcn = octave_call_stack::current (); |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
98 |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
99 if (curr_fcn) |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
100 { |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
101 symbol_table::scope_id parent_scope |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
102 = curr_fcn->parent_fcn_scope (); |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
103 |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
104 if (parent_scope < 0) |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
105 parent_scope = curr_fcn->scope (); |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
106 |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
107 uf->stash_parent_fcn_scope (parent_scope); |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
108 } |
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
109 } |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
110 } |
4933 | 111 } |
5007 | 112 |
113 if (fcn.is_undefined ()) | |
4933 | 114 error ("inline: unable to define function"); |
115 } | |
116 | |
6625 | 117 // This function is supplied to allow a Matlab style class structure |
118 // to be returned.. | |
119 Octave_map | |
120 octave_fcn_inline::map_value (void) const | |
121 { | |
122 Octave_map m; | |
123 string_vector args = fcn_arg_names (); | |
124 m.assign ("version", octave_value (1.0)); | |
125 m.assign ("isEmpty", octave_value (0.0)); | |
126 m.assign ("expr", octave_value (fcn_text ())); | |
127 m.assign ("numArgs", octave_value (args.length ())); | |
128 m.assign ("args", octave_value (args)); | |
129 std::ostringstream buf; | |
130 for (int i = 0; i < args.length (); i++) | |
131 buf << args(i) << " = INLINE_INPUTS_{" << i + 1 << "}; "; | |
132 m.assign ("inputExpr", octave_value (buf.str ())); | |
133 | |
134 return m; | |
135 } | |
136 | |
4972 | 137 bool |
6974 | 138 octave_fcn_inline::save_ascii (std::ostream& os) |
4972 | 139 { |
4973 | 140 os << "# nargs: " << ifargs.length () << "\n"; |
4972 | 141 for (int i = 0; i < ifargs.length (); i++) |
4973 | 142 os << ifargs(i) << "\n"; |
143 if (nm.length () < 1) | |
144 // Write an invalid value to flag empty fcn handle name. | |
4972 | 145 os << "0\n"; |
146 else | |
147 os << nm << "\n"; | |
148 os << iftext << "\n"; | |
149 return true; | |
150 } | |
151 | |
152 bool | |
153 octave_fcn_inline::load_ascii (std::istream& is) | |
154 { | |
155 int nargs; | |
156 if (extract_keyword (is, "nargs", nargs, true)) | |
157 { | |
158 ifargs.resize (nargs); | |
159 for (int i = 0; i < nargs; i++) | |
4973 | 160 is >> ifargs(i); |
4972 | 161 is >> nm; |
162 if (nm == "0") | |
163 nm = ""; | |
4988 | 164 |
165 char c; | |
5765 | 166 std::ostringstream buf; |
4988 | 167 |
168 // Skip preceeding newline(s) | |
7908
47a18b8c9000
ov-fcn-inline.cc (octave_fcn_inline::load_ascii): Avoid GCC warning
John W. Eaton <jwe@octave.org>
parents:
7764
diff
changeset
|
169 while (is.get (c) && c == '\n') |
47a18b8c9000
ov-fcn-inline.cc (octave_fcn_inline::load_ascii): Avoid GCC warning
John W. Eaton <jwe@octave.org>
parents:
7764
diff
changeset
|
170 /* do nothing */; |
4988 | 171 |
172 if (is) | |
173 { | |
174 buf << c; | |
175 | |
176 // Get a line of text whitespace characters included, leaving | |
177 // newline in the stream | |
178 while (is.peek () != '\n') | |
179 { | |
180 is.get (c); | |
181 if (! is) | |
182 break; | |
183 buf << c; | |
184 } | |
185 } | |
186 | |
5765 | 187 iftext = buf.str (); |
4972 | 188 |
189 octave_fcn_inline tmp (iftext, ifargs, nm); | |
190 fcn = tmp.fcn; | |
191 | |
192 return true; | |
193 } | |
194 else | |
195 return false; | |
196 } | |
197 | |
4973 | 198 bool |
4972 | 199 octave_fcn_inline::save_binary (std::ostream& os, bool&) |
200 { | |
5828 | 201 int32_t tmp = ifargs.length (); |
5760 | 202 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 203 for (int i = 0; i < ifargs.length (); i++) |
4972 | 204 { |
4973 | 205 tmp = ifargs(i).length (); |
5760 | 206 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 207 os.write (ifargs(i).c_str (), ifargs(i).length ()); |
4972 | 208 } |
4973 | 209 tmp = nm.length (); |
5760 | 210 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 211 os.write (nm.c_str (), nm.length ()); |
212 tmp = iftext.length (); | |
5760 | 213 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 214 os.write (iftext.c_str (), iftext.length ()); |
4972 | 215 return true; |
216 } | |
217 | |
4973 | 218 bool |
4972 | 219 octave_fcn_inline::load_binary (std::istream& is, bool swap, |
220 oct_mach_info::float_format) | |
221 { | |
5828 | 222 int32_t nargs; |
5760 | 223 if (! is.read (reinterpret_cast<char *> (&nargs), 4)) |
4972 | 224 return false; |
225 if (swap) | |
226 swap_bytes<4> (&nargs); | |
227 | |
228 if (nargs < 1) | |
229 return false; | |
230 else | |
231 { | |
5828 | 232 int32_t tmp; |
4973 | 233 ifargs.resize (nargs); |
4972 | 234 for (int i = 0; i < nargs; i++) |
235 { | |
5760 | 236 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4972 | 237 return false; |
238 if (swap) | |
239 swap_bytes<4> (&tmp); | |
240 | |
241 OCTAVE_LOCAL_BUFFER (char, ctmp, tmp+1); | |
242 is.read (ctmp, tmp); | |
243 ifargs(i) = std::string (ctmp); | |
244 | |
245 if (! is) | |
246 return false; | |
4973 | 247 } |
4972 | 248 |
5760 | 249 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4972 | 250 return false; |
251 if (swap) | |
252 swap_bytes<4> (&tmp); | |
253 | |
254 OCTAVE_LOCAL_BUFFER (char, ctmp1, tmp+1); | |
255 is.read (ctmp1, tmp); | |
256 nm = std::string (ctmp1); | |
257 | |
258 if (! is) | |
259 return false; | |
260 | |
5760 | 261 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4972 | 262 return false; |
263 if (swap) | |
264 swap_bytes<4> (&tmp); | |
265 | |
266 OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1); | |
267 is.read (ctmp2, tmp); | |
268 iftext = std::string (ctmp2); | |
269 | |
270 if (! is) | |
271 return false; | |
272 | |
273 octave_fcn_inline ftmp (iftext, ifargs, nm); | |
274 fcn = ftmp.fcn; | |
275 } | |
276 return true; | |
277 } | |
278 | |
279 #if defined (HAVE_HDF5) | |
280 bool | |
281 octave_fcn_inline::save_hdf5 (hid_t loc_id, const char *name, | |
282 bool /* save_as_floats */) | |
283 { | |
284 hid_t group_hid = -1; | |
285 group_hid = H5Gcreate (loc_id, name, 0); | |
286 if (group_hid < 0 ) return false; | |
287 | |
288 size_t len = 0; | |
4973 | 289 for (int i = 0; i < ifargs.length (); i++) |
290 if (len < ifargs(i).length ()) | |
291 len = ifargs(i).length (); | |
4972 | 292 |
293 hid_t space_hid = -1, data_hid = -1, type_hid = -1;; | |
294 bool retval = true; | |
295 | |
5775 | 296 // FIXME Is there a better way of saving string vectors, than a |
4972 | 297 // null padded matrix? |
298 | |
299 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2); | |
300 | |
301 // Octave uses column-major, while HDF5 uses row-major ordering | |
4973 | 302 hdims[1] = ifargs.length (); |
4972 | 303 hdims[0] = len + 1; |
304 | |
305 space_hid = H5Screate_simple (2, hdims, 0); | |
306 if (space_hid < 0) | |
307 { | |
308 H5Gclose (group_hid); | |
309 return false; | |
310 } | |
311 | |
4973 | 312 data_hid = H5Dcreate (group_hid, "args", H5T_NATIVE_CHAR, space_hid, |
4972 | 313 H5P_DEFAULT); |
314 if (data_hid < 0) | |
315 { | |
316 H5Sclose (space_hid); | |
317 H5Gclose (group_hid); | |
318 return false; | |
319 } | |
320 | |
4973 | 321 OCTAVE_LOCAL_BUFFER (char, s, ifargs.length () * (len + 1)); |
4972 | 322 |
323 // Save the args as a null teminated list | |
4973 | 324 for (int i = 0; i < ifargs.length (); i++) |
4972 | 325 { |
4973 | 326 const char * cptr = ifargs(i).c_str (); |
327 for (size_t j = 0; j < ifargs(i).length (); j++) | |
4972 | 328 s[i*(len+1)+j] = *cptr++; |
4973 | 329 s[ifargs(i).length ()] = '\0'; |
4972 | 330 } |
331 | |
4973 | 332 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4972 | 333 H5P_DEFAULT, s) >= 0; |
334 | |
335 H5Dclose (data_hid); | |
336 H5Sclose (space_hid); | |
337 | |
338 if (!retval) | |
339 { | |
340 H5Gclose (group_hid); | |
341 return false; | |
4973 | 342 } |
4972 | 343 |
344 // attach the type of the variable | |
4973 | 345 type_hid = H5Tcopy (H5T_C_S1); |
4972 | 346 H5Tset_size (type_hid, nm.length () + 1); |
347 if (type_hid < 0) | |
348 { | |
349 H5Gclose (group_hid); | |
350 return false; | |
4973 | 351 } |
4972 | 352 |
353 hdims[0] = 0; | |
5760 | 354 space_hid = H5Screate_simple (0 , hdims, 0); |
4972 | 355 if (space_hid < 0) |
356 { | |
357 H5Tclose (type_hid); | |
358 H5Gclose (group_hid); | |
359 return false; | |
4973 | 360 } |
4972 | 361 |
362 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT); | |
4973 | 363 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
5760 | 364 H5P_DEFAULT, nm.c_str ()) < 0) |
4972 | 365 { |
366 H5Sclose (space_hid); | |
367 H5Tclose (type_hid); | |
368 H5Gclose (group_hid); | |
369 return false; | |
4973 | 370 } |
4972 | 371 H5Dclose (data_hid); |
372 | |
373 // attach the type of the variable | |
374 H5Tset_size (type_hid, iftext.length () + 1); | |
375 if (type_hid < 0) | |
376 { | |
377 H5Gclose (group_hid); | |
378 return false; | |
4973 | 379 } |
4972 | 380 |
4973 | 381 data_hid = H5Dcreate (group_hid, "iftext", type_hid, space_hid, |
4972 | 382 H5P_DEFAULT); |
4973 | 383 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
5760 | 384 H5P_DEFAULT, iftext.c_str ()) < 0) |
4972 | 385 { |
386 H5Sclose (space_hid); | |
387 H5Tclose (type_hid); | |
388 H5Gclose (group_hid); | |
389 return false; | |
4973 | 390 } |
4972 | 391 |
392 H5Dclose (data_hid); | |
4988 | 393 H5Sclose (space_hid); |
394 H5Tclose (type_hid); | |
395 H5Gclose (group_hid); | |
4972 | 396 |
397 return retval; | |
398 } | |
399 | |
400 bool | |
401 octave_fcn_inline::load_hdf5 (hid_t loc_id, const char *name, | |
4973 | 402 bool /* have_h5giterate_bug */) |
4972 | 403 { |
404 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; | |
405 hsize_t rank; | |
406 int slen; | |
407 | |
408 group_hid = H5Gopen (loc_id, name); | |
409 if (group_hid < 0 ) return false; | |
410 | |
411 data_hid = H5Dopen (group_hid, "args"); | |
412 space_hid = H5Dget_space (data_hid); | |
413 rank = H5Sget_simple_extent_ndims (space_hid); | |
414 | |
415 if (rank != 2) | |
4973 | 416 { |
4972 | 417 H5Dclose (data_hid); |
418 H5Sclose (space_hid); | |
419 H5Gclose (group_hid); | |
420 return false; | |
421 } | |
422 | |
423 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
424 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
425 | |
426 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
427 | |
4973 | 428 ifargs.resize (hdims[1]); |
4972 | 429 |
430 OCTAVE_LOCAL_BUFFER (char, s1, hdims[0] * hdims[1]); | |
431 | |
4973 | 432 if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, |
4972 | 433 H5P_DEFAULT, s1) < 0) |
4973 | 434 { |
4972 | 435 H5Dclose (data_hid); |
436 H5Sclose (space_hid); | |
437 H5Gclose (group_hid); | |
438 return false; | |
439 } | |
440 | |
441 H5Dclose (data_hid); | |
442 H5Sclose (space_hid); | |
443 | |
444 for (size_t i = 0; i < hdims[1]; i++) | |
445 ifargs(i) = std::string (s1 + i*hdims[0]); | |
446 | |
447 data_hid = H5Dopen (group_hid, "nm"); | |
448 | |
449 if (data_hid < 0) | |
450 { | |
451 H5Gclose (group_hid); | |
452 return false; | |
453 } | |
454 | |
455 type_hid = H5Dget_type (data_hid); | |
456 type_class_hid = H5Tget_class (type_hid); | |
457 | |
458 if (type_class_hid != H5T_STRING) | |
459 { | |
460 H5Tclose (type_hid); | |
461 H5Dclose (data_hid); | |
462 H5Gclose (group_hid); | |
463 return false; | |
464 } | |
4973 | 465 |
4972 | 466 space_hid = H5Dget_space (data_hid); |
467 rank = H5Sget_simple_extent_ndims (space_hid); | |
468 | |
469 if (rank != 0) | |
470 { | |
471 H5Sclose (space_hid); | |
472 H5Tclose (type_hid); | |
473 H5Dclose (data_hid); | |
474 H5Gclose (group_hid); | |
475 return false; | |
476 } | |
477 | |
478 slen = H5Tget_size (type_hid); | |
479 if (slen < 0) | |
480 { | |
481 H5Sclose (space_hid); | |
482 H5Tclose (type_hid); | |
483 H5Dclose (data_hid); | |
484 H5Gclose (group_hid); | |
485 return false; | |
486 } | |
487 | |
488 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); | |
489 | |
490 // create datatype for (null-terminated) string to read into: | |
491 st_id = H5Tcopy (H5T_C_S1); | |
492 H5Tset_size (st_id, slen); | |
493 | |
5760 | 494 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0) |
4972 | 495 { |
496 H5Sclose (space_hid); | |
497 H5Tclose (type_hid); | |
498 H5Gclose (group_hid); | |
499 return false; | |
500 } | |
501 H5Tclose (st_id); | |
502 H5Dclose (data_hid); | |
503 nm = nm_tmp; | |
504 | |
505 data_hid = H5Dopen (group_hid, "iftext"); | |
506 | |
507 if (data_hid < 0) | |
508 { | |
509 H5Gclose (group_hid); | |
510 return false; | |
511 } | |
512 | |
513 type_hid = H5Dget_type (data_hid); | |
514 type_class_hid = H5Tget_class (type_hid); | |
515 | |
516 if (type_class_hid != H5T_STRING) | |
517 { | |
518 H5Tclose (type_hid); | |
519 H5Dclose (data_hid); | |
520 H5Gclose (group_hid); | |
521 return false; | |
522 } | |
4973 | 523 |
4972 | 524 space_hid = H5Dget_space (data_hid); |
525 rank = H5Sget_simple_extent_ndims (space_hid); | |
526 | |
527 if (rank != 0) | |
528 { | |
529 H5Sclose (space_hid); | |
530 H5Tclose (type_hid); | |
531 H5Dclose (data_hid); | |
532 H5Gclose (group_hid); | |
533 return false; | |
534 } | |
535 | |
536 slen = H5Tget_size (type_hid); | |
537 if (slen < 0) | |
538 { | |
539 H5Sclose (space_hid); | |
540 H5Tclose (type_hid); | |
541 H5Dclose (data_hid); | |
542 H5Gclose (group_hid); | |
543 return false; | |
544 } | |
545 | |
546 OCTAVE_LOCAL_BUFFER (char, iftext_tmp, slen); | |
547 | |
548 // create datatype for (null-terminated) string to read into: | |
549 st_id = H5Tcopy (H5T_C_S1); | |
550 H5Tset_size (st_id, slen); | |
551 | |
5760 | 552 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, iftext_tmp) < 0) |
4972 | 553 { |
554 H5Sclose (space_hid); | |
555 H5Tclose (type_hid); | |
556 H5Gclose (group_hid); | |
557 return false; | |
558 } | |
559 H5Tclose (st_id); | |
560 H5Dclose (data_hid); | |
561 iftext = iftext_tmp; | |
562 | |
563 octave_fcn_inline ftmp (iftext, ifargs, nm); | |
564 fcn = ftmp.fcn; | |
565 | |
566 return true; | |
567 } | |
568 #endif | |
569 | |
4933 | 570 void |
571 octave_fcn_inline::print (std::ostream& os, bool pr_as_read_syntax) const | |
572 { | |
573 print_raw (os, pr_as_read_syntax); | |
574 newline (os); | |
575 } | |
576 | |
577 void | |
578 octave_fcn_inline::print_raw (std::ostream& os, bool pr_as_read_syntax) const | |
579 { | |
5765 | 580 std::ostringstream buf; |
4933 | 581 |
582 if (nm.empty ()) | |
583 buf << "f("; | |
584 else | |
585 buf << nm << "("; | |
586 | |
587 for (int i = 0; i < ifargs.length (); i++) | |
588 { | |
589 if (i) | |
590 buf << ", "; | |
591 | |
592 buf << ifargs(i); | |
593 } | |
594 | |
5765 | 595 buf << ") = " << iftext; |
4933 | 596 |
5765 | 597 octave_print_internal (os, buf.str (), pr_as_read_syntax, |
4933 | 598 current_print_indent_level ()); |
599 } | |
600 | |
601 octave_value | |
5279 | 602 octave_fcn_inline::convert_to_str_internal (bool, bool, char type) const |
4933 | 603 { |
5279 | 604 return octave_value (fcn_text (), type); |
4933 | 605 } |
606 | |
6973 | 607 DEFUNX ("inline", Finline, args, , |
4933 | 608 "-*- texinfo -*-\n\ |
609 @deftypefn {Built-in Function} {} inline (@var{str})\n\ | |
6547 | 610 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, @dots{})\n\ |
4933 | 611 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\ |
612 Create an inline function from the character string @var{str}.\n\ | |
5034 | 613 If called with a single argument, the arguments of the generated\n\ |
614 function are extracted from the function itself. The generated\n\ | |
615 function arguments will then be in alphabetical order. It should\n\ | |
616 be noted that i, and j are ignored as arguments due to the\n\ | |
617 ambiguity between their use as a variable or their use as an inbuilt\n\ | |
7001 | 618 constant. All arguments followed by a parenthesis are considered\n\ |
5034 | 619 to be functions.\n\ |
4933 | 620 \n\ |
621 If the second and subsequent arguments are character strings,\n\ | |
622 they are the names of the arguments of the function.\n\ | |
623 \n\ | |
624 If the second argument is an integer @var{n}, the arguments are\n\ | |
625 @code{\"x\"}, @code{\"P1\"}, @dots{}, @code{\"P@var{N}\"}.\n\ | |
5642 | 626 @seealso{argnames, formula, vectorize}\n\ |
627 @end deftypefn") | |
4933 | 628 { |
629 octave_value retval; | |
630 | |
631 int nargin = args.length (); | |
632 | |
633 if (nargin > 0) | |
634 { | |
635 std::string fun = args(0).string_value (); | |
636 | |
637 if (! error_state) | |
638 { | |
639 string_vector fargs; | |
640 | |
641 if (nargin == 1) | |
642 { | |
5034 | 643 bool is_arg = false; |
5037 | 644 bool in_string = false; |
5034 | 645 std::string tmp_arg; |
646 size_t i = 0; | |
647 | |
648 while (i < fun.length ()) | |
649 { | |
5037 | 650 bool terminate_arg = false; |
5034 | 651 char c = fun[i++]; |
4933 | 652 |
5037 | 653 if (in_string) |
654 { | |
655 if (c == '\'' || c == '\"') | |
656 in_string = false; | |
657 } | |
658 else if (c == '\'' || c == '\"') | |
659 { | |
660 in_string = true; | |
661 if (is_arg) | |
662 terminate_arg = true; | |
663 } | |
664 else if (! isalpha (c) && c != '_') | |
5034 | 665 if (! is_arg) |
666 continue; | |
667 else if (isdigit (c)) | |
5037 | 668 tmp_arg.append (1, c); |
5034 | 669 else |
670 { | |
5037 | 671 // Before we do anything remove trailing whitespaces. |
5034 | 672 while (i < fun.length () && isspace (c)) |
673 c = fun[i++]; | |
5037 | 674 |
5034 | 675 // Do we have a variable or a function? |
676 if (c != '(') | |
5037 | 677 terminate_arg = true; |
678 else | |
5034 | 679 { |
5037 | 680 tmp_arg = std::string (); |
681 is_arg = false; | |
5034 | 682 } |
683 } | |
684 else | |
685 { | |
686 tmp_arg.append (1, c); | |
687 is_arg = true; | |
5037 | 688 } |
5034 | 689 |
5037 | 690 if (terminate_arg || (i == fun.length () && is_arg)) |
691 { | |
692 bool have_arg = false; | |
693 | |
694 for (int j = 0; j < fargs.length (); j++) | |
695 if (tmp_arg == fargs (j)) | |
696 { | |
697 have_arg = true; | |
698 break; | |
699 } | |
5034 | 700 |
7764
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
701 if (! have_arg && tmp_arg != "i" && tmp_arg != "j" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
702 tmp_arg != "NaN" && tmp_arg != "nan" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
703 tmp_arg != "Inf" && tmp_arg != "inf" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
704 tmp_arg != "NA" && tmp_arg != "pi" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
705 tmp_arg != "eps") |
5037 | 706 fargs.append (tmp_arg); |
707 | |
708 tmp_arg = std::string (); | |
709 is_arg = false; | |
5022 | 710 } |
711 } | |
5034 | 712 |
5037 | 713 // Sort the arguments into ascii order. |
8503
8ba2ee57c594
remove qsort in favor of sort
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
714 fargs.sort (); |
4933 | 715 } |
716 else if (nargin == 2 && args(1).is_numeric_type ()) | |
717 { | |
718 int n = args(1).int_value (); | |
719 | |
720 if (! error_state) | |
721 { | |
722 if (n >= 0) | |
723 { | |
724 fargs.resize (n+1); | |
725 | |
726 fargs(0) = "x"; | |
727 | |
728 for (int i = 1; i < n+1; i++) | |
729 { | |
5765 | 730 std::ostringstream buf; |
731 buf << "P" << i; | |
732 fargs(i) = buf.str (); | |
4933 | 733 } |
734 } | |
735 else | |
736 { | |
737 error ("inline: numeric argument must be nonnegative"); | |
738 return retval; | |
739 } | |
740 } | |
741 else | |
742 { | |
743 error ("inline: expecting second argument to be an integer"); | |
744 return retval; | |
745 } | |
746 } | |
747 else | |
748 { | |
749 fargs.resize (nargin - 1); | |
750 | |
751 for (int i = 1; i < nargin; i++) | |
752 { | |
753 std::string s = args(i).string_value (); | |
754 | |
755 if (! error_state) | |
756 fargs(i-1) = s; | |
757 else | |
758 { | |
759 error ("inline: expecting string arguments"); | |
760 return retval; | |
761 } | |
762 } | |
763 } | |
764 | |
765 retval = octave_value (new octave_fcn_inline (fun, fargs)); | |
766 } | |
767 else | |
768 error ("inline: first argument must be a string"); | |
769 } | |
770 else | |
5823 | 771 print_usage (); |
4933 | 772 |
773 return retval; | |
774 } | |
775 | |
7394 | 776 /* |
777 %!shared fn | |
778 %! fn = inline ("x.^2 + 1","x"); | |
779 %!assert (feval (fn, 6), 37) | |
780 %!assert (fn (6), 37) | |
781 */ | |
782 | |
4933 | 783 DEFUN (formula, args, , |
784 "-*- texinfo -*-\n\ | |
785 @deftypefn {Built-in Function} {} formula (@var{fun})\n\ | |
786 Return a character string representing the inline function @var{fun}.\n\ | |
787 Note that @code{char (@var{fun})} is equivalent to\n\ | |
788 @code{formula (@var{fun})}.\n\ | |
5642 | 789 @seealso{argnames, inline, vectorize}\n\ |
790 @end deftypefn") | |
4933 | 791 { |
792 octave_value retval; | |
793 | |
794 int nargin = args.length (); | |
795 | |
796 if (nargin == 1) | |
797 { | |
798 octave_fcn_inline* fn = args(0).fcn_inline_value (true); | |
799 | |
800 if (fn) | |
801 retval = octave_value (fn->fcn_text ()); | |
802 else | |
803 error ("formula: must be an inline function"); | |
804 } | |
805 else | |
5823 | 806 print_usage (); |
4933 | 807 |
808 return retval; | |
809 } | |
810 | |
811 DEFUN (argnames, args, , | |
812 "-*- texinfo -*-\n\ | |
813 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ | |
814 Return a cell array of character strings containing the names of\n\ | |
815 the arguments of the inline function @var{fun}.\n\ | |
6529 | 816 @seealso{inline, formula, vectorize}\n\ |
5642 | 817 @end deftypefn") |
4933 | 818 { |
819 octave_value retval; | |
820 | |
821 int nargin = args.length (); | |
822 | |
823 if (nargin == 1) | |
824 { | |
825 octave_fcn_inline *fn = args(0).fcn_inline_value (true); | |
826 | |
827 if (fn) | |
828 { | |
829 string_vector t1 = fn->fcn_arg_names (); | |
830 | |
831 Cell t2 (dim_vector (t1.length (), 1)); | |
832 | |
833 for (int i = 0; i < t1.length (); i++) | |
834 t2(i) = t1(i); | |
835 | |
836 retval = t2; | |
837 } | |
838 else | |
839 error ("argnames: argument must be an inline function"); | |
840 } | |
841 else | |
5823 | 842 print_usage (); |
4933 | 843 |
844 return retval; | |
845 } | |
846 | |
847 DEFUN (vectorize, args, , | |
848 "-*- texinfo -*-\n\ | |
6635 | 849 @deftypefn {Built-in Function} {} vectorize (@var{fun})\n\ |
4933 | 850 Create a vectorized version of the inline function @var{fun}\n\ |
851 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\ | |
852 @code{.*}, @code{./}, etc.\n\ | |
5642 | 853 @end deftypefn") |
4933 | 854 { |
855 octave_value retval; | |
856 | |
857 int nargin = args.length (); | |
858 | |
859 if (nargin == 1) | |
860 { | |
5409 | 861 std::string old_func; |
862 octave_fcn_inline* old = 0; | |
863 bool func_is_string = true; | |
4933 | 864 |
5409 | 865 if (args(0).is_string ()) |
866 old_func = args(0).string_value (); | |
867 else | |
4933 | 868 { |
5409 | 869 old = args(0).fcn_inline_value (true); |
870 func_is_string = false; | |
871 | |
872 if (old) | |
873 old_func = old->fcn_text (); | |
874 else | |
875 error ("vectorize: must be a string or inline function"); | |
876 } | |
877 | |
878 if (! error_state) | |
879 { | |
4933 | 880 std::string new_func; |
881 size_t i = 0; | |
882 | |
883 while (i < old_func.length ()) | |
884 { | |
885 std::string t1 = old_func.substr (i, 1); | |
886 | |
887 if (t1 == "*" || t1 == "/" || t1 == "\\" || t1 == "^") | |
888 { | |
889 if (i && old_func.substr (i-1, 1) != ".") | |
890 new_func.append ("."); | |
891 | |
892 // Special case for ** operator. | |
4973 | 893 if (t1 == "*" && i < (old_func.length () - 1) |
4933 | 894 && old_func.substr (i+1, 1) == "*") |
895 { | |
896 new_func.append ("*"); | |
897 i++; | |
898 } | |
899 } | |
900 new_func.append (t1); | |
901 i++; | |
902 } | |
903 | |
5409 | 904 if (func_is_string) |
905 retval = octave_value (new_func); | |
906 else | |
907 retval = octave_value (new octave_fcn_inline | |
908 (new_func, old->fcn_arg_names ())); | |
4933 | 909 } |
910 } | |
911 else | |
5823 | 912 print_usage (); |
4933 | 913 |
914 return retval; | |
915 } | |
916 | |
917 /* | |
918 ;;; Local Variables: *** | |
919 ;;; mode: C++ *** | |
920 ;;; End: *** | |
921 */ |