2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2376
|
21 |
|
22 */ |
|
23 |
|
24 #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" |
4643
|
58 #include "ov-streamoff.h" |
2880
|
59 #include "ov-list.h" |
3977
|
60 #include "ov-cs-list.h" |
2376
|
61 #include "ov-colon.h" |
2974
|
62 #include "ov-builtin.h" |
|
63 #include "ov-mapper.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" |
|
76 #include "utils.h" |
|
77 #include "variables.h" |
|
78 |
2477
|
79 // We are likely to have a lot of octave_value objects to allocate, so |
|
80 // make the grow_size large. |
3219
|
81 DEFINE_OCTAVE_ALLOCATOR2(octave_value, 1024); |
2477
|
82 |
5775
|
83 // FIXME |
2880
|
84 |
2376
|
85 // Octave's value type. |
|
86 |
3536
|
87 std::string |
3203
|
88 octave_value::unary_op_as_string (unary_op op) |
|
89 { |
3523
|
90 std::string retval; |
3203
|
91 |
|
92 switch (op) |
|
93 { |
3533
|
94 case op_not: |
3203
|
95 retval = "!"; |
|
96 break; |
|
97 |
4965
|
98 case op_uplus: |
|
99 retval = "+"; |
|
100 break; |
|
101 |
3533
|
102 case op_uminus: |
3203
|
103 retval = "-"; |
|
104 break; |
|
105 |
3533
|
106 case op_transpose: |
3203
|
107 retval = ".'"; |
|
108 break; |
|
109 |
3533
|
110 case op_hermitian: |
3203
|
111 retval = "'"; |
|
112 break; |
|
113 |
3533
|
114 case op_incr: |
3203
|
115 retval = "++"; |
|
116 break; |
|
117 |
3533
|
118 case op_decr: |
3203
|
119 retval = "--"; |
|
120 break; |
|
121 |
|
122 default: |
|
123 retval = "<unknown>"; |
|
124 } |
|
125 |
|
126 return retval; |
|
127 } |
|
128 |
3536
|
129 std::string |
2376
|
130 octave_value::binary_op_as_string (binary_op op) |
|
131 { |
3523
|
132 std::string retval; |
2376
|
133 |
|
134 switch (op) |
|
135 { |
3533
|
136 case op_add: |
2376
|
137 retval = "+"; |
|
138 break; |
|
139 |
3533
|
140 case op_sub: |
2376
|
141 retval = "-"; |
|
142 break; |
|
143 |
3533
|
144 case op_mul: |
2376
|
145 retval = "*"; |
|
146 break; |
|
147 |
3533
|
148 case op_div: |
2376
|
149 retval = "/"; |
|
150 break; |
|
151 |
3533
|
152 case op_pow: |
2376
|
153 retval = "^"; |
|
154 break; |
|
155 |
3533
|
156 case op_ldiv: |
2376
|
157 retval = "\\"; |
|
158 break; |
|
159 |
3533
|
160 case op_lshift: |
2903
|
161 retval = "<<"; |
|
162 break; |
|
163 |
3533
|
164 case op_rshift: |
2903
|
165 retval = ">>"; |
|
166 break; |
|
167 |
3533
|
168 case op_lt: |
2376
|
169 retval = "<"; |
|
170 break; |
|
171 |
3533
|
172 case op_le: |
2376
|
173 retval = "<="; |
|
174 break; |
|
175 |
3533
|
176 case op_eq: |
2376
|
177 retval = "=="; |
|
178 break; |
|
179 |
3533
|
180 case op_ge: |
2376
|
181 retval = ">="; |
|
182 break; |
|
183 |
3533
|
184 case op_gt: |
2376
|
185 retval = ">"; |
|
186 break; |
|
187 |
3533
|
188 case op_ne: |
2376
|
189 retval = "!="; |
|
190 break; |
|
191 |
3533
|
192 case op_el_mul: |
2376
|
193 retval = ".*"; |
|
194 break; |
|
195 |
3533
|
196 case op_el_div: |
2376
|
197 retval = "./"; |
|
198 break; |
|
199 |
3533
|
200 case op_el_pow: |
2376
|
201 retval = ".^"; |
|
202 break; |
|
203 |
3533
|
204 case op_el_ldiv: |
2376
|
205 retval = ".\\"; |
|
206 break; |
|
207 |
3533
|
208 case op_el_and: |
2376
|
209 retval = "&"; |
|
210 break; |
|
211 |
3533
|
212 case op_el_or: |
2376
|
213 retval = "|"; |
|
214 break; |
|
215 |
3533
|
216 case op_struct_ref: |
2376
|
217 retval = "."; |
|
218 break; |
|
219 |
|
220 default: |
|
221 retval = "<unknown>"; |
|
222 } |
|
223 |
|
224 return retval; |
|
225 } |
|
226 |
3536
|
227 std::string |
2880
|
228 octave_value::assign_op_as_string (assign_op op) |
|
229 { |
3523
|
230 std::string retval; |
2880
|
231 |
|
232 switch (op) |
|
233 { |
3533
|
234 case op_asn_eq: |
2880
|
235 retval = "="; |
|
236 break; |
|
237 |
3533
|
238 case op_add_eq: |
2880
|
239 retval = "+="; |
|
240 break; |
|
241 |
3533
|
242 case op_sub_eq: |
2880
|
243 retval = "-="; |
|
244 break; |
|
245 |
3533
|
246 case op_mul_eq: |
2880
|
247 retval = "*="; |
|
248 break; |
|
249 |
3533
|
250 case op_div_eq: |
2880
|
251 retval = "/="; |
|
252 break; |
|
253 |
3533
|
254 case op_ldiv_eq: |
3204
|
255 retval = "\\="; |
|
256 break; |
|
257 |
4018
|
258 case op_pow_eq: |
|
259 retval = "^="; |
|
260 break; |
|
261 |
3533
|
262 case op_lshift_eq: |
2903
|
263 retval = "<<="; |
|
264 break; |
|
265 |
3533
|
266 case op_rshift_eq: |
2903
|
267 retval = ">>="; |
|
268 break; |
|
269 |
3533
|
270 case op_el_mul_eq: |
2880
|
271 retval = ".*="; |
|
272 break; |
|
273 |
3533
|
274 case op_el_div_eq: |
2880
|
275 retval = "./="; |
|
276 break; |
|
277 |
3533
|
278 case op_el_ldiv_eq: |
3204
|
279 retval = ".\\="; |
|
280 break; |
|
281 |
4018
|
282 case op_el_pow_eq: |
|
283 retval = ".^="; |
|
284 break; |
|
285 |
3533
|
286 case op_el_and_eq: |
2880
|
287 retval = "&="; |
|
288 break; |
|
289 |
3533
|
290 case op_el_or_eq: |
2880
|
291 retval = "|="; |
|
292 break; |
|
293 |
|
294 default: |
|
295 retval = "<unknown>"; |
|
296 } |
|
297 |
|
298 return retval; |
|
299 } |
|
300 |
5759
|
301 octave_value::octave_value (void) |
|
302 : rep (new octave_base_value ()) |
4513
|
303 { |
2825
|
304 } |
2376
|
305 |
4254
|
306 octave_value::octave_value (short int i) |
|
307 : rep (new octave_scalar (i)) |
|
308 { |
|
309 } |
|
310 |
|
311 octave_value::octave_value (unsigned short int i) |
|
312 : rep (new octave_scalar (i)) |
|
313 { |
|
314 } |
|
315 |
4233
|
316 octave_value::octave_value (int i) |
|
317 : rep (new octave_scalar (i)) |
|
318 { |
|
319 } |
|
320 |
4254
|
321 octave_value::octave_value (unsigned int i) |
|
322 : rep (new octave_scalar (i)) |
|
323 { |
|
324 } |
|
325 |
|
326 octave_value::octave_value (long int i) |
|
327 : rep (new octave_scalar (i)) |
|
328 { |
|
329 } |
|
330 |
|
331 octave_value::octave_value (unsigned long int i) |
|
332 : rep (new octave_scalar (i)) |
|
333 { |
|
334 } |
|
335 |
4353
|
336 #if defined (HAVE_LONG_LONG_INT) |
|
337 octave_value::octave_value (long long int i) |
|
338 : rep (new octave_scalar (i)) |
|
339 { |
|
340 } |
|
341 #endif |
|
342 |
4355
|
343 #if defined (HAVE_UNSIGNED_LONG_LONG_INT) |
4353
|
344 octave_value::octave_value (unsigned long long int i) |
|
345 : rep (new octave_scalar (i)) |
|
346 { |
|
347 } |
|
348 #endif |
|
349 |
4254
|
350 octave_value::octave_value (octave_time t) |
|
351 : rep (new octave_scalar (t)) |
|
352 { |
|
353 } |
|
354 |
2376
|
355 octave_value::octave_value (double d) |
2825
|
356 : rep (new octave_scalar (d)) |
|
357 { |
|
358 } |
2376
|
359 |
4587
|
360 octave_value::octave_value (const Cell& c, bool is_csl) |
5477
|
361 : rep (is_csl |
5759
|
362 ? dynamic_cast<octave_base_value *> (new octave_cs_list (c)) |
|
363 : dynamic_cast<octave_base_value *> (new octave_cell (c))) |
3351
|
364 { |
|
365 } |
|
366 |
5147
|
367 octave_value::octave_value (const ArrayN<octave_value>& a, bool is_csl) |
5477
|
368 : rep (is_csl |
5759
|
369 ? dynamic_cast<octave_base_value *> (new octave_cs_list (Cell (a))) |
|
370 : dynamic_cast<octave_base_value *> (new octave_cell (Cell (a)))) |
5147
|
371 { |
|
372 } |
|
373 |
5785
|
374 octave_value::octave_value (const Matrix& m, const MatrixType& t) |
|
375 : rep (new octave_matrix (m, t)) |
2423
|
376 { |
|
377 maybe_mutate (); |
|
378 } |
2376
|
379 |
4507
|
380 octave_value::octave_value (const NDArray& a) |
4513
|
381 : rep (new octave_matrix (a)) |
4478
|
382 { |
|
383 maybe_mutate (); |
|
384 } |
|
385 |
4911
|
386 octave_value::octave_value (const ArrayN<double>& a) |
|
387 : rep (new octave_matrix (a)) |
|
388 { |
|
389 maybe_mutate (); |
|
390 } |
|
391 |
2376
|
392 octave_value::octave_value (const DiagMatrix& d) |
2423
|
393 : rep (new octave_matrix (d)) |
|
394 { |
|
395 maybe_mutate (); |
|
396 } |
2376
|
397 |
3418
|
398 octave_value::octave_value (const RowVector& v) |
|
399 : rep (new octave_matrix (v)) |
2423
|
400 { |
|
401 maybe_mutate (); |
|
402 } |
2376
|
403 |
3418
|
404 octave_value::octave_value (const ColumnVector& v) |
|
405 : rep (new octave_matrix (v)) |
2423
|
406 { |
|
407 maybe_mutate (); |
|
408 } |
2376
|
409 |
|
410 octave_value::octave_value (const Complex& C) |
2423
|
411 : rep (new octave_complex (C)) |
|
412 { |
|
413 maybe_mutate (); |
|
414 } |
2376
|
415 |
5785
|
416 octave_value::octave_value (const ComplexMatrix& m, const MatrixType& t) |
|
417 : rep (new octave_complex_matrix (m, t)) |
2423
|
418 { |
|
419 maybe_mutate (); |
|
420 } |
2376
|
421 |
4513
|
422 octave_value::octave_value (const ComplexNDArray& a) |
|
423 : rep (new octave_complex_matrix (a)) |
4478
|
424 { |
|
425 maybe_mutate (); |
|
426 } |
|
427 |
4911
|
428 octave_value::octave_value (const ArrayN<Complex>& a) |
|
429 : rep (new octave_complex_matrix (a)) |
|
430 { |
|
431 maybe_mutate (); |
|
432 } |
|
433 |
2376
|
434 octave_value::octave_value (const ComplexDiagMatrix& d) |
2423
|
435 : rep (new octave_complex_matrix (d)) |
|
436 { |
|
437 maybe_mutate (); |
|
438 } |
2376
|
439 |
3418
|
440 octave_value::octave_value (const ComplexRowVector& v) |
|
441 : rep (new octave_complex_matrix (v)) |
2423
|
442 { |
|
443 maybe_mutate (); |
|
444 } |
2376
|
445 |
3418
|
446 octave_value::octave_value (const ComplexColumnVector& v) |
|
447 : rep (new octave_complex_matrix (v)) |
2423
|
448 { |
|
449 maybe_mutate (); |
|
450 } |
2376
|
451 |
2825
|
452 octave_value::octave_value (bool b) |
|
453 : rep (new octave_bool (b)) |
|
454 { |
|
455 } |
|
456 |
5785
|
457 octave_value::octave_value (const boolMatrix& bm, const MatrixType& t) |
|
458 : rep (new octave_bool_matrix (bm, t)) |
2825
|
459 { |
|
460 maybe_mutate (); |
|
461 } |
|
462 |
4513
|
463 octave_value::octave_value (const boolNDArray& bnda) |
|
464 : rep (new octave_bool_matrix (bnda)) |
|
465 { |
|
466 maybe_mutate (); |
|
467 } |
|
468 |
5279
|
469 octave_value::octave_value (char c, char type) |
|
470 : rep (type == '"' |
|
471 ? new octave_char_matrix_dq_str (c) |
|
472 : new octave_char_matrix_sq_str (c)) |
3189
|
473 { |
|
474 maybe_mutate (); |
|
475 } |
|
476 |
5279
|
477 octave_value::octave_value (const char *s, char type) |
|
478 : rep (type == '"' |
|
479 ? new octave_char_matrix_dq_str (s) |
|
480 : new octave_char_matrix_sq_str (s)) |
2423
|
481 { |
|
482 maybe_mutate (); |
|
483 } |
2376
|
484 |
5279
|
485 octave_value::octave_value (const std::string& s, char type) |
|
486 : rep (type == '"' |
|
487 ? new octave_char_matrix_dq_str (s) |
|
488 : new octave_char_matrix_sq_str (s)) |
2423
|
489 { |
|
490 maybe_mutate (); |
|
491 } |
2376
|
492 |
5279
|
493 octave_value::octave_value (const string_vector& s, char type) |
|
494 : rep (type == '"' |
|
495 ? new octave_char_matrix_dq_str (s) |
|
496 : new octave_char_matrix_sq_str (s)) |
2423
|
497 { |
|
498 maybe_mutate (); |
|
499 } |
2376
|
500 |
5279
|
501 octave_value::octave_value (const charMatrix& chm, bool is_str, char type) |
4587
|
502 : rep (is_str |
5279
|
503 ? (type == '"' |
|
504 ? new octave_char_matrix_dq_str (chm) |
|
505 : new octave_char_matrix_sq_str (chm)) |
4513
|
506 : new octave_char_matrix (chm)) |
2409
|
507 { |
4513
|
508 maybe_mutate (); |
|
509 } |
2376
|
510 |
5279
|
511 octave_value::octave_value (const charNDArray& chm, bool is_str, char type) |
4587
|
512 : rep (is_str |
5279
|
513 ? (type == '"' |
|
514 ? new octave_char_matrix_dq_str (chm) |
|
515 : new octave_char_matrix_sq_str (chm)) |
4513
|
516 : new octave_char_matrix (chm)) |
|
517 { |
2423
|
518 maybe_mutate (); |
2409
|
519 } |
2376
|
520 |
5279
|
521 octave_value::octave_value (const ArrayN<char>& chm, bool is_str, char type) |
4997
|
522 : rep (is_str |
5279
|
523 ? (type == '"' |
|
524 ? new octave_char_matrix_dq_str (chm) |
|
525 : new octave_char_matrix_sq_str (chm)) |
4997
|
526 : new octave_char_matrix (chm)) |
|
527 { |
|
528 maybe_mutate (); |
|
529 } |
|
530 |
5785
|
531 octave_value::octave_value (const SparseMatrix& m, const MatrixType &t) |
5164
|
532 : rep (new octave_sparse_matrix (m, t)) |
|
533 { |
|
534 maybe_mutate (); |
|
535 } |
|
536 |
5785
|
537 octave_value::octave_value (const SparseComplexMatrix& m, const MatrixType &t) |
5164
|
538 : rep (new octave_sparse_complex_matrix (m, t)) |
|
539 { |
|
540 maybe_mutate (); |
|
541 } |
|
542 |
5785
|
543 octave_value::octave_value (const SparseBoolMatrix& bm, const MatrixType &t) |
5164
|
544 : rep (new octave_sparse_bool_matrix (bm, t)) |
|
545 { |
|
546 maybe_mutate (); |
|
547 } |
|
548 |
4901
|
549 octave_value::octave_value (const octave_int8& i) |
|
550 : rep (new octave_int8_scalar (i)) |
|
551 { |
|
552 maybe_mutate (); |
|
553 } |
|
554 |
|
555 octave_value::octave_value (const octave_uint8& i) |
|
556 : rep (new octave_uint8_scalar (i)) |
|
557 { |
|
558 maybe_mutate (); |
|
559 } |
|
560 |
|
561 octave_value::octave_value (const octave_int16& i) |
|
562 : rep (new octave_int16_scalar (i)) |
|
563 { |
|
564 maybe_mutate (); |
|
565 } |
|
566 |
|
567 octave_value::octave_value (const octave_uint16& i) |
|
568 : rep (new octave_uint16_scalar (i)) |
|
569 { |
|
570 maybe_mutate (); |
|
571 } |
|
572 |
|
573 octave_value::octave_value (const octave_int32& i) |
|
574 : rep (new octave_int32_scalar (i)) |
|
575 { |
|
576 maybe_mutate (); |
|
577 } |
|
578 |
|
579 octave_value::octave_value (const octave_uint32& i) |
|
580 : rep (new octave_uint32_scalar (i)) |
|
581 { |
|
582 maybe_mutate (); |
|
583 } |
|
584 |
|
585 octave_value::octave_value (const octave_int64& i) |
|
586 : rep (new octave_int64_scalar (i)) |
|
587 { |
|
588 maybe_mutate (); |
|
589 } |
|
590 |
|
591 octave_value::octave_value (const octave_uint64& i) |
|
592 : rep (new octave_uint64_scalar (i)) |
|
593 { |
|
594 maybe_mutate (); |
|
595 } |
|
596 |
|
597 octave_value::octave_value (const int8NDArray& inda) |
|
598 : rep (new octave_int8_matrix (inda)) |
|
599 { |
|
600 maybe_mutate (); |
|
601 } |
|
602 |
|
603 octave_value::octave_value (const uint8NDArray& inda) |
|
604 : rep (new octave_uint8_matrix (inda)) |
|
605 { |
|
606 maybe_mutate (); |
|
607 } |
|
608 |
|
609 octave_value::octave_value (const int16NDArray& inda) |
|
610 : rep (new octave_int16_matrix (inda)) |
|
611 { |
|
612 maybe_mutate (); |
|
613 } |
|
614 |
|
615 octave_value::octave_value (const uint16NDArray& inda) |
|
616 : rep (new octave_uint16_matrix (inda)) |
|
617 { |
|
618 maybe_mutate (); |
|
619 } |
|
620 |
|
621 octave_value::octave_value (const int32NDArray& inda) |
|
622 : rep (new octave_int32_matrix (inda)) |
|
623 { |
|
624 maybe_mutate (); |
|
625 } |
|
626 |
|
627 octave_value::octave_value (const uint32NDArray& inda) |
|
628 : rep (new octave_uint32_matrix (inda)) |
|
629 { |
|
630 maybe_mutate (); |
|
631 } |
|
632 |
|
633 octave_value::octave_value (const int64NDArray& inda) |
|
634 : rep (new octave_int64_matrix (inda)) |
|
635 { |
|
636 maybe_mutate (); |
|
637 } |
|
638 |
|
639 octave_value::octave_value (const uint64NDArray& inda) |
|
640 : rep (new octave_uint64_matrix (inda)) |
|
641 { |
|
642 maybe_mutate (); |
|
643 } |
|
644 |
2376
|
645 octave_value::octave_value (double base, double limit, double inc) |
2423
|
646 : rep (new octave_range (base, limit, inc)) |
|
647 { |
|
648 maybe_mutate (); |
|
649 } |
2376
|
650 |
|
651 octave_value::octave_value (const Range& r) |
2423
|
652 : rep (new octave_range (r)) |
|
653 { |
|
654 maybe_mutate (); |
|
655 } |
2376
|
656 |
|
657 octave_value::octave_value (const Octave_map& m) |
2825
|
658 : rep (new octave_struct (m)) |
|
659 { |
2880
|
660 } |
|
661 |
4643
|
662 octave_value::octave_value (const streamoff_array& off) |
|
663 : rep (new octave_streamoff (off)) |
|
664 { |
|
665 } |
|
666 |
4587
|
667 octave_value::octave_value (const octave_value_list& l, bool is_csl) |
5477
|
668 : rep (is_csl |
5759
|
669 ? dynamic_cast<octave_base_value *> (new octave_cs_list (l)) |
|
670 : dynamic_cast<octave_base_value *> (new octave_list (l))) |
2880
|
671 { |
|
672 } |
2376
|
673 |
|
674 octave_value::octave_value (octave_value::magic_colon) |
2825
|
675 : rep (new octave_magic_colon ()) |
|
676 { |
|
677 } |
2376
|
678 |
5759
|
679 octave_value::octave_value (octave_base_value *new_rep) |
2825
|
680 : rep (new_rep) |
|
681 { |
|
682 } |
2376
|
683 |
|
684 octave_value::~octave_value (void) |
|
685 { |
|
686 #if defined (MDEBUG) |
5759
|
687 std::cerr << "~octave_value: rep: " << rep |
|
688 << " rep->count: " << rep->count << std::endl; |
2376
|
689 #endif |
|
690 |
5759
|
691 if (--rep->count == 0) |
|
692 delete rep; |
2376
|
693 } |
|
694 |
5759
|
695 octave_base_value * |
3933
|
696 octave_value::clone (void) const |
2880
|
697 { |
|
698 panic_impossible (); |
3546
|
699 return 0; |
2880
|
700 } |
|
701 |
2409
|
702 void |
|
703 octave_value::maybe_mutate (void) |
|
704 { |
5759
|
705 octave_base_value *tmp = rep->try_narrowing_conversion (); |
2409
|
706 |
|
707 if (tmp && tmp != rep) |
|
708 { |
|
709 if (--rep->count == 0) |
|
710 delete rep; |
|
711 |
|
712 rep = tmp; |
|
713 } |
|
714 } |
|
715 |
4247
|
716 octave_value |
4271
|
717 octave_value::single_subsref (const std::string& type, |
|
718 const octave_value_list& idx) |
4247
|
719 { |
|
720 std::list<octave_value_list> i; |
|
721 |
|
722 i.push_back (idx); |
|
723 |
|
724 return rep->subsref (type, i); |
|
725 } |
|
726 |
2974
|
727 octave_value_list |
4247
|
728 octave_value::subsref (const std::string& type, |
4219
|
729 const std::list<octave_value_list>& idx, int nargout) |
3933
|
730 { |
|
731 if (is_constant ()) |
|
732 return rep->subsref (type, idx); |
|
733 else |
|
734 return rep->subsref (type, idx, nargout); |
|
735 } |
|
736 |
|
737 octave_value |
4247
|
738 octave_value::next_subsref (const std::string& type, |
4219
|
739 const std::list<octave_value_list>& idx, |
4233
|
740 size_t skip) |
3933
|
741 { |
4582
|
742 if (! error_state && idx.size () > skip) |
3933
|
743 { |
4219
|
744 std::list<octave_value_list> new_idx (idx); |
4233
|
745 for (size_t i = 0; i < skip; i++) |
4219
|
746 new_idx.erase (new_idx.begin ()); |
3933
|
747 return subsref (type.substr (skip), new_idx); |
|
748 } |
|
749 else |
|
750 return *this; |
|
751 } |
|
752 |
|
753 octave_value_list |
4994
|
754 octave_value::next_subsref (int nargout, const std::string& type, |
|
755 const std::list<octave_value_list>& idx, |
|
756 size_t skip) |
|
757 { |
|
758 if (! error_state && idx.size () > skip) |
|
759 { |
|
760 std::list<octave_value_list> new_idx (idx); |
|
761 for (size_t i = 0; i < skip; i++) |
|
762 new_idx.erase (new_idx.begin ()); |
|
763 return subsref (type.substr (skip), new_idx, nargout); |
|
764 } |
|
765 else |
|
766 return *this; |
|
767 } |
|
768 |
|
769 octave_value_list |
3544
|
770 octave_value::do_multi_index_op (int nargout, const octave_value_list& idx) |
2974
|
771 { |
3544
|
772 return rep->do_multi_index_op (nargout, idx); |
2974
|
773 } |
|
774 |
3933
|
775 #if 0 |
3204
|
776 static void |
3933
|
777 gripe_assign_failed (const std::string& on, const std::string& tn1, |
|
778 const std::string& tn2) |
3204
|
779 { |
|
780 error ("assignment failed for `%s %s %s'", |
|
781 tn1.c_str (), on.c_str (), tn2.c_str ()); |
|
782 } |
3933
|
783 #endif |
3204
|
784 |
|
785 static void |
3933
|
786 gripe_assign_failed_or_no_method (const std::string& on, |
|
787 const std::string& tn1, |
3523
|
788 const std::string& tn2) |
3204
|
789 { |
|
790 error ("assignment failed, or no method for `%s %s %s'", |
|
791 tn1.c_str (), on.c_str (), tn2.c_str ()); |
|
792 } |
|
793 |
3933
|
794 octave_value |
4247
|
795 octave_value::subsasgn (const std::string& type, |
4219
|
796 const std::list<octave_value_list>& idx, |
3933
|
797 const octave_value& rhs) |
|
798 { |
|
799 return rep->subsasgn (type, idx, rhs); |
|
800 } |
|
801 |
|
802 octave_value |
4247
|
803 octave_value::assign (assign_op op, const std::string& type, |
4219
|
804 const std::list<octave_value_list>& idx, |
3933
|
805 const octave_value& rhs) |
|
806 { |
|
807 octave_value retval; |
|
808 |
|
809 make_unique (); |
|
810 |
|
811 octave_value t_rhs = rhs; |
|
812 |
|
813 if (op != op_asn_eq) |
|
814 { |
5775
|
815 // FIXME -- only do the following stuff if we can't find |
3933
|
816 // a specific function to call to handle the op= operation for |
|
817 // the types we have. |
|
818 |
5001
|
819 octave_value t; |
|
820 if (is_constant ()) |
|
821 t = subsref (type, idx); |
|
822 else |
|
823 { |
|
824 octave_value_list tl = subsref (type, idx, 1); |
|
825 if (tl.length () > 0) |
|
826 t = tl(0); |
|
827 } |
3933
|
828 |
|
829 if (! error_state) |
|
830 { |
|
831 binary_op binop = op_eq_to_binary_op (op); |
|
832 |
|
833 if (! error_state) |
|
834 t_rhs = do_binary_op (binop, t, rhs); |
|
835 } |
|
836 } |
|
837 |
|
838 if (! error_state) |
|
839 { |
|
840 if (type[0] == '.' && ! is_map ()) |
|
841 { |
|
842 octave_value tmp = Octave_map (); |
|
843 retval = tmp.subsasgn (type, idx, t_rhs); |
|
844 } |
|
845 else |
|
846 retval = subsasgn (type, idx, t_rhs); |
|
847 } |
|
848 |
|
849 if (error_state) |
|
850 gripe_assign_failed_or_no_method (assign_op_as_string (op), |
|
851 type_name (), rhs.type_name ()); |
|
852 |
|
853 return retval; |
|
854 } |
|
855 |
|
856 const octave_value& |
3203
|
857 octave_value::assign (assign_op op, const octave_value& rhs) |
2880
|
858 { |
3533
|
859 if (op == op_asn_eq) |
3203
|
860 operator = (rhs); |
|
861 else |
|
862 { |
5775
|
863 // FIXME -- only do the following stuff if we can't find |
3204
|
864 // a specific function to call to handle the op= operation for |
|
865 // the types we have. |
|
866 |
|
867 binary_op binop = op_eq_to_binary_op (op); |
|
868 |
|
869 if (! error_state) |
|
870 { |
|
871 octave_value t = do_binary_op (binop, *this, rhs); |
|
872 |
|
873 if (! error_state) |
|
874 operator = (t); |
|
875 } |
|
876 |
|
877 if (error_state) |
|
878 gripe_assign_failed_or_no_method (assign_op_as_string (op), |
|
879 type_name (), rhs.type_name ()); |
|
880 } |
|
881 |
3933
|
882 return *this; |
2376
|
883 } |
|
884 |
5275
|
885 octave_idx_type |
4563
|
886 octave_value::length (void) const |
|
887 { |
|
888 int retval = 0; |
|
889 |
|
890 dim_vector dv = dims (); |
4584
|
891 |
4563
|
892 for (int i = 0; i < dv.length (); i++) |
|
893 { |
|
894 if (dv(i) < 0) |
|
895 { |
|
896 retval = -1; |
|
897 break; |
|
898 } |
|
899 |
4584
|
900 if (dv(i) == 0) |
|
901 { |
|
902 retval = 0; |
|
903 break; |
|
904 } |
|
905 |
4563
|
906 if (dv(i) > retval) |
|
907 retval = dv(i); |
|
908 } |
|
909 |
|
910 return retval; |
|
911 } |
|
912 |
5659
|
913 Matrix |
|
914 octave_value::size (void) const |
|
915 { |
|
916 dim_vector dv = dims (); |
|
917 |
|
918 int n_dims = dv.length (); |
|
919 |
|
920 Matrix retval (1, n_dims); |
|
921 |
|
922 while (n_dims--) |
|
923 retval(n_dims) = dv(n_dims); |
|
924 |
|
925 return retval; |
|
926 } |
|
927 |
3351
|
928 Cell |
|
929 octave_value::cell_value (void) const |
|
930 { |
|
931 return rep->cell_value (); |
|
932 } |
|
933 |
2376
|
934 Octave_map |
|
935 octave_value::map_value (void) const |
|
936 { |
|
937 return rep->map_value (); |
|
938 } |
|
939 |
4645
|
940 std::streamoff |
4643
|
941 octave_value::streamoff_value (void) const |
|
942 { |
|
943 return rep->streamoff_value (); |
|
944 } |
|
945 |
4645
|
946 streamoff_array |
|
947 octave_value::streamoff_array_value (void) const |
|
948 { |
|
949 return rep->streamoff_array_value (); |
|
950 } |
|
951 |
2974
|
952 octave_function * |
|
953 octave_value::function_value (bool silent) |
|
954 { |
|
955 return rep->function_value (silent); |
|
956 } |
|
957 |
4700
|
958 octave_user_function * |
|
959 octave_value::user_function_value (bool silent) |
|
960 { |
|
961 return rep->user_function_value (silent); |
|
962 } |
|
963 |
4346
|
964 octave_fcn_handle * |
4343
|
965 octave_value::fcn_handle_value (bool silent) |
|
966 { |
|
967 return rep->fcn_handle_value (silent); |
|
968 } |
|
969 |
4933
|
970 octave_fcn_inline * |
|
971 octave_value::fcn_inline_value (bool silent) |
|
972 { |
|
973 return rep->fcn_inline_value (silent); |
|
974 } |
|
975 |
2880
|
976 octave_value_list |
|
977 octave_value::list_value (void) const |
|
978 { |
|
979 return rep->list_value (); |
|
980 } |
|
981 |
2376
|
982 ColumnVector |
3419
|
983 octave_value::column_vector_value (bool force_string_conv, |
4661
|
984 bool /* frc_vec_conv */) const |
3419
|
985 { |
|
986 ColumnVector retval; |
|
987 |
|
988 Matrix m = matrix_value (force_string_conv); |
|
989 |
|
990 if (error_state) |
|
991 return retval; |
|
992 |
5275
|
993 octave_idx_type nr = m.rows (); |
|
994 octave_idx_type nc = m.columns (); |
3419
|
995 |
|
996 if (nc == 1) |
|
997 { |
|
998 retval.resize (nr); |
5275
|
999 for (octave_idx_type i = 0; i < nr; i++) |
3419
|
1000 retval (i) = m (i, 0); |
|
1001 } |
|
1002 else |
|
1003 { |
3523
|
1004 std::string tn = type_name (); |
3419
|
1005 gripe_invalid_conversion (tn.c_str (), "real column vector"); |
|
1006 } |
|
1007 |
|
1008 return retval; |
|
1009 } |
|
1010 |
|
1011 ComplexColumnVector |
|
1012 octave_value::complex_column_vector_value (bool force_string_conv, |
4661
|
1013 bool /* frc_vec_conv */) const |
3419
|
1014 { |
|
1015 ComplexColumnVector retval; |
|
1016 |
|
1017 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
1018 |
|
1019 if (error_state) |
|
1020 return retval; |
|
1021 |
5275
|
1022 octave_idx_type nr = m.rows (); |
|
1023 octave_idx_type nc = m.columns (); |
3419
|
1024 |
|
1025 if (nc == 1) |
|
1026 { |
3465
|
1027 retval.resize (nr); |
5275
|
1028 for (octave_idx_type i = 0; i < nr; i++) |
3419
|
1029 retval (i) = m (i, 0); |
|
1030 } |
|
1031 else |
|
1032 { |
3523
|
1033 std::string tn = type_name (); |
3419
|
1034 gripe_invalid_conversion (tn.c_str (), "complex column vector"); |
|
1035 } |
|
1036 |
|
1037 return retval; |
|
1038 } |
|
1039 |
|
1040 RowVector |
|
1041 octave_value::row_vector_value (bool force_string_conv, |
4661
|
1042 bool /* frc_vec_conv */) const |
3419
|
1043 { |
|
1044 RowVector retval; |
|
1045 |
|
1046 Matrix m = matrix_value (force_string_conv); |
|
1047 |
|
1048 if (error_state) |
|
1049 return retval; |
|
1050 |
5275
|
1051 octave_idx_type nr = m.rows (); |
|
1052 octave_idx_type nc = m.columns (); |
3419
|
1053 |
|
1054 if (nr == 1) |
|
1055 { |
|
1056 retval.resize (nc); |
5275
|
1057 for (octave_idx_type i = 0; i < nc; i++) |
3419
|
1058 retval (i) = m (0, i); |
|
1059 } |
|
1060 else |
|
1061 { |
3523
|
1062 std::string tn = type_name (); |
3419
|
1063 gripe_invalid_conversion (tn.c_str (), "real row vector"); |
|
1064 } |
|
1065 |
|
1066 return retval; |
|
1067 } |
|
1068 |
|
1069 ComplexRowVector |
|
1070 octave_value::complex_row_vector_value (bool force_string_conv, |
4661
|
1071 bool /* frc_vec_conv */) const |
3419
|
1072 { |
|
1073 ComplexRowVector retval; |
|
1074 |
|
1075 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
1076 |
|
1077 if (error_state) |
|
1078 return retval; |
|
1079 |
5275
|
1080 octave_idx_type nr = m.rows (); |
|
1081 octave_idx_type nc = m.columns (); |
3419
|
1082 |
|
1083 if (nr == 1) |
|
1084 { |
|
1085 retval.resize (nc); |
5275
|
1086 for (octave_idx_type i = 0; i < nc; i++) |
3419
|
1087 retval (i) = m (0, i); |
|
1088 } |
|
1089 else |
|
1090 { |
3523
|
1091 std::string tn = type_name (); |
3419
|
1092 gripe_invalid_conversion (tn.c_str (), "complex row vector"); |
|
1093 } |
|
1094 |
|
1095 return retval; |
|
1096 } |
|
1097 |
|
1098 // Sloppy... |
|
1099 |
|
1100 Array<double> |
2376
|
1101 octave_value::vector_value (bool force_string_conv, |
|
1102 bool force_vector_conversion) const |
|
1103 { |
3419
|
1104 Array<double> retval; |
2376
|
1105 |
|
1106 Matrix m = matrix_value (force_string_conv); |
|
1107 |
|
1108 if (error_state) |
|
1109 return retval; |
|
1110 |
5275
|
1111 octave_idx_type nr = m.rows (); |
|
1112 octave_idx_type nc = m.columns (); |
2376
|
1113 |
|
1114 if (nr == 1) |
|
1115 { |
|
1116 retval.resize (nc); |
5275
|
1117 for (octave_idx_type i = 0; i < nc; i++) |
2376
|
1118 retval (i) = m (0, i); |
|
1119 } |
|
1120 else if (nc == 1) |
|
1121 { |
|
1122 retval.resize (nr); |
5275
|
1123 for (octave_idx_type i = 0; i < nr; i++) |
2376
|
1124 retval (i) = m (i, 0); |
|
1125 } |
4455
|
1126 else if (nr > 0 && nc > 0) |
2376
|
1127 { |
5781
|
1128 if (! force_vector_conversion) |
|
1129 gripe_implicit_conversion ("Octave:array-as-vector", |
|
1130 type_name (), "real vector"); |
4455
|
1131 |
2376
|
1132 retval.resize (nr * nc); |
5275
|
1133 octave_idx_type k = 0; |
|
1134 for (octave_idx_type j = 0; j < nc; j++) |
|
1135 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
1136 { |
|
1137 OCTAVE_QUIT; |
|
1138 |
|
1139 retval (k++) = m (i, j); |
|
1140 } |
2376
|
1141 } |
|
1142 else |
|
1143 { |
3523
|
1144 std::string tn = type_name (); |
2376
|
1145 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
1146 } |
|
1147 |
|
1148 return retval; |
|
1149 } |
|
1150 |
4044
|
1151 Array<int> |
|
1152 octave_value::int_vector_value (bool force_string_conv, bool require_int, |
|
1153 bool force_vector_conversion) const |
|
1154 { |
|
1155 Array<int> retval; |
|
1156 |
|
1157 Matrix m = matrix_value (force_string_conv); |
|
1158 |
|
1159 if (error_state) |
|
1160 return retval; |
|
1161 |
5275
|
1162 octave_idx_type nr = m.rows (); |
|
1163 octave_idx_type nc = m.columns (); |
4044
|
1164 |
|
1165 if (nr == 1) |
|
1166 { |
|
1167 retval.resize (nc); |
5275
|
1168 for (octave_idx_type i = 0; i < nc; i++) |
4044
|
1169 { |
4153
|
1170 OCTAVE_QUIT; |
|
1171 |
4044
|
1172 double d = m (0, i); |
|
1173 |
|
1174 if (require_int && D_NINT (d) != d) |
|
1175 { |
|
1176 error ("conversion to integer value failed"); |
|
1177 return retval; |
|
1178 } |
|
1179 |
|
1180 retval (i) = static_cast<int> (d); |
|
1181 } |
|
1182 } |
|
1183 else if (nc == 1) |
|
1184 { |
|
1185 retval.resize (nr); |
5275
|
1186 for (octave_idx_type i = 0; i < nr; i++) |
4044
|
1187 { |
4153
|
1188 OCTAVE_QUIT; |
|
1189 |
4044
|
1190 double d = m (i, 0); |
|
1191 |
|
1192 if (require_int && D_NINT (d) != d) |
|
1193 { |
|
1194 error ("conversion to integer value failed"); |
|
1195 return retval; |
|
1196 } |
|
1197 |
|
1198 retval (i) = static_cast<int> (d); |
|
1199 } |
|
1200 } |
4455
|
1201 else if (nr > 0 && nc > 0) |
4044
|
1202 { |
5781
|
1203 if (! force_vector_conversion) |
|
1204 gripe_implicit_conversion ("Octave:array-as-vector", |
|
1205 type_name (), "real vector"); |
4455
|
1206 |
4044
|
1207 retval.resize (nr * nc); |
5275
|
1208 octave_idx_type k = 0; |
|
1209 for (octave_idx_type j = 0; j < nc; j++) |
4044
|
1210 { |
5275
|
1211 for (octave_idx_type i = 0; i < nr; i++) |
4044
|
1212 { |
4153
|
1213 OCTAVE_QUIT; |
|
1214 |
4044
|
1215 double d = m (i, j); |
|
1216 |
|
1217 if (require_int && D_NINT (d) != d) |
|
1218 { |
|
1219 error ("conversion to integer value failed"); |
|
1220 return retval; |
|
1221 } |
|
1222 |
|
1223 retval (k++) = static_cast<int> (d); |
|
1224 } |
|
1225 } |
|
1226 } |
|
1227 else |
|
1228 { |
|
1229 std::string tn = type_name (); |
|
1230 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
1231 } |
|
1232 |
|
1233 return retval; |
|
1234 } |
|
1235 |
3419
|
1236 Array<Complex> |
2376
|
1237 octave_value::complex_vector_value (bool force_string_conv, |
|
1238 bool force_vector_conversion) const |
|
1239 { |
3419
|
1240 Array<Complex> retval; |
2376
|
1241 |
|
1242 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
1243 |
|
1244 if (error_state) |
|
1245 return retval; |
|
1246 |
5275
|
1247 octave_idx_type nr = m.rows (); |
|
1248 octave_idx_type nc = m.columns (); |
2376
|
1249 |
|
1250 if (nr == 1) |
|
1251 { |
|
1252 retval.resize (nc); |
5275
|
1253 for (octave_idx_type i = 0; i < nc; i++) |
4153
|
1254 { |
|
1255 OCTAVE_QUIT; |
|
1256 retval (i) = m (0, i); |
|
1257 } |
2376
|
1258 } |
|
1259 else if (nc == 1) |
|
1260 { |
|
1261 retval.resize (nr); |
5275
|
1262 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
1263 { |
|
1264 OCTAVE_QUIT; |
|
1265 retval (i) = m (i, 0); |
|
1266 } |
2376
|
1267 } |
4455
|
1268 else if (nr > 0 && nc > 0) |
2376
|
1269 { |
5781
|
1270 if (! force_vector_conversion) |
|
1271 gripe_implicit_conversion ("Octave:array-as-vector", |
|
1272 type_name (), "complex vector"); |
4455
|
1273 |
2376
|
1274 retval.resize (nr * nc); |
5275
|
1275 octave_idx_type k = 0; |
|
1276 for (octave_idx_type j = 0; j < nc; j++) |
|
1277 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
1278 { |
|
1279 OCTAVE_QUIT; |
|
1280 retval (k++) = m (i, j); |
|
1281 } |
2376
|
1282 } |
|
1283 else |
|
1284 { |
3523
|
1285 std::string tn = type_name (); |
2376
|
1286 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
|
1287 } |
|
1288 |
|
1289 return retval; |
|
1290 } |
|
1291 |
4944
|
1292 int |
|
1293 octave_value::write (octave_stream& os, int block_size, |
|
1294 oct_data_conv::data_type output_type, int skip, |
|
1295 oct_mach_info::float_format flt_fmt) const |
|
1296 { |
|
1297 return rep->write (os, block_size, output_type, skip, flt_fmt); |
|
1298 } |
|
1299 |
2413
|
1300 static void |
3933
|
1301 gripe_binary_op (const std::string& on, const std::string& tn1, |
|
1302 const std::string& tn2) |
2376
|
1303 { |
2903
|
1304 error ("binary operator `%s' not implemented for `%s' by `%s' operations", |
2376
|
1305 on.c_str (), tn1.c_str (), tn2.c_str ()); |
|
1306 } |
|
1307 |
3203
|
1308 static void |
3523
|
1309 gripe_binary_op_conv (const std::string& on) |
3203
|
1310 { |
|
1311 error ("type conversion failed for binary operator `%s'", on.c_str ()); |
|
1312 } |
|
1313 |
2376
|
1314 octave_value |
3933
|
1315 do_binary_op (octave_value::binary_op op, |
|
1316 const octave_value& v1, const octave_value& v2) |
2376
|
1317 { |
|
1318 octave_value retval; |
|
1319 |
|
1320 int t1 = v1.type_id (); |
|
1321 int t2 = v2.type_id (); |
|
1322 |
5759
|
1323 octave_value_typeinfo::binary_op_fcn f |
|
1324 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
1325 |
|
1326 if (f) |
|
1327 retval = f (*v1.rep, *v2.rep); |
|
1328 else |
|
1329 { |
|
1330 octave_value tv1; |
5759
|
1331 octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); |
2376
|
1332 |
|
1333 if (cf1) |
|
1334 { |
5759
|
1335 octave_base_value *tmp = cf1 (*v1.rep); |
3203
|
1336 |
|
1337 if (tmp) |
|
1338 { |
|
1339 tv1 = octave_value (tmp); |
|
1340 t1 = tv1.type_id (); |
|
1341 } |
|
1342 else |
|
1343 { |
|
1344 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); |
|
1345 return retval; |
|
1346 } |
2376
|
1347 } |
|
1348 else |
|
1349 tv1 = v1; |
|
1350 |
|
1351 octave_value tv2; |
5759
|
1352 octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); |
2376
|
1353 |
|
1354 if (cf2) |
|
1355 { |
5759
|
1356 octave_base_value *tmp = cf2 (*v2.rep); |
3203
|
1357 |
|
1358 if (tmp) |
|
1359 { |
|
1360 tv2 = octave_value (tmp); |
|
1361 t2 = tv2.type_id (); |
|
1362 } |
|
1363 else |
|
1364 { |
|
1365 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); |
|
1366 return retval; |
|
1367 } |
2376
|
1368 } |
|
1369 else |
|
1370 tv2 = v2; |
|
1371 |
|
1372 if (cf1 || cf2) |
|
1373 { |
4587
|
1374 f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
1375 |
|
1376 if (f) |
|
1377 retval = f (*tv1.rep, *tv2.rep); |
|
1378 else |
|
1379 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
1380 v1.type_name (), v2.type_name ()); |
|
1381 } |
|
1382 else |
|
1383 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
1384 v1.type_name (), v2.type_name ()); |
|
1385 } |
|
1386 |
|
1387 return retval; |
|
1388 } |
|
1389 |
4915
|
1390 static void |
|
1391 gripe_cat_op (const std::string& tn1, const std::string& tn2) |
|
1392 { |
|
1393 error ("concatenation operator not implemented for `%s' by `%s' operations", |
|
1394 tn1.c_str (), tn2.c_str ()); |
|
1395 } |
|
1396 |
|
1397 static void |
|
1398 gripe_cat_op_conv (void) |
|
1399 { |
|
1400 error ("type conversion failed for concatenation operator"); |
|
1401 } |
|
1402 |
|
1403 octave_value |
|
1404 do_cat_op (const octave_value& v1, const octave_value& v2, |
|
1405 const Array<int>& ra_idx) |
|
1406 { |
|
1407 octave_value retval; |
|
1408 |
5164
|
1409 // Rapid return for concatenation with an empty object. Dimension |
|
1410 // checking handled elsewhere. |
|
1411 if (v1.all_zero_dims ()) |
|
1412 return v2; |
|
1413 if (v2.all_zero_dims ()) |
|
1414 return v1; |
|
1415 |
4915
|
1416 int t1 = v1.type_id (); |
|
1417 int t2 = v2.type_id (); |
|
1418 |
5759
|
1419 octave_value_typeinfo::cat_op_fcn f |
|
1420 = octave_value_typeinfo::lookup_cat_op (t1, t2); |
4915
|
1421 |
|
1422 if (f) |
|
1423 retval = f (*v1.rep, *v2.rep, ra_idx); |
|
1424 else |
|
1425 { |
|
1426 octave_value tv1; |
5759
|
1427 octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); |
4915
|
1428 |
|
1429 if (cf1) |
|
1430 { |
5759
|
1431 octave_base_value *tmp = cf1 (*v1.rep); |
4915
|
1432 |
|
1433 if (tmp) |
|
1434 { |
|
1435 tv1 = octave_value (tmp); |
|
1436 t1 = tv1.type_id (); |
|
1437 } |
|
1438 else |
|
1439 { |
|
1440 gripe_cat_op_conv (); |
|
1441 return retval; |
|
1442 } |
|
1443 } |
|
1444 else |
|
1445 tv1 = v1; |
|
1446 |
|
1447 octave_value tv2; |
5759
|
1448 octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); |
4915
|
1449 |
|
1450 if (cf2) |
|
1451 { |
5759
|
1452 octave_base_value *tmp = cf2 (*v2.rep); |
4915
|
1453 |
|
1454 if (tmp) |
|
1455 { |
|
1456 tv2 = octave_value (tmp); |
|
1457 t2 = tv2.type_id (); |
|
1458 } |
|
1459 else |
|
1460 { |
|
1461 gripe_cat_op_conv (); |
|
1462 return retval; |
|
1463 } |
|
1464 } |
|
1465 else |
|
1466 tv2 = v2; |
|
1467 |
|
1468 if (cf1 || cf2) |
|
1469 { |
|
1470 f = octave_value_typeinfo::lookup_cat_op (t1, t2); |
|
1471 |
|
1472 if (f) |
|
1473 retval = f (*tv1.rep, *tv2.rep, ra_idx); |
|
1474 else |
|
1475 gripe_cat_op (v1.type_name (), v2.type_name ()); |
|
1476 } |
|
1477 else |
|
1478 gripe_cat_op (v1.type_name (), v2.type_name ()); |
|
1479 } |
|
1480 |
|
1481 return retval; |
|
1482 } |
|
1483 |
3933
|
1484 void |
|
1485 octave_value::print_info (std::ostream& os, const std::string& prefix) const |
|
1486 { |
|
1487 os << prefix << "type_name: " << type_name () << "\n" |
|
1488 << prefix << "count: " << get_count () << "\n" |
|
1489 << prefix << "rep info: "; |
|
1490 |
|
1491 rep->print_info (os, prefix + " "); |
|
1492 } |
|
1493 |
3203
|
1494 static void |
3523
|
1495 gripe_unary_op (const std::string& on, const std::string& tn) |
3203
|
1496 { |
|
1497 error ("unary operator `%s' not implemented for `%s' operands", |
|
1498 on.c_str (), tn.c_str ()); |
|
1499 } |
|
1500 |
|
1501 static void |
3523
|
1502 gripe_unary_op_conv (const std::string& on) |
3203
|
1503 { |
|
1504 error ("type conversion failed for unary operator `%s'", on.c_str ()); |
|
1505 } |
|
1506 |
|
1507 octave_value |
|
1508 do_unary_op (octave_value::unary_op op, const octave_value& v) |
|
1509 { |
|
1510 octave_value retval; |
|
1511 |
|
1512 int t = v.type_id (); |
|
1513 |
5759
|
1514 octave_value_typeinfo::unary_op_fcn f |
|
1515 = octave_value_typeinfo::lookup_unary_op (op, t); |
3203
|
1516 |
|
1517 if (f) |
|
1518 retval = f (*v.rep); |
|
1519 else |
|
1520 { |
|
1521 octave_value tv; |
5759
|
1522 octave_base_value::type_conv_fcn cf = v.numeric_conversion_function (); |
3203
|
1523 |
|
1524 if (cf) |
|
1525 { |
5759
|
1526 octave_base_value *tmp = cf (*v.rep); |
3203
|
1527 |
|
1528 if (tmp) |
|
1529 { |
|
1530 tv = octave_value (tmp); |
|
1531 t = tv.type_id (); |
|
1532 |
4587
|
1533 f = octave_value_typeinfo::lookup_unary_op (op, t); |
3203
|
1534 |
|
1535 if (f) |
|
1536 retval = f (*tv.rep); |
|
1537 else |
|
1538 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1539 v.type_name ()); |
|
1540 } |
|
1541 else |
|
1542 gripe_unary_op_conv (octave_value::unary_op_as_string (op)); |
|
1543 } |
|
1544 else |
|
1545 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1546 v.type_name ()); |
|
1547 } |
|
1548 |
|
1549 return retval; |
|
1550 } |
|
1551 |
|
1552 static void |
3933
|
1553 gripe_unary_op_conversion_failed (const std::string& op, |
|
1554 const std::string& tn) |
3203
|
1555 { |
|
1556 error ("operator %s: type conversion for `%s' failed", |
|
1557 op.c_str (), tn.c_str ()); |
|
1558 } |
|
1559 |
3933
|
1560 const octave_value& |
|
1561 octave_value::do_non_const_unary_op (unary_op op) |
3203
|
1562 { |
|
1563 octave_value retval; |
|
1564 |
|
1565 int t = type_id (); |
|
1566 |
5759
|
1567 octave_value_typeinfo::non_const_unary_op_fcn f |
3203
|
1568 = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
|
1569 |
|
1570 if (f) |
|
1571 { |
|
1572 make_unique (); |
|
1573 |
|
1574 f (*rep); |
|
1575 } |
|
1576 else |
|
1577 { |
5759
|
1578 octave_base_value::type_conv_fcn cf = numeric_conversion_function (); |
3203
|
1579 |
|
1580 if (cf) |
|
1581 { |
5759
|
1582 octave_base_value *tmp = cf (*rep); |
3203
|
1583 |
|
1584 if (tmp) |
|
1585 { |
5759
|
1586 octave_base_value *old_rep = rep; |
3203
|
1587 rep = tmp; |
|
1588 |
|
1589 t = type_id (); |
|
1590 |
|
1591 f = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
|
1592 |
|
1593 if (f) |
|
1594 { |
|
1595 f (*rep); |
|
1596 |
|
1597 if (old_rep && --old_rep->count == 0) |
|
1598 delete old_rep; |
|
1599 } |
|
1600 else |
|
1601 { |
|
1602 if (old_rep) |
|
1603 { |
|
1604 if (--rep->count == 0) |
|
1605 delete rep; |
|
1606 |
|
1607 rep = old_rep; |
|
1608 } |
|
1609 |
|
1610 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1611 type_name ()); |
|
1612 } |
|
1613 } |
|
1614 else |
|
1615 gripe_unary_op_conversion_failed |
|
1616 (octave_value::unary_op_as_string (op), type_name ()); |
|
1617 } |
|
1618 else |
|
1619 gripe_unary_op (octave_value::unary_op_as_string (op), type_name ()); |
|
1620 } |
3933
|
1621 |
|
1622 return *this; |
3203
|
1623 } |
|
1624 |
3933
|
1625 #if 0 |
3205
|
1626 static void |
3933
|
1627 gripe_unary_op_failed_or_no_method (const std::string& on, |
|
1628 const std::string& tn) |
3205
|
1629 { |
|
1630 error ("operator %s: no method, or unable to evaluate for %s operand", |
|
1631 on.c_str (), tn.c_str ()); |
|
1632 } |
3933
|
1633 #endif |
3205
|
1634 |
|
1635 void |
4661
|
1636 octave_value::do_non_const_unary_op (unary_op, const octave_value_list&) |
3205
|
1637 { |
3933
|
1638 abort (); |
|
1639 } |
|
1640 |
|
1641 octave_value |
4247
|
1642 octave_value::do_non_const_unary_op (unary_op op, const std::string& type, |
4219
|
1643 const std::list<octave_value_list>& idx) |
3933
|
1644 { |
|
1645 octave_value retval; |
|
1646 |
|
1647 if (idx.empty ()) |
|
1648 { |
|
1649 do_non_const_unary_op (op); |
3205
|
1650 |
3933
|
1651 retval = *this; |
|
1652 } |
|
1653 else |
|
1654 { |
5775
|
1655 // FIXME -- only do the following stuff if we can't find a |
3933
|
1656 // specific function to call to handle the op= operation for the |
|
1657 // types we have. |
3205
|
1658 |
3933
|
1659 assign_op assop = unary_op_to_assign_op (op); |
|
1660 |
|
1661 retval = assign (assop, type, idx, 1.0); |
|
1662 } |
|
1663 |
|
1664 return retval; |
3205
|
1665 } |
|
1666 |
|
1667 octave_value::assign_op |
|
1668 octave_value::unary_op_to_assign_op (unary_op op) |
|
1669 { |
|
1670 assign_op binop = unknown_assign_op; |
|
1671 |
|
1672 switch (op) |
|
1673 { |
3533
|
1674 case op_incr: |
|
1675 binop = op_add_eq; |
3205
|
1676 break; |
|
1677 |
3533
|
1678 case op_decr: |
|
1679 binop = op_sub_eq; |
3205
|
1680 break; |
|
1681 |
|
1682 default: |
|
1683 { |
3523
|
1684 std::string on = unary_op_as_string (op); |
3205
|
1685 error ("operator %s: no assign operator found", on.c_str ()); |
|
1686 } |
|
1687 } |
|
1688 |
|
1689 return binop; |
|
1690 } |
|
1691 |
3204
|
1692 octave_value::binary_op |
|
1693 octave_value::op_eq_to_binary_op (assign_op op) |
|
1694 { |
|
1695 binary_op binop = unknown_binary_op; |
|
1696 |
|
1697 switch (op) |
|
1698 { |
3533
|
1699 case op_add_eq: |
|
1700 binop = op_add; |
3204
|
1701 break; |
|
1702 |
3533
|
1703 case op_sub_eq: |
|
1704 binop = op_sub; |
3204
|
1705 break; |
|
1706 |
3533
|
1707 case op_mul_eq: |
|
1708 binop = op_mul; |
3204
|
1709 break; |
|
1710 |
3533
|
1711 case op_div_eq: |
|
1712 binop = op_div; |
3204
|
1713 break; |
|
1714 |
3533
|
1715 case op_ldiv_eq: |
|
1716 binop = op_ldiv; |
3204
|
1717 break; |
|
1718 |
4018
|
1719 case op_pow_eq: |
|
1720 binop = op_pow; |
|
1721 break; |
|
1722 |
3533
|
1723 case op_lshift_eq: |
|
1724 binop = op_lshift; |
3204
|
1725 break; |
|
1726 |
3533
|
1727 case op_rshift_eq: |
|
1728 binop = op_rshift; |
3204
|
1729 break; |
|
1730 |
3533
|
1731 case op_el_mul_eq: |
|
1732 binop = op_el_mul; |
3204
|
1733 break; |
|
1734 |
3533
|
1735 case op_el_div_eq: |
|
1736 binop = op_el_div; |
3204
|
1737 break; |
|
1738 |
3533
|
1739 case op_el_ldiv_eq: |
|
1740 binop = op_el_ldiv; |
3204
|
1741 break; |
|
1742 |
4018
|
1743 case op_el_pow_eq: |
|
1744 binop = op_el_pow; |
|
1745 break; |
|
1746 |
3533
|
1747 case op_el_and_eq: |
|
1748 binop = op_el_and; |
3204
|
1749 break; |
|
1750 |
3533
|
1751 case op_el_or_eq: |
|
1752 binop = op_el_or; |
3204
|
1753 break; |
|
1754 |
|
1755 default: |
|
1756 { |
3523
|
1757 std::string on = assign_op_as_string (op); |
3204
|
1758 error ("operator %s: no binary operator found", on.c_str ()); |
|
1759 } |
|
1760 } |
|
1761 |
|
1762 return binop; |
|
1763 } |
|
1764 |
3933
|
1765 octave_value |
|
1766 octave_value::empty_conv (const std::string& type, const octave_value& rhs) |
|
1767 { |
|
1768 octave_value retval; |
|
1769 |
|
1770 if (type.length () > 0) |
|
1771 { |
|
1772 switch (type[0]) |
|
1773 { |
|
1774 case '(': |
|
1775 { |
|
1776 if (type.length () > 1 && type[1] == '.') |
|
1777 retval = Octave_map (); |
|
1778 else |
|
1779 retval = octave_value (rhs.empty_clone ()); |
|
1780 } |
|
1781 break; |
|
1782 |
|
1783 case '{': |
|
1784 retval = Cell (); |
|
1785 break; |
|
1786 |
|
1787 case '.': |
|
1788 retval = Octave_map (); |
|
1789 break; |
|
1790 |
|
1791 default: |
|
1792 panic_impossible (); |
|
1793 } |
|
1794 } |
|
1795 else |
|
1796 retval = octave_value (rhs.empty_clone ()); |
|
1797 |
|
1798 return retval; |
|
1799 } |
|
1800 |
2376
|
1801 void |
|
1802 install_types (void) |
|
1803 { |
|
1804 octave_base_value::register_type (); |
3928
|
1805 octave_cell::register_type (); |
2376
|
1806 octave_scalar::register_type (); |
|
1807 octave_complex::register_type (); |
|
1808 octave_matrix::register_type (); |
|
1809 octave_complex_matrix::register_type (); |
|
1810 octave_range::register_type (); |
2825
|
1811 octave_bool::register_type (); |
|
1812 octave_bool_matrix::register_type (); |
2376
|
1813 octave_char_matrix::register_type (); |
|
1814 octave_char_matrix_str::register_type (); |
5279
|
1815 octave_char_matrix_sq_str::register_type (); |
4901
|
1816 octave_int8_scalar::register_type (); |
|
1817 octave_int16_scalar::register_type (); |
|
1818 octave_int32_scalar::register_type (); |
|
1819 octave_int64_scalar::register_type (); |
|
1820 octave_uint8_scalar::register_type (); |
|
1821 octave_uint16_scalar::register_type (); |
|
1822 octave_uint32_scalar::register_type (); |
|
1823 octave_uint64_scalar::register_type (); |
|
1824 octave_int8_matrix::register_type (); |
|
1825 octave_int16_matrix::register_type (); |
|
1826 octave_int32_matrix::register_type (); |
|
1827 octave_int64_matrix::register_type (); |
|
1828 octave_uint8_matrix::register_type (); |
|
1829 octave_uint16_matrix::register_type (); |
|
1830 octave_uint32_matrix::register_type (); |
|
1831 octave_uint64_matrix::register_type (); |
5164
|
1832 octave_sparse_bool_matrix::register_type (); |
|
1833 octave_sparse_matrix::register_type (); |
|
1834 octave_sparse_complex_matrix::register_type (); |
2376
|
1835 octave_struct::register_type (); |
2880
|
1836 octave_list::register_type (); |
3977
|
1837 octave_cs_list::register_type (); |
2376
|
1838 octave_magic_colon::register_type (); |
2974
|
1839 octave_builtin::register_type (); |
|
1840 octave_mapper::register_type (); |
|
1841 octave_user_function::register_type (); |
4649
|
1842 octave_dld_function::register_type (); |
4643
|
1843 octave_fcn_handle::register_type (); |
4966
|
1844 octave_fcn_inline::register_type (); |
4643
|
1845 octave_streamoff::register_type (); |
2376
|
1846 } |
|
1847 |
4970
|
1848 #if 0 |
|
1849 DEFUN (cast, args, , |
|
1850 "-*- texinfo -*-\n\ |
|
1851 @deftypefn {Built-in Function} {} cast (@var{val}, @var{type})\n\ |
|
1852 Convert @var{val} to the new data type @var{type}.\n\ |
5642
|
1853 @seealso{class, typeinfo}\n\ |
|
1854 @end deftypefn") |
4970
|
1855 { |
|
1856 octave_value retval; |
|
1857 |
|
1858 if (args.length () == 2) |
|
1859 error ("cast: not implemented"); |
|
1860 else |
5823
|
1861 print_usage (); |
4970
|
1862 |
|
1863 return retval; |
|
1864 } |
|
1865 #endif |
|
1866 |
4791
|
1867 DEFUN (sizeof, args, , |
|
1868 "-*- texinfo -*-\n\ |
|
1869 @deftypefn {Built-in Function} {} sizeof (@var{val})\n\ |
|
1870 Return the size of @var{val} in bytes\n\ |
|
1871 @end deftypefn") |
|
1872 { |
|
1873 octave_value retval; |
|
1874 |
|
1875 if (args.length () == 1) |
|
1876 retval = args(0).byte_size (); |
|
1877 else |
5823
|
1878 print_usage (); |
4791
|
1879 |
|
1880 return retval; |
|
1881 } |
|
1882 |
2376
|
1883 /* |
|
1884 ;;; Local Variables: *** |
|
1885 ;;; mode: C++ *** |
|
1886 ;;; End: *** |
|
1887 */ |