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 |
5759
|
684 octave_base_value * |
3933
|
685 octave_value::clone (void) const |
2880
|
686 { |
|
687 panic_impossible (); |
3546
|
688 return 0; |
2880
|
689 } |
|
690 |
2409
|
691 void |
|
692 octave_value::maybe_mutate (void) |
|
693 { |
5759
|
694 octave_base_value *tmp = rep->try_narrowing_conversion (); |
2409
|
695 |
|
696 if (tmp && tmp != rep) |
|
697 { |
|
698 if (--rep->count == 0) |
|
699 delete rep; |
|
700 |
|
701 rep = tmp; |
|
702 } |
|
703 } |
|
704 |
4247
|
705 octave_value |
4271
|
706 octave_value::single_subsref (const std::string& type, |
|
707 const octave_value_list& idx) |
4247
|
708 { |
|
709 std::list<octave_value_list> i; |
|
710 |
|
711 i.push_back (idx); |
|
712 |
|
713 return rep->subsref (type, i); |
|
714 } |
|
715 |
2974
|
716 octave_value_list |
4247
|
717 octave_value::subsref (const std::string& type, |
4219
|
718 const std::list<octave_value_list>& idx, int nargout) |
3933
|
719 { |
|
720 if (is_constant ()) |
|
721 return rep->subsref (type, idx); |
|
722 else |
|
723 return rep->subsref (type, idx, nargout); |
|
724 } |
|
725 |
|
726 octave_value |
4247
|
727 octave_value::next_subsref (const std::string& type, |
4219
|
728 const std::list<octave_value_list>& idx, |
4233
|
729 size_t skip) |
3933
|
730 { |
4582
|
731 if (! error_state && idx.size () > skip) |
3933
|
732 { |
4219
|
733 std::list<octave_value_list> new_idx (idx); |
4233
|
734 for (size_t i = 0; i < skip; i++) |
4219
|
735 new_idx.erase (new_idx.begin ()); |
3933
|
736 return subsref (type.substr (skip), new_idx); |
|
737 } |
|
738 else |
|
739 return *this; |
|
740 } |
|
741 |
|
742 octave_value_list |
4994
|
743 octave_value::next_subsref (int nargout, const std::string& type, |
|
744 const std::list<octave_value_list>& idx, |
|
745 size_t skip) |
|
746 { |
|
747 if (! error_state && idx.size () > skip) |
|
748 { |
|
749 std::list<octave_value_list> new_idx (idx); |
|
750 for (size_t i = 0; i < skip; i++) |
|
751 new_idx.erase (new_idx.begin ()); |
|
752 return subsref (type.substr (skip), new_idx, nargout); |
|
753 } |
|
754 else |
|
755 return *this; |
|
756 } |
|
757 |
|
758 octave_value_list |
3544
|
759 octave_value::do_multi_index_op (int nargout, const octave_value_list& idx) |
2974
|
760 { |
3544
|
761 return rep->do_multi_index_op (nargout, idx); |
2974
|
762 } |
|
763 |
3933
|
764 #if 0 |
3204
|
765 static void |
3933
|
766 gripe_assign_failed (const std::string& on, const std::string& tn1, |
|
767 const std::string& tn2) |
3204
|
768 { |
|
769 error ("assignment failed for `%s %s %s'", |
|
770 tn1.c_str (), on.c_str (), tn2.c_str ()); |
|
771 } |
3933
|
772 #endif |
3204
|
773 |
|
774 static void |
3933
|
775 gripe_assign_failed_or_no_method (const std::string& on, |
|
776 const std::string& tn1, |
3523
|
777 const std::string& tn2) |
3204
|
778 { |
|
779 error ("assignment failed, or no method for `%s %s %s'", |
|
780 tn1.c_str (), on.c_str (), tn2.c_str ()); |
|
781 } |
|
782 |
3933
|
783 octave_value |
4247
|
784 octave_value::subsasgn (const std::string& type, |
4219
|
785 const std::list<octave_value_list>& idx, |
3933
|
786 const octave_value& rhs) |
|
787 { |
|
788 return rep->subsasgn (type, idx, rhs); |
|
789 } |
|
790 |
|
791 octave_value |
4247
|
792 octave_value::assign (assign_op op, const std::string& type, |
4219
|
793 const std::list<octave_value_list>& idx, |
3933
|
794 const octave_value& rhs) |
|
795 { |
|
796 octave_value retval; |
|
797 |
|
798 make_unique (); |
|
799 |
|
800 octave_value t_rhs = rhs; |
|
801 |
|
802 if (op != op_asn_eq) |
|
803 { |
5775
|
804 // FIXME -- only do the following stuff if we can't find |
3933
|
805 // a specific function to call to handle the op= operation for |
|
806 // the types we have. |
|
807 |
5001
|
808 octave_value t; |
|
809 if (is_constant ()) |
|
810 t = subsref (type, idx); |
|
811 else |
|
812 { |
|
813 octave_value_list tl = subsref (type, idx, 1); |
|
814 if (tl.length () > 0) |
|
815 t = tl(0); |
|
816 } |
3933
|
817 |
|
818 if (! error_state) |
|
819 { |
|
820 binary_op binop = op_eq_to_binary_op (op); |
|
821 |
|
822 if (! error_state) |
|
823 t_rhs = do_binary_op (binop, t, rhs); |
|
824 } |
|
825 } |
|
826 |
|
827 if (! error_state) |
|
828 { |
|
829 if (type[0] == '.' && ! is_map ()) |
|
830 { |
|
831 octave_value tmp = Octave_map (); |
|
832 retval = tmp.subsasgn (type, idx, t_rhs); |
|
833 } |
|
834 else |
|
835 retval = subsasgn (type, idx, t_rhs); |
|
836 } |
|
837 |
|
838 if (error_state) |
|
839 gripe_assign_failed_or_no_method (assign_op_as_string (op), |
|
840 type_name (), rhs.type_name ()); |
|
841 |
|
842 return retval; |
|
843 } |
|
844 |
|
845 const octave_value& |
3203
|
846 octave_value::assign (assign_op op, const octave_value& rhs) |
2880
|
847 { |
3533
|
848 if (op == op_asn_eq) |
3203
|
849 operator = (rhs); |
|
850 else |
|
851 { |
5775
|
852 // FIXME -- only do the following stuff if we can't find |
3204
|
853 // a specific function to call to handle the op= operation for |
|
854 // the types we have. |
|
855 |
|
856 binary_op binop = op_eq_to_binary_op (op); |
|
857 |
|
858 if (! error_state) |
|
859 { |
|
860 octave_value t = do_binary_op (binop, *this, rhs); |
|
861 |
|
862 if (! error_state) |
|
863 operator = (t); |
|
864 } |
|
865 |
|
866 if (error_state) |
|
867 gripe_assign_failed_or_no_method (assign_op_as_string (op), |
|
868 type_name (), rhs.type_name ()); |
|
869 } |
|
870 |
3933
|
871 return *this; |
2376
|
872 } |
|
873 |
5275
|
874 octave_idx_type |
4563
|
875 octave_value::length (void) const |
|
876 { |
|
877 int retval = 0; |
|
878 |
|
879 dim_vector dv = dims (); |
4584
|
880 |
4563
|
881 for (int i = 0; i < dv.length (); i++) |
|
882 { |
|
883 if (dv(i) < 0) |
|
884 { |
|
885 retval = -1; |
|
886 break; |
|
887 } |
|
888 |
4584
|
889 if (dv(i) == 0) |
|
890 { |
|
891 retval = 0; |
|
892 break; |
|
893 } |
|
894 |
4563
|
895 if (dv(i) > retval) |
|
896 retval = dv(i); |
|
897 } |
|
898 |
|
899 return retval; |
|
900 } |
|
901 |
5659
|
902 Matrix |
|
903 octave_value::size (void) const |
|
904 { |
|
905 dim_vector dv = dims (); |
|
906 |
|
907 int n_dims = dv.length (); |
|
908 |
|
909 Matrix retval (1, n_dims); |
|
910 |
|
911 while (n_dims--) |
|
912 retval(n_dims) = dv(n_dims); |
|
913 |
|
914 return retval; |
|
915 } |
|
916 |
3351
|
917 Cell |
|
918 octave_value::cell_value (void) const |
|
919 { |
|
920 return rep->cell_value (); |
|
921 } |
|
922 |
2376
|
923 Octave_map |
|
924 octave_value::map_value (void) const |
|
925 { |
|
926 return rep->map_value (); |
|
927 } |
|
928 |
4645
|
929 std::streamoff |
4643
|
930 octave_value::streamoff_value (void) const |
|
931 { |
|
932 return rep->streamoff_value (); |
|
933 } |
|
934 |
4645
|
935 streamoff_array |
|
936 octave_value::streamoff_array_value (void) const |
|
937 { |
|
938 return rep->streamoff_array_value (); |
|
939 } |
|
940 |
2974
|
941 octave_function * |
|
942 octave_value::function_value (bool silent) |
|
943 { |
|
944 return rep->function_value (silent); |
|
945 } |
|
946 |
4700
|
947 octave_user_function * |
|
948 octave_value::user_function_value (bool silent) |
|
949 { |
|
950 return rep->user_function_value (silent); |
|
951 } |
|
952 |
4346
|
953 octave_fcn_handle * |
4343
|
954 octave_value::fcn_handle_value (bool silent) |
|
955 { |
|
956 return rep->fcn_handle_value (silent); |
|
957 } |
|
958 |
4933
|
959 octave_fcn_inline * |
|
960 octave_value::fcn_inline_value (bool silent) |
|
961 { |
|
962 return rep->fcn_inline_value (silent); |
|
963 } |
|
964 |
2880
|
965 octave_value_list |
|
966 octave_value::list_value (void) const |
|
967 { |
|
968 return rep->list_value (); |
|
969 } |
|
970 |
2376
|
971 ColumnVector |
3419
|
972 octave_value::column_vector_value (bool force_string_conv, |
4661
|
973 bool /* frc_vec_conv */) const |
3419
|
974 { |
|
975 ColumnVector retval; |
|
976 |
|
977 Matrix m = matrix_value (force_string_conv); |
|
978 |
|
979 if (error_state) |
|
980 return retval; |
|
981 |
5275
|
982 octave_idx_type nr = m.rows (); |
|
983 octave_idx_type nc = m.columns (); |
3419
|
984 |
|
985 if (nc == 1) |
|
986 { |
|
987 retval.resize (nr); |
5275
|
988 for (octave_idx_type i = 0; i < nr; i++) |
3419
|
989 retval (i) = m (i, 0); |
|
990 } |
|
991 else |
|
992 { |
3523
|
993 std::string tn = type_name (); |
3419
|
994 gripe_invalid_conversion (tn.c_str (), "real column vector"); |
|
995 } |
|
996 |
|
997 return retval; |
|
998 } |
|
999 |
|
1000 ComplexColumnVector |
|
1001 octave_value::complex_column_vector_value (bool force_string_conv, |
4661
|
1002 bool /* frc_vec_conv */) const |
3419
|
1003 { |
|
1004 ComplexColumnVector retval; |
|
1005 |
|
1006 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
1007 |
|
1008 if (error_state) |
|
1009 return retval; |
|
1010 |
5275
|
1011 octave_idx_type nr = m.rows (); |
|
1012 octave_idx_type nc = m.columns (); |
3419
|
1013 |
|
1014 if (nc == 1) |
|
1015 { |
3465
|
1016 retval.resize (nr); |
5275
|
1017 for (octave_idx_type i = 0; i < nr; i++) |
3419
|
1018 retval (i) = m (i, 0); |
|
1019 } |
|
1020 else |
|
1021 { |
3523
|
1022 std::string tn = type_name (); |
3419
|
1023 gripe_invalid_conversion (tn.c_str (), "complex column vector"); |
|
1024 } |
|
1025 |
|
1026 return retval; |
|
1027 } |
|
1028 |
|
1029 RowVector |
|
1030 octave_value::row_vector_value (bool force_string_conv, |
4661
|
1031 bool /* frc_vec_conv */) const |
3419
|
1032 { |
|
1033 RowVector retval; |
|
1034 |
|
1035 Matrix m = matrix_value (force_string_conv); |
|
1036 |
|
1037 if (error_state) |
|
1038 return retval; |
|
1039 |
5275
|
1040 octave_idx_type nr = m.rows (); |
|
1041 octave_idx_type nc = m.columns (); |
3419
|
1042 |
|
1043 if (nr == 1) |
|
1044 { |
|
1045 retval.resize (nc); |
5275
|
1046 for (octave_idx_type i = 0; i < nc; i++) |
3419
|
1047 retval (i) = m (0, i); |
|
1048 } |
|
1049 else |
|
1050 { |
3523
|
1051 std::string tn = type_name (); |
3419
|
1052 gripe_invalid_conversion (tn.c_str (), "real row vector"); |
|
1053 } |
|
1054 |
|
1055 return retval; |
|
1056 } |
|
1057 |
|
1058 ComplexRowVector |
|
1059 octave_value::complex_row_vector_value (bool force_string_conv, |
4661
|
1060 bool /* frc_vec_conv */) const |
3419
|
1061 { |
|
1062 ComplexRowVector retval; |
|
1063 |
|
1064 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
1065 |
|
1066 if (error_state) |
|
1067 return retval; |
|
1068 |
5275
|
1069 octave_idx_type nr = m.rows (); |
|
1070 octave_idx_type nc = m.columns (); |
3419
|
1071 |
|
1072 if (nr == 1) |
|
1073 { |
|
1074 retval.resize (nc); |
5275
|
1075 for (octave_idx_type i = 0; i < nc; i++) |
3419
|
1076 retval (i) = m (0, i); |
|
1077 } |
|
1078 else |
|
1079 { |
3523
|
1080 std::string tn = type_name (); |
3419
|
1081 gripe_invalid_conversion (tn.c_str (), "complex row vector"); |
|
1082 } |
|
1083 |
|
1084 return retval; |
|
1085 } |
|
1086 |
|
1087 // Sloppy... |
|
1088 |
|
1089 Array<double> |
2376
|
1090 octave_value::vector_value (bool force_string_conv, |
|
1091 bool force_vector_conversion) const |
|
1092 { |
3419
|
1093 Array<double> retval; |
2376
|
1094 |
|
1095 Matrix m = matrix_value (force_string_conv); |
|
1096 |
|
1097 if (error_state) |
|
1098 return retval; |
|
1099 |
5275
|
1100 octave_idx_type nr = m.rows (); |
|
1101 octave_idx_type nc = m.columns (); |
2376
|
1102 |
|
1103 if (nr == 1) |
|
1104 { |
|
1105 retval.resize (nc); |
5275
|
1106 for (octave_idx_type i = 0; i < nc; i++) |
2376
|
1107 retval (i) = m (0, i); |
|
1108 } |
|
1109 else if (nc == 1) |
|
1110 { |
|
1111 retval.resize (nr); |
5275
|
1112 for (octave_idx_type i = 0; i < nr; i++) |
2376
|
1113 retval (i) = m (i, 0); |
|
1114 } |
4455
|
1115 else if (nr > 0 && nc > 0) |
2376
|
1116 { |
5781
|
1117 if (! force_vector_conversion) |
|
1118 gripe_implicit_conversion ("Octave:array-as-vector", |
|
1119 type_name (), "real vector"); |
4455
|
1120 |
2376
|
1121 retval.resize (nr * nc); |
5275
|
1122 octave_idx_type k = 0; |
|
1123 for (octave_idx_type j = 0; j < nc; j++) |
|
1124 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
1125 { |
|
1126 OCTAVE_QUIT; |
|
1127 |
|
1128 retval (k++) = m (i, j); |
|
1129 } |
2376
|
1130 } |
|
1131 else |
|
1132 { |
3523
|
1133 std::string tn = type_name (); |
2376
|
1134 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
1135 } |
|
1136 |
|
1137 return retval; |
|
1138 } |
|
1139 |
4044
|
1140 Array<int> |
|
1141 octave_value::int_vector_value (bool force_string_conv, bool require_int, |
|
1142 bool force_vector_conversion) const |
|
1143 { |
|
1144 Array<int> retval; |
|
1145 |
|
1146 Matrix m = matrix_value (force_string_conv); |
|
1147 |
|
1148 if (error_state) |
|
1149 return retval; |
|
1150 |
5275
|
1151 octave_idx_type nr = m.rows (); |
|
1152 octave_idx_type nc = m.columns (); |
4044
|
1153 |
|
1154 if (nr == 1) |
|
1155 { |
|
1156 retval.resize (nc); |
5275
|
1157 for (octave_idx_type i = 0; i < nc; i++) |
4044
|
1158 { |
4153
|
1159 OCTAVE_QUIT; |
|
1160 |
4044
|
1161 double d = m (0, i); |
|
1162 |
|
1163 if (require_int && D_NINT (d) != d) |
|
1164 { |
|
1165 error ("conversion to integer value failed"); |
|
1166 return retval; |
|
1167 } |
|
1168 |
|
1169 retval (i) = static_cast<int> (d); |
|
1170 } |
|
1171 } |
|
1172 else if (nc == 1) |
|
1173 { |
|
1174 retval.resize (nr); |
5275
|
1175 for (octave_idx_type i = 0; i < nr; i++) |
4044
|
1176 { |
4153
|
1177 OCTAVE_QUIT; |
|
1178 |
4044
|
1179 double d = m (i, 0); |
|
1180 |
|
1181 if (require_int && D_NINT (d) != d) |
|
1182 { |
|
1183 error ("conversion to integer value failed"); |
|
1184 return retval; |
|
1185 } |
|
1186 |
|
1187 retval (i) = static_cast<int> (d); |
|
1188 } |
|
1189 } |
4455
|
1190 else if (nr > 0 && nc > 0) |
4044
|
1191 { |
5781
|
1192 if (! force_vector_conversion) |
|
1193 gripe_implicit_conversion ("Octave:array-as-vector", |
|
1194 type_name (), "real vector"); |
4455
|
1195 |
4044
|
1196 retval.resize (nr * nc); |
5275
|
1197 octave_idx_type k = 0; |
|
1198 for (octave_idx_type j = 0; j < nc; j++) |
4044
|
1199 { |
5275
|
1200 for (octave_idx_type i = 0; i < nr; i++) |
4044
|
1201 { |
4153
|
1202 OCTAVE_QUIT; |
|
1203 |
4044
|
1204 double d = m (i, j); |
|
1205 |
|
1206 if (require_int && D_NINT (d) != d) |
|
1207 { |
|
1208 error ("conversion to integer value failed"); |
|
1209 return retval; |
|
1210 } |
|
1211 |
|
1212 retval (k++) = static_cast<int> (d); |
|
1213 } |
|
1214 } |
|
1215 } |
|
1216 else |
|
1217 { |
|
1218 std::string tn = type_name (); |
|
1219 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
1220 } |
|
1221 |
|
1222 return retval; |
|
1223 } |
|
1224 |
3419
|
1225 Array<Complex> |
2376
|
1226 octave_value::complex_vector_value (bool force_string_conv, |
|
1227 bool force_vector_conversion) const |
|
1228 { |
3419
|
1229 Array<Complex> retval; |
2376
|
1230 |
|
1231 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
1232 |
|
1233 if (error_state) |
|
1234 return retval; |
|
1235 |
5275
|
1236 octave_idx_type nr = m.rows (); |
|
1237 octave_idx_type nc = m.columns (); |
2376
|
1238 |
|
1239 if (nr == 1) |
|
1240 { |
|
1241 retval.resize (nc); |
5275
|
1242 for (octave_idx_type i = 0; i < nc; i++) |
4153
|
1243 { |
|
1244 OCTAVE_QUIT; |
|
1245 retval (i) = m (0, i); |
|
1246 } |
2376
|
1247 } |
|
1248 else if (nc == 1) |
|
1249 { |
|
1250 retval.resize (nr); |
5275
|
1251 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
1252 { |
|
1253 OCTAVE_QUIT; |
|
1254 retval (i) = m (i, 0); |
|
1255 } |
2376
|
1256 } |
4455
|
1257 else if (nr > 0 && nc > 0) |
2376
|
1258 { |
5781
|
1259 if (! force_vector_conversion) |
|
1260 gripe_implicit_conversion ("Octave:array-as-vector", |
|
1261 type_name (), "complex vector"); |
4455
|
1262 |
2376
|
1263 retval.resize (nr * nc); |
5275
|
1264 octave_idx_type k = 0; |
|
1265 for (octave_idx_type j = 0; j < nc; j++) |
|
1266 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
1267 { |
|
1268 OCTAVE_QUIT; |
|
1269 retval (k++) = m (i, j); |
|
1270 } |
2376
|
1271 } |
|
1272 else |
|
1273 { |
3523
|
1274 std::string tn = type_name (); |
2376
|
1275 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
|
1276 } |
|
1277 |
|
1278 return retval; |
|
1279 } |
|
1280 |
4944
|
1281 int |
|
1282 octave_value::write (octave_stream& os, int block_size, |
|
1283 oct_data_conv::data_type output_type, int skip, |
|
1284 oct_mach_info::float_format flt_fmt) const |
|
1285 { |
|
1286 return rep->write (os, block_size, output_type, skip, flt_fmt); |
|
1287 } |
|
1288 |
2413
|
1289 static void |
3933
|
1290 gripe_binary_op (const std::string& on, const std::string& tn1, |
|
1291 const std::string& tn2) |
2376
|
1292 { |
2903
|
1293 error ("binary operator `%s' not implemented for `%s' by `%s' operations", |
2376
|
1294 on.c_str (), tn1.c_str (), tn2.c_str ()); |
|
1295 } |
|
1296 |
3203
|
1297 static void |
3523
|
1298 gripe_binary_op_conv (const std::string& on) |
3203
|
1299 { |
|
1300 error ("type conversion failed for binary operator `%s'", on.c_str ()); |
|
1301 } |
|
1302 |
2376
|
1303 octave_value |
3933
|
1304 do_binary_op (octave_value::binary_op op, |
|
1305 const octave_value& v1, const octave_value& v2) |
2376
|
1306 { |
|
1307 octave_value retval; |
|
1308 |
|
1309 int t1 = v1.type_id (); |
|
1310 int t2 = v2.type_id (); |
|
1311 |
5759
|
1312 octave_value_typeinfo::binary_op_fcn f |
|
1313 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
1314 |
|
1315 if (f) |
|
1316 retval = f (*v1.rep, *v2.rep); |
|
1317 else |
|
1318 { |
|
1319 octave_value tv1; |
5759
|
1320 octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); |
2376
|
1321 |
|
1322 if (cf1) |
|
1323 { |
5759
|
1324 octave_base_value *tmp = cf1 (*v1.rep); |
3203
|
1325 |
|
1326 if (tmp) |
|
1327 { |
|
1328 tv1 = octave_value (tmp); |
|
1329 t1 = tv1.type_id (); |
|
1330 } |
|
1331 else |
|
1332 { |
|
1333 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); |
|
1334 return retval; |
|
1335 } |
2376
|
1336 } |
|
1337 else |
|
1338 tv1 = v1; |
|
1339 |
|
1340 octave_value tv2; |
5759
|
1341 octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); |
2376
|
1342 |
|
1343 if (cf2) |
|
1344 { |
5759
|
1345 octave_base_value *tmp = cf2 (*v2.rep); |
3203
|
1346 |
|
1347 if (tmp) |
|
1348 { |
|
1349 tv2 = octave_value (tmp); |
|
1350 t2 = tv2.type_id (); |
|
1351 } |
|
1352 else |
|
1353 { |
|
1354 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); |
|
1355 return retval; |
|
1356 } |
2376
|
1357 } |
|
1358 else |
|
1359 tv2 = v2; |
|
1360 |
|
1361 if (cf1 || cf2) |
|
1362 { |
4587
|
1363 f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
1364 |
|
1365 if (f) |
|
1366 retval = f (*tv1.rep, *tv2.rep); |
|
1367 else |
|
1368 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
1369 v1.type_name (), v2.type_name ()); |
|
1370 } |
|
1371 else |
|
1372 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
1373 v1.type_name (), v2.type_name ()); |
|
1374 } |
|
1375 |
|
1376 return retval; |
|
1377 } |
|
1378 |
4915
|
1379 static void |
|
1380 gripe_cat_op (const std::string& tn1, const std::string& tn2) |
|
1381 { |
|
1382 error ("concatenation operator not implemented for `%s' by `%s' operations", |
|
1383 tn1.c_str (), tn2.c_str ()); |
|
1384 } |
|
1385 |
|
1386 static void |
|
1387 gripe_cat_op_conv (void) |
|
1388 { |
|
1389 error ("type conversion failed for concatenation operator"); |
|
1390 } |
|
1391 |
|
1392 octave_value |
|
1393 do_cat_op (const octave_value& v1, const octave_value& v2, |
|
1394 const Array<int>& ra_idx) |
|
1395 { |
|
1396 octave_value retval; |
|
1397 |
5164
|
1398 // Rapid return for concatenation with an empty object. Dimension |
|
1399 // checking handled elsewhere. |
|
1400 if (v1.all_zero_dims ()) |
|
1401 return v2; |
|
1402 if (v2.all_zero_dims ()) |
|
1403 return v1; |
|
1404 |
4915
|
1405 int t1 = v1.type_id (); |
|
1406 int t2 = v2.type_id (); |
|
1407 |
5759
|
1408 octave_value_typeinfo::cat_op_fcn f |
|
1409 = octave_value_typeinfo::lookup_cat_op (t1, t2); |
4915
|
1410 |
|
1411 if (f) |
|
1412 retval = f (*v1.rep, *v2.rep, ra_idx); |
|
1413 else |
|
1414 { |
|
1415 octave_value tv1; |
5759
|
1416 octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); |
4915
|
1417 |
|
1418 if (cf1) |
|
1419 { |
5759
|
1420 octave_base_value *tmp = cf1 (*v1.rep); |
4915
|
1421 |
|
1422 if (tmp) |
|
1423 { |
|
1424 tv1 = octave_value (tmp); |
|
1425 t1 = tv1.type_id (); |
|
1426 } |
|
1427 else |
|
1428 { |
|
1429 gripe_cat_op_conv (); |
|
1430 return retval; |
|
1431 } |
|
1432 } |
|
1433 else |
|
1434 tv1 = v1; |
|
1435 |
|
1436 octave_value tv2; |
5759
|
1437 octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); |
4915
|
1438 |
|
1439 if (cf2) |
|
1440 { |
5759
|
1441 octave_base_value *tmp = cf2 (*v2.rep); |
4915
|
1442 |
|
1443 if (tmp) |
|
1444 { |
|
1445 tv2 = octave_value (tmp); |
|
1446 t2 = tv2.type_id (); |
|
1447 } |
|
1448 else |
|
1449 { |
|
1450 gripe_cat_op_conv (); |
|
1451 return retval; |
|
1452 } |
|
1453 } |
|
1454 else |
|
1455 tv2 = v2; |
|
1456 |
|
1457 if (cf1 || cf2) |
|
1458 { |
|
1459 f = octave_value_typeinfo::lookup_cat_op (t1, t2); |
|
1460 |
|
1461 if (f) |
|
1462 retval = f (*tv1.rep, *tv2.rep, ra_idx); |
|
1463 else |
|
1464 gripe_cat_op (v1.type_name (), v2.type_name ()); |
|
1465 } |
|
1466 else |
|
1467 gripe_cat_op (v1.type_name (), v2.type_name ()); |
|
1468 } |
|
1469 |
|
1470 return retval; |
|
1471 } |
|
1472 |
3933
|
1473 void |
|
1474 octave_value::print_info (std::ostream& os, const std::string& prefix) const |
|
1475 { |
|
1476 os << prefix << "type_name: " << type_name () << "\n" |
|
1477 << prefix << "count: " << get_count () << "\n" |
|
1478 << prefix << "rep info: "; |
|
1479 |
|
1480 rep->print_info (os, prefix + " "); |
|
1481 } |
|
1482 |
3203
|
1483 static void |
3523
|
1484 gripe_unary_op (const std::string& on, const std::string& tn) |
3203
|
1485 { |
|
1486 error ("unary operator `%s' not implemented for `%s' operands", |
|
1487 on.c_str (), tn.c_str ()); |
|
1488 } |
|
1489 |
|
1490 static void |
3523
|
1491 gripe_unary_op_conv (const std::string& on) |
3203
|
1492 { |
|
1493 error ("type conversion failed for unary operator `%s'", on.c_str ()); |
|
1494 } |
|
1495 |
|
1496 octave_value |
|
1497 do_unary_op (octave_value::unary_op op, const octave_value& v) |
|
1498 { |
|
1499 octave_value retval; |
|
1500 |
|
1501 int t = v.type_id (); |
|
1502 |
5759
|
1503 octave_value_typeinfo::unary_op_fcn f |
|
1504 = octave_value_typeinfo::lookup_unary_op (op, t); |
3203
|
1505 |
|
1506 if (f) |
|
1507 retval = f (*v.rep); |
|
1508 else |
|
1509 { |
|
1510 octave_value tv; |
5759
|
1511 octave_base_value::type_conv_fcn cf = v.numeric_conversion_function (); |
3203
|
1512 |
|
1513 if (cf) |
|
1514 { |
5759
|
1515 octave_base_value *tmp = cf (*v.rep); |
3203
|
1516 |
|
1517 if (tmp) |
|
1518 { |
|
1519 tv = octave_value (tmp); |
|
1520 t = tv.type_id (); |
|
1521 |
4587
|
1522 f = octave_value_typeinfo::lookup_unary_op (op, t); |
3203
|
1523 |
|
1524 if (f) |
|
1525 retval = f (*tv.rep); |
|
1526 else |
|
1527 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1528 v.type_name ()); |
|
1529 } |
|
1530 else |
|
1531 gripe_unary_op_conv (octave_value::unary_op_as_string (op)); |
|
1532 } |
|
1533 else |
|
1534 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1535 v.type_name ()); |
|
1536 } |
|
1537 |
|
1538 return retval; |
|
1539 } |
|
1540 |
|
1541 static void |
3933
|
1542 gripe_unary_op_conversion_failed (const std::string& op, |
|
1543 const std::string& tn) |
3203
|
1544 { |
|
1545 error ("operator %s: type conversion for `%s' failed", |
|
1546 op.c_str (), tn.c_str ()); |
|
1547 } |
|
1548 |
3933
|
1549 const octave_value& |
|
1550 octave_value::do_non_const_unary_op (unary_op op) |
3203
|
1551 { |
|
1552 octave_value retval; |
|
1553 |
|
1554 int t = type_id (); |
|
1555 |
5759
|
1556 octave_value_typeinfo::non_const_unary_op_fcn f |
3203
|
1557 = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
|
1558 |
|
1559 if (f) |
|
1560 { |
|
1561 make_unique (); |
|
1562 |
|
1563 f (*rep); |
|
1564 } |
|
1565 else |
|
1566 { |
5759
|
1567 octave_base_value::type_conv_fcn cf = numeric_conversion_function (); |
3203
|
1568 |
|
1569 if (cf) |
|
1570 { |
5759
|
1571 octave_base_value *tmp = cf (*rep); |
3203
|
1572 |
|
1573 if (tmp) |
|
1574 { |
5759
|
1575 octave_base_value *old_rep = rep; |
3203
|
1576 rep = tmp; |
|
1577 |
|
1578 t = type_id (); |
|
1579 |
|
1580 f = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
|
1581 |
|
1582 if (f) |
|
1583 { |
|
1584 f (*rep); |
|
1585 |
|
1586 if (old_rep && --old_rep->count == 0) |
|
1587 delete old_rep; |
|
1588 } |
|
1589 else |
|
1590 { |
|
1591 if (old_rep) |
|
1592 { |
|
1593 if (--rep->count == 0) |
|
1594 delete rep; |
|
1595 |
|
1596 rep = old_rep; |
|
1597 } |
|
1598 |
|
1599 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1600 type_name ()); |
|
1601 } |
|
1602 } |
|
1603 else |
|
1604 gripe_unary_op_conversion_failed |
|
1605 (octave_value::unary_op_as_string (op), type_name ()); |
|
1606 } |
|
1607 else |
|
1608 gripe_unary_op (octave_value::unary_op_as_string (op), type_name ()); |
|
1609 } |
3933
|
1610 |
|
1611 return *this; |
3203
|
1612 } |
|
1613 |
3933
|
1614 #if 0 |
3205
|
1615 static void |
3933
|
1616 gripe_unary_op_failed_or_no_method (const std::string& on, |
|
1617 const std::string& tn) |
3205
|
1618 { |
|
1619 error ("operator %s: no method, or unable to evaluate for %s operand", |
|
1620 on.c_str (), tn.c_str ()); |
|
1621 } |
3933
|
1622 #endif |
3205
|
1623 |
|
1624 void |
4661
|
1625 octave_value::do_non_const_unary_op (unary_op, const octave_value_list&) |
3205
|
1626 { |
3933
|
1627 abort (); |
|
1628 } |
|
1629 |
|
1630 octave_value |
4247
|
1631 octave_value::do_non_const_unary_op (unary_op op, const std::string& type, |
4219
|
1632 const std::list<octave_value_list>& idx) |
3933
|
1633 { |
|
1634 octave_value retval; |
|
1635 |
|
1636 if (idx.empty ()) |
|
1637 { |
|
1638 do_non_const_unary_op (op); |
3205
|
1639 |
3933
|
1640 retval = *this; |
|
1641 } |
|
1642 else |
|
1643 { |
5775
|
1644 // FIXME -- only do the following stuff if we can't find a |
3933
|
1645 // specific function to call to handle the op= operation for the |
|
1646 // types we have. |
3205
|
1647 |
3933
|
1648 assign_op assop = unary_op_to_assign_op (op); |
|
1649 |
|
1650 retval = assign (assop, type, idx, 1.0); |
|
1651 } |
|
1652 |
|
1653 return retval; |
3205
|
1654 } |
|
1655 |
|
1656 octave_value::assign_op |
|
1657 octave_value::unary_op_to_assign_op (unary_op op) |
|
1658 { |
|
1659 assign_op binop = unknown_assign_op; |
|
1660 |
|
1661 switch (op) |
|
1662 { |
3533
|
1663 case op_incr: |
|
1664 binop = op_add_eq; |
3205
|
1665 break; |
|
1666 |
3533
|
1667 case op_decr: |
|
1668 binop = op_sub_eq; |
3205
|
1669 break; |
|
1670 |
|
1671 default: |
|
1672 { |
3523
|
1673 std::string on = unary_op_as_string (op); |
3205
|
1674 error ("operator %s: no assign operator found", on.c_str ()); |
|
1675 } |
|
1676 } |
|
1677 |
|
1678 return binop; |
|
1679 } |
|
1680 |
3204
|
1681 octave_value::binary_op |
|
1682 octave_value::op_eq_to_binary_op (assign_op op) |
|
1683 { |
|
1684 binary_op binop = unknown_binary_op; |
|
1685 |
|
1686 switch (op) |
|
1687 { |
3533
|
1688 case op_add_eq: |
|
1689 binop = op_add; |
3204
|
1690 break; |
|
1691 |
3533
|
1692 case op_sub_eq: |
|
1693 binop = op_sub; |
3204
|
1694 break; |
|
1695 |
3533
|
1696 case op_mul_eq: |
|
1697 binop = op_mul; |
3204
|
1698 break; |
|
1699 |
3533
|
1700 case op_div_eq: |
|
1701 binop = op_div; |
3204
|
1702 break; |
|
1703 |
3533
|
1704 case op_ldiv_eq: |
|
1705 binop = op_ldiv; |
3204
|
1706 break; |
|
1707 |
4018
|
1708 case op_pow_eq: |
|
1709 binop = op_pow; |
|
1710 break; |
|
1711 |
3533
|
1712 case op_lshift_eq: |
|
1713 binop = op_lshift; |
3204
|
1714 break; |
|
1715 |
3533
|
1716 case op_rshift_eq: |
|
1717 binop = op_rshift; |
3204
|
1718 break; |
|
1719 |
3533
|
1720 case op_el_mul_eq: |
|
1721 binop = op_el_mul; |
3204
|
1722 break; |
|
1723 |
3533
|
1724 case op_el_div_eq: |
|
1725 binop = op_el_div; |
3204
|
1726 break; |
|
1727 |
3533
|
1728 case op_el_ldiv_eq: |
|
1729 binop = op_el_ldiv; |
3204
|
1730 break; |
|
1731 |
4018
|
1732 case op_el_pow_eq: |
|
1733 binop = op_el_pow; |
|
1734 break; |
|
1735 |
3533
|
1736 case op_el_and_eq: |
|
1737 binop = op_el_and; |
3204
|
1738 break; |
|
1739 |
3533
|
1740 case op_el_or_eq: |
|
1741 binop = op_el_or; |
3204
|
1742 break; |
|
1743 |
|
1744 default: |
|
1745 { |
3523
|
1746 std::string on = assign_op_as_string (op); |
3204
|
1747 error ("operator %s: no binary operator found", on.c_str ()); |
|
1748 } |
|
1749 } |
|
1750 |
|
1751 return binop; |
|
1752 } |
|
1753 |
3933
|
1754 octave_value |
|
1755 octave_value::empty_conv (const std::string& type, const octave_value& rhs) |
|
1756 { |
|
1757 octave_value retval; |
|
1758 |
|
1759 if (type.length () > 0) |
|
1760 { |
|
1761 switch (type[0]) |
|
1762 { |
|
1763 case '(': |
|
1764 { |
|
1765 if (type.length () > 1 && type[1] == '.') |
|
1766 retval = Octave_map (); |
|
1767 else |
|
1768 retval = octave_value (rhs.empty_clone ()); |
|
1769 } |
|
1770 break; |
|
1771 |
|
1772 case '{': |
|
1773 retval = Cell (); |
|
1774 break; |
|
1775 |
|
1776 case '.': |
|
1777 retval = Octave_map (); |
|
1778 break; |
|
1779 |
|
1780 default: |
|
1781 panic_impossible (); |
|
1782 } |
|
1783 } |
|
1784 else |
|
1785 retval = octave_value (rhs.empty_clone ()); |
|
1786 |
|
1787 return retval; |
|
1788 } |
|
1789 |
2376
|
1790 void |
|
1791 install_types (void) |
|
1792 { |
|
1793 octave_base_value::register_type (); |
3928
|
1794 octave_cell::register_type (); |
2376
|
1795 octave_scalar::register_type (); |
|
1796 octave_complex::register_type (); |
|
1797 octave_matrix::register_type (); |
|
1798 octave_complex_matrix::register_type (); |
|
1799 octave_range::register_type (); |
2825
|
1800 octave_bool::register_type (); |
|
1801 octave_bool_matrix::register_type (); |
2376
|
1802 octave_char_matrix::register_type (); |
|
1803 octave_char_matrix_str::register_type (); |
5279
|
1804 octave_char_matrix_sq_str::register_type (); |
4901
|
1805 octave_int8_scalar::register_type (); |
|
1806 octave_int16_scalar::register_type (); |
|
1807 octave_int32_scalar::register_type (); |
|
1808 octave_int64_scalar::register_type (); |
|
1809 octave_uint8_scalar::register_type (); |
|
1810 octave_uint16_scalar::register_type (); |
|
1811 octave_uint32_scalar::register_type (); |
|
1812 octave_uint64_scalar::register_type (); |
|
1813 octave_int8_matrix::register_type (); |
|
1814 octave_int16_matrix::register_type (); |
|
1815 octave_int32_matrix::register_type (); |
|
1816 octave_int64_matrix::register_type (); |
|
1817 octave_uint8_matrix::register_type (); |
|
1818 octave_uint16_matrix::register_type (); |
|
1819 octave_uint32_matrix::register_type (); |
|
1820 octave_uint64_matrix::register_type (); |
5164
|
1821 octave_sparse_bool_matrix::register_type (); |
|
1822 octave_sparse_matrix::register_type (); |
|
1823 octave_sparse_complex_matrix::register_type (); |
2376
|
1824 octave_struct::register_type (); |
2880
|
1825 octave_list::register_type (); |
3977
|
1826 octave_cs_list::register_type (); |
2376
|
1827 octave_magic_colon::register_type (); |
2974
|
1828 octave_builtin::register_type (); |
|
1829 octave_mapper::register_type (); |
|
1830 octave_user_function::register_type (); |
4649
|
1831 octave_dld_function::register_type (); |
4643
|
1832 octave_fcn_handle::register_type (); |
4966
|
1833 octave_fcn_inline::register_type (); |
4643
|
1834 octave_streamoff::register_type (); |
2376
|
1835 } |
|
1836 |
4970
|
1837 #if 0 |
|
1838 DEFUN (cast, args, , |
|
1839 "-*- texinfo -*-\n\ |
|
1840 @deftypefn {Built-in Function} {} cast (@var{val}, @var{type})\n\ |
|
1841 Convert @var{val} to the new data type @var{type}.\n\ |
5642
|
1842 @seealso{class, typeinfo}\n\ |
|
1843 @end deftypefn") |
4970
|
1844 { |
|
1845 octave_value retval; |
|
1846 |
|
1847 if (args.length () == 2) |
|
1848 error ("cast: not implemented"); |
|
1849 else |
5823
|
1850 print_usage (); |
4970
|
1851 |
|
1852 return retval; |
|
1853 } |
|
1854 #endif |
|
1855 |
4791
|
1856 DEFUN (sizeof, args, , |
|
1857 "-*- texinfo -*-\n\ |
|
1858 @deftypefn {Built-in Function} {} sizeof (@var{val})\n\ |
|
1859 Return the size of @var{val} in bytes\n\ |
|
1860 @end deftypefn") |
|
1861 { |
|
1862 octave_value retval; |
|
1863 |
|
1864 if (args.length () == 1) |
|
1865 retval = args(0).byte_size (); |
|
1866 else |
5823
|
1867 print_usage (); |
4791
|
1868 |
|
1869 return retval; |
|
1870 } |
|
1871 |
6153
|
1872 static void |
|
1873 decode_subscripts (const char* name, const octave_value& arg, |
|
1874 std::string& type_string, |
|
1875 std::list<octave_value_list>& idx) |
|
1876 { |
|
1877 Octave_map m = arg.map_value (); |
|
1878 |
|
1879 if (! error_state |
|
1880 && m.length () == 2 && m.contains ("type") && m.contains ("subs")) |
|
1881 { |
|
1882 Cell& type = m.contents ("type"); |
|
1883 Cell& subs = m.contents ("subs"); |
|
1884 |
|
1885 type_string = std::string (type.length(), '\0'); |
|
1886 |
|
1887 for (int k = 0; k < type.length (); k++) |
|
1888 { |
|
1889 std::string item = type(k).string_value (); |
|
1890 |
|
1891 if (! error_state) |
|
1892 { |
|
1893 if (item == "{}") |
|
1894 type_string[k] = '{'; |
|
1895 else if (item == "()") |
|
1896 type_string[k] = '('; |
|
1897 else if (item == ".") |
|
1898 type_string[k] = '.'; |
|
1899 else |
|
1900 { |
|
1901 error("%s: invalid indexing type `%s'", name, item.c_str ()); |
|
1902 return; |
|
1903 } |
|
1904 } |
|
1905 else |
|
1906 { |
|
1907 error ("%s: expecting type(%d) to be a character string", |
|
1908 name, k+1); |
|
1909 return; |
|
1910 } |
|
1911 |
|
1912 octave_value_list idx_item; |
|
1913 |
|
1914 if (subs(k).is_string ()) |
|
1915 idx_item(0) = subs(k); |
|
1916 else if (subs(k).is_cell ()) |
|
1917 { |
|
1918 Cell subs_cell = subs(k).cell_value (); |
|
1919 |
|
1920 for (int n = 0; n < subs_cell.length (); n++) |
|
1921 { |
|
1922 if (subs_cell(n).is_string () |
|
1923 && subs_cell(n).string_value () == ":") |
|
1924 idx_item(n) = octave_value(octave_value::magic_colon_t); |
|
1925 else |
|
1926 idx_item(n) = subs_cell(n); |
|
1927 } |
|
1928 } |
|
1929 else |
|
1930 { |
|
1931 error ("%s: expecting subs(%d) to be a character string or cell array", |
|
1932 name, k+1); |
|
1933 return; |
|
1934 } |
|
1935 |
|
1936 idx.push_back (idx_item); |
|
1937 } |
|
1938 } |
|
1939 else |
|
1940 error ("%s: second argument must be a structure with fields `type' and `subs'", name); |
|
1941 } |
|
1942 |
|
1943 DEFUN (subsref, args, nargout, |
|
1944 "-*- texinfo -*-\n\ |
|
1945 @deftypefn {Built-in Function} {} subsref (@var{val}, @var{idx})\n\ |
|
1946 Perform the subscripted element selection operation according to\n\ |
|
1947 the subscript specified by @var{idx}.\n\ |
|
1948 \n\ |
|
1949 The subscript @var{idx} is expected to be a structure array with\n\ |
|
1950 fields @samp{type} and @samp{subs}. Valid values for @samp{type}\n\ |
6550
|
1951 are @samp{\"()\"}, @samp{\"@{@}\"}, and @samp{\".\"}.\n\ |
6153
|
1952 The @samp{subs} field may be either @samp{\":\"} or a cell array\n\ |
|
1953 of index values.\n\ |
6157
|
1954 @seealso{subsasgn, substruct}\n\ |
6153
|
1955 @end deftypefn") |
|
1956 { |
|
1957 octave_value_list retval; |
|
1958 |
|
1959 if (args.length () == 2) |
|
1960 { |
|
1961 std::string type; |
|
1962 std::list<octave_value_list> idx; |
|
1963 |
|
1964 decode_subscripts ("subsref", args(1), type, idx); |
|
1965 |
|
1966 if (! error_state) |
|
1967 retval = args(0).subsref (type, idx, nargout); |
|
1968 } |
|
1969 else |
|
1970 print_usage (); |
|
1971 |
|
1972 return retval; |
|
1973 } |
|
1974 |
|
1975 DEFUN (subsasgn, args, , |
|
1976 "-*- texinfo -*-\n\ |
|
1977 @deftypefn {Built-in Function} {} subsasgn (@var{val}, @var{idx}, @var{rhs})\n\ |
|
1978 Perform the subscripted assignment operation according to\n\ |
|
1979 the subscript specified by @var{idx}.\n\ |
|
1980 \n\ |
|
1981 The subscript @var{idx} is expected to be a structure array with\n\ |
|
1982 fields @samp{type} and @samp{subs}. Valid values for @samp{type}\n\ |
6248
|
1983 are @samp{\"()\"}, @samp{\"@{@}\"}, and @samp{\".\"}.\n\ |
6153
|
1984 The @samp{subs} field may be either @samp{\":\"} or a cell array\n\ |
|
1985 of index values.\n\ |
6157
|
1986 @seealso{subsref, substruct}\n\ |
6153
|
1987 @end deftypefn") |
|
1988 { |
|
1989 octave_value retval; |
|
1990 |
|
1991 if (args.length () == 3) |
|
1992 { |
|
1993 std::string type; |
|
1994 std::list<octave_value_list> idx; |
|
1995 |
|
1996 decode_subscripts ("subsasgn", args(1), type, idx); |
|
1997 |
|
1998 if (! error_state) |
|
1999 retval = args(0).subsasgn (type, idx, args(2)); |
|
2000 } |
|
2001 else |
|
2002 print_usage (); |
|
2003 |
|
2004 return retval; |
|
2005 } |
|
2006 |
2376
|
2007 /* |
|
2008 ;;; Local Variables: *** |
|
2009 ;;; mode: C++ *** |
|
2010 ;;; End: *** |
|
2011 */ |