1
|
1 /* |
|
2 |
1827
|
3 Copyright (C) 1996 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
1299
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1558
|
31 #include <cctype> |
1355
|
32 #include <cstring> |
|
33 |
1728
|
34 #include <string> |
|
35 |
1558
|
36 #include <fstream.h> |
581
|
37 #include <iostream.h> |
|
38 |
1742
|
39 #include <SLList.h> |
|
40 |
2181
|
41 #include "Array-flags.h" |
|
42 |
1558
|
43 #include "mx-base.h" |
|
44 #include "Range.h" |
1755
|
45 #include "str-vec.h" |
1558
|
46 |
2181
|
47 #include "defun.h" |
1
|
48 #include "error.h" |
|
49 #include "gripes.h" |
1558
|
50 #include "idx-vector.h" |
1742
|
51 #include "mappers.h" |
747
|
52 #include "oct-map.h" |
1742
|
53 #include "oct-obj.h" |
1355
|
54 #include "pager.h" |
1558
|
55 #include "pr-output.h" |
|
56 #include "sysdep.h" |
1742
|
57 #include "pt-const.h" |
2124
|
58 #include "pt-walk.h" |
1558
|
59 #include "unwind-prot.h" |
1430
|
60 #include "utils.h" |
1558
|
61 #include "variables.h" |
|
62 |
2390
|
63 Octave_map |
|
64 tree_constant::map_value (void) const |
|
65 { |
|
66 return val.map_value (); |
|
67 } |
|
68 |
|
69 void |
|
70 tree_constant::print (void) |
|
71 { |
|
72 } |
|
73 |
|
74 #if 0 |
|
75 octave_value |
|
76 tree_constant::assign_map_element (SLList<string>&, const octave_value&) |
|
77 { |
|
78 octave_value retval; |
|
79 error ("tree_constant::assign_map_element(): not implemented"); |
|
80 return retval; |
|
81 } |
|
82 |
|
83 octave_value |
|
84 tree_constant::assign_map_element (SLList<string>&, const octave_value_list&, |
|
85 const octave_value&) |
|
86 { |
|
87 octave_value retval; |
|
88 error ("tree_constant::assign_map_element(): not implemented"); |
|
89 return retval; |
|
90 } |
|
91 #endif |
|
92 |
|
93 octave_value |
|
94 tree_constant::eval (bool print_result) |
|
95 { |
|
96 if (print_result) |
|
97 val.print (); |
|
98 |
|
99 return val; |
|
100 } |
|
101 |
|
102 octave_value_list |
|
103 tree_constant::eval (bool, int, const octave_value_list& idx) |
|
104 { |
|
105 octave_value_list retval; |
|
106 |
|
107 if (idx.length () > 0) |
|
108 retval (0) = index (idx); |
|
109 else |
|
110 retval (0) = val; |
|
111 |
|
112 return retval; |
|
113 } |
|
114 |
|
115 octave_value |
|
116 tree_constant::lookup_map_element (const string&, bool, bool) |
|
117 { |
|
118 octave_value retval; |
|
119 error ("tree_constant::lookup_map_element() not implemented"); |
|
120 return retval; |
|
121 } |
|
122 |
|
123 octave_value |
|
124 tree_constant::lookup_map_element (SLList<string>&, bool, bool) |
|
125 { |
|
126 octave_value retval; |
|
127 error ("tree_constant::lookup_map_element() not implemented"); |
|
128 return retval; |
|
129 } |
|
130 |
|
131 void |
|
132 tree_constant::accept (tree_walker& tw) |
|
133 { |
|
134 tw.visit_constant (*this); |
|
135 } |
|
136 |
|
137 #if 0 |
|
138 |
2086
|
139 #ifndef OCT_VAL_REP |
|
140 #define OCT_VAL_REP octave_value::octave_value_rep |
1558
|
141 #endif |
|
142 |
|
143 #ifndef MAX |
|
144 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
145 #endif |
|
146 |
2086
|
147 #ifndef OCT_VAL_REP |
|
148 #define OCT_VAL_REP octave_value::octave_value_rep |
1558
|
149 #endif |
|
150 |
|
151 #ifndef MAX |
|
152 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
153 #endif |
|
154 |
|
155 // The following three variables could be made static members of the |
2086
|
156 // OCT_VAL_REP class. |
1558
|
157 |
|
158 // Pointer to the blocks of memory we manage. |
2086
|
159 static OCT_VAL_REP *tc_rep_newlist = 0; |
1558
|
160 |
|
161 // Multiplier for allocating new blocks. |
|
162 static const int tc_rep_newlist_grow_size = 128; |
|
163 |
2338
|
164 // XXX FIXME XXX -- these should be member functions. |
|
165 |
1827
|
166 static bool |
1558
|
167 any_element_is_complex (const ComplexMatrix& a) |
|
168 { |
|
169 int nr = a.rows (); |
|
170 int nc = a.columns (); |
1827
|
171 |
1558
|
172 for (int j = 0; j < nc; j++) |
|
173 for (int i = 0; i < nr; i++) |
2305
|
174 if (imag (a (i, j)) != 0.0) |
1827
|
175 return true; |
|
176 |
|
177 return false; |
1558
|
178 } |
747
|
179 |
2338
|
180 static bool |
|
181 any_element_is_complex (const ComplexDiagMatrix& d) |
|
182 { |
|
183 int len = d.length (); |
|
184 |
|
185 for (int i = 0; i < len; i++) |
|
186 if (imag (d (i, i)) != 0.0) |
|
187 return true; |
|
188 |
|
189 return false; |
|
190 } |
|
191 |
|
192 static bool |
|
193 any_element_is_complex (const ComplexRowVector& v) |
|
194 { |
|
195 int len = v.length (); |
|
196 |
|
197 for (int i = 0; i < len; i++) |
|
198 if (imag (v (i)) != 0.0) |
|
199 return true; |
|
200 |
|
201 return false; |
|
202 } |
|
203 |
|
204 static bool |
|
205 any_element_is_complex (const ComplexColumnVector& v) |
|
206 { |
|
207 int len = v.length (); |
|
208 |
|
209 for (int i = 0; i < len; i++) |
|
210 if (imag (v (i)) != 0.0) |
|
211 return true; |
|
212 |
|
213 return false; |
|
214 } |
|
215 |
1168
|
216 // The following three variables could be made static members of the |
2086
|
217 // octave_value class. |
1168
|
218 |
|
219 // Pointer to the blocks of memory we manage. |
2086
|
220 static octave_value *tc_newlist = 0; |
1168
|
221 |
|
222 // Multiplier for allocating new blocks. |
1299
|
223 static const int tc_newlist_grow_size = 128; |
1168
|
224 |
747
|
225 Octave_map |
2086
|
226 octave_value::map_value (void) const |
747
|
227 { |
|
228 return rep->map_value (); |
|
229 } |
1
|
230 |
2086
|
231 octave_value::~octave_value (void) |
1
|
232 { |
|
233 #if defined (MDEBUG) |
2086
|
234 cerr << "~octave_value: rep: " << rep |
1
|
235 << " rep->count: " << rep->count << "\n"; |
|
236 #endif |
|
237 |
|
238 if (--rep->count <= 0) |
|
239 { |
|
240 delete rep; |
531
|
241 rep = 0; |
1
|
242 } |
|
243 } |
|
244 |
|
245 void * |
2086
|
246 octave_value::operator new (size_t size) |
1
|
247 { |
2086
|
248 assert (size == sizeof (octave_value)); |
1168
|
249 |
1299
|
250 if (! tc_newlist) |
|
251 { |
2086
|
252 int block_size = tc_newlist_grow_size * sizeof (octave_value); |
|
253 tc_newlist = (octave_value *) new char [block_size]; |
1299
|
254 |
1321
|
255 int i = 0; |
|
256 |
|
257 for (i = 0; i < tc_newlist_grow_size - 1; i++) |
1299
|
258 tc_newlist[i].freeptr = &tc_newlist[i+1]; |
|
259 |
|
260 tc_newlist[i].freeptr = 0; |
|
261 } |
|
262 |
2086
|
263 octave_value *tmp = tc_newlist; |
1299
|
264 tc_newlist = tc_newlist->freeptr; |
1168
|
265 return tmp; |
1
|
266 } |
|
267 |
|
268 void |
2086
|
269 octave_value::operator delete (void *p, size_t /* size */) |
1
|
270 { |
2086
|
271 octave_value *tmp = (octave_value *) p; |
1299
|
272 tmp->freeptr = tc_newlist; |
|
273 tc_newlist = tmp; |
1
|
274 } |
|
275 |
747
|
276 // Simple assignment. |
|
277 |
2086
|
278 octave_value |
|
279 octave_value::operator = (const octave_value& a) |
747
|
280 { |
|
281 if (rep != a.rep) |
|
282 { |
|
283 if (--rep->count <= 0) |
|
284 delete rep; |
|
285 rep = a.rep; |
|
286 rep->count++; |
|
287 } |
|
288 return *this; |
|
289 } |
|
290 |
|
291 // Simple structure assignment. |
|
292 |
|
293 void |
2086
|
294 octave_value::make_unique (void) |
747
|
295 { |
|
296 if (rep->count > 1) |
|
297 { |
|
298 --rep->count; |
2086
|
299 rep = new octave_value_rep (*rep); |
747
|
300 rep->count = 1; |
|
301 } |
|
302 |
|
303 if (rep->is_map ()) |
|
304 { |
|
305 for (Pix p = rep->a_map->first (); p != 0; rep->a_map->next (p)) |
|
306 { |
|
307 rep->a_map->contents (p) . make_unique (); |
|
308 } |
|
309 } |
|
310 } |
|
311 |
2086
|
312 octave_value::octave_value_rep * |
|
313 octave_value::make_unique_map (void) |
747
|
314 { |
|
315 if (! rep->is_map ()) |
|
316 { |
|
317 if (--rep->count <= 0) |
|
318 delete rep; |
|
319 |
|
320 Octave_map m; |
2086
|
321 rep = new octave_value_rep (m); |
747
|
322 rep->count = 1; |
|
323 } |
|
324 |
|
325 make_unique (); |
|
326 |
|
327 return rep; |
|
328 } |
|
329 |
2086
|
330 octave_value |
|
331 octave_value::assign_map_element (SLList<string>& list, |
|
332 octave_value& rhs) |
747
|
333 { |
2086
|
334 octave_value_rep *tmp_rep = make_unique_map (); |
747
|
335 |
|
336 if (rhs.is_map ()) |
|
337 rhs.make_unique (); |
|
338 |
|
339 Pix p = list.first (); |
|
340 while (p) |
|
341 { |
1755
|
342 string elt = list (p); |
747
|
343 |
|
344 list.next (p); |
|
345 |
2086
|
346 octave_value& tmp = tmp_rep->lookup_map_element (elt, 1); |
747
|
347 |
|
348 if (! p) |
|
349 { |
|
350 tmp = rhs; |
|
351 return tmp; |
|
352 } |
|
353 |
|
354 tmp_rep = tmp.make_unique_map (); |
|
355 } |
|
356 |
2086
|
357 return octave_value (); |
747
|
358 } |
|
359 |
|
360 // Indexed structure assignment. |
|
361 |
2086
|
362 octave_value |
|
363 octave_value::assign_map_element (SLList<string>& list, |
|
364 octave_value& rhs, |
|
365 const octave_value_list& args) |
747
|
366 { |
2086
|
367 octave_value_rep *tmp_rep = make_unique_map (); |
747
|
368 |
|
369 if (rhs.is_map ()) |
|
370 rhs.make_unique (); |
|
371 |
|
372 Pix p = list.first (); |
|
373 while (p) |
|
374 { |
1755
|
375 string elt = list (p); |
747
|
376 |
|
377 list.next (p); |
|
378 |
2086
|
379 octave_value& tmp = tmp_rep->lookup_map_element (elt, 1); |
747
|
380 |
|
381 if (! p) |
|
382 { |
|
383 tmp.assign (rhs, args); |
|
384 return tmp; |
|
385 } |
|
386 |
|
387 tmp_rep = tmp.make_unique_map (); |
|
388 } |
|
389 |
2086
|
390 return octave_value (); |
747
|
391 } |
|
392 |
2086
|
393 octave_value_list |
|
394 octave_value::eval (bool print, int, const octave_value_list& args) |
1742
|
395 { |
2086
|
396 octave_value_list retval; |
1742
|
397 |
|
398 if (args.length () > 0) |
|
399 retval(0) = rep->do_index (args); |
|
400 else |
|
401 retval(0) = *this; |
|
402 |
|
403 if (retval(0).is_defined ()) |
|
404 retval(0).eval (print); |
|
405 |
|
406 return retval; |
|
407 } |
|
408 |
1299
|
409 void * |
2086
|
410 OCT_VAL_REP::operator new (size_t size) |
1299
|
411 { |
2086
|
412 assert (size == sizeof (OCT_VAL_REP)); |
1299
|
413 |
|
414 if (! tc_rep_newlist) |
|
415 { |
2086
|
416 int block_size = tc_rep_newlist_grow_size * sizeof (OCT_VAL_REP); |
|
417 tc_rep_newlist = (OCT_VAL_REP *) new char [block_size]; |
1299
|
418 |
1321
|
419 int i = 0; |
|
420 |
|
421 for (i = 0; i < tc_rep_newlist_grow_size - 1; i++) |
1299
|
422 tc_rep_newlist[i].freeptr = &tc_rep_newlist[i+1]; |
|
423 |
|
424 tc_rep_newlist[i].freeptr = 0; |
|
425 } |
|
426 |
2086
|
427 OCT_VAL_REP *tmp = tc_rep_newlist; |
1299
|
428 tc_rep_newlist = tc_rep_newlist->freeptr; |
|
429 return tmp; |
|
430 } |
|
431 |
|
432 void |
2086
|
433 OCT_VAL_REP::operator delete (void *p, size_t /* size */) |
1299
|
434 { |
2086
|
435 OCT_VAL_REP *tmp = (OCT_VAL_REP *) p; |
1299
|
436 tmp->freeptr = tc_rep_newlist; |
|
437 tc_rep_newlist = tmp; |
|
438 } |
|
439 |
2086
|
440 octave_value |
|
441 OCT_VAL_REP::all (void) const |
1299
|
442 { |
2086
|
443 octave_value retval; |
1299
|
444 |
|
445 if (error_state) |
|
446 return retval; |
|
447 |
|
448 if (! is_numeric_type ()) |
|
449 { |
2086
|
450 octave_value tmp = make_numeric (); |
1299
|
451 |
|
452 if (error_state) |
|
453 return retval; |
|
454 |
|
455 return tmp.all (); |
|
456 } |
|
457 |
|
458 switch (type_tag) |
|
459 { |
|
460 default: |
|
461 gripe_wrong_type_arg ("all", *this); |
|
462 break; |
|
463 } |
|
464 |
|
465 return retval; |
|
466 } |
|
467 |
2086
|
468 octave_value |
|
469 OCT_VAL_REP::any (void) const |
1299
|
470 { |
2086
|
471 octave_value retval; |
1299
|
472 |
|
473 if (error_state) |
|
474 return retval; |
|
475 |
|
476 if (! is_numeric_type ()) |
|
477 { |
2086
|
478 octave_value tmp = make_numeric (); |
1299
|
479 |
|
480 if (error_state) |
|
481 return retval; |
|
482 |
|
483 return tmp.any (); |
|
484 } |
|
485 |
|
486 switch (type_tag) |
|
487 { |
|
488 default: |
|
489 gripe_wrong_type_arg ("any", *this); |
|
490 break; |
|
491 } |
|
492 |
|
493 return retval; |
|
494 } |
|
495 |
2067
|
496 // XXX FIXME XXX -- we need a better way of handling conversions. |
|
497 |
1299
|
498 double |
2086
|
499 OCT_VAL_REP::double_value (bool force_string_conv) const |
1299
|
500 { |
|
501 double retval = octave_NaN; |
|
502 |
|
503 switch (type_tag) |
|
504 { |
1572
|
505 case char_matrix_constant_str: |
|
506 { |
1827
|
507 int flag = force_string_conv; |
1299
|
508 if (! flag) |
2181
|
509 flag = Vimplicit_str_to_num_ok; |
1299
|
510 |
|
511 if (flag < 0) |
2390
|
512 gripe_implicit_conversion ("string", "real scalar"); |
1299
|
513 |
1572
|
514 int len = char_matrix->rows (); |
1355
|
515 if (flag |
1572
|
516 && ((char_matrix->rows () == 1 && len == 1) |
2181
|
517 || (len > 1 && Vdo_fortran_indexing))) |
2305
|
518 retval = toascii ((int) (*char_matrix) (0, 0)); |
1299
|
519 else |
|
520 gripe_invalid_conversion ("string", "real scalar"); |
|
521 } |
|
522 break; |
|
523 |
|
524 default: |
2390
|
525 gripe_invalid_conversion (type_name (), "real scalar"); |
1299
|
526 break; |
|
527 } |
|
528 |
|
529 return retval; |
|
530 } |
|
531 |
|
532 Matrix |
2086
|
533 OCT_VAL_REP::matrix_value (bool force_string_conv) const |
1299
|
534 { |
|
535 Matrix retval; |
|
536 |
|
537 switch (type_tag) |
|
538 { |
1572
|
539 case char_matrix_constant: |
|
540 retval = Matrix (*char_matrix); |
|
541 break; |
|
542 |
|
543 case char_matrix_constant_str: |
1299
|
544 { |
1827
|
545 int flag = force_string_conv; |
1299
|
546 if (! flag) |
2181
|
547 flag = Vimplicit_str_to_num_ok; |
1299
|
548 |
|
549 if (flag < 0) |
2390
|
550 gripe_implicit_conversion ("string", "real matrix"); |
1299
|
551 |
|
552 if (flag) |
1572
|
553 retval = Matrix (*char_matrix); |
1299
|
554 else |
|
555 gripe_invalid_conversion ("string", "real matrix"); |
|
556 } |
|
557 break; |
|
558 |
|
559 default: |
2390
|
560 gripe_invalid_conversion (type_name (), "real matrix"); |
1299
|
561 break; |
|
562 } |
|
563 |
|
564 return retval; |
|
565 } |
|
566 |
|
567 Complex |
2086
|
568 OCT_VAL_REP::complex_value (bool force_string_conv) const |
1299
|
569 { |
|
570 Complex retval (octave_NaN, octave_NaN); |
|
571 |
|
572 switch (type_tag) |
|
573 { |
1572
|
574 case char_matrix_constant: |
1299
|
575 { |
1572
|
576 int len = char_matrix->cols (); |
|
577 if ((char_matrix->rows () == 1 && len == 1) |
2181
|
578 || (len > 1 && Vdo_fortran_indexing)) |
2305
|
579 retval = toascii ((int) (*char_matrix) (0, 0)); |
1572
|
580 else |
|
581 gripe_invalid_conversion ("char matrix", "complex scalar"); |
|
582 } |
|
583 break; |
|
584 |
|
585 case char_matrix_constant_str: |
|
586 { |
1827
|
587 int flag = force_string_conv; |
1299
|
588 if (! flag) |
2181
|
589 flag = Vimplicit_str_to_num_ok; |
1299
|
590 |
|
591 if (flag < 0) |
2390
|
592 gripe_implicit_conversion ("string", "complex scalar"); |
1299
|
593 |
1572
|
594 int len = char_matrix->cols (); |
1355
|
595 if (flag |
1572
|
596 && ((char_matrix->rows () == 1 && len == 1) |
2181
|
597 || (len > 1 && Vdo_fortran_indexing))) |
2305
|
598 retval = toascii ((int) (*char_matrix) (0, 0)); |
1299
|
599 else |
|
600 gripe_invalid_conversion ("string", "complex scalar"); |
|
601 } |
|
602 break; |
|
603 |
|
604 default: |
2390
|
605 gripe_invalid_conversion (type_name (), "complex scalar"); |
1299
|
606 break; |
|
607 } |
|
608 |
|
609 return retval; |
|
610 } |
|
611 |
|
612 ComplexMatrix |
2086
|
613 OCT_VAL_REP::complex_matrix_value (bool force_string_conv) const |
1299
|
614 { |
|
615 ComplexMatrix retval; |
|
616 |
|
617 switch (type_tag) |
|
618 { |
1572
|
619 case char_matrix_constant: |
|
620 retval = ComplexMatrix (*char_matrix); |
|
621 break; |
|
622 |
|
623 case char_matrix_constant_str: |
1299
|
624 { |
1827
|
625 int flag = force_string_conv; |
1299
|
626 if (! flag) |
2181
|
627 flag = Vimplicit_str_to_num_ok; |
1299
|
628 |
|
629 if (flag < 0) |
2390
|
630 gripe_implicit_conversion ("string", "complex matrix"); |
1299
|
631 |
|
632 if (flag) |
1572
|
633 retval = ComplexMatrix (*char_matrix); |
1299
|
634 else |
1572
|
635 gripe_invalid_conversion ("complex", "real matrix"); |
1299
|
636 } |
|
637 break; |
|
638 |
|
639 default: |
2390
|
640 gripe_invalid_conversion (type_name (), "complex matrix"); |
1299
|
641 break; |
|
642 } |
|
643 |
|
644 return retval; |
|
645 } |
|
646 |
1572
|
647 // XXX FIXME XXX -- this needs to try to do some conversions... |
|
648 |
|
649 charMatrix |
2086
|
650 OCT_VAL_REP::char_matrix_value (bool force_string_conv) const |
1572
|
651 { |
|
652 charMatrix retval; |
|
653 |
1827
|
654 int flag = force_string_conv; |
1572
|
655 if (! flag) |
2181
|
656 flag = Vimplicit_str_to_num_ok; |
1572
|
657 |
|
658 switch (type_tag) |
|
659 { |
|
660 case char_matrix_constant: |
|
661 case char_matrix_constant_str: |
|
662 retval = *char_matrix; |
|
663 break; |
|
664 |
|
665 default: |
1652
|
666 if (! (rows () == 0 && columns () == 0)) |
2390
|
667 gripe_invalid_conversion (type_name (), "string"); |
1572
|
668 break; |
|
669 } |
|
670 |
|
671 return retval; |
|
672 } |
|
673 |
|
674 charMatrix |
2086
|
675 OCT_VAL_REP::all_strings (void) const |
1355
|
676 { |
1572
|
677 if (type_tag == char_matrix_constant_str) |
|
678 return *char_matrix; |
1355
|
679 else |
|
680 { |
2390
|
681 gripe_invalid_conversion (type_name (), "string"); |
1355
|
682 return 0; |
|
683 } |
|
684 } |
|
685 |
1728
|
686 string |
2086
|
687 OCT_VAL_REP::string_value (void) const |
1299
|
688 { |
1728
|
689 string retval; |
|
690 |
1572
|
691 if (type_tag == char_matrix_constant_str) |
1728
|
692 retval = char_matrix->row_as_string (0); // XXX FIXME??? XXX |
1299
|
693 else |
2390
|
694 gripe_invalid_conversion (type_name (), "string"); |
1728
|
695 |
|
696 return retval; |
1299
|
697 } |
|
698 |
|
699 Octave_map |
2086
|
700 OCT_VAL_REP::map_value (void) const |
1299
|
701 { |
|
702 assert (type_tag == map_constant); |
|
703 return *a_map; |
|
704 } |
|
705 |
|
706 // This could be made more efficient by doing all the work here rather |
|
707 // than relying on matrix_value() to do any possible type conversions. |
|
708 |
|
709 ColumnVector |
2086
|
710 OCT_VAL_REP::vector_value (bool force_string_conv, |
1827
|
711 bool force_vector_conversion) const |
1299
|
712 { |
|
713 ColumnVector retval; |
|
714 |
1827
|
715 Matrix m = matrix_value (force_string_conv); |
1299
|
716 |
|
717 if (error_state) |
|
718 return retval; |
|
719 |
|
720 int nr = m.rows (); |
|
721 int nc = m.columns (); |
|
722 if (nr == 1) |
|
723 { |
|
724 retval.resize (nc); |
|
725 for (int i = 0; i < nc; i++) |
2305
|
726 retval (i) = m (0, i); |
1299
|
727 } |
|
728 else if (nc == 1) |
|
729 { |
|
730 retval.resize (nr); |
|
731 for (int i = 0; i < nr; i++) |
2305
|
732 retval (i) = m (i, 0); |
1299
|
733 } |
|
734 else if (nr > 0 && nc > 0 |
2181
|
735 && (Vdo_fortran_indexing || force_vector_conversion)) |
1299
|
736 { |
|
737 retval.resize (nr * nc); |
|
738 int k = 0; |
|
739 for (int j = 0; j < nc; j++) |
|
740 for (int i = 0; i < nr; i++) |
2305
|
741 retval (k++) = m (i, j); |
1299
|
742 } |
|
743 else |
|
744 gripe_invalid_conversion ("real matrix", "real vector"); |
|
745 |
|
746 return retval; |
|
747 } |
|
748 |
|
749 // This could be made more efficient by doing all the work here rather |
|
750 // than relying on complex_matrix_value() to do any possible type |
|
751 // conversions. |
|
752 |
|
753 ComplexColumnVector |
2086
|
754 OCT_VAL_REP::complex_vector_value (bool force_string_conv, |
1827
|
755 bool force_vector_conversion) const |
1299
|
756 { |
|
757 ComplexColumnVector retval; |
|
758 |
1827
|
759 ComplexMatrix m = complex_matrix_value (force_string_conv); |
1299
|
760 |
|
761 if (error_state) |
|
762 return retval; |
|
763 |
|
764 int nr = m.rows (); |
|
765 int nc = m.columns (); |
|
766 if (nr == 1) |
|
767 { |
|
768 retval.resize (nc); |
|
769 for (int i = 0; i < nc; i++) |
2305
|
770 retval (i) = m (0, i); |
1299
|
771 } |
|
772 else if (nc == 1) |
|
773 { |
|
774 retval.resize (nr); |
|
775 for (int i = 0; i < nr; i++) |
2305
|
776 retval (i) = m (i, 0); |
1299
|
777 } |
|
778 else if (nr > 0 && nc > 0 |
2181
|
779 && (Vdo_fortran_indexing || force_vector_conversion)) |
1299
|
780 { |
|
781 retval.resize (nr * nc); |
|
782 int k = 0; |
|
783 for (int j = 0; j < nc; j++) |
|
784 for (int i = 0; i < nr; i++) |
2305
|
785 retval (k++) = m (i, j); |
1299
|
786 } |
|
787 else |
|
788 gripe_invalid_conversion ("complex matrix", "complex vector"); |
|
789 |
|
790 return retval; |
|
791 } |
|
792 |
2086
|
793 octave_value |
|
794 OCT_VAL_REP::convert_to_str (void) const |
1299
|
795 { |
2086
|
796 octave_value retval; |
1299
|
797 |
|
798 switch (type_tag) |
|
799 { |
|
800 case complex_scalar_constant: |
|
801 case scalar_constant: |
|
802 { |
|
803 double d = double_value (); |
|
804 |
|
805 if (xisnan (d)) |
|
806 { |
|
807 ::error ("invalid conversion from NaN to character"); |
|
808 return retval; |
|
809 } |
|
810 else |
|
811 { |
1358
|
812 // XXX FIXME XXX -- warn about out of range conversions? |
|
813 |
1299
|
814 int i = NINT (d); |
|
815 char s[2]; |
|
816 s[0] = (char) i; |
|
817 s[1] = '\0'; |
2086
|
818 retval = octave_value (s, 1); |
1299
|
819 } |
|
820 } |
|
821 break; |
|
822 |
|
823 case complex_matrix_constant: |
|
824 case matrix_constant: |
|
825 { |
|
826 if (rows () == 0 && columns () == 0) |
|
827 { |
|
828 char s = '\0'; |
2086
|
829 retval = octave_value (&s, 1); |
1299
|
830 } |
|
831 else |
|
832 { |
1355
|
833 Matrix m = matrix_value (); |
|
834 |
|
835 int nr = m.rows (); |
|
836 int nc = m.columns (); |
|
837 |
|
838 if (nr == 0 || nc == 0) |
1299
|
839 { |
|
840 char s = '\0'; |
2086
|
841 retval = octave_value (&s, 1); |
1299
|
842 } |
|
843 else |
|
844 { |
1572
|
845 charMatrix chm (nr, nc); |
|
846 |
|
847 for (int j = 0; j < nc; j++) |
1299
|
848 { |
1572
|
849 for (int i = 0; i < nr; i++) |
1299
|
850 { |
2305
|
851 double d = m (i, j); |
1355
|
852 |
|
853 if (xisnan (d)) |
|
854 { |
|
855 ::error ("invalid conversion from NaN to character"); |
|
856 return retval; |
|
857 } |
|
858 else |
|
859 { |
1358
|
860 // XXX FIXME XXX -- warn about out of |
|
861 // range conversions? |
1355
|
862 |
|
863 int ival = NINT (d); |
2305
|
864 chm (i, j) = (char) ival; |
1355
|
865 } |
1299
|
866 } |
|
867 } |
1355
|
868 |
2086
|
869 retval = octave_value (chm, 1); |
1299
|
870 } |
|
871 } |
|
872 } |
|
873 break; |
|
874 |
|
875 case range_constant: |
|
876 { |
|
877 Range r = range_value (); |
|
878 double b = r.base (); |
|
879 double incr = r.inc (); |
|
880 int nel = r.nelem (); |
|
881 char *s = new char [nel+1]; |
|
882 s[nel] = '\0'; |
|
883 for (int i = 0; i < nel; i++) |
|
884 { |
|
885 double d = b + i * incr; |
|
886 |
|
887 if (xisnan (d)) |
|
888 { |
|
889 ::error ("invalid conversion from NaN to character"); |
|
890 delete [] s; |
|
891 return retval; |
|
892 } |
|
893 else |
|
894 { |
1358
|
895 // XXX FIXME XXX -- warn about out of range |
|
896 // conversions? |
|
897 |
1299
|
898 int ival = NINT (d); |
|
899 s[i] = (char) ival; |
|
900 } |
|
901 } |
2086
|
902 retval = octave_value (s, 1); |
1299
|
903 delete [] s; |
|
904 } |
|
905 break; |
|
906 |
1572
|
907 case char_matrix_constant: |
2086
|
908 retval = octave_value (*char_matrix, 1); |
1572
|
909 break; |
|
910 |
|
911 case char_matrix_constant_str: |
2086
|
912 retval = octave_value (*char_matrix, 1); |
1299
|
913 break; |
|
914 |
|
915 default: |
2390
|
916 gripe_invalid_conversion (type_name (), "string"); |
1299
|
917 break; |
|
918 } |
|
919 |
|
920 return retval; |
|
921 } |
|
922 |
|
923 void |
2086
|
924 OCT_VAL_REP::convert_to_row_or_column_vector (void) |
1299
|
925 { |
|
926 assert (type_tag == matrix_constant || type_tag == complex_matrix_constant); |
|
927 |
|
928 int nr = rows (); |
|
929 int nc = columns (); |
|
930 |
|
931 if (nr == 1 || nc == 1) |
|
932 return; |
|
933 |
|
934 int len = nr * nc; |
|
935 |
|
936 assert (len > 0); |
|
937 |
|
938 int new_nr = 1; |
|
939 int new_nc = 1; |
|
940 |
2181
|
941 if (Vprefer_column_vectors) |
1299
|
942 new_nr = len; |
|
943 else |
|
944 new_nc = len; |
|
945 |
|
946 if (type_tag == matrix_constant) |
|
947 { |
|
948 Matrix *m = new Matrix (new_nr, new_nc); |
|
949 |
|
950 double *cop_out = matrix->fortran_vec (); |
|
951 |
|
952 for (int i = 0; i < len; i++) |
|
953 { |
|
954 if (new_nr == 1) |
2305
|
955 (*m) (0, i) = *cop_out++; |
1299
|
956 else |
2305
|
957 (*m) (i, 0) = *cop_out++; |
1299
|
958 } |
|
959 |
|
960 delete matrix; |
|
961 matrix = m; |
|
962 } |
|
963 else |
|
964 { |
|
965 ComplexMatrix *cm = new ComplexMatrix (new_nr, new_nc); |
|
966 |
|
967 Complex *cop_out = complex_matrix->fortran_vec (); |
|
968 |
|
969 for (int i = 0; i < len; i++) |
|
970 { |
|
971 if (new_nr == 1) |
2305
|
972 (*cm) (0, i) = *cop_out++; |
1299
|
973 else |
2305
|
974 (*cm) (i, 0) = *cop_out++; |
1299
|
975 } |
|
976 |
|
977 delete complex_matrix; |
|
978 complex_matrix = cm; |
|
979 } |
|
980 } |
|
981 |
|
982 void |
2086
|
983 OCT_VAL_REP::convert_to_matrix_type (bool make_complex) |
1558
|
984 { |
|
985 switch (type_tag) |
|
986 { |
|
987 case complex_scalar_constant: |
|
988 { |
|
989 Complex *old_complex = complex_scalar; |
|
990 complex_matrix = new ComplexMatrix (1, 1, *complex_scalar); |
|
991 type_tag = complex_matrix_constant; |
|
992 delete old_complex; |
|
993 } |
|
994 break; |
|
995 |
|
996 case scalar_constant: |
|
997 { |
1581
|
998 if (make_complex) |
|
999 { |
|
1000 complex_matrix = new ComplexMatrix (1, 1, scalar); |
|
1001 type_tag = complex_matrix_constant; |
|
1002 } |
|
1003 else |
|
1004 { |
|
1005 matrix = new Matrix (1, 1, scalar); |
|
1006 type_tag = matrix_constant; |
|
1007 } |
1558
|
1008 } |
|
1009 break; |
|
1010 |
|
1011 case unknown_constant: |
|
1012 { |
1581
|
1013 if (make_complex) |
|
1014 { |
|
1015 complex_matrix = new ComplexMatrix (); |
|
1016 type_tag = complex_matrix_constant; |
|
1017 } |
|
1018 else |
|
1019 { |
|
1020 matrix = new Matrix (); |
|
1021 type_tag = matrix_constant; |
|
1022 } |
1558
|
1023 } |
|
1024 break; |
|
1025 |
1572
|
1026 case range_constant: |
|
1027 { |
1581
|
1028 if (make_complex) |
|
1029 { |
|
1030 ComplexMatrix *tmp = new ComplexMatrix (range->matrix_value ()); |
|
1031 delete range; |
|
1032 complex_matrix = tmp; |
|
1033 type_tag = complex_matrix_constant; |
|
1034 } |
|
1035 else |
|
1036 { |
|
1037 Matrix *tmp = new Matrix (range->matrix_value ()); |
|
1038 delete range; |
|
1039 matrix = tmp; |
|
1040 type_tag = matrix_constant; |
|
1041 } |
1572
|
1042 } |
|
1043 break; |
|
1044 |
1558
|
1045 default: |
|
1046 panic_impossible (); |
|
1047 break; |
|
1048 } |
|
1049 } |
|
1050 |
|
1051 void |
2086
|
1052 OCT_VAL_REP::force_numeric (bool force_string_conv) |
1299
|
1053 { |
|
1054 switch (type_tag) |
|
1055 { |
|
1056 case scalar_constant: |
|
1057 case matrix_constant: |
|
1058 case complex_scalar_constant: |
|
1059 case complex_matrix_constant: |
1572
|
1060 case char_matrix_constant: |
1299
|
1061 break; |
|
1062 |
1572
|
1063 case char_matrix_constant_str: |
1299
|
1064 { |
2181
|
1065 if (! force_string_conv && ! Vimplicit_str_to_num_ok) |
1299
|
1066 { |
1490
|
1067 ::error ("string to numeric conversion failed --\ |
|
1068 default conversion turned off"); |
1299
|
1069 return; |
|
1070 } |
|
1071 |
1572
|
1072 int nr = char_matrix->rows (); |
|
1073 int nc = char_matrix->cols (); |
1355
|
1074 |
|
1075 if (nr == 1 && nc == 1) |
1299
|
1076 { |
|
1077 type_tag = scalar_constant; |
2305
|
1078 double tmp = toascii ((int) (*char_matrix) (0, 0)); |
1572
|
1079 delete char_matrix; |
|
1080 scalar = tmp; |
1299
|
1081 } |
1355
|
1082 else if (nr == 0 || nc == 0) |
1299
|
1083 { |
1572
|
1084 delete char_matrix; |
1299
|
1085 type_tag = matrix_constant; |
|
1086 matrix = new Matrix (0, 0); |
|
1087 } |
1355
|
1088 else if (nr > 0 && nc > 0) |
|
1089 { |
|
1090 type_tag = matrix_constant; |
|
1091 |
|
1092 Matrix *tm = new Matrix (nr, nc); |
|
1093 |
|
1094 for (int i = 0; i < nr; i++) |
|
1095 { |
|
1096 for (int j = 0; j < nc; j++) |
|
1097 { |
2305
|
1098 int c = (int) (*char_matrix) (i, j); |
|
1099 (*tm) (i, j) = toascii (c); |
1355
|
1100 } |
|
1101 } |
1572
|
1102 delete char_matrix; |
1355
|
1103 matrix = tm; |
|
1104 } |
1299
|
1105 else |
|
1106 panic_impossible (); |
|
1107 } |
|
1108 break; |
|
1109 |
|
1110 case range_constant: |
|
1111 { |
|
1112 int len = range->nelem (); |
|
1113 if (len > 1) |
|
1114 { |
|
1115 type_tag = matrix_constant; |
|
1116 Matrix *tm = new Matrix (1, len); |
|
1117 double b = range->base (); |
|
1118 double increment = range->inc (); |
|
1119 for (int i = 0; i < len; i++) |
2305
|
1120 (*tm) (0, i) = b + i * increment; |
1572
|
1121 delete range; |
1299
|
1122 matrix = tm; |
|
1123 } |
|
1124 else if (len == 1) |
|
1125 { |
|
1126 type_tag = scalar_constant; |
|
1127 scalar = range->base (); |
|
1128 } |
|
1129 } |
|
1130 break; |
|
1131 |
|
1132 default: |
2390
|
1133 gripe_invalid_conversion (type_name (), "numeric type"); |
1299
|
1134 break; |
|
1135 } |
|
1136 } |
|
1137 |
2086
|
1138 octave_value |
|
1139 OCT_VAL_REP::make_numeric (bool force_string_conv) const |
1299
|
1140 { |
2086
|
1141 octave_value retval; |
1299
|
1142 |
|
1143 switch (type_tag) |
|
1144 { |
|
1145 case scalar_constant: |
1355
|
1146 retval = scalar; |
1299
|
1147 break; |
|
1148 |
|
1149 case matrix_constant: |
1355
|
1150 retval = *matrix; |
1299
|
1151 break; |
|
1152 |
|
1153 case complex_scalar_constant: |
1355
|
1154 retval = *complex_scalar; |
1299
|
1155 break; |
|
1156 |
|
1157 case complex_matrix_constant: |
1355
|
1158 retval = *complex_matrix; |
1299
|
1159 break; |
|
1160 |
1572
|
1161 case char_matrix_constant: |
|
1162 retval = *char_matrix; |
|
1163 break; |
|
1164 |
|
1165 case char_matrix_constant_str: |
|
1166 { |
1827
|
1167 int flag = force_string_conv; |
1572
|
1168 if (! flag) |
2181
|
1169 flag = Vimplicit_str_to_num_ok; |
1572
|
1170 |
|
1171 if (flag < 0) |
2390
|
1172 gripe_implicit_conversion ("string", "char matrix"); |
1572
|
1173 |
|
1174 if (flag) |
|
1175 { |
2260
|
1176 retval = octave_value (*char_matrix, true); |
1827
|
1177 retval.force_numeric (force_string_conv); |
1572
|
1178 } |
|
1179 else |
|
1180 gripe_invalid_conversion ("string", "char matrix"); |
|
1181 } |
1299
|
1182 break; |
|
1183 |
|
1184 case range_constant: |
1355
|
1185 retval = *range; |
1827
|
1186 retval.force_numeric (force_string_conv); |
1299
|
1187 break; |
|
1188 |
|
1189 default: |
2390
|
1190 gripe_invalid_conversion (type_name (), "numeric value"); |
1299
|
1191 break; |
|
1192 } |
|
1193 |
|
1194 return retval; |
|
1195 } |
|
1196 |
|
1197 void |
2086
|
1198 OCT_VAL_REP::maybe_mutate (void) |
1299
|
1199 { |
|
1200 switch (type_tag) |
|
1201 { |
|
1202 case complex_scalar_constant: |
|
1203 if (::imag (*complex_scalar) == 0.0) |
|
1204 { |
|
1205 double d = ::real (*complex_scalar); |
|
1206 delete complex_scalar; |
|
1207 scalar = d; |
|
1208 type_tag = scalar_constant; |
|
1209 } |
|
1210 break; |
|
1211 |
|
1212 case complex_matrix_constant: |
|
1213 if (! any_element_is_complex (*complex_matrix)) |
|
1214 { |
|
1215 Matrix *m = new Matrix (::real (*complex_matrix)); |
|
1216 delete complex_matrix; |
|
1217 matrix = m; |
|
1218 type_tag = matrix_constant; |
|
1219 } |
|
1220 break; |
|
1221 |
|
1222 default: |
|
1223 break; |
|
1224 } |
|
1225 |
1358
|
1226 // Avoid calling rows() and columns() for things like magic_colon. |
1299
|
1227 |
|
1228 int nr = 1; |
|
1229 int nc = 1; |
|
1230 if (type_tag == matrix_constant |
|
1231 || type_tag == complex_matrix_constant |
|
1232 || type_tag == range_constant) |
|
1233 { |
|
1234 nr = rows (); |
|
1235 nc = columns (); |
|
1236 } |
|
1237 |
|
1238 switch (type_tag) |
|
1239 { |
|
1240 case matrix_constant: |
|
1241 if (nr == 1 && nc == 1) |
|
1242 { |
2305
|
1243 double d = (*matrix) (0, 0); |
1299
|
1244 delete matrix; |
|
1245 scalar = d; |
|
1246 type_tag = scalar_constant; |
|
1247 } |
|
1248 break; |
|
1249 |
|
1250 case complex_matrix_constant: |
|
1251 if (nr == 1 && nc == 1) |
|
1252 { |
2305
|
1253 Complex c = (*complex_matrix) (0, 0); |
1299
|
1254 delete complex_matrix; |
|
1255 complex_scalar = new Complex (c); |
|
1256 type_tag = complex_scalar_constant; |
|
1257 } |
|
1258 break; |
|
1259 |
|
1260 case range_constant: |
|
1261 if (nr == 1 && nc == 1) |
|
1262 { |
|
1263 double d = range->base (); |
|
1264 delete range; |
|
1265 scalar = d; |
|
1266 type_tag = scalar_constant; |
|
1267 } |
|
1268 break; |
|
1269 |
|
1270 default: |
|
1271 break; |
|
1272 } |
|
1273 } |
|
1274 |
|
1275 void |
2086
|
1276 OCT_VAL_REP::print (ostream& output_buf) |
1299
|
1277 { |
|
1278 if (error_state) |
|
1279 return; |
|
1280 |
|
1281 case map_constant: |
|
1282 { |
1358
|
1283 // XXX FIXME XXX -- would be nice to print the output in some |
|
1284 // standard order. Maybe all substructures first, maybe |
|
1285 // alphabetize entries, etc. |
|
1286 |
2086
|
1287 begin_unwind_frame ("OCT_VAL_REP_print"); |
1299
|
1288 |
1973
|
1289 unwind_protect_int (struct_indent); |
2181
|
1290 unwind_protect_int (Vstruct_levels_to_print); |
|
1291 |
|
1292 if (Vstruct_levels_to_print-- > 0) |
1299
|
1293 { |
1973
|
1294 output_buf.form ("\n%*s{\n", struct_indent, ""); |
|
1295 |
|
1296 increment_struct_indent (); |
|
1297 |
|
1298 Pix p = a_map->first (); |
|
1299 |
|
1300 while (p) |
1299
|
1301 { |
1973
|
1302 bool pad_after = false; |
|
1303 |
1755
|
1304 string key = a_map->key (p); |
2086
|
1305 octave_value val = a_map->contents (p); |
1299
|
1306 |
1973
|
1307 a_map->next (p); |
|
1308 |
|
1309 output_buf.form ("%*s%s =", struct_indent, |
1755
|
1310 "", key.c_str ()); |
|
1311 |
1973
|
1312 if (val.print_as_scalar ()) |
|
1313 output_buf << " "; |
|
1314 else if (val.print_as_structure ()) |
|
1315 { |
|
1316 if (p) |
|
1317 pad_after = true; |
|
1318 } |
|
1319 else |
|
1320 { |
|
1321 if (p) |
|
1322 pad_after = true; |
|
1323 |
|
1324 output_buf << "\n\n"; |
|
1325 } |
1299
|
1326 |
|
1327 val.print (output_buf); |
1973
|
1328 |
|
1329 if (pad_after) |
|
1330 output_buf << "\n"; |
1299
|
1331 } |
|
1332 |
1973
|
1333 decrement_struct_indent (); |
|
1334 |
|
1335 output_buf.form ("%*s%s", struct_indent, "", "}\n"); |
1299
|
1336 } |
|
1337 else |
1973
|
1338 output_buf << " <structure>\n"; |
1299
|
1339 |
2086
|
1340 run_unwind_frame ("OCT_VAL_REP_print"); |
1299
|
1341 } |
|
1342 break; |
|
1343 } |
|
1344 |
|
1345 // Indexing operations for the tree-constant representation class. |
1558
|
1346 |
|
1347 void |
2086
|
1348 OCT_VAL_REP::clear_index (void) |
1558
|
1349 { |
|
1350 switch (type_tag) |
|
1351 { |
|
1352 case matrix_constant: |
|
1353 matrix->clear_index (); |
|
1354 break; |
|
1355 |
2086
|
1356 case OCT_VAL_REP::complex_matrix_constant: |
1558
|
1357 complex_matrix->clear_index (); |
|
1358 break; |
|
1359 |
1572
|
1360 case char_matrix_constant: |
|
1361 case char_matrix_constant_str: |
|
1362 char_matrix->clear_index (); |
|
1363 break; |
|
1364 |
1558
|
1365 default: |
|
1366 panic_impossible (); |
|
1367 break; |
|
1368 } |
|
1369 } |
|
1370 |
|
1371 #if 0 |
|
1372 void |
2086
|
1373 OCT_VAL_REP::set_index (double d) |
1558
|
1374 { |
|
1375 switch (type_tag) |
|
1376 { |
|
1377 case matrix_constant: |
|
1378 matrix->set_index (d); |
|
1379 break; |
|
1380 |
2086
|
1381 case OCT_VAL_REP::complex_matrix_constant: |
1558
|
1382 complex_matrix->set_index (d); |
|
1383 break; |
|
1384 |
2086
|
1385 case OCT_VAL_REP::char_matrix_constant: |
|
1386 case OCT_VAL_REP::char_matrix_constant_str: |
1572
|
1387 char_matrix->set_index (d); |
|
1388 break; |
|
1389 |
1558
|
1390 default: |
|
1391 panic_impossible (); |
|
1392 break; |
|
1393 } |
|
1394 } |
|
1395 #endif |
|
1396 |
|
1397 void |
2086
|
1398 OCT_VAL_REP::set_index (const Range& r) |
1558
|
1399 { |
|
1400 switch (type_tag) |
|
1401 { |
|
1402 case matrix_constant: |
|
1403 matrix->set_index (r); |
|
1404 break; |
|
1405 |
2086
|
1406 case OCT_VAL_REP::complex_matrix_constant: |
1558
|
1407 complex_matrix->set_index (r); |
|
1408 break; |
|
1409 |
2086
|
1410 case OCT_VAL_REP::char_matrix_constant: |
|
1411 case OCT_VAL_REP::char_matrix_constant_str: |
1572
|
1412 char_matrix->set_index (r); |
|
1413 break; |
|
1414 |
1558
|
1415 default: |
|
1416 panic_impossible (); |
|
1417 break; |
|
1418 } |
|
1419 } |
|
1420 |
|
1421 void |
2086
|
1422 OCT_VAL_REP::set_index (const ColumnVector& v) |
1558
|
1423 { |
|
1424 switch (type_tag) |
|
1425 { |
|
1426 case matrix_constant: |
|
1427 matrix->set_index (v); |
|
1428 break; |
|
1429 |
2086
|
1430 case OCT_VAL_REP::complex_matrix_constant: |
1558
|
1431 complex_matrix->set_index (v); |
|
1432 break; |
|
1433 |
2086
|
1434 case OCT_VAL_REP::char_matrix_constant: |
|
1435 case OCT_VAL_REP::char_matrix_constant_str: |
1572
|
1436 char_matrix->set_index (v); |
|
1437 break; |
|
1438 |
1558
|
1439 default: |
|
1440 panic_impossible (); |
|
1441 break; |
|
1442 } |
|
1443 } |
|
1444 |
|
1445 void |
2086
|
1446 OCT_VAL_REP::set_index (const Matrix& m) |
1558
|
1447 { |
|
1448 int nr = m.rows (); |
|
1449 int nc = m.cols (); |
|
1450 |
|
1451 if (nr <= 1 || nc <= 1 |
2181
|
1452 || Vdo_fortran_indexing) |
1558
|
1453 { |
|
1454 switch (type_tag) |
|
1455 { |
|
1456 case matrix_constant: |
|
1457 matrix->set_index (m); |
|
1458 break; |
|
1459 |
2086
|
1460 case OCT_VAL_REP::complex_matrix_constant: |
1558
|
1461 complex_matrix->set_index (m); |
|
1462 break; |
|
1463 |
2086
|
1464 case OCT_VAL_REP::char_matrix_constant: |
|
1465 case OCT_VAL_REP::char_matrix_constant_str: |
1572
|
1466 char_matrix->set_index (m); |
|
1467 break; |
|
1468 |
1558
|
1469 default: |
|
1470 panic_impossible (); |
|
1471 break; |
|
1472 } |
|
1473 } |
|
1474 else |
|
1475 ::error ("invalid matrix used as index"); |
|
1476 } |
|
1477 |
|
1478 // XXX FIXME XXX -- this should probably be handled some other way... |
|
1479 // The arg here is expected to be ':'. |
|
1480 void |
2086
|
1481 OCT_VAL_REP::set_index (char c) |
1558
|
1482 { |
|
1483 switch (type_tag) |
|
1484 { |
|
1485 case matrix_constant: |
|
1486 matrix->set_index (c); |
|
1487 break; |
|
1488 |
2086
|
1489 case OCT_VAL_REP::complex_matrix_constant: |
1558
|
1490 complex_matrix->set_index (c); |
|
1491 break; |
|
1492 |
2086
|
1493 case OCT_VAL_REP::char_matrix_constant: |
|
1494 case OCT_VAL_REP::char_matrix_constant_str: |
1572
|
1495 char_matrix->set_index (c); |
|
1496 break; |
|
1497 |
1558
|
1498 default: |
|
1499 panic_impossible (); |
|
1500 break; |
|
1501 } |
|
1502 } |
|
1503 |
|
1504 void |
2086
|
1505 OCT_VAL_REP::set_index (const octave_value_list& args, bool rhs_is_complex) |
1558
|
1506 { |
2115
|
1507 // XXX FIXME XXX -- it's not good that we have to list all the types |
|
1508 // that can be indexed here. |
|
1509 |
1558
|
1510 switch (type_tag) |
|
1511 { |
|
1512 case unknown_constant: |
|
1513 case scalar_constant: |
|
1514 case complex_scalar_constant: |
|
1515 case range_constant: |
1581
|
1516 convert_to_matrix_type (rhs_is_complex); |
1558
|
1517 break; |
|
1518 |
2115
|
1519 case matrix_constant: |
|
1520 case complex_matrix_constant: |
|
1521 case char_matrix_constant: |
|
1522 case char_matrix_constant_str: |
|
1523 break; |
|
1524 |
1558
|
1525 default: |
2390
|
1526 ::error ("indexing %s type not implemented", type_name ()); |
1558
|
1527 break; |
|
1528 } |
|
1529 |
2115
|
1530 if (! error_state) |
1558
|
1531 { |
2115
|
1532 int n = args.length (); |
|
1533 |
|
1534 for (int i = 0; i < n; i++) |
1558
|
1535 { |
2115
|
1536 octave_value arg = args (i); |
|
1537 |
|
1538 switch (arg.const_type ()) |
|
1539 { |
|
1540 case range_constant: |
|
1541 set_index (arg.range_value ()); |
|
1542 break; |
|
1543 |
|
1544 case magic_colon: |
|
1545 set_index (':'); |
|
1546 break; |
|
1547 |
|
1548 default: |
|
1549 set_index (arg.matrix_value ()); |
|
1550 break; |
|
1551 } |
|
1552 |
|
1553 if (error_state) |
|
1554 { |
|
1555 clear_index (); |
|
1556 break; |
|
1557 } |
1558
|
1558 } |
|
1559 } |
|
1560 } |
|
1561 |
1827
|
1562 static inline bool |
2086
|
1563 valid_scalar_indices (const octave_value_list& args) |
1558
|
1564 { |
|
1565 int nargin = args.length (); |
|
1566 |
|
1567 for (int i = 0; i < nargin; i++) |
|
1568 if (! args(i).valid_as_scalar_index ()) |
1827
|
1569 return false; |
|
1570 |
|
1571 return true; |
1558
|
1572 } |
1299
|
1573 |
2086
|
1574 octave_value |
|
1575 OCT_VAL_REP::do_index (const octave_value_list& args) |
1299
|
1576 { |
2086
|
1577 octave_value retval; |
1299
|
1578 |
|
1579 if (error_state) |
|
1580 return retval; |
|
1581 |
1827
|
1582 bool originally_scalar_type = is_scalar_type (); |
1558
|
1583 |
|
1584 if (originally_scalar_type && valid_scalar_indices (args)) |
1299
|
1585 { |
1558
|
1586 switch (type_tag) |
1299
|
1587 { |
1558
|
1588 case scalar_constant: |
|
1589 retval = scalar; |
|
1590 break; |
|
1591 |
|
1592 case complex_scalar_constant: |
|
1593 retval = *complex_scalar; |
1299
|
1594 break; |
|
1595 |
|
1596 default: |
1558
|
1597 panic_impossible (); |
1299
|
1598 break; |
|
1599 } |
|
1600 } |
|
1601 else |
|
1602 { |
1558
|
1603 set_index (args); |
|
1604 |
|
1605 if (! error_state) |
|
1606 { |
|
1607 switch (type_tag) |
|
1608 { |
|
1609 case range_constant: |
|
1610 force_numeric (); |
|
1611 // Fall through... |
|
1612 |
|
1613 case matrix_constant: |
|
1614 retval = Matrix (matrix->value ()); |
|
1615 break; |
|
1616 |
|
1617 case complex_matrix_constant: |
|
1618 retval = ComplexMatrix (complex_matrix->value ()); |
|
1619 break; |
|
1620 |
1572
|
1621 case char_matrix_constant: |
|
1622 retval = charMatrix (char_matrix->value ()); |
|
1623 break; |
|
1624 |
|
1625 case char_matrix_constant_str: |
2275
|
1626 { |
|
1627 // Kluge to prevent s([]) from turning into a string |
|
1628 // with no rows... |
|
1629 charMatrix tmp (char_matrix->value ()); |
|
1630 |
|
1631 if (tmp.rows () == 0 && tmp.columns () == 0) |
|
1632 tmp.resize (1, 0); |
|
1633 |
|
1634 retval = octave_value (tmp, 1); |
|
1635 } |
1572
|
1636 break; |
|
1637 |
1558
|
1638 default: |
2390
|
1639 error ("can't index %s variables", type_name ()); |
1558
|
1640 break; |
|
1641 } |
2090
|
1642 |
|
1643 // We may have converted this value from a scalar to a |
|
1644 // matrix to allow indexing to work. |
|
1645 |
|
1646 if (! error_state) |
|
1647 maybe_mutate (); |
1558
|
1648 } |
1299
|
1649 } |
|
1650 |
|
1651 return retval; |
|
1652 } |
|
1653 |
1558
|
1654 void |
2086
|
1655 OCT_VAL_REP::maybe_widen (OCT_VAL_REP::constant_type rhs_type) |
1299
|
1656 { |
1558
|
1657 switch (type_tag) |
1299
|
1658 { |
|
1659 case matrix_constant: |
1558
|
1660 switch (rhs_type) |
|
1661 { |
|
1662 case complex_scalar_constant: |
|
1663 case complex_matrix_constant: |
1299
|
1664 { |
1558
|
1665 ComplexMatrix *cm = new ComplexMatrix (*matrix); |
|
1666 delete matrix; |
|
1667 complex_matrix = cm; |
|
1668 type_tag = complex_matrix_constant; |
1299
|
1669 } |
1558
|
1670 break; |
|
1671 |
|
1672 default: |
|
1673 break; |
|
1674 } |
1299
|
1675 break; |
|
1676 |
1572
|
1677 case char_matrix_constant: |
|
1678 switch (rhs_type) |
|
1679 { |
|
1680 case scalar_constant: |
|
1681 case matrix_constant: |
|
1682 { |
|
1683 Matrix *m = new Matrix (*char_matrix); |
|
1684 delete matrix; |
|
1685 matrix = m; |
|
1686 type_tag = matrix_constant; |
|
1687 } |
|
1688 break; |
|
1689 |
|
1690 case complex_scalar_constant: |
|
1691 case complex_matrix_constant: |
|
1692 { |
|
1693 ComplexMatrix *cm = new ComplexMatrix (*char_matrix); |
|
1694 delete matrix; |
|
1695 complex_matrix = cm; |
|
1696 type_tag = complex_matrix_constant; |
|
1697 } |
|
1698 break; |
|
1699 |
|
1700 default: |
|
1701 break; |
|
1702 } |
|
1703 break; |
|
1704 |
1299
|
1705 default: |
|
1706 break; |
|
1707 } |
|
1708 } |
|
1709 |
|
1710 // Assignment operations for the tree-constant representation class. |
|
1711 |
|
1712 // Top-level tree-constant function that handles assignments. Only |
|
1713 // decide if the left-hand side is currently a scalar or a matrix and |
|
1714 // hand off to other functions to do the real work. |
|
1715 |
1572
|
1716 // XXX FIXME XXX -- need some other way to make these functions |
|
1717 // visible here (they should be in some header file...) |
|
1718 |
1558
|
1719 extern void assign (Array2<Complex>&, const Array2<Complex>&); |
|
1720 extern void assign (Array2<Complex>&, const Array2<double>&); |
1572
|
1721 extern void assign (Array2<Complex>&, const Array2<char>&); |
|
1722 |
1558
|
1723 extern void assign (Array2<double>&, const Array2<double>&); |
1572
|
1724 extern void assign (Array2<double>&, const Array2<char>&); |
|
1725 |
|
1726 extern void assign (Array2<char>&, const Array2<char>&); |
1558
|
1727 |
1299
|
1728 void |
2086
|
1729 OCT_VAL_REP::assign (octave_value& rhs, const octave_value_list& args) |
1299
|
1730 { |
1593
|
1731 // XXX FIXME XXX -- we should probably have special cases for rhs |
|
1732 // being a range type, since converting to a matrix can waste a lot |
|
1733 // of memory. |
|
1734 |
2086
|
1735 octave_value rhs_tmp = rhs; |
1593
|
1736 |
1652
|
1737 if (! (is_string () |
|
1738 && (rhs_tmp.is_string () |
|
1739 || rhs_tmp.is_zero_by_zero ()))) |
|
1740 { |
2269
|
1741 rhs_tmp = rhs_tmp.make_numeric (); |
1652
|
1742 |
|
1743 if (error_state) |
|
1744 return; |
|
1745 } |
|
1746 |
|
1747 if (rhs_tmp.is_string |
|
1748 && rhs_tmp.rows () == 1 |
|
1749 && rhs_tmp.columns () == 0) |
|
1750 { |
2269
|
1751 rhs_tmp = rhs_tmp.make_numeric (1); |
1652
|
1752 |
|
1753 if (error_state) |
|
1754 return; |
|
1755 } |
1299
|
1756 |
1572
|
1757 // An assignment to a range will normally require a conversion to a |
|
1758 // vector in the end anyway, since it will normally destroy the |
|
1759 // equally-spaced property of the range elements. This is not as |
|
1760 // memory efficient as possible, but it is much simpler than writing |
|
1761 // additional indexing and assignment functions especially for |
|
1762 // Ranges. |
|
1763 |
|
1764 if (is_defined () && ! (is_numeric_type () || is_string ())) |
1652
|
1765 { |
|
1766 force_numeric (); |
|
1767 |
|
1768 if (error_state) |
|
1769 return; |
|
1770 } |
|
1771 |
|
1772 if (! rhs_tmp.is_zero_by_zero ()) |
|
1773 { |
|
1774 maybe_widen (rhs_tmp.const_type ()); |
|
1775 |
|
1776 if (error_state) |
|
1777 return; |
|
1778 } |
|
1779 |
|
1780 set_index (args, rhs_tmp.is_complex_type ()); |
1299
|
1781 |
|
1782 if (error_state) |
|
1783 return; |
|
1784 |
1652
|
1785 switch (type_tag) |
1299
|
1786 { |
1652
|
1787 case complex_matrix_constant: |
|
1788 { |
|
1789 switch (rhs_tmp.const_type ()) |
1299
|
1790 { |
1652
|
1791 case complex_scalar_constant: |
|
1792 case complex_matrix_constant: |
|
1793 ::assign (*complex_matrix, rhs_tmp.complex_matrix_value ()); |
|
1794 break; |
|
1795 |
|
1796 case scalar_constant: |
|
1797 case matrix_constant: |
|
1798 ::assign (*complex_matrix, rhs_tmp.matrix_value ()); |
|
1799 break; |
|
1800 |
|
1801 default: |
|
1802 panic_impossible ();; |
|
1803 break; |
1299
|
1804 } |
1652
|
1805 } |
|
1806 break; |
|
1807 |
|
1808 case scalar_constant: |
|
1809 case matrix_constant: |
|
1810 { |
|
1811 switch (rhs_tmp.const_type ()) |
1572
|
1812 { |
1652
|
1813 case scalar_constant: |
|
1814 case matrix_constant: |
|
1815 ::assign (*matrix, rhs_tmp.matrix_value ()); |
|
1816 break; |
|
1817 |
|
1818 case char_matrix_constant: |
|
1819 ::assign (*matrix, rhs_tmp.char_matrix_value ()); |
|
1820 break; |
|
1821 |
|
1822 default: |
|
1823 panic_impossible (); |
|
1824 break; |
1572
|
1825 } |
1652
|
1826 } |
|
1827 break; |
|
1828 |
|
1829 case char_matrix_constant: |
|
1830 ::assign (*char_matrix, rhs_tmp.char_matrix_value ()); |
|
1831 break; |
|
1832 |
|
1833 case char_matrix_constant_str: |
|
1834 ::assign (*char_matrix, rhs_tmp.char_matrix_value ()); |
|
1835 if (char_matrix->rows () == 0 && char_matrix->columns () == 0) |
|
1836 char_matrix->resize (1, 0); |
|
1837 break; |
|
1838 |
|
1839 default: |
|
1840 panic_impossible (); |
|
1841 break; |
1299
|
1842 } |
1688
|
1843 |
|
1844 // Do the right thing for assignments like `x(1) = pi' when x is |
|
1845 // undefined before the assignment. |
|
1846 |
|
1847 if (is_matrix_type () || is_range ()) |
|
1848 maybe_mutate (); |
1299
|
1849 } |
|
1850 |
2390
|
1851 #endif |
2181
|
1852 |
96
|
1853 /* |
1
|
1854 ;;; Local Variables: *** |
|
1855 ;;; mode: C++ *** |
|
1856 ;;; End: *** |
|
1857 */ |