Mercurial > octave-nkf
annotate src/ov.cc @ 7503:8c32f95c2639
convert mapper functions to new format
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 20 Feb 2008 04:22:50 -0500 |
parents | 1e01db14700b |
children | 443a8f5a50fd |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, |
4 2006, 2007 John W. Eaton | |
2376 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2376 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2376 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
4970 | 28 #include "data-conv.h" |
29 #include "quit.h" | |
2942 | 30 #include "str-vec.h" |
2376 | 31 |
2974 | 32 #include "oct-obj.h" |
4944 | 33 #include "oct-stream.h" |
2376 | 34 #include "ov.h" |
35 #include "ov-base.h" | |
2825 | 36 #include "ov-bool.h" |
37 #include "ov-bool-mat.h" | |
3351 | 38 #include "ov-cell.h" |
2376 | 39 #include "ov-scalar.h" |
40 #include "ov-re-mat.h" | |
5164 | 41 #include "ov-bool-sparse.h" |
42 #include "ov-cx-sparse.h" | |
43 #include "ov-re-sparse.h" | |
4901 | 44 #include "ov-int8.h" |
45 #include "ov-int16.h" | |
46 #include "ov-int32.h" | |
47 #include "ov-int64.h" | |
48 #include "ov-uint8.h" | |
49 #include "ov-uint16.h" | |
50 #include "ov-uint32.h" | |
51 #include "ov-uint64.h" | |
2376 | 52 #include "ov-complex.h" |
53 #include "ov-cx-mat.h" | |
54 #include "ov-ch-mat.h" | |
55 #include "ov-str-mat.h" | |
56 #include "ov-range.h" | |
57 #include "ov-struct.h" | |
7336 | 58 #include "ov-class.h" |
4643 | 59 #include "ov-streamoff.h" |
2880 | 60 #include "ov-list.h" |
3977 | 61 #include "ov-cs-list.h" |
2376 | 62 #include "ov-colon.h" |
2974 | 63 #include "ov-builtin.h" |
4649 | 64 #include "ov-dld-fcn.h" |
2974 | 65 #include "ov-usr-fcn.h" |
4342 | 66 #include "ov-fcn-handle.h" |
4966 | 67 #include "ov-fcn-inline.h" |
2376 | 68 #include "ov-typeinfo.h" |
69 | |
70 #include "defun.h" | |
2880 | 71 #include "error.h" |
2376 | 72 #include "gripes.h" |
73 #include "pager.h" | |
4005 | 74 #include "parse.h" |
2376 | 75 #include "pr-output.h" |
7336 | 76 #include "symtab.h" |
2376 | 77 #include "utils.h" |
78 #include "variables.h" | |
79 | |
2477 | 80 // We are likely to have a lot of octave_value objects to allocate, so |
81 // make the grow_size large. | |
3219 | 82 DEFINE_OCTAVE_ALLOCATOR2(octave_value, 1024); |
2477 | 83 |
5775 | 84 // FIXME |
2880 | 85 |
2376 | 86 // Octave's value type. |
87 | |
3536 | 88 std::string |
3203 | 89 octave_value::unary_op_as_string (unary_op op) |
90 { | |
3523 | 91 std::string retval; |
3203 | 92 |
93 switch (op) | |
94 { | |
3533 | 95 case op_not: |
3203 | 96 retval = "!"; |
97 break; | |
98 | |
4965 | 99 case op_uplus: |
100 retval = "+"; | |
101 break; | |
102 | |
3533 | 103 case op_uminus: |
3203 | 104 retval = "-"; |
105 break; | |
106 | |
3533 | 107 case op_transpose: |
3203 | 108 retval = ".'"; |
109 break; | |
110 | |
3533 | 111 case op_hermitian: |
3203 | 112 retval = "'"; |
113 break; | |
114 | |
3533 | 115 case op_incr: |
3203 | 116 retval = "++"; |
117 break; | |
118 | |
3533 | 119 case op_decr: |
3203 | 120 retval = "--"; |
121 break; | |
122 | |
123 default: | |
124 retval = "<unknown>"; | |
125 } | |
126 | |
127 return retval; | |
128 } | |
129 | |
3536 | 130 std::string |
7336 | 131 octave_value::unary_op_fcn_name (unary_op op) |
132 { | |
133 std::string retval; | |
134 | |
135 switch (op) | |
136 { | |
137 case op_not: | |
138 retval = "not"; | |
139 break; | |
140 | |
141 case op_uplus: | |
142 retval = "uplus"; | |
143 break; | |
144 | |
145 case op_uminus: | |
146 retval = "uminus"; | |
147 break; | |
148 | |
149 case op_transpose: | |
150 retval = "transpose"; | |
151 break; | |
152 | |
153 case op_hermitian: | |
154 retval = "ctranspose"; | |
155 break; | |
156 | |
157 default: | |
158 break; | |
159 } | |
160 | |
161 return retval; | |
162 } | |
163 | |
164 std::string | |
2376 | 165 octave_value::binary_op_as_string (binary_op op) |
166 { | |
3523 | 167 std::string retval; |
2376 | 168 |
169 switch (op) | |
170 { | |
3533 | 171 case op_add: |
2376 | 172 retval = "+"; |
173 break; | |
174 | |
3533 | 175 case op_sub: |
2376 | 176 retval = "-"; |
177 break; | |
178 | |
3533 | 179 case op_mul: |
2376 | 180 retval = "*"; |
181 break; | |
182 | |
3533 | 183 case op_div: |
2376 | 184 retval = "/"; |
185 break; | |
186 | |
3533 | 187 case op_pow: |
2376 | 188 retval = "^"; |
189 break; | |
190 | |
3533 | 191 case op_ldiv: |
2376 | 192 retval = "\\"; |
193 break; | |
194 | |
3533 | 195 case op_lshift: |
2903 | 196 retval = "<<"; |
197 break; | |
198 | |
3533 | 199 case op_rshift: |
2903 | 200 retval = ">>"; |
201 break; | |
202 | |
3533 | 203 case op_lt: |
2376 | 204 retval = "<"; |
205 break; | |
206 | |
3533 | 207 case op_le: |
2376 | 208 retval = "<="; |
209 break; | |
210 | |
3533 | 211 case op_eq: |
2376 | 212 retval = "=="; |
213 break; | |
214 | |
3533 | 215 case op_ge: |
2376 | 216 retval = ">="; |
217 break; | |
218 | |
3533 | 219 case op_gt: |
2376 | 220 retval = ">"; |
221 break; | |
222 | |
3533 | 223 case op_ne: |
2376 | 224 retval = "!="; |
225 break; | |
226 | |
3533 | 227 case op_el_mul: |
2376 | 228 retval = ".*"; |
229 break; | |
230 | |
3533 | 231 case op_el_div: |
2376 | 232 retval = "./"; |
233 break; | |
234 | |
3533 | 235 case op_el_pow: |
2376 | 236 retval = ".^"; |
237 break; | |
238 | |
3533 | 239 case op_el_ldiv: |
2376 | 240 retval = ".\\"; |
241 break; | |
242 | |
3533 | 243 case op_el_and: |
2376 | 244 retval = "&"; |
245 break; | |
246 | |
3533 | 247 case op_el_or: |
2376 | 248 retval = "|"; |
249 break; | |
250 | |
3533 | 251 case op_struct_ref: |
2376 | 252 retval = "."; |
253 break; | |
254 | |
255 default: | |
256 retval = "<unknown>"; | |
257 } | |
258 | |
259 return retval; | |
260 } | |
261 | |
3536 | 262 std::string |
7336 | 263 octave_value::binary_op_fcn_name (binary_op op) |
264 { | |
265 std::string retval; | |
266 | |
267 switch (op) | |
268 { | |
269 case op_add: | |
270 retval = "plus"; | |
271 break; | |
272 | |
273 case op_sub: | |
274 retval = "minus"; | |
275 break; | |
276 | |
277 case op_mul: | |
278 retval = "mtimes"; | |
279 break; | |
280 | |
281 case op_div: | |
282 retval = "mrdivide"; | |
283 break; | |
284 | |
285 case op_pow: | |
286 retval = "mpower"; | |
287 break; | |
288 | |
289 case op_ldiv: | |
290 retval = "mldivide"; | |
291 break; | |
292 | |
293 case op_lt: | |
294 retval = "lt"; | |
295 break; | |
296 | |
297 case op_le: | |
298 retval = "le"; | |
299 break; | |
300 | |
301 case op_eq: | |
302 retval = "eq"; | |
303 break; | |
304 | |
305 case op_ge: | |
306 retval = "ge"; | |
307 break; | |
308 | |
309 case op_gt: | |
310 retval = "gt"; | |
311 break; | |
312 | |
313 case op_ne: | |
314 retval = "ne"; | |
315 break; | |
316 | |
317 case op_el_mul: | |
318 retval = "times"; | |
319 break; | |
320 | |
321 case op_el_div: | |
322 retval = "rdivide"; | |
323 break; | |
324 | |
325 case op_el_pow: | |
326 retval = "power"; | |
327 break; | |
328 | |
329 case op_el_ldiv: | |
330 retval = "ldivide"; | |
331 break; | |
332 | |
333 case op_el_and: | |
334 retval = "and"; | |
335 break; | |
336 | |
337 case op_el_or: | |
338 retval = "or"; | |
339 break; | |
340 | |
341 default: | |
342 break; | |
343 } | |
344 | |
345 return retval; | |
346 } | |
347 | |
348 std::string | |
2880 | 349 octave_value::assign_op_as_string (assign_op op) |
350 { | |
3523 | 351 std::string retval; |
2880 | 352 |
353 switch (op) | |
354 { | |
3533 | 355 case op_asn_eq: |
2880 | 356 retval = "="; |
357 break; | |
358 | |
3533 | 359 case op_add_eq: |
2880 | 360 retval = "+="; |
361 break; | |
362 | |
3533 | 363 case op_sub_eq: |
2880 | 364 retval = "-="; |
365 break; | |
366 | |
3533 | 367 case op_mul_eq: |
2880 | 368 retval = "*="; |
369 break; | |
370 | |
3533 | 371 case op_div_eq: |
2880 | 372 retval = "/="; |
373 break; | |
374 | |
3533 | 375 case op_ldiv_eq: |
3204 | 376 retval = "\\="; |
377 break; | |
378 | |
4018 | 379 case op_pow_eq: |
380 retval = "^="; | |
381 break; | |
382 | |
3533 | 383 case op_lshift_eq: |
2903 | 384 retval = "<<="; |
385 break; | |
386 | |
3533 | 387 case op_rshift_eq: |
2903 | 388 retval = ">>="; |
389 break; | |
390 | |
3533 | 391 case op_el_mul_eq: |
2880 | 392 retval = ".*="; |
393 break; | |
394 | |
3533 | 395 case op_el_div_eq: |
2880 | 396 retval = "./="; |
397 break; | |
398 | |
3533 | 399 case op_el_ldiv_eq: |
3204 | 400 retval = ".\\="; |
401 break; | |
402 | |
4018 | 403 case op_el_pow_eq: |
404 retval = ".^="; | |
405 break; | |
406 | |
3533 | 407 case op_el_and_eq: |
2880 | 408 retval = "&="; |
409 break; | |
410 | |
3533 | 411 case op_el_or_eq: |
2880 | 412 retval = "|="; |
413 break; | |
414 | |
415 default: | |
416 retval = "<unknown>"; | |
417 } | |
418 | |
419 return retval; | |
420 } | |
421 | |
5759 | 422 octave_value::octave_value (void) |
423 : rep (new octave_base_value ()) | |
4513 | 424 { |
2825 | 425 } |
2376 | 426 |
4254 | 427 octave_value::octave_value (short int i) |
428 : rep (new octave_scalar (i)) | |
429 { | |
430 } | |
431 | |
432 octave_value::octave_value (unsigned short int i) | |
433 : rep (new octave_scalar (i)) | |
434 { | |
435 } | |
436 | |
4233 | 437 octave_value::octave_value (int i) |
438 : rep (new octave_scalar (i)) | |
439 { | |
440 } | |
441 | |
4254 | 442 octave_value::octave_value (unsigned int i) |
443 : rep (new octave_scalar (i)) | |
444 { | |
445 } | |
446 | |
447 octave_value::octave_value (long int i) | |
448 : rep (new octave_scalar (i)) | |
449 { | |
450 } | |
451 | |
452 octave_value::octave_value (unsigned long int i) | |
453 : rep (new octave_scalar (i)) | |
454 { | |
455 } | |
456 | |
4353 | 457 #if defined (HAVE_LONG_LONG_INT) |
458 octave_value::octave_value (long long int i) | |
459 : rep (new octave_scalar (i)) | |
460 { | |
461 } | |
462 #endif | |
463 | |
4355 | 464 #if defined (HAVE_UNSIGNED_LONG_LONG_INT) |
4353 | 465 octave_value::octave_value (unsigned long long int i) |
466 : rep (new octave_scalar (i)) | |
467 { | |
468 } | |
469 #endif | |
470 | |
4254 | 471 octave_value::octave_value (octave_time t) |
7065 | 472 : rep (new octave_scalar (t.double_value ())) |
4254 | 473 { |
474 } | |
475 | |
2376 | 476 octave_value::octave_value (double d) |
2825 | 477 : rep (new octave_scalar (d)) |
478 { | |
479 } | |
2376 | 480 |
4587 | 481 octave_value::octave_value (const Cell& c, bool is_csl) |
5477 | 482 : rep (is_csl |
5759 | 483 ? dynamic_cast<octave_base_value *> (new octave_cs_list (c)) |
484 : dynamic_cast<octave_base_value *> (new octave_cell (c))) | |
3351 | 485 { |
486 } | |
487 | |
5147 | 488 octave_value::octave_value (const ArrayN<octave_value>& a, bool is_csl) |
5477 | 489 : rep (is_csl |
5759 | 490 ? dynamic_cast<octave_base_value *> (new octave_cs_list (Cell (a))) |
491 : dynamic_cast<octave_base_value *> (new octave_cell (Cell (a)))) | |
5147 | 492 { |
493 } | |
494 | |
5785 | 495 octave_value::octave_value (const Matrix& m, const MatrixType& t) |
496 : rep (new octave_matrix (m, t)) | |
2423 | 497 { |
498 maybe_mutate (); | |
499 } | |
2376 | 500 |
4507 | 501 octave_value::octave_value (const NDArray& a) |
4513 | 502 : rep (new octave_matrix (a)) |
4478 | 503 { |
504 maybe_mutate (); | |
505 } | |
506 | |
4911 | 507 octave_value::octave_value (const ArrayN<double>& a) |
508 : rep (new octave_matrix (a)) | |
509 { | |
510 maybe_mutate (); | |
511 } | |
512 | |
2376 | 513 octave_value::octave_value (const DiagMatrix& d) |
2423 | 514 : rep (new octave_matrix (d)) |
515 { | |
516 maybe_mutate (); | |
517 } | |
2376 | 518 |
3418 | 519 octave_value::octave_value (const RowVector& v) |
520 : rep (new octave_matrix (v)) | |
2423 | 521 { |
522 maybe_mutate (); | |
523 } | |
2376 | 524 |
3418 | 525 octave_value::octave_value (const ColumnVector& v) |
526 : rep (new octave_matrix (v)) | |
2423 | 527 { |
528 maybe_mutate (); | |
529 } | |
2376 | 530 |
531 octave_value::octave_value (const Complex& C) | |
2423 | 532 : rep (new octave_complex (C)) |
533 { | |
534 maybe_mutate (); | |
535 } | |
2376 | 536 |
5785 | 537 octave_value::octave_value (const ComplexMatrix& m, const MatrixType& t) |
538 : rep (new octave_complex_matrix (m, t)) | |
2423 | 539 { |
540 maybe_mutate (); | |
541 } | |
2376 | 542 |
4513 | 543 octave_value::octave_value (const ComplexNDArray& a) |
544 : rep (new octave_complex_matrix (a)) | |
4478 | 545 { |
546 maybe_mutate (); | |
547 } | |
548 | |
4911 | 549 octave_value::octave_value (const ArrayN<Complex>& a) |
550 : rep (new octave_complex_matrix (a)) | |
551 { | |
552 maybe_mutate (); | |
553 } | |
554 | |
2376 | 555 octave_value::octave_value (const ComplexDiagMatrix& d) |
2423 | 556 : rep (new octave_complex_matrix (d)) |
557 { | |
558 maybe_mutate (); | |
559 } | |
2376 | 560 |
3418 | 561 octave_value::octave_value (const ComplexRowVector& v) |
562 : rep (new octave_complex_matrix (v)) | |
2423 | 563 { |
564 maybe_mutate (); | |
565 } | |
2376 | 566 |
3418 | 567 octave_value::octave_value (const ComplexColumnVector& v) |
568 : rep (new octave_complex_matrix (v)) | |
2423 | 569 { |
570 maybe_mutate (); | |
571 } | |
2376 | 572 |
2825 | 573 octave_value::octave_value (bool b) |
574 : rep (new octave_bool (b)) | |
575 { | |
576 } | |
577 | |
5785 | 578 octave_value::octave_value (const boolMatrix& bm, const MatrixType& t) |
579 : rep (new octave_bool_matrix (bm, t)) | |
2825 | 580 { |
581 maybe_mutate (); | |
582 } | |
583 | |
4513 | 584 octave_value::octave_value (const boolNDArray& bnda) |
585 : rep (new octave_bool_matrix (bnda)) | |
586 { | |
587 maybe_mutate (); | |
588 } | |
589 | |
7433 | 590 octave_value::octave_value (const ArrayN<bool>& bnda) |
591 : rep (new octave_bool_matrix (bnda)) | |
592 { | |
593 maybe_mutate (); | |
594 } | |
595 | |
5279 | 596 octave_value::octave_value (char c, char type) |
597 : rep (type == '"' | |
598 ? new octave_char_matrix_dq_str (c) | |
599 : new octave_char_matrix_sq_str (c)) | |
3189 | 600 { |
601 maybe_mutate (); | |
602 } | |
603 | |
5279 | 604 octave_value::octave_value (const char *s, char type) |
605 : rep (type == '"' | |
606 ? new octave_char_matrix_dq_str (s) | |
607 : new octave_char_matrix_sq_str (s)) | |
2423 | 608 { |
609 maybe_mutate (); | |
610 } | |
2376 | 611 |
5279 | 612 octave_value::octave_value (const std::string& s, char type) |
613 : rep (type == '"' | |
614 ? new octave_char_matrix_dq_str (s) | |
615 : new octave_char_matrix_sq_str (s)) | |
2423 | 616 { |
617 maybe_mutate (); | |
618 } | |
2376 | 619 |
5279 | 620 octave_value::octave_value (const string_vector& s, char type) |
621 : rep (type == '"' | |
622 ? new octave_char_matrix_dq_str (s) | |
623 : new octave_char_matrix_sq_str (s)) | |
2423 | 624 { |
625 maybe_mutate (); | |
626 } | |
2376 | 627 |
5279 | 628 octave_value::octave_value (const charMatrix& chm, bool is_str, char type) |
4587 | 629 : rep (is_str |
5279 | 630 ? (type == '"' |
631 ? new octave_char_matrix_dq_str (chm) | |
632 : new octave_char_matrix_sq_str (chm)) | |
4513 | 633 : new octave_char_matrix (chm)) |
2409 | 634 { |
4513 | 635 maybe_mutate (); |
636 } | |
2376 | 637 |
5279 | 638 octave_value::octave_value (const charNDArray& chm, bool is_str, char type) |
4587 | 639 : rep (is_str |
5279 | 640 ? (type == '"' |
641 ? new octave_char_matrix_dq_str (chm) | |
642 : new octave_char_matrix_sq_str (chm)) | |
4513 | 643 : new octave_char_matrix (chm)) |
644 { | |
2423 | 645 maybe_mutate (); |
2409 | 646 } |
2376 | 647 |
5279 | 648 octave_value::octave_value (const ArrayN<char>& chm, bool is_str, char type) |
4997 | 649 : rep (is_str |
5279 | 650 ? (type == '"' |
651 ? new octave_char_matrix_dq_str (chm) | |
652 : new octave_char_matrix_sq_str (chm)) | |
4997 | 653 : new octave_char_matrix (chm)) |
654 { | |
655 maybe_mutate (); | |
656 } | |
657 | |
5785 | 658 octave_value::octave_value (const SparseMatrix& m, const MatrixType &t) |
5164 | 659 : rep (new octave_sparse_matrix (m, t)) |
660 { | |
661 maybe_mutate (); | |
662 } | |
663 | |
6863 | 664 octave_value::octave_value (const Sparse<double>& m, const MatrixType &t) |
665 : rep (new octave_sparse_matrix (m, t)) | |
666 { | |
667 maybe_mutate (); | |
668 } | |
669 | |
5785 | 670 octave_value::octave_value (const SparseComplexMatrix& m, const MatrixType &t) |
5164 | 671 : rep (new octave_sparse_complex_matrix (m, t)) |
672 { | |
673 maybe_mutate (); | |
674 } | |
675 | |
6863 | 676 octave_value::octave_value (const Sparse<Complex>& m, const MatrixType &t) |
677 : rep (new octave_sparse_complex_matrix (m, t)) | |
678 { | |
679 maybe_mutate (); | |
680 } | |
681 | |
5785 | 682 octave_value::octave_value (const SparseBoolMatrix& bm, const MatrixType &t) |
5164 | 683 : rep (new octave_sparse_bool_matrix (bm, t)) |
684 { | |
685 maybe_mutate (); | |
686 } | |
687 | |
7433 | 688 octave_value::octave_value (const Sparse<bool>& bm, const MatrixType &t) |
689 : rep (new octave_sparse_bool_matrix (bm, t)) | |
690 { | |
691 maybe_mutate (); | |
692 } | |
693 | |
4901 | 694 octave_value::octave_value (const octave_int8& i) |
695 : rep (new octave_int8_scalar (i)) | |
696 { | |
697 maybe_mutate (); | |
698 } | |
699 | |
700 octave_value::octave_value (const octave_uint8& i) | |
701 : rep (new octave_uint8_scalar (i)) | |
702 { | |
703 maybe_mutate (); | |
704 } | |
705 | |
706 octave_value::octave_value (const octave_int16& i) | |
707 : rep (new octave_int16_scalar (i)) | |
708 { | |
709 maybe_mutate (); | |
710 } | |
711 | |
712 octave_value::octave_value (const octave_uint16& i) | |
713 : rep (new octave_uint16_scalar (i)) | |
714 { | |
715 maybe_mutate (); | |
716 } | |
717 | |
718 octave_value::octave_value (const octave_int32& i) | |
719 : rep (new octave_int32_scalar (i)) | |
720 { | |
721 maybe_mutate (); | |
722 } | |
723 | |
724 octave_value::octave_value (const octave_uint32& i) | |
725 : rep (new octave_uint32_scalar (i)) | |
726 { | |
727 maybe_mutate (); | |
728 } | |
729 | |
730 octave_value::octave_value (const octave_int64& i) | |
731 : rep (new octave_int64_scalar (i)) | |
732 { | |
733 maybe_mutate (); | |
734 } | |
735 | |
736 octave_value::octave_value (const octave_uint64& i) | |
737 : rep (new octave_uint64_scalar (i)) | |
738 { | |
739 maybe_mutate (); | |
740 } | |
741 | |
742 octave_value::octave_value (const int8NDArray& inda) | |
743 : rep (new octave_int8_matrix (inda)) | |
744 { | |
745 maybe_mutate (); | |
746 } | |
747 | |
7064 | 748 octave_value::octave_value (const ArrayN<octave_int8>& inda) |
749 : rep (new octave_int8_matrix (inda)) | |
750 { | |
751 maybe_mutate (); | |
752 } | |
753 | |
4901 | 754 octave_value::octave_value (const uint8NDArray& inda) |
755 : rep (new octave_uint8_matrix (inda)) | |
756 { | |
757 maybe_mutate (); | |
758 } | |
759 | |
7064 | 760 octave_value::octave_value (const ArrayN<octave_uint8>& inda) |
761 : rep (new octave_uint8_matrix (inda)) | |
762 { | |
763 maybe_mutate (); | |
764 } | |
765 | |
4901 | 766 octave_value::octave_value (const int16NDArray& inda) |
767 : rep (new octave_int16_matrix (inda)) | |
768 { | |
769 maybe_mutate (); | |
770 } | |
771 | |
7064 | 772 octave_value::octave_value (const ArrayN<octave_int16>& inda) |
773 : rep (new octave_int16_matrix (inda)) | |
774 { | |
775 maybe_mutate (); | |
776 } | |
777 | |
4901 | 778 octave_value::octave_value (const uint16NDArray& inda) |
779 : rep (new octave_uint16_matrix (inda)) | |
780 { | |
781 maybe_mutate (); | |
782 } | |
783 | |
7064 | 784 octave_value::octave_value (const ArrayN<octave_uint16>& inda) |
785 : rep (new octave_uint16_matrix (inda)) | |
786 { | |
787 maybe_mutate (); | |
788 } | |
789 | |
4901 | 790 octave_value::octave_value (const int32NDArray& inda) |
791 : rep (new octave_int32_matrix (inda)) | |
792 { | |
793 maybe_mutate (); | |
794 } | |
795 | |
7064 | 796 octave_value::octave_value (const ArrayN<octave_int32>& inda) |
797 : rep (new octave_int32_matrix (inda)) | |
798 { | |
799 maybe_mutate (); | |
800 } | |
801 | |
4901 | 802 octave_value::octave_value (const uint32NDArray& inda) |
803 : rep (new octave_uint32_matrix (inda)) | |
804 { | |
805 maybe_mutate (); | |
806 } | |
807 | |
7064 | 808 octave_value::octave_value (const ArrayN<octave_uint32>& inda) |
809 : rep (new octave_uint32_matrix (inda)) | |
810 { | |
811 maybe_mutate (); | |
812 } | |
813 | |
4901 | 814 octave_value::octave_value (const int64NDArray& inda) |
815 : rep (new octave_int64_matrix (inda)) | |
816 { | |
817 maybe_mutate (); | |
818 } | |
819 | |
7064 | 820 octave_value::octave_value (const ArrayN<octave_int64>& inda) |
821 : rep (new octave_int64_matrix (inda)) | |
822 { | |
823 maybe_mutate (); | |
824 } | |
825 | |
4901 | 826 octave_value::octave_value (const uint64NDArray& inda) |
827 : rep (new octave_uint64_matrix (inda)) | |
828 { | |
829 maybe_mutate (); | |
830 } | |
831 | |
7064 | 832 octave_value::octave_value (const ArrayN<octave_uint64>& inda) |
833 : rep (new octave_uint64_matrix (inda)) | |
834 { | |
835 maybe_mutate (); | |
836 } | |
837 | |
2376 | 838 octave_value::octave_value (double base, double limit, double inc) |
2423 | 839 : rep (new octave_range (base, limit, inc)) |
840 { | |
841 maybe_mutate (); | |
842 } | |
2376 | 843 |
844 octave_value::octave_value (const Range& r) | |
2423 | 845 : rep (new octave_range (r)) |
846 { | |
847 maybe_mutate (); | |
848 } | |
2376 | 849 |
850 octave_value::octave_value (const Octave_map& m) | |
2825 | 851 : rep (new octave_struct (m)) |
852 { | |
2880 | 853 } |
854 | |
7336 | 855 octave_value::octave_value (const Octave_map& m, const std::string& id) |
856 : rep (new octave_class (m, id)) | |
857 { | |
858 } | |
859 | |
4643 | 860 octave_value::octave_value (const streamoff_array& off) |
861 : rep (new octave_streamoff (off)) | |
862 { | |
863 } | |
864 | |
7433 | 865 octave_value::octave_value (const ArrayN<std::streamoff>& inda) |
866 : rep (new octave_streamoff (inda)) | |
867 { | |
868 } | |
869 | |
4587 | 870 octave_value::octave_value (const octave_value_list& l, bool is_csl) |
5477 | 871 : rep (is_csl |
5759 | 872 ? dynamic_cast<octave_base_value *> (new octave_cs_list (l)) |
873 : dynamic_cast<octave_base_value *> (new octave_list (l))) | |
2880 | 874 { |
875 } | |
2376 | 876 |
877 octave_value::octave_value (octave_value::magic_colon) | |
2825 | 878 : rep (new octave_magic_colon ()) |
879 { | |
880 } | |
2376 | 881 |
5759 | 882 octave_value::octave_value (octave_base_value *new_rep) |
2825 | 883 : rep (new_rep) |
884 { | |
885 } | |
2376 | 886 |
7336 | 887 octave_value::octave_value (octave_base_value *new_rep, int xcount) |
888 : rep (new_rep) | |
889 { | |
890 rep->count = xcount; | |
891 } | |
892 | |
5759 | 893 octave_base_value * |
3933 | 894 octave_value::clone (void) const |
2880 | 895 { |
896 panic_impossible (); | |
3546 | 897 return 0; |
2880 | 898 } |
899 | |
2409 | 900 void |
901 octave_value::maybe_mutate (void) | |
902 { | |
5759 | 903 octave_base_value *tmp = rep->try_narrowing_conversion (); |
2409 | 904 |
905 if (tmp && tmp != rep) | |
906 { | |
907 if (--rep->count == 0) | |
908 delete rep; | |
909 | |
910 rep = tmp; | |
911 } | |
912 } | |
913 | |
4247 | 914 octave_value |
4271 | 915 octave_value::single_subsref (const std::string& type, |
916 const octave_value_list& idx) | |
4247 | 917 { |
918 std::list<octave_value_list> i; | |
919 | |
920 i.push_back (idx); | |
921 | |
922 return rep->subsref (type, i); | |
923 } | |
924 | |
2974 | 925 octave_value_list |
4247 | 926 octave_value::subsref (const std::string& type, |
4219 | 927 const std::list<octave_value_list>& idx, int nargout) |
3933 | 928 { |
929 if (is_constant ()) | |
930 return rep->subsref (type, idx); | |
931 else | |
932 return rep->subsref (type, idx, nargout); | |
933 } | |
934 | |
935 octave_value | |
4247 | 936 octave_value::next_subsref (const std::string& type, |
4219 | 937 const std::list<octave_value_list>& idx, |
4233 | 938 size_t skip) |
3933 | 939 { |
4582 | 940 if (! error_state && idx.size () > skip) |
3933 | 941 { |
4219 | 942 std::list<octave_value_list> new_idx (idx); |
4233 | 943 for (size_t i = 0; i < skip; i++) |
4219 | 944 new_idx.erase (new_idx.begin ()); |
3933 | 945 return subsref (type.substr (skip), new_idx); |
946 } | |
947 else | |
948 return *this; | |
949 } | |
950 | |
951 octave_value_list | |
4994 | 952 octave_value::next_subsref (int nargout, const std::string& type, |
953 const std::list<octave_value_list>& idx, | |
954 size_t skip) | |
955 { | |
956 if (! error_state && idx.size () > skip) | |
957 { | |
958 std::list<octave_value_list> new_idx (idx); | |
959 for (size_t i = 0; i < skip; i++) | |
960 new_idx.erase (new_idx.begin ()); | |
961 return subsref (type.substr (skip), new_idx, nargout); | |
962 } | |
963 else | |
964 return *this; | |
965 } | |
966 | |
967 octave_value_list | |
3544 | 968 octave_value::do_multi_index_op (int nargout, const octave_value_list& idx) |
2974 | 969 { |
3544 | 970 return rep->do_multi_index_op (nargout, idx); |
2974 | 971 } |
972 | |
3933 | 973 #if 0 |
3204 | 974 static void |
3933 | 975 gripe_assign_failed (const std::string& on, const std::string& tn1, |
976 const std::string& tn2) | |
3204 | 977 { |
978 error ("assignment failed for `%s %s %s'", | |
979 tn1.c_str (), on.c_str (), tn2.c_str ()); | |
980 } | |
3933 | 981 #endif |
3204 | 982 |
983 static void | |
3933 | 984 gripe_assign_failed_or_no_method (const std::string& on, |
985 const std::string& tn1, | |
3523 | 986 const std::string& tn2) |
3204 | 987 { |
988 error ("assignment failed, or no method for `%s %s %s'", | |
989 tn1.c_str (), on.c_str (), tn2.c_str ()); | |
990 } | |
991 | |
3933 | 992 octave_value |
4247 | 993 octave_value::subsasgn (const std::string& type, |
4219 | 994 const std::list<octave_value_list>& idx, |
3933 | 995 const octave_value& rhs) |
996 { | |
997 return rep->subsasgn (type, idx, rhs); | |
998 } | |
999 | |
1000 octave_value | |
4247 | 1001 octave_value::assign (assign_op op, const std::string& type, |
4219 | 1002 const std::list<octave_value_list>& idx, |
3933 | 1003 const octave_value& rhs) |
1004 { | |
1005 octave_value retval; | |
1006 | |
1007 make_unique (); | |
1008 | |
1009 octave_value t_rhs = rhs; | |
1010 | |
1011 if (op != op_asn_eq) | |
1012 { | |
5775 | 1013 // FIXME -- only do the following stuff if we can't find |
3933 | 1014 // a specific function to call to handle the op= operation for |
1015 // the types we have. | |
1016 | |
5001 | 1017 octave_value t; |
1018 if (is_constant ()) | |
1019 t = subsref (type, idx); | |
1020 else | |
1021 { | |
1022 octave_value_list tl = subsref (type, idx, 1); | |
1023 if (tl.length () > 0) | |
1024 t = tl(0); | |
1025 } | |
3933 | 1026 |
1027 if (! error_state) | |
1028 { | |
1029 binary_op binop = op_eq_to_binary_op (op); | |
1030 | |
1031 if (! error_state) | |
1032 t_rhs = do_binary_op (binop, t, rhs); | |
1033 } | |
1034 } | |
1035 | |
1036 if (! error_state) | |
1037 { | |
7336 | 1038 if (type[0] == '.' && ! (is_map () || is_object ())) |
3933 | 1039 { |
1040 octave_value tmp = Octave_map (); | |
1041 retval = tmp.subsasgn (type, idx, t_rhs); | |
1042 } | |
1043 else | |
1044 retval = subsasgn (type, idx, t_rhs); | |
1045 } | |
1046 | |
1047 if (error_state) | |
1048 gripe_assign_failed_or_no_method (assign_op_as_string (op), | |
1049 type_name (), rhs.type_name ()); | |
1050 | |
1051 return retval; | |
1052 } | |
1053 | |
1054 const octave_value& | |
3203 | 1055 octave_value::assign (assign_op op, const octave_value& rhs) |
2880 | 1056 { |
3533 | 1057 if (op == op_asn_eq) |
3203 | 1058 operator = (rhs); |
1059 else | |
1060 { | |
5775 | 1061 // FIXME -- only do the following stuff if we can't find |
3204 | 1062 // a specific function to call to handle the op= operation for |
1063 // the types we have. | |
1064 | |
1065 binary_op binop = op_eq_to_binary_op (op); | |
1066 | |
1067 if (! error_state) | |
1068 { | |
1069 octave_value t = do_binary_op (binop, *this, rhs); | |
1070 | |
1071 if (! error_state) | |
1072 operator = (t); | |
1073 } | |
1074 | |
1075 if (error_state) | |
1076 gripe_assign_failed_or_no_method (assign_op_as_string (op), | |
1077 type_name (), rhs.type_name ()); | |
1078 } | |
1079 | |
3933 | 1080 return *this; |
2376 | 1081 } |
1082 | |
5275 | 1083 octave_idx_type |
4563 | 1084 octave_value::length (void) const |
1085 { | |
1086 int retval = 0; | |
1087 | |
1088 dim_vector dv = dims (); | |
4584 | 1089 |
4563 | 1090 for (int i = 0; i < dv.length (); i++) |
1091 { | |
1092 if (dv(i) < 0) | |
1093 { | |
1094 retval = -1; | |
1095 break; | |
1096 } | |
1097 | |
4584 | 1098 if (dv(i) == 0) |
1099 { | |
1100 retval = 0; | |
1101 break; | |
1102 } | |
1103 | |
4563 | 1104 if (dv(i) > retval) |
1105 retval = dv(i); | |
1106 } | |
1107 | |
1108 return retval; | |
1109 } | |
1110 | |
5659 | 1111 Matrix |
1112 octave_value::size (void) const | |
1113 { | |
1114 dim_vector dv = dims (); | |
1115 | |
1116 int n_dims = dv.length (); | |
1117 | |
1118 Matrix retval (1, n_dims); | |
1119 | |
1120 while (n_dims--) | |
1121 retval(n_dims) = dv(n_dims); | |
1122 | |
1123 return retval; | |
1124 } | |
1125 | |
3351 | 1126 Cell |
1127 octave_value::cell_value (void) const | |
1128 { | |
1129 return rep->cell_value (); | |
1130 } | |
1131 | |
2376 | 1132 Octave_map |
1133 octave_value::map_value (void) const | |
1134 { | |
1135 return rep->map_value (); | |
1136 } | |
1137 | |
4645 | 1138 std::streamoff |
4643 | 1139 octave_value::streamoff_value (void) const |
1140 { | |
1141 return rep->streamoff_value (); | |
1142 } | |
1143 | |
4645 | 1144 streamoff_array |
1145 octave_value::streamoff_array_value (void) const | |
1146 { | |
1147 return rep->streamoff_array_value (); | |
1148 } | |
1149 | |
2974 | 1150 octave_function * |
1151 octave_value::function_value (bool silent) | |
1152 { | |
1153 return rep->function_value (silent); | |
1154 } | |
1155 | |
4700 | 1156 octave_user_function * |
1157 octave_value::user_function_value (bool silent) | |
1158 { | |
1159 return rep->user_function_value (silent); | |
1160 } | |
1161 | |
4346 | 1162 octave_fcn_handle * |
4343 | 1163 octave_value::fcn_handle_value (bool silent) |
1164 { | |
1165 return rep->fcn_handle_value (silent); | |
1166 } | |
1167 | |
4933 | 1168 octave_fcn_inline * |
1169 octave_value::fcn_inline_value (bool silent) | |
1170 { | |
1171 return rep->fcn_inline_value (silent); | |
1172 } | |
1173 | |
2880 | 1174 octave_value_list |
1175 octave_value::list_value (void) const | |
1176 { | |
1177 return rep->list_value (); | |
1178 } | |
1179 | |
2376 | 1180 ColumnVector |
3419 | 1181 octave_value::column_vector_value (bool force_string_conv, |
4661 | 1182 bool /* frc_vec_conv */) const |
3419 | 1183 { |
1184 ColumnVector retval; | |
1185 | |
1186 Matrix m = matrix_value (force_string_conv); | |
1187 | |
1188 if (error_state) | |
1189 return retval; | |
1190 | |
5275 | 1191 octave_idx_type nr = m.rows (); |
1192 octave_idx_type nc = m.columns (); | |
3419 | 1193 |
1194 if (nc == 1) | |
1195 { | |
1196 retval.resize (nr); | |
5275 | 1197 for (octave_idx_type i = 0; i < nr; i++) |
3419 | 1198 retval (i) = m (i, 0); |
1199 } | |
1200 else | |
1201 { | |
3523 | 1202 std::string tn = type_name (); |
3419 | 1203 gripe_invalid_conversion (tn.c_str (), "real column vector"); |
1204 } | |
1205 | |
1206 return retval; | |
1207 } | |
1208 | |
1209 ComplexColumnVector | |
1210 octave_value::complex_column_vector_value (bool force_string_conv, | |
4661 | 1211 bool /* frc_vec_conv */) const |
3419 | 1212 { |
1213 ComplexColumnVector retval; | |
1214 | |
1215 ComplexMatrix m = complex_matrix_value (force_string_conv); | |
1216 | |
1217 if (error_state) | |
1218 return retval; | |
1219 | |
5275 | 1220 octave_idx_type nr = m.rows (); |
1221 octave_idx_type nc = m.columns (); | |
3419 | 1222 |
1223 if (nc == 1) | |
1224 { | |
3465 | 1225 retval.resize (nr); |
5275 | 1226 for (octave_idx_type i = 0; i < nr; i++) |
3419 | 1227 retval (i) = m (i, 0); |
1228 } | |
1229 else | |
1230 { | |
3523 | 1231 std::string tn = type_name (); |
3419 | 1232 gripe_invalid_conversion (tn.c_str (), "complex column vector"); |
1233 } | |
1234 | |
1235 return retval; | |
1236 } | |
1237 | |
1238 RowVector | |
1239 octave_value::row_vector_value (bool force_string_conv, | |
4661 | 1240 bool /* frc_vec_conv */) const |
3419 | 1241 { |
1242 RowVector retval; | |
1243 | |
1244 Matrix m = matrix_value (force_string_conv); | |
1245 | |
1246 if (error_state) | |
1247 return retval; | |
1248 | |
5275 | 1249 octave_idx_type nr = m.rows (); |
1250 octave_idx_type nc = m.columns (); | |
3419 | 1251 |
1252 if (nr == 1) | |
1253 { | |
1254 retval.resize (nc); | |
5275 | 1255 for (octave_idx_type i = 0; i < nc; i++) |
3419 | 1256 retval (i) = m (0, i); |
1257 } | |
1258 else | |
1259 { | |
3523 | 1260 std::string tn = type_name (); |
3419 | 1261 gripe_invalid_conversion (tn.c_str (), "real row vector"); |
1262 } | |
1263 | |
1264 return retval; | |
1265 } | |
1266 | |
1267 ComplexRowVector | |
1268 octave_value::complex_row_vector_value (bool force_string_conv, | |
4661 | 1269 bool /* frc_vec_conv */) const |
3419 | 1270 { |
1271 ComplexRowVector retval; | |
1272 | |
1273 ComplexMatrix m = complex_matrix_value (force_string_conv); | |
1274 | |
1275 if (error_state) | |
1276 return retval; | |
1277 | |
5275 | 1278 octave_idx_type nr = m.rows (); |
1279 octave_idx_type nc = m.columns (); | |
3419 | 1280 |
1281 if (nr == 1) | |
1282 { | |
1283 retval.resize (nc); | |
5275 | 1284 for (octave_idx_type i = 0; i < nc; i++) |
3419 | 1285 retval (i) = m (0, i); |
1286 } | |
1287 else | |
1288 { | |
3523 | 1289 std::string tn = type_name (); |
3419 | 1290 gripe_invalid_conversion (tn.c_str (), "complex row vector"); |
1291 } | |
1292 | |
1293 return retval; | |
1294 } | |
1295 | |
1296 // Sloppy... | |
1297 | |
1298 Array<double> | |
2376 | 1299 octave_value::vector_value (bool force_string_conv, |
1300 bool force_vector_conversion) const | |
1301 { | |
3419 | 1302 Array<double> retval; |
2376 | 1303 |
1304 Matrix m = matrix_value (force_string_conv); | |
1305 | |
1306 if (error_state) | |
1307 return retval; | |
1308 | |
5275 | 1309 octave_idx_type nr = m.rows (); |
1310 octave_idx_type nc = m.columns (); | |
2376 | 1311 |
1312 if (nr == 1) | |
1313 { | |
1314 retval.resize (nc); | |
5275 | 1315 for (octave_idx_type i = 0; i < nc; i++) |
2376 | 1316 retval (i) = m (0, i); |
1317 } | |
1318 else if (nc == 1) | |
1319 { | |
1320 retval.resize (nr); | |
5275 | 1321 for (octave_idx_type i = 0; i < nr; i++) |
2376 | 1322 retval (i) = m (i, 0); |
1323 } | |
4455 | 1324 else if (nr > 0 && nc > 0) |
2376 | 1325 { |
5781 | 1326 if (! force_vector_conversion) |
1327 gripe_implicit_conversion ("Octave:array-as-vector", | |
1328 type_name (), "real vector"); | |
4455 | 1329 |
2376 | 1330 retval.resize (nr * nc); |
5275 | 1331 octave_idx_type k = 0; |
1332 for (octave_idx_type j = 0; j < nc; j++) | |
1333 for (octave_idx_type i = 0; i < nr; i++) | |
4153 | 1334 { |
1335 OCTAVE_QUIT; | |
1336 | |
1337 retval (k++) = m (i, j); | |
1338 } | |
2376 | 1339 } |
1340 else | |
1341 { | |
3523 | 1342 std::string tn = type_name (); |
2376 | 1343 gripe_invalid_conversion (tn.c_str (), "real vector"); |
1344 } | |
1345 | |
1346 return retval; | |
1347 } | |
1348 | |
4044 | 1349 Array<int> |
1350 octave_value::int_vector_value (bool force_string_conv, bool require_int, | |
1351 bool force_vector_conversion) const | |
1352 { | |
1353 Array<int> retval; | |
1354 | |
1355 Matrix m = matrix_value (force_string_conv); | |
1356 | |
1357 if (error_state) | |
1358 return retval; | |
1359 | |
5275 | 1360 octave_idx_type nr = m.rows (); |
1361 octave_idx_type nc = m.columns (); | |
4044 | 1362 |
1363 if (nr == 1) | |
1364 { | |
1365 retval.resize (nc); | |
5275 | 1366 for (octave_idx_type i = 0; i < nc; i++) |
4044 | 1367 { |
4153 | 1368 OCTAVE_QUIT; |
1369 | |
4044 | 1370 double d = m (0, i); |
1371 | |
1372 if (require_int && D_NINT (d) != d) | |
1373 { | |
1374 error ("conversion to integer value failed"); | |
1375 return retval; | |
1376 } | |
1377 | |
1378 retval (i) = static_cast<int> (d); | |
1379 } | |
1380 } | |
1381 else if (nc == 1) | |
1382 { | |
1383 retval.resize (nr); | |
5275 | 1384 for (octave_idx_type i = 0; i < nr; i++) |
4044 | 1385 { |
4153 | 1386 OCTAVE_QUIT; |
1387 | |
4044 | 1388 double d = m (i, 0); |
1389 | |
1390 if (require_int && D_NINT (d) != d) | |
1391 { | |
1392 error ("conversion to integer value failed"); | |
1393 return retval; | |
1394 } | |
1395 | |
1396 retval (i) = static_cast<int> (d); | |
1397 } | |
1398 } | |
4455 | 1399 else if (nr > 0 && nc > 0) |
4044 | 1400 { |
5781 | 1401 if (! force_vector_conversion) |
1402 gripe_implicit_conversion ("Octave:array-as-vector", | |
1403 type_name (), "real vector"); | |
4455 | 1404 |
4044 | 1405 retval.resize (nr * nc); |
5275 | 1406 octave_idx_type k = 0; |
1407 for (octave_idx_type j = 0; j < nc; j++) | |
4044 | 1408 { |
5275 | 1409 for (octave_idx_type i = 0; i < nr; i++) |
4044 | 1410 { |
4153 | 1411 OCTAVE_QUIT; |
1412 | |
4044 | 1413 double d = m (i, j); |
1414 | |
1415 if (require_int && D_NINT (d) != d) | |
1416 { | |
1417 error ("conversion to integer value failed"); | |
1418 return retval; | |
1419 } | |
1420 | |
1421 retval (k++) = static_cast<int> (d); | |
1422 } | |
1423 } | |
1424 } | |
1425 else | |
1426 { | |
1427 std::string tn = type_name (); | |
1428 gripe_invalid_conversion (tn.c_str (), "real vector"); | |
1429 } | |
1430 | |
1431 return retval; | |
1432 } | |
1433 | |
3419 | 1434 Array<Complex> |
2376 | 1435 octave_value::complex_vector_value (bool force_string_conv, |
1436 bool force_vector_conversion) const | |
1437 { | |
3419 | 1438 Array<Complex> retval; |
2376 | 1439 |
1440 ComplexMatrix m = complex_matrix_value (force_string_conv); | |
1441 | |
1442 if (error_state) | |
1443 return retval; | |
1444 | |
5275 | 1445 octave_idx_type nr = m.rows (); |
1446 octave_idx_type nc = m.columns (); | |
2376 | 1447 |
1448 if (nr == 1) | |
1449 { | |
1450 retval.resize (nc); | |
5275 | 1451 for (octave_idx_type i = 0; i < nc; i++) |
4153 | 1452 { |
1453 OCTAVE_QUIT; | |
1454 retval (i) = m (0, i); | |
1455 } | |
2376 | 1456 } |
1457 else if (nc == 1) | |
1458 { | |
1459 retval.resize (nr); | |
5275 | 1460 for (octave_idx_type i = 0; i < nr; i++) |
4153 | 1461 { |
1462 OCTAVE_QUIT; | |
1463 retval (i) = m (i, 0); | |
1464 } | |
2376 | 1465 } |
4455 | 1466 else if (nr > 0 && nc > 0) |
2376 | 1467 { |
5781 | 1468 if (! force_vector_conversion) |
1469 gripe_implicit_conversion ("Octave:array-as-vector", | |
1470 type_name (), "complex vector"); | |
4455 | 1471 |
2376 | 1472 retval.resize (nr * nc); |
5275 | 1473 octave_idx_type k = 0; |
1474 for (octave_idx_type j = 0; j < nc; j++) | |
1475 for (octave_idx_type i = 0; i < nr; i++) | |
4153 | 1476 { |
1477 OCTAVE_QUIT; | |
1478 retval (k++) = m (i, j); | |
1479 } | |
2376 | 1480 } |
1481 else | |
1482 { | |
3523 | 1483 std::string tn = type_name (); |
2376 | 1484 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
1485 } | |
1486 | |
1487 return retval; | |
1488 } | |
1489 | |
4944 | 1490 int |
1491 octave_value::write (octave_stream& os, int block_size, | |
1492 oct_data_conv::data_type output_type, int skip, | |
1493 oct_mach_info::float_format flt_fmt) const | |
1494 { | |
1495 return rep->write (os, block_size, output_type, skip, flt_fmt); | |
1496 } | |
1497 | |
2413 | 1498 static void |
3933 | 1499 gripe_binary_op (const std::string& on, const std::string& tn1, |
1500 const std::string& tn2) | |
2376 | 1501 { |
2903 | 1502 error ("binary operator `%s' not implemented for `%s' by `%s' operations", |
2376 | 1503 on.c_str (), tn1.c_str (), tn2.c_str ()); |
1504 } | |
1505 | |
3203 | 1506 static void |
3523 | 1507 gripe_binary_op_conv (const std::string& on) |
3203 | 1508 { |
1509 error ("type conversion failed for binary operator `%s'", on.c_str ()); | |
1510 } | |
1511 | |
2376 | 1512 octave_value |
3933 | 1513 do_binary_op (octave_value::binary_op op, |
1514 const octave_value& v1, const octave_value& v2) | |
2376 | 1515 { |
1516 octave_value retval; | |
1517 | |
1518 int t1 = v1.type_id (); | |
1519 int t2 = v2.type_id (); | |
1520 | |
7336 | 1521 if (t1 == octave_class::static_type_id () |
1522 || t2 == octave_class::static_type_id ()) | |
1523 { | |
1524 octave_value_typeinfo::binary_class_op_fcn f | |
1525 = octave_value_typeinfo::lookup_binary_class_op (op); | |
2376 | 1526 |
7336 | 1527 if (f) |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1528 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1529 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1530 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1531 retval = f (v1, v2); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1532 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1533 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1534 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1535 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1536 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1537 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1538 } |
7336 | 1539 else |
1540 gripe_binary_op (octave_value::binary_op_as_string (op), | |
1541 v1.class_name (), v2.class_name ()); | |
1542 } | |
2376 | 1543 else |
1544 { | |
7336 | 1545 // FIXME -- we need to handle overloading operators for built-in |
1546 // classes (double, char, int8, etc.) | |
2376 | 1547 |
7336 | 1548 octave_value_typeinfo::binary_op_fcn f |
1549 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); | |
1550 | |
1551 if (f) | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1552 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1553 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1554 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1555 retval = f (*v1.rep, *v2.rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1556 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1557 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1558 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1559 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1560 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1561 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1562 } |
7336 | 1563 else |
2376 | 1564 { |
7336 | 1565 octave_value tv1; |
1566 octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); | |
1567 | |
1568 if (cf1) | |
1569 { | |
1570 octave_base_value *tmp = cf1 (*v1.rep); | |
3203 | 1571 |
7336 | 1572 if (tmp) |
1573 { | |
1574 tv1 = octave_value (tmp); | |
1575 t1 = tv1.type_id (); | |
1576 } | |
1577 else | |
1578 { | |
1579 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); | |
1580 return retval; | |
1581 } | |
3203 | 1582 } |
1583 else | |
7336 | 1584 tv1 = v1; |
1585 | |
1586 octave_value tv2; | |
1587 octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); | |
1588 | |
1589 if (cf2) | |
3203 | 1590 { |
7336 | 1591 octave_base_value *tmp = cf2 (*v2.rep); |
2376 | 1592 |
7336 | 1593 if (tmp) |
1594 { | |
1595 tv2 = octave_value (tmp); | |
1596 t2 = tv2.type_id (); | |
1597 } | |
1598 else | |
1599 { | |
1600 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); | |
1601 return retval; | |
1602 } | |
3203 | 1603 } |
1604 else | |
7336 | 1605 tv2 = v2; |
1606 | |
1607 if (cf1 || cf2) | |
3203 | 1608 { |
7336 | 1609 f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
1610 | |
1611 if (f) | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1612 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1613 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1614 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1615 retval = f (*tv1.rep, *tv2.rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1616 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1617 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1618 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1619 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1620 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1621 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1622 } |
7336 | 1623 else |
1624 gripe_binary_op (octave_value::binary_op_as_string (op), | |
1625 v1.type_name (), v2.type_name ()); | |
3203 | 1626 } |
2376 | 1627 else |
1628 gripe_binary_op (octave_value::binary_op_as_string (op), | |
1629 v1.type_name (), v2.type_name ()); | |
1630 } | |
1631 } | |
1632 | |
1633 return retval; | |
1634 } | |
1635 | |
4915 | 1636 static void |
1637 gripe_cat_op (const std::string& tn1, const std::string& tn2) | |
1638 { | |
1639 error ("concatenation operator not implemented for `%s' by `%s' operations", | |
1640 tn1.c_str (), tn2.c_str ()); | |
1641 } | |
1642 | |
1643 static void | |
1644 gripe_cat_op_conv (void) | |
1645 { | |
1646 error ("type conversion failed for concatenation operator"); | |
1647 } | |
1648 | |
1649 octave_value | |
1650 do_cat_op (const octave_value& v1, const octave_value& v2, | |
6867 | 1651 const Array<octave_idx_type>& ra_idx) |
4915 | 1652 { |
1653 octave_value retval; | |
1654 | |
5164 | 1655 // Rapid return for concatenation with an empty object. Dimension |
1656 // checking handled elsewhere. | |
1657 if (v1.all_zero_dims ()) | |
1658 return v2; | |
1659 if (v2.all_zero_dims ()) | |
1660 return v1; | |
1661 | |
4915 | 1662 int t1 = v1.type_id (); |
1663 int t2 = v2.type_id (); | |
1664 | |
5759 | 1665 octave_value_typeinfo::cat_op_fcn f |
1666 = octave_value_typeinfo::lookup_cat_op (t1, t2); | |
4915 | 1667 |
1668 if (f) | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1669 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1670 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1671 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1672 retval = f (*v1.rep, *v2.rep, ra_idx); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1673 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1674 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1675 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1676 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1677 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1678 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1679 } |
4915 | 1680 else |
1681 { | |
1682 octave_value tv1; | |
5759 | 1683 octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); |
4915 | 1684 |
1685 if (cf1) | |
1686 { | |
5759 | 1687 octave_base_value *tmp = cf1 (*v1.rep); |
4915 | 1688 |
1689 if (tmp) | |
1690 { | |
1691 tv1 = octave_value (tmp); | |
1692 t1 = tv1.type_id (); | |
1693 } | |
1694 else | |
1695 { | |
1696 gripe_cat_op_conv (); | |
1697 return retval; | |
1698 } | |
1699 } | |
1700 else | |
1701 tv1 = v1; | |
1702 | |
1703 octave_value tv2; | |
5759 | 1704 octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); |
4915 | 1705 |
1706 if (cf2) | |
1707 { | |
5759 | 1708 octave_base_value *tmp = cf2 (*v2.rep); |
4915 | 1709 |
1710 if (tmp) | |
1711 { | |
1712 tv2 = octave_value (tmp); | |
1713 t2 = tv2.type_id (); | |
1714 } | |
1715 else | |
1716 { | |
1717 gripe_cat_op_conv (); | |
1718 return retval; | |
1719 } | |
1720 } | |
1721 else | |
1722 tv2 = v2; | |
1723 | |
1724 if (cf1 || cf2) | |
1725 { | |
1726 f = octave_value_typeinfo::lookup_cat_op (t1, t2); | |
1727 | |
1728 if (f) | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1729 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1730 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1731 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1732 retval = f (*tv1.rep, *tv2.rep, ra_idx); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1733 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1734 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1735 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1736 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1737 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1738 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1739 } |
4915 | 1740 else |
1741 gripe_cat_op (v1.type_name (), v2.type_name ()); | |
1742 } | |
1743 else | |
1744 gripe_cat_op (v1.type_name (), v2.type_name ()); | |
1745 } | |
1746 | |
1747 return retval; | |
1748 } | |
1749 | |
3933 | 1750 void |
1751 octave_value::print_info (std::ostream& os, const std::string& prefix) const | |
1752 { | |
1753 os << prefix << "type_name: " << type_name () << "\n" | |
1754 << prefix << "count: " << get_count () << "\n" | |
1755 << prefix << "rep info: "; | |
1756 | |
1757 rep->print_info (os, prefix + " "); | |
1758 } | |
1759 | |
3203 | 1760 static void |
3523 | 1761 gripe_unary_op (const std::string& on, const std::string& tn) |
3203 | 1762 { |
1763 error ("unary operator `%s' not implemented for `%s' operands", | |
1764 on.c_str (), tn.c_str ()); | |
1765 } | |
1766 | |
1767 static void | |
3523 | 1768 gripe_unary_op_conv (const std::string& on) |
3203 | 1769 { |
1770 error ("type conversion failed for unary operator `%s'", on.c_str ()); | |
1771 } | |
1772 | |
1773 octave_value | |
1774 do_unary_op (octave_value::unary_op op, const octave_value& v) | |
1775 { | |
1776 octave_value retval; | |
1777 | |
1778 int t = v.type_id (); | |
1779 | |
7336 | 1780 if (t == octave_class::static_type_id ()) |
1781 { | |
1782 octave_value_typeinfo::unary_class_op_fcn f | |
1783 = octave_value_typeinfo::lookup_unary_class_op (op); | |
3203 | 1784 |
7336 | 1785 if (f) |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1786 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1787 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1788 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1789 retval = f (v); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1790 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1791 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1792 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1793 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1794 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1795 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1796 } |
7336 | 1797 else |
1798 gripe_unary_op (octave_value::unary_op_as_string (op), | |
1799 v.class_name ()); | |
1800 } | |
3203 | 1801 else |
1802 { | |
7336 | 1803 // FIXME -- we need to handle overloading operators for built-in |
1804 // classes (double, char, int8, etc.) | |
1805 | |
1806 octave_value_typeinfo::unary_op_fcn f | |
1807 = octave_value_typeinfo::lookup_unary_op (op, t); | |
3203 | 1808 |
7336 | 1809 if (f) |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1810 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1811 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1812 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1813 retval = f (*v.rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1814 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1815 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1816 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1817 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1818 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1819 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1820 } |
7336 | 1821 else |
3203 | 1822 { |
7336 | 1823 octave_value tv; |
1824 octave_base_value::type_conv_fcn cf | |
1825 = v.numeric_conversion_function (); | |
3203 | 1826 |
7336 | 1827 if (cf) |
3203 | 1828 { |
7336 | 1829 octave_base_value *tmp = cf (*v.rep); |
3203 | 1830 |
7336 | 1831 if (tmp) |
1832 { | |
1833 tv = octave_value (tmp); | |
1834 t = tv.type_id (); | |
1835 | |
1836 f = octave_value_typeinfo::lookup_unary_op (op, t); | |
3203 | 1837 |
7336 | 1838 if (f) |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1839 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1840 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1841 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1842 retval = f (*tv.rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1843 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1844 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1845 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1846 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1847 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1848 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1849 } |
7336 | 1850 else |
1851 gripe_unary_op (octave_value::unary_op_as_string (op), | |
1852 v.type_name ()); | |
1853 } | |
3203 | 1854 else |
7336 | 1855 gripe_unary_op_conv (octave_value::unary_op_as_string (op)); |
3203 | 1856 } |
1857 else | |
7336 | 1858 gripe_unary_op (octave_value::unary_op_as_string (op), |
1859 v.type_name ()); | |
3203 | 1860 } |
1861 } | |
1862 | |
1863 return retval; | |
1864 } | |
1865 | |
1866 static void | |
3933 | 1867 gripe_unary_op_conversion_failed (const std::string& op, |
1868 const std::string& tn) | |
3203 | 1869 { |
1870 error ("operator %s: type conversion for `%s' failed", | |
1871 op.c_str (), tn.c_str ()); | |
1872 } | |
1873 | |
3933 | 1874 const octave_value& |
1875 octave_value::do_non_const_unary_op (unary_op op) | |
3203 | 1876 { |
1877 octave_value retval; | |
1878 | |
1879 int t = type_id (); | |
1880 | |
5759 | 1881 octave_value_typeinfo::non_const_unary_op_fcn f |
3203 | 1882 = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
1883 | |
1884 if (f) | |
1885 { | |
1886 make_unique (); | |
1887 | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1888 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1889 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1890 f (*rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1891 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1892 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1893 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1894 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1895 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1896 } |
3203 | 1897 } |
1898 else | |
1899 { | |
5759 | 1900 octave_base_value::type_conv_fcn cf = numeric_conversion_function (); |
3203 | 1901 |
1902 if (cf) | |
1903 { | |
5759 | 1904 octave_base_value *tmp = cf (*rep); |
3203 | 1905 |
1906 if (tmp) | |
1907 { | |
5759 | 1908 octave_base_value *old_rep = rep; |
3203 | 1909 rep = tmp; |
1910 | |
1911 t = type_id (); | |
1912 | |
1913 f = octave_value_typeinfo::lookup_non_const_unary_op (op, t); | |
1914 | |
1915 if (f) | |
1916 { | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1917 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1918 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1919 f (*rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1920 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1921 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1922 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1923 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1924 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1925 } |
3203 | 1926 |
1927 if (old_rep && --old_rep->count == 0) | |
1928 delete old_rep; | |
1929 } | |
1930 else | |
1931 { | |
1932 if (old_rep) | |
1933 { | |
1934 if (--rep->count == 0) | |
1935 delete rep; | |
1936 | |
1937 rep = old_rep; | |
1938 } | |
1939 | |
1940 gripe_unary_op (octave_value::unary_op_as_string (op), | |
1941 type_name ()); | |
1942 } | |
1943 } | |
1944 else | |
1945 gripe_unary_op_conversion_failed | |
1946 (octave_value::unary_op_as_string (op), type_name ()); | |
1947 } | |
1948 else | |
1949 gripe_unary_op (octave_value::unary_op_as_string (op), type_name ()); | |
1950 } | |
3933 | 1951 |
1952 return *this; | |
3203 | 1953 } |
1954 | |
3933 | 1955 #if 0 |
3205 | 1956 static void |
3933 | 1957 gripe_unary_op_failed_or_no_method (const std::string& on, |
1958 const std::string& tn) | |
3205 | 1959 { |
1960 error ("operator %s: no method, or unable to evaluate for %s operand", | |
1961 on.c_str (), tn.c_str ()); | |
1962 } | |
3933 | 1963 #endif |
3205 | 1964 |
1965 void | |
4661 | 1966 octave_value::do_non_const_unary_op (unary_op, const octave_value_list&) |
3205 | 1967 { |
3933 | 1968 abort (); |
1969 } | |
1970 | |
1971 octave_value | |
4247 | 1972 octave_value::do_non_const_unary_op (unary_op op, const std::string& type, |
4219 | 1973 const std::list<octave_value_list>& idx) |
3933 | 1974 { |
1975 octave_value retval; | |
1976 | |
1977 if (idx.empty ()) | |
1978 { | |
1979 do_non_const_unary_op (op); | |
3205 | 1980 |
3933 | 1981 retval = *this; |
1982 } | |
1983 else | |
1984 { | |
5775 | 1985 // FIXME -- only do the following stuff if we can't find a |
3933 | 1986 // specific function to call to handle the op= operation for the |
1987 // types we have. | |
3205 | 1988 |
3933 | 1989 assign_op assop = unary_op_to_assign_op (op); |
1990 | |
1991 retval = assign (assop, type, idx, 1.0); | |
1992 } | |
1993 | |
1994 return retval; | |
3205 | 1995 } |
1996 | |
1997 octave_value::assign_op | |
1998 octave_value::unary_op_to_assign_op (unary_op op) | |
1999 { | |
2000 assign_op binop = unknown_assign_op; | |
2001 | |
2002 switch (op) | |
2003 { | |
3533 | 2004 case op_incr: |
2005 binop = op_add_eq; | |
3205 | 2006 break; |
2007 | |
3533 | 2008 case op_decr: |
2009 binop = op_sub_eq; | |
3205 | 2010 break; |
2011 | |
2012 default: | |
2013 { | |
3523 | 2014 std::string on = unary_op_as_string (op); |
3205 | 2015 error ("operator %s: no assign operator found", on.c_str ()); |
2016 } | |
2017 } | |
2018 | |
2019 return binop; | |
2020 } | |
2021 | |
3204 | 2022 octave_value::binary_op |
2023 octave_value::op_eq_to_binary_op (assign_op op) | |
2024 { | |
2025 binary_op binop = unknown_binary_op; | |
2026 | |
2027 switch (op) | |
2028 { | |
3533 | 2029 case op_add_eq: |
2030 binop = op_add; | |
3204 | 2031 break; |
2032 | |
3533 | 2033 case op_sub_eq: |
2034 binop = op_sub; | |
3204 | 2035 break; |
2036 | |
3533 | 2037 case op_mul_eq: |
2038 binop = op_mul; | |
3204 | 2039 break; |
2040 | |
3533 | 2041 case op_div_eq: |
2042 binop = op_div; | |
3204 | 2043 break; |
2044 | |
3533 | 2045 case op_ldiv_eq: |
2046 binop = op_ldiv; | |
3204 | 2047 break; |
2048 | |
4018 | 2049 case op_pow_eq: |
2050 binop = op_pow; | |
2051 break; | |
2052 | |
3533 | 2053 case op_lshift_eq: |
2054 binop = op_lshift; | |
3204 | 2055 break; |
2056 | |
3533 | 2057 case op_rshift_eq: |
2058 binop = op_rshift; | |
3204 | 2059 break; |
2060 | |
3533 | 2061 case op_el_mul_eq: |
2062 binop = op_el_mul; | |
3204 | 2063 break; |
2064 | |
3533 | 2065 case op_el_div_eq: |
2066 binop = op_el_div; | |
3204 | 2067 break; |
2068 | |
3533 | 2069 case op_el_ldiv_eq: |
2070 binop = op_el_ldiv; | |
3204 | 2071 break; |
2072 | |
4018 | 2073 case op_el_pow_eq: |
2074 binop = op_el_pow; | |
2075 break; | |
2076 | |
3533 | 2077 case op_el_and_eq: |
2078 binop = op_el_and; | |
3204 | 2079 break; |
2080 | |
3533 | 2081 case op_el_or_eq: |
2082 binop = op_el_or; | |
3204 | 2083 break; |
2084 | |
2085 default: | |
2086 { | |
3523 | 2087 std::string on = assign_op_as_string (op); |
3204 | 2088 error ("operator %s: no binary operator found", on.c_str ()); |
2089 } | |
2090 } | |
2091 | |
2092 return binop; | |
2093 } | |
2094 | |
3933 | 2095 octave_value |
2096 octave_value::empty_conv (const std::string& type, const octave_value& rhs) | |
2097 { | |
2098 octave_value retval; | |
2099 | |
2100 if (type.length () > 0) | |
2101 { | |
2102 switch (type[0]) | |
2103 { | |
2104 case '(': | |
2105 { | |
2106 if (type.length () > 1 && type[1] == '.') | |
2107 retval = Octave_map (); | |
2108 else | |
2109 retval = octave_value (rhs.empty_clone ()); | |
2110 } | |
2111 break; | |
2112 | |
2113 case '{': | |
2114 retval = Cell (); | |
2115 break; | |
2116 | |
2117 case '.': | |
2118 retval = Octave_map (); | |
2119 break; | |
2120 | |
2121 default: | |
2122 panic_impossible (); | |
2123 } | |
2124 } | |
2125 else | |
2126 retval = octave_value (rhs.empty_clone ()); | |
2127 | |
2128 return retval; | |
2129 } | |
2130 | |
2376 | 2131 void |
2132 install_types (void) | |
2133 { | |
2134 octave_base_value::register_type (); | |
3928 | 2135 octave_cell::register_type (); |
2376 | 2136 octave_scalar::register_type (); |
2137 octave_complex::register_type (); | |
2138 octave_matrix::register_type (); | |
2139 octave_complex_matrix::register_type (); | |
2140 octave_range::register_type (); | |
2825 | 2141 octave_bool::register_type (); |
2142 octave_bool_matrix::register_type (); | |
2376 | 2143 octave_char_matrix::register_type (); |
2144 octave_char_matrix_str::register_type (); | |
5279 | 2145 octave_char_matrix_sq_str::register_type (); |
4901 | 2146 octave_int8_scalar::register_type (); |
2147 octave_int16_scalar::register_type (); | |
2148 octave_int32_scalar::register_type (); | |
2149 octave_int64_scalar::register_type (); | |
2150 octave_uint8_scalar::register_type (); | |
2151 octave_uint16_scalar::register_type (); | |
2152 octave_uint32_scalar::register_type (); | |
2153 octave_uint64_scalar::register_type (); | |
2154 octave_int8_matrix::register_type (); | |
2155 octave_int16_matrix::register_type (); | |
2156 octave_int32_matrix::register_type (); | |
2157 octave_int64_matrix::register_type (); | |
2158 octave_uint8_matrix::register_type (); | |
2159 octave_uint16_matrix::register_type (); | |
2160 octave_uint32_matrix::register_type (); | |
2161 octave_uint64_matrix::register_type (); | |
5164 | 2162 octave_sparse_bool_matrix::register_type (); |
2163 octave_sparse_matrix::register_type (); | |
2164 octave_sparse_complex_matrix::register_type (); | |
2376 | 2165 octave_struct::register_type (); |
7336 | 2166 octave_class::register_type (); |
2880 | 2167 octave_list::register_type (); |
3977 | 2168 octave_cs_list::register_type (); |
2376 | 2169 octave_magic_colon::register_type (); |
2974 | 2170 octave_builtin::register_type (); |
2171 octave_user_function::register_type (); | |
4649 | 2172 octave_dld_function::register_type (); |
4643 | 2173 octave_fcn_handle::register_type (); |
4966 | 2174 octave_fcn_inline::register_type (); |
4643 | 2175 octave_streamoff::register_type (); |
2376 | 2176 } |
2177 | |
4970 | 2178 #if 0 |
2179 DEFUN (cast, args, , | |
2180 "-*- texinfo -*-\n\ | |
2181 @deftypefn {Built-in Function} {} cast (@var{val}, @var{type})\n\ | |
2182 Convert @var{val} to the new data type @var{type}.\n\ | |
5642 | 2183 @seealso{class, typeinfo}\n\ |
2184 @end deftypefn") | |
4970 | 2185 { |
2186 octave_value retval; | |
2187 | |
2188 if (args.length () == 2) | |
2189 error ("cast: not implemented"); | |
2190 else | |
5823 | 2191 print_usage (); |
4970 | 2192 |
2193 return retval; | |
2194 } | |
2195 #endif | |
2196 | |
4791 | 2197 DEFUN (sizeof, args, , |
2198 "-*- texinfo -*-\n\ | |
2199 @deftypefn {Built-in Function} {} sizeof (@var{val})\n\ | |
2200 Return the size of @var{val} in bytes\n\ | |
2201 @end deftypefn") | |
2202 { | |
2203 octave_value retval; | |
2204 | |
2205 if (args.length () == 1) | |
2206 retval = args(0).byte_size (); | |
2207 else | |
5823 | 2208 print_usage (); |
4791 | 2209 |
2210 return retval; | |
2211 } | |
2212 | |
6153 | 2213 static void |
2214 decode_subscripts (const char* name, const octave_value& arg, | |
2215 std::string& type_string, | |
2216 std::list<octave_value_list>& idx) | |
2217 { | |
2218 Octave_map m = arg.map_value (); | |
2219 | |
2220 if (! error_state | |
6639 | 2221 && m.nfields () == 2 && m.contains ("type") && m.contains ("subs")) |
6153 | 2222 { |
2223 Cell& type = m.contents ("type"); | |
2224 Cell& subs = m.contents ("subs"); | |
2225 | |
2226 type_string = std::string (type.length(), '\0'); | |
2227 | |
2228 for (int k = 0; k < type.length (); k++) | |
2229 { | |
2230 std::string item = type(k).string_value (); | |
2231 | |
2232 if (! error_state) | |
2233 { | |
2234 if (item == "{}") | |
2235 type_string[k] = '{'; | |
2236 else if (item == "()") | |
2237 type_string[k] = '('; | |
2238 else if (item == ".") | |
2239 type_string[k] = '.'; | |
2240 else | |
2241 { | |
2242 error("%s: invalid indexing type `%s'", name, item.c_str ()); | |
2243 return; | |
2244 } | |
2245 } | |
2246 else | |
2247 { | |
2248 error ("%s: expecting type(%d) to be a character string", | |
2249 name, k+1); | |
2250 return; | |
2251 } | |
2252 | |
2253 octave_value_list idx_item; | |
2254 | |
2255 if (subs(k).is_string ()) | |
2256 idx_item(0) = subs(k); | |
2257 else if (subs(k).is_cell ()) | |
2258 { | |
2259 Cell subs_cell = subs(k).cell_value (); | |
2260 | |
2261 for (int n = 0; n < subs_cell.length (); n++) | |
2262 { | |
2263 if (subs_cell(n).is_string () | |
2264 && subs_cell(n).string_value () == ":") | |
2265 idx_item(n) = octave_value(octave_value::magic_colon_t); | |
2266 else | |
2267 idx_item(n) = subs_cell(n); | |
2268 } | |
2269 } | |
2270 else | |
2271 { | |
2272 error ("%s: expecting subs(%d) to be a character string or cell array", | |
2273 name, k+1); | |
2274 return; | |
2275 } | |
2276 | |
2277 idx.push_back (idx_item); | |
2278 } | |
2279 } | |
2280 else | |
2281 error ("%s: second argument must be a structure with fields `type' and `subs'", name); | |
2282 } | |
2283 | |
2284 DEFUN (subsref, args, nargout, | |
2285 "-*- texinfo -*-\n\ | |
2286 @deftypefn {Built-in Function} {} subsref (@var{val}, @var{idx})\n\ | |
2287 Perform the subscripted element selection operation according to\n\ | |
2288 the subscript specified by @var{idx}.\n\ | |
2289 \n\ | |
2290 The subscript @var{idx} is expected to be a structure array with\n\ | |
2291 fields @samp{type} and @samp{subs}. Valid values for @samp{type}\n\ | |
6550 | 2292 are @samp{\"()\"}, @samp{\"@{@}\"}, and @samp{\".\"}.\n\ |
6153 | 2293 The @samp{subs} field may be either @samp{\":\"} or a cell array\n\ |
2294 of index values.\n\ | |
6631 | 2295 \n\ |
2296 The following example shows how to extract the two first columns of\n\ | |
2297 a matrix\n\ | |
2298 \n\ | |
2299 @example\n\ | |
2300 val = magic(3)\n\ | |
2301 @result{} val = [ 8 1 6\n\ | |
2302 3 5 7\n\ | |
2303 4 9 2 ]\n\ | |
2304 idx.type = \"()\";\n\ | |
2305 idx.subs = @{\":\", 1:2@};\n\ | |
2306 subsref(val, idx)\n\ | |
2307 @result{} [ 8 1 \n\ | |
2308 3 5 \n\ | |
2309 4 9 ]\n\ | |
2310 @end example\n\ | |
2311 \n\ | |
2312 @noindent\n\ | |
2313 Note that this is the same as writing @code{val(:,1:2)}.\n\ | |
6157 | 2314 @seealso{subsasgn, substruct}\n\ |
6153 | 2315 @end deftypefn") |
2316 { | |
2317 octave_value_list retval; | |
2318 | |
2319 if (args.length () == 2) | |
2320 { | |
2321 std::string type; | |
2322 std::list<octave_value_list> idx; | |
2323 | |
2324 decode_subscripts ("subsref", args(1), type, idx); | |
2325 | |
2326 if (! error_state) | |
2327 retval = args(0).subsref (type, idx, nargout); | |
2328 } | |
2329 else | |
2330 print_usage (); | |
2331 | |
2332 return retval; | |
2333 } | |
2334 | |
2335 DEFUN (subsasgn, args, , | |
2336 "-*- texinfo -*-\n\ | |
2337 @deftypefn {Built-in Function} {} subsasgn (@var{val}, @var{idx}, @var{rhs})\n\ | |
2338 Perform the subscripted assignment operation according to\n\ | |
2339 the subscript specified by @var{idx}.\n\ | |
2340 \n\ | |
2341 The subscript @var{idx} is expected to be a structure array with\n\ | |
2342 fields @samp{type} and @samp{subs}. Valid values for @samp{type}\n\ | |
6248 | 2343 are @samp{\"()\"}, @samp{\"@{@}\"}, and @samp{\".\"}.\n\ |
6153 | 2344 The @samp{subs} field may be either @samp{\":\"} or a cell array\n\ |
2345 of index values.\n\ | |
6631 | 2346 \n\ |
2347 The following example shows how to set the two first columns of a\n\ | |
2348 3-by-3 matrix to zero.\n\ | |
2349 \n\ | |
2350 @example\n\ | |
2351 val = magic(3);\n\ | |
2352 idx.type = \"()\";\n\ | |
2353 idx.subs = @{\":\", 1:2@};\n\ | |
2354 subsasgn (val, idx, 0)\n\ | |
2355 @result{} [ 0 0 6\n\ | |
2356 0 0 7\n\ | |
2357 0 0 2 ]\n\ | |
2358 @end example\n\ | |
2359 \n\ | |
2360 Note that this is the same as writing @code{val(:,1:2) = 0}.\n\ | |
6157 | 2361 @seealso{subsref, substruct}\n\ |
6153 | 2362 @end deftypefn") |
2363 { | |
2364 octave_value retval; | |
2365 | |
2366 if (args.length () == 3) | |
2367 { | |
2368 std::string type; | |
2369 std::list<octave_value_list> idx; | |
2370 | |
2371 decode_subscripts ("subsasgn", args(1), type, idx); | |
2372 | |
2373 if (! error_state) | |
2374 retval = args(0).subsasgn (type, idx, args(2)); | |
2375 } | |
2376 else | |
2377 print_usage (); | |
2378 | |
2379 return retval; | |
2380 } | |
2381 | |
2376 | 2382 /* |
2383 ;;; Local Variables: *** | |
2384 ;;; mode: C++ *** | |
2385 ;;; End: *** | |
2386 */ |