1741
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1741
|
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. |
1741
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
1741
|
29 |
5502
|
30 #include "quit.h" |
|
31 |
2172
|
32 #include "defun.h" |
1741
|
33 #include "error.h" |
|
34 #include "oct-obj.h" |
2982
|
35 #include "pt-arg-list.h" |
3770
|
36 #include "pt-bp.h" |
1741
|
37 #include "pt-exp.h" |
|
38 #include "pt-mat.h" |
2124
|
39 #include "pt-walk.h" |
2201
|
40 #include "utils.h" |
2371
|
41 #include "ov.h" |
|
42 #include "variables.h" |
1741
|
43 |
4460
|
44 // If TRUE, print a warning message for empty elements in a matrix list. |
|
45 static bool Vwarn_empty_list_elements; |
2172
|
46 |
2254
|
47 // The character to fill with when creating string arrays. |
3836
|
48 char Vstring_fill_char = ' '; |
2254
|
49 |
5280
|
50 // Warn if concatenating double and single quoted strings. |
5391
|
51 char Vwarn_string_concat = false; |
5280
|
52 |
1741
|
53 // General matrices. This list type is much more work to handle than |
|
54 // constant matrices, but it allows us to construct matrices from |
|
55 // other matrices, variables, and functions. |
|
56 |
1827
|
57 // But first, some internal classes that make our job much easier. |
1741
|
58 |
1827
|
59 class |
|
60 tm_row_const |
1741
|
61 { |
1827
|
62 private: |
|
63 |
|
64 class |
4219
|
65 tm_row_const_rep : public octave_base_list<octave_value> |
1827
|
66 { |
|
67 public: |
|
68 |
|
69 tm_row_const_rep (void) |
5514
|
70 : count (1), dv (0, 0), all_str (false), |
5280
|
71 all_sq_str (false), all_dq_str (false), |
5502
|
72 some_str (false), all_real (false), all_cmplx (false), |
5533
|
73 all_mt (true), class_nm (octave_base_value::static_class_name ()), |
|
74 ok (false) |
|
75 { } |
1827
|
76 |
2971
|
77 tm_row_const_rep (const tree_argument_list& row) |
5514
|
78 : count (1), dv (0, 0), all_str (false), all_sq_str (false), |
5502
|
79 some_str (false), all_real (false), all_cmplx (false), |
5533
|
80 all_mt (true), class_nm (octave_base_value::static_class_name ()), |
|
81 ok (false) |
3110
|
82 { init (row); } |
1827
|
83 |
|
84 ~tm_row_const_rep (void) { } |
|
85 |
|
86 int count; |
|
87 |
4765
|
88 dim_vector dv; |
1741
|
89 |
1827
|
90 bool all_str; |
5280
|
91 bool all_sq_str; |
|
92 bool all_dq_str; |
3110
|
93 bool some_str; |
5502
|
94 bool all_real; |
|
95 bool all_cmplx; |
2602
|
96 bool all_mt; |
1827
|
97 |
5533
|
98 std::string class_nm; |
|
99 |
1827
|
100 bool ok; |
|
101 |
4501
|
102 bool do_init_element (tree_expression *, const octave_value&, bool&); |
|
103 |
2971
|
104 void init (const tree_argument_list&); |
1827
|
105 |
|
106 private: |
|
107 |
|
108 tm_row_const_rep (const tm_row_const_rep&); |
|
109 |
2971
|
110 tm_row_const_rep& operator = (const tm_row_const_rep&); |
2419
|
111 |
3661
|
112 void eval_error (const char *msg, int l, int c, |
|
113 int x = -1, int y = -1) const; |
2419
|
114 |
|
115 void eval_warning (const char *msg, int l, int c) const; |
1827
|
116 }; |
|
117 |
|
118 public: |
|
119 |
4219
|
120 typedef tm_row_const_rep::iterator iterator; |
|
121 typedef tm_row_const_rep::const_iterator const_iterator; |
|
122 |
2990
|
123 tm_row_const (void) |
|
124 : rep (0) { } |
1827
|
125 |
2971
|
126 tm_row_const (const tree_argument_list& row) |
|
127 : rep (new tm_row_const_rep (row)) { } |
1827
|
128 |
2990
|
129 tm_row_const (const tm_row_const& x) |
|
130 : rep (x.rep) |
|
131 { |
|
132 if (rep) |
|
133 rep->count++; |
|
134 } |
1827
|
135 |
|
136 tm_row_const& operator = (const tm_row_const& x) |
2990
|
137 { |
|
138 if (this != &x && rep != x.rep) |
|
139 { |
|
140 if (rep && --rep->count == 0) |
|
141 delete rep; |
1741
|
142 |
2990
|
143 rep = x.rep; |
1827
|
144 |
2990
|
145 if (rep) |
|
146 rep->count++; |
|
147 } |
1741
|
148 |
2990
|
149 return *this; |
|
150 } |
1827
|
151 |
|
152 ~tm_row_const (void) |
2990
|
153 { |
|
154 if (rep && --rep->count == 0) |
|
155 delete rep; |
|
156 } |
1741
|
157 |
5514
|
158 octave_idx_type rows (void) { return rep->dv(0); } |
|
159 octave_idx_type cols (void) { return rep->dv(1); } |
4765
|
160 |
|
161 dim_vector dims (void) { return rep->dv; } |
1827
|
162 |
2868
|
163 bool all_strings_p (void) const { return rep->all_str; } |
5280
|
164 bool all_sq_strings_p (void) const { return rep->all_sq_str; } |
|
165 bool all_dq_strings_p (void) const { return rep->all_dq_str; } |
3110
|
166 bool some_strings_p (void) const { return rep->some_str; } |
5502
|
167 bool all_real_p (void) const { return rep->all_real; } |
|
168 bool all_complex_p (void) const { return rep->all_cmplx; } |
2868
|
169 bool all_empty_p (void) const { return rep->all_mt; } |
1827
|
170 |
5533
|
171 std::string class_name (void) const { return rep->class_nm; } |
|
172 |
4219
|
173 operator bool () const { return (rep && rep->ok); } |
1827
|
174 |
4219
|
175 iterator begin (void) { return rep->begin (); } |
|
176 const_iterator begin (void) const { return rep->begin (); } |
|
177 |
|
178 iterator end (void) { return rep->end (); } |
|
179 const_iterator end (void) const { return rep->end (); } |
1827
|
180 |
|
181 private: |
|
182 |
|
183 tm_row_const_rep *rep; |
|
184 }; |
|
185 |
5533
|
186 static std::string |
|
187 get_concat_class (const std::string& c1, const std::string& c2) |
|
188 { |
|
189 std::string retval = octave_base_value::static_class_name (); |
|
190 |
|
191 if (c1 == c2) |
|
192 retval = c1; |
|
193 else |
|
194 { |
|
195 bool c1_is_int = (c1 == "int8" || c1 == "uint8" |
|
196 || c1 == "int16" || c1 == "uint16" |
|
197 || c1 == "int32" || c1 == "uint32" |
|
198 || c1 == "int64" || c1 == "uint64"); |
|
199 bool c2_is_int = (c2 == "int8" || c2 == "uint8" |
|
200 || c2 == "int16" || c2 == "uint16" |
|
201 || c2 == "int32" || c2 == "uint32" |
|
202 || c2 == "int64" || c2 == "uint64"); |
|
203 |
|
204 bool c1_is_char = (c1 == "char"); |
|
205 bool c2_is_char = (c2 == "char"); |
|
206 |
|
207 bool c1_is_double = (c1 == "double"); |
|
208 bool c2_is_double = (c2 == "double"); |
|
209 |
|
210 bool c1_is_single = (c1 == "single"); |
|
211 bool c2_is_single = (c2 == "single"); |
|
212 |
|
213 bool c1_is_logical = (c1 == "logical"); |
|
214 bool c2_is_logical = (c2 == "logical"); |
|
215 |
|
216 bool c1_is_built_in_type |
|
217 = (c1_is_int || c1_is_char || c1_is_double || c1_is_single |
|
218 || c1_is_logical); |
|
219 |
|
220 bool c2_is_built_in_type |
|
221 = (c2_is_int || c2_is_char || c2_is_double || c2_is_single |
|
222 || c2_is_logical); |
|
223 |
|
224 // Order is important here... |
|
225 |
|
226 if (c1_is_char && c2_is_built_in_type) |
|
227 retval = c1; |
|
228 else if (c2_is_char && c1_is_built_in_type) |
|
229 retval = c2; |
|
230 else if (c1_is_int && c2_is_built_in_type) |
|
231 retval = c1; |
|
232 else if (c2_is_int && c1_is_built_in_type) |
|
233 retval = c2; |
|
234 else if (c1_is_single && c2_is_built_in_type) |
|
235 retval = c1; |
|
236 else if (c2_is_single && c1_is_built_in_type) |
|
237 retval = c2; |
|
238 else if (c1_is_double && c2_is_built_in_type) |
|
239 retval = c1; |
|
240 else if (c2_is_double && c1_is_built_in_type) |
|
241 retval = c2; |
|
242 else if (c1_is_logical && c2_is_logical) |
|
243 retval = c1; |
|
244 } |
|
245 |
|
246 return retval; |
|
247 } |
|
248 |
4501
|
249 bool |
|
250 tm_row_const::tm_row_const_rep::do_init_element (tree_expression *elt, |
|
251 const octave_value& val, |
|
252 bool& first_elem) |
|
253 { |
5275
|
254 octave_idx_type this_elt_nr = val.rows (); |
|
255 octave_idx_type this_elt_nc = val.columns (); |
4501
|
256 |
5533
|
257 std::string this_elt_class_nm = val.class_name (); |
|
258 |
4765
|
259 dim_vector this_elt_dv = val.dims (); |
|
260 |
5502
|
261 if (! this_elt_dv.all_zero ()) |
4501
|
262 { |
|
263 all_mt = false; |
|
264 |
|
265 if (first_elem) |
|
266 { |
|
267 first_elem = false; |
|
268 |
5533
|
269 class_nm = this_elt_class_nm; |
|
270 |
4765
|
271 dv.resize (this_elt_dv.length ()); |
|
272 for (int i = 2; i < dv.length (); i++) |
|
273 dv.elem (i) = this_elt_dv.elem (i); |
|
274 |
|
275 dv.elem (0) = this_elt_nr; |
|
276 |
|
277 dv.elem (1) = 0; |
4501
|
278 } |
4765
|
279 else |
4501
|
280 { |
5533
|
281 class_nm = get_concat_class (class_nm, this_elt_class_nm); |
|
282 |
5045
|
283 int len = (this_elt_dv.length () < dv.length () |
|
284 ? this_elt_dv.length () : dv.length ()); |
|
285 |
4765
|
286 if (this_elt_nr != dv (0)) |
|
287 { |
|
288 eval_error ("number of rows must match", |
|
289 elt->line (), elt->column (), this_elt_nr, dv (0)); |
|
290 return false; |
|
291 } |
5045
|
292 for (int i = 2; i < len; i++) |
4765
|
293 { |
|
294 if (this_elt_dv (i) != dv (i)) |
|
295 { |
|
296 eval_error ("dimensions mismatch", elt->line (), elt->column (), this_elt_dv (i), dv (i)); |
|
297 return false; |
|
298 } |
|
299 } |
5045
|
300 |
|
301 if (this_elt_dv.length () > len) |
|
302 for (int i = len; i < this_elt_dv.length (); i++) |
|
303 if (this_elt_dv (i) != 1) |
|
304 { |
|
305 eval_error ("dimensions mismatch", elt->line (), elt->column (), this_elt_dv (i), 1); |
|
306 return false; |
|
307 } |
|
308 |
|
309 if (dv.length () > len) |
|
310 for (int i = len; i < dv.length (); i++) |
|
311 if (dv (i) != 1) |
|
312 { |
|
313 eval_error ("dimensions mismatch", elt->line (), elt->column (), 1, dv (i)); |
|
314 return false; |
|
315 } |
4501
|
316 } |
4765
|
317 dv.elem (1) = dv.elem (1) + this_elt_nc; |
4501
|
318 |
|
319 } |
|
320 else if (Vwarn_empty_list_elements) |
|
321 eval_warning ("empty matrix found in matrix list", |
|
322 elt->line (), elt->column ()); |
|
323 |
4915
|
324 append (val); |
|
325 |
4501
|
326 if (all_str && ! val.is_string ()) |
|
327 all_str = false; |
|
328 |
5280
|
329 if (all_sq_str && ! val.is_sq_string ()) |
|
330 all_sq_str = false; |
|
331 |
|
332 if (all_dq_str && ! val.is_dq_string ()) |
|
333 all_dq_str = false; |
|
334 |
4501
|
335 if (! some_str && val.is_string ()) |
|
336 some_str = true; |
|
337 |
5502
|
338 if (all_real && ! val.is_real_type ()) |
|
339 all_real = false; |
|
340 |
|
341 if (all_cmplx && ! (val.is_complex_type () || val.is_real_type ())) |
|
342 all_cmplx = false; |
4501
|
343 |
|
344 return true; |
|
345 } |
|
346 |
1827
|
347 void |
2971
|
348 tm_row_const::tm_row_const_rep::init (const tree_argument_list& row) |
1827
|
349 { |
|
350 all_str = true; |
5280
|
351 all_sq_str = true; |
|
352 all_dq_str = true; |
5502
|
353 all_real = true; |
|
354 all_cmplx = true; |
1827
|
355 |
|
356 bool first_elem = true; |
|
357 |
4219
|
358 for (tree_argument_list::const_iterator p = row.begin (); |
|
359 p != row.end (); |
|
360 p++) |
1827
|
361 { |
5502
|
362 OCTAVE_QUIT; |
|
363 |
4219
|
364 tree_expression *elt = *p; |
1827
|
365 |
2971
|
366 octave_value tmp = elt->rvalue (); |
1827
|
367 |
|
368 if (error_state || tmp.is_undefined ()) |
|
369 break; |
|
370 else |
|
371 { |
4501
|
372 if (tmp.is_cs_list ()) |
1827
|
373 { |
4587
|
374 octave_value_list tlst = tmp.list_value (); |
2602
|
375 |
5275
|
376 for (octave_idx_type i = 0; i < tlst.length (); i++) |
1827
|
377 { |
5502
|
378 OCTAVE_QUIT; |
|
379 |
4587
|
380 if (! do_init_element (elt, tlst(i), first_elem)) |
4501
|
381 goto done; |
1827
|
382 } |
|
383 } |
4501
|
384 else |
|
385 { |
|
386 if (! do_init_element (elt, tmp, first_elem)) |
|
387 goto done; |
|
388 } |
1827
|
389 } |
|
390 } |
|
391 |
4501
|
392 done: |
|
393 |
1827
|
394 ok = ! error_state; |
1741
|
395 } |
|
396 |
2419
|
397 void |
|
398 tm_row_const::tm_row_const_rep::eval_error (const char *msg, int l, |
3661
|
399 int c, int x, int y) const |
2419
|
400 { |
|
401 if (l == -1 && c == -1) |
3661
|
402 { |
|
403 if (x == -1 || y == -1) |
|
404 ::error ("%s", msg); |
|
405 else |
|
406 ::error ("%s (%d != %d)", msg, x, y); |
|
407 } |
2419
|
408 else |
3661
|
409 { |
|
410 if (x == -1 || y == -1) |
|
411 ::error ("%s near line %d, column %d", msg, l, c); |
|
412 else |
|
413 ::error ("%s (%d != %d) near line %d, column %d", msg, x, y, l, c); |
|
414 } |
2419
|
415 } |
|
416 |
|
417 void |
|
418 tm_row_const::tm_row_const_rep::eval_warning (const char *msg, int l, |
|
419 int c) const |
|
420 { |
|
421 if (l == -1 && c == -1) |
|
422 ::warning ("%s", msg); |
|
423 else |
|
424 ::warning ("%s near line %d, column %d", msg, l, c); |
|
425 } |
|
426 |
1827
|
427 class |
4219
|
428 tm_const : public octave_base_list<tm_row_const> |
1827
|
429 { |
|
430 public: |
|
431 |
|
432 tm_const (const tree_matrix& tm) |
5514
|
433 : dv (0, 0), all_str (false), all_sq_str (false), all_dq_str (false), |
5502
|
434 some_str (false), all_real (false), all_cmplx (false), |
5533
|
435 all_mt (true), class_nm (octave_base_value::static_class_name ()), |
|
436 ok (false) |
|
437 { init (tm); } |
1827
|
438 |
|
439 ~tm_const (void) { } |
|
440 |
5514
|
441 octave_idx_type rows (void) const { return dv.elem (0); } |
|
442 octave_idx_type cols (void) const { return dv.elem (1); } |
4765
|
443 |
|
444 dim_vector dims (void) const { return dv; } |
1827
|
445 |
2868
|
446 bool all_strings_p (void) const { return all_str; } |
5280
|
447 bool all_sq_strings_p (void) const { return all_sq_str; } |
|
448 bool all_dq_strings_p (void) const { return all_dq_str; } |
3110
|
449 bool some_strings_p (void) const { return some_str; } |
5502
|
450 bool all_real_p (void) const { return all_real; } |
|
451 bool all_complex_p (void) const { return all_cmplx; } |
2868
|
452 bool all_empty_p (void) const { return all_mt; } |
1827
|
453 |
5533
|
454 std::string class_name (void) const { return class_nm; } |
|
455 |
3145
|
456 operator bool () const { return ok; } |
1827
|
457 |
|
458 private: |
|
459 |
4765
|
460 dim_vector dv; |
1827
|
461 |
|
462 bool all_str; |
5280
|
463 bool all_sq_str; |
|
464 bool all_dq_str; |
3110
|
465 bool some_str; |
5502
|
466 bool all_real; |
|
467 bool all_cmplx; |
2602
|
468 bool all_mt; |
1827
|
469 |
5533
|
470 std::string class_nm; |
|
471 |
1827
|
472 bool ok; |
|
473 |
|
474 tm_const (void); |
|
475 |
|
476 tm_const (const tm_const&); |
|
477 |
|
478 tm_const& operator = (const tm_const&); |
|
479 |
|
480 void init (const tree_matrix& tm); |
|
481 }; |
|
482 |
|
483 void |
|
484 tm_const::init (const tree_matrix& tm) |
1741
|
485 { |
1827
|
486 all_str = true; |
5280
|
487 all_sq_str = true; |
|
488 all_dq_str = true; |
5502
|
489 all_real = true; |
|
490 all_cmplx = true; |
1827
|
491 |
|
492 bool first_elem = true; |
|
493 |
|
494 // Just eval and figure out if what we have is complex or all |
|
495 // strings. We can't check columns until we know that this is a |
|
496 // numeric matrix -- collections of strings can have elements of |
|
497 // different lengths. |
|
498 |
4219
|
499 for (tree_matrix::const_iterator p = tm.begin (); p != tm.end (); p++) |
1827
|
500 { |
5502
|
501 OCTAVE_QUIT; |
|
502 |
4219
|
503 tree_argument_list *elt = *p; |
1827
|
504 |
|
505 tm_row_const tmp (*elt); |
|
506 |
|
507 if (tmp) |
|
508 { |
2868
|
509 if (all_str && ! tmp.all_strings_p ()) |
1827
|
510 all_str = false; |
|
511 |
5280
|
512 if (all_sq_str && ! tmp.all_sq_strings_p ()) |
|
513 all_sq_str = false; |
|
514 |
|
515 if (all_dq_str && ! tmp.all_dq_strings_p ()) |
|
516 all_dq_str = false; |
|
517 |
3110
|
518 if (! some_str && tmp.some_strings_p ()) |
|
519 some_str = true; |
|
520 |
5502
|
521 if (all_real && ! tmp.all_real_p ()) |
|
522 all_real = false; |
|
523 |
|
524 if (all_cmplx && ! tmp.all_complex_p ()) |
|
525 all_cmplx = false; |
1827
|
526 |
2868
|
527 if (all_mt && ! tmp.all_empty_p ()) |
2602
|
528 all_mt = false; |
|
529 |
1827
|
530 append (tmp); |
|
531 } |
|
532 else |
|
533 break; |
|
534 } |
|
535 |
|
536 if (! error_state) |
|
537 { |
4219
|
538 for (iterator p = begin (); p != end (); p++) |
1827
|
539 { |
5502
|
540 OCTAVE_QUIT; |
|
541 |
4219
|
542 tm_row_const elt = *p; |
1827
|
543 |
5275
|
544 octave_idx_type this_elt_nr = elt.rows (); |
|
545 octave_idx_type this_elt_nc = elt.cols (); |
1827
|
546 |
5533
|
547 std::string this_elt_class_nm = elt.class_name (); |
|
548 |
4765
|
549 dim_vector this_elt_dv = elt.dims (); |
|
550 |
|
551 if (!this_elt_dv.all_zero ()) |
1827
|
552 { |
2602
|
553 all_mt = false; |
|
554 |
1827
|
555 if (first_elem) |
|
556 { |
|
557 first_elem = false; |
|
558 |
5533
|
559 class_nm = this_elt_class_nm; |
|
560 |
4765
|
561 dv.resize (this_elt_dv.length ()); |
|
562 for (int i = 2; i < dv.length (); i++) |
|
563 dv.elem (i) = this_elt_dv.elem (i); |
|
564 |
|
565 dv.elem (0) = 0; |
|
566 |
|
567 dv.elem (1) = this_elt_nc; |
1827
|
568 } |
|
569 else if (all_str) |
|
570 { |
5533
|
571 class_nm = get_concat_class (class_nm, this_elt_class_nm); |
|
572 |
4765
|
573 if (this_elt_nc > cols ()) |
|
574 dv.elem (1) = this_elt_nc; |
1827
|
575 } |
4765
|
576 else |
1827
|
577 { |
5533
|
578 class_nm = get_concat_class (class_nm, this_elt_class_nm); |
|
579 |
4765
|
580 bool get_out = false; |
5045
|
581 int len = (this_elt_dv.length () < dv.length () |
|
582 ? this_elt_dv.length () : dv.length ()); |
4765
|
583 |
5045
|
584 for (int i = 1; i < len; i++) |
4765
|
585 { |
|
586 if (i == 1 && this_elt_nc != dv (1)) |
|
587 { |
|
588 ::error ("number of columns must match (%d != %d)", |
|
589 this_elt_nc, dv (1)); |
|
590 get_out = true; |
|
591 break; |
|
592 } |
|
593 else if (this_elt_dv (i) != dv (i)) |
|
594 { |
|
595 ::error ("dimensions mismatch (dim = %i, %d != %d)", i+1, this_elt_dv (i), dv (i)); |
|
596 get_out = true; |
|
597 break; |
|
598 } |
|
599 } |
|
600 |
5045
|
601 if (this_elt_dv.length () > len) |
|
602 for (int i = len; i < this_elt_dv.length (); i++) |
|
603 if (this_elt_dv (i) != 1) |
|
604 { |
|
605 ::error ("dimensions mismatch (dim = %i, %d != %d)", i+1, this_elt_dv (i), 1); |
|
606 get_out = true; |
|
607 break; |
|
608 } |
|
609 |
|
610 if (dv.length () > len) |
|
611 for (int i = len; i < dv.length (); i++) |
|
612 if (dv (i) != 1) |
|
613 { |
|
614 ::error ("dimensions mismatch (dim = %i, %d != %d)", i+1, 1, dv(i)); |
|
615 get_out = true; |
|
616 break; |
|
617 } |
|
618 |
4765
|
619 if (get_out) |
|
620 break; |
1827
|
621 } |
4765
|
622 dv.elem (0) = dv.elem (0) + this_elt_nr; |
1827
|
623 } |
4460
|
624 else if (Vwarn_empty_list_elements) |
|
625 warning ("empty matrix found in matrix list"); |
1827
|
626 } |
|
627 } |
|
628 |
|
629 ok = ! error_state; |
1741
|
630 } |
|
631 |
2990
|
632 tree_matrix::~tree_matrix (void) |
|
633 { |
4219
|
634 while (! empty ()) |
2990
|
635 { |
4219
|
636 iterator p = begin (); |
|
637 delete *p; |
|
638 erase (p); |
2990
|
639 } |
|
640 } |
|
641 |
1827
|
642 bool |
4267
|
643 tree_matrix::has_magic_end (void) const |
|
644 { |
|
645 for (const_iterator p = begin (); p != end (); p++) |
|
646 { |
5502
|
647 OCTAVE_QUIT; |
|
648 |
4267
|
649 tree_argument_list *elt = *p; |
|
650 |
|
651 if (elt && elt->has_magic_end ()) |
|
652 return true; |
|
653 } |
|
654 |
|
655 return false; |
|
656 } |
|
657 |
|
658 bool |
2529
|
659 tree_matrix::all_elements_are_constant (void) const |
1827
|
660 { |
4219
|
661 for (const_iterator p = begin (); p != end (); p++) |
1827
|
662 { |
5502
|
663 OCTAVE_QUIT; |
|
664 |
4219
|
665 tree_argument_list *elt = *p; |
1827
|
666 |
2529
|
667 if (! elt->all_elements_are_constant ()) |
1827
|
668 return false; |
|
669 } |
|
670 |
|
671 return true; |
|
672 } |
|
673 |
2971
|
674 octave_value_list |
|
675 tree_matrix::rvalue (int nargout) |
|
676 { |
|
677 octave_value_list retval; |
|
678 |
3770
|
679 MAYBE_DO_BREAKPOINT; |
|
680 |
2971
|
681 if (nargout > 1) |
|
682 error ("invalid number of output arguments for matrix list"); |
|
683 else |
|
684 retval = rvalue (); |
|
685 |
|
686 return retval; |
|
687 } |
|
688 |
5280
|
689 static void |
|
690 maybe_warn_string_concat (bool all_dq_strings_p, bool all_sq_strings_p) |
|
691 { |
|
692 if (Vwarn_string_concat && ! (all_dq_strings_p || all_sq_strings_p)) |
|
693 ::warning ("concatenation of different character string types may have unintended consequences"); |
|
694 } |
|
695 |
5502
|
696 #define SINGLE_TYPE_CONCAT(TYPE, EXTRACTOR) \ |
|
697 do \ |
|
698 { \ |
|
699 int dv_len = dv.length (); \ |
|
700 Array<octave_idx_type> ra_idx (dv_len > 1 ? dv_len : 2, 0); \ |
|
701 \ |
|
702 for (tm_const::iterator p = tmp.begin (); p != tmp.end (); p++) \ |
|
703 { \ |
|
704 OCTAVE_QUIT; \ |
|
705 \ |
|
706 tm_row_const row = *p; \ |
|
707 \ |
|
708 for (tm_row_const::iterator q = row.begin (); \ |
|
709 q != row.end (); \ |
|
710 q++) \ |
|
711 { \ |
|
712 OCTAVE_QUIT; \ |
|
713 \ |
|
714 TYPE ra = q->EXTRACTOR (); \ |
|
715 \ |
|
716 if (! error_state) \ |
|
717 { \ |
|
718 result.insert (ra, ra_idx); \ |
|
719 \ |
|
720 if (! error_state) \ |
|
721 ra_idx(1) += ra.columns (); \ |
|
722 else \ |
|
723 goto done; \ |
|
724 } \ |
|
725 else \ |
|
726 goto done; \ |
|
727 } \ |
|
728 \ |
|
729 ra_idx(0) += row.rows (); \ |
|
730 ra_idx(1) = 0; \ |
|
731 } \ |
|
732 } \ |
|
733 while (0) |
|
734 |
5533
|
735 #define DO_SINGLE_TYPE_CONCAT(TYPE, EXTRACTOR) \ |
|
736 do \ |
|
737 { \ |
|
738 TYPE result (dv); \ |
|
739 \ |
|
740 SINGLE_TYPE_CONCAT(TYPE, EXTRACTOR); \ |
|
741 \ |
|
742 retval = result; \ |
|
743 } \ |
|
744 while (0) |
|
745 |
2086
|
746 octave_value |
2971
|
747 tree_matrix::rvalue (void) |
1741
|
748 { |
2086
|
749 octave_value retval; |
1741
|
750 |
4915
|
751 bool all_strings_p = false; |
5280
|
752 bool all_sq_strings_p = false; |
|
753 bool all_dq_strings_p = false; |
4915
|
754 bool all_empty_p = false; |
5502
|
755 bool all_real_p = false; |
|
756 bool all_complex_p = false; |
4915
|
757 bool frc_str_conv = false; |
1741
|
758 |
4915
|
759 tm_const tmp (*this); |
3110
|
760 |
1827
|
761 if (tmp) |
|
762 { |
4765
|
763 dim_vector dv = tmp.dims (); |
4915
|
764 all_strings_p = tmp.all_strings_p (); |
5280
|
765 all_sq_strings_p = tmp.all_sq_strings_p (); |
|
766 all_dq_strings_p = tmp.all_dq_strings_p (); |
4915
|
767 all_empty_p = tmp.all_empty_p (); |
5502
|
768 all_real_p = tmp.all_real_p (); |
|
769 all_complex_p = tmp.all_complex_p (); |
4915
|
770 frc_str_conv = tmp.some_strings_p (); |
1741
|
771 |
5502
|
772 // Try to speed up the common cases. |
4915
|
773 |
5533
|
774 std::string result_type = tmp.class_name (); |
|
775 |
|
776 if (result_type == "double") |
|
777 { |
|
778 if (all_real_p) |
|
779 DO_SINGLE_TYPE_CONCAT (NDArray, array_value); |
|
780 else |
|
781 DO_SINGLE_TYPE_CONCAT (ComplexNDArray, complex_array_value); |
|
782 } |
|
783 #if 0 |
|
784 else if (result_type == "single") |
|
785 #endif |
|
786 else if (result_type == "char") |
5280
|
787 { |
|
788 char type = all_sq_strings_p ? '\'' : '"'; |
|
789 |
|
790 maybe_warn_string_concat (all_dq_strings_p, all_sq_strings_p); |
|
791 |
5502
|
792 charNDArray result (dv, Vstring_fill_char); |
|
793 |
|
794 SINGLE_TYPE_CONCAT (charNDArray, char_array_value); |
|
795 |
|
796 retval = octave_value (result, true, type); |
|
797 } |
5533
|
798 else if (result_type == "logical") |
|
799 DO_SINGLE_TYPE_CONCAT (boolNDArray, bool_array_value); |
|
800 else if (result_type == "int8") |
|
801 DO_SINGLE_TYPE_CONCAT (int8NDArray, int8_array_value); |
|
802 else if (result_type == "int16") |
|
803 DO_SINGLE_TYPE_CONCAT (int16NDArray, int16_array_value); |
|
804 else if (result_type == "int32") |
|
805 DO_SINGLE_TYPE_CONCAT (int32NDArray, int32_array_value); |
|
806 else if (result_type == "int64") |
|
807 DO_SINGLE_TYPE_CONCAT (int64NDArray, int64_array_value); |
|
808 else if (result_type == "uint8") |
|
809 DO_SINGLE_TYPE_CONCAT (uint8NDArray, uint8_array_value); |
|
810 else if (result_type == "uint16") |
|
811 DO_SINGLE_TYPE_CONCAT (uint16NDArray, uint16_array_value); |
|
812 else if (result_type == "uint32") |
|
813 DO_SINGLE_TYPE_CONCAT (uint32NDArray, uint32_array_value); |
|
814 else if (result_type == "uint64") |
|
815 DO_SINGLE_TYPE_CONCAT (uint64NDArray, uint64_array_value); |
4915
|
816 else |
|
817 { |
5502
|
818 // The line below might seem crazy, since we take a copy of |
|
819 // the first argument, resize it to be empty and then resize |
|
820 // it to be full. This is done since it means that there is |
|
821 // no recopying of data, as would happen if we used a single |
|
822 // resize. It should be noted that resize operation is also |
|
823 // significantly slower than the do_cat_op function, so it |
|
824 // makes sense to have an empty matrix and copy all data. |
|
825 // |
|
826 // We might also start with a empty octave_value using |
|
827 // |
|
828 // ctmp = octave_value_typeinfo::lookup_type |
|
829 // (tmp.begin() -> begin() -> type_name()); |
|
830 // |
|
831 // and then directly resize. However, for some types there |
|
832 // might be some additional setup needed, and so this should |
|
833 // be avoided. |
|
834 |
|
835 octave_value ctmp; |
|
836 |
5164
|
837 // Find the first non-empty object |
5502
|
838 |
5164
|
839 for (tm_const::iterator p = tmp.begin (); p != tmp.end (); p++) |
|
840 { |
5502
|
841 OCTAVE_QUIT; |
|
842 |
5164
|
843 tm_row_const row = *p; |
5502
|
844 |
5164
|
845 for (tm_row_const::iterator q = row.begin (); |
|
846 q != row.end (); q++) |
|
847 { |
5502
|
848 OCTAVE_QUIT; |
|
849 |
5164
|
850 ctmp = *q; |
5502
|
851 |
5164
|
852 if (! ctmp.all_zero_dims ()) |
|
853 goto found_non_empty; |
|
854 } |
|
855 } |
|
856 |
|
857 ctmp = (*(tmp.begin() -> begin())); |
|
858 |
|
859 found_non_empty: |
5502
|
860 |
5164
|
861 if (! all_empty_p) |
|
862 ctmp = ctmp.resize (dim_vector (0,0)).resize (dv); |
4915
|
863 |
5502
|
864 if (! error_state) |
1827
|
865 { |
5502
|
866 // Now, extract the values from the individual elements and |
|
867 // insert them in the result matrix. |
|
868 |
|
869 int dv_len = dv.length (); |
|
870 Array<int> ra_idx (dv_len > 1 ? dv_len : 2, 0); |
|
871 |
|
872 for (tm_const::iterator p = tmp.begin (); p != tmp.end (); p++) |
|
873 { |
|
874 OCTAVE_QUIT; |
|
875 |
|
876 tm_row_const row = *p; |
|
877 |
|
878 for (tm_row_const::iterator q = row.begin (); |
|
879 q != row.end (); |
|
880 q++) |
|
881 { |
|
882 OCTAVE_QUIT; |
1741
|
883 |
5502
|
884 octave_value elt = *q; |
|
885 |
|
886 ctmp = do_cat_op (ctmp, elt, ra_idx); |
|
887 |
|
888 if (error_state) |
|
889 goto done; |
|
890 |
|
891 ra_idx (1) += elt.columns (); |
|
892 } |
|
893 |
|
894 ra_idx (0) += row.rows (); |
|
895 ra_idx (1) = 0; |
|
896 } |
|
897 |
|
898 retval = ctmp; |
|
899 |
|
900 if (frc_str_conv && ! retval.is_string ()) |
|
901 retval = retval.convert_to_str (); |
1741
|
902 } |
|
903 } |
|
904 } |
|
905 |
1827
|
906 done: |
1741
|
907 return retval; |
|
908 } |
|
909 |
|
910 void |
2124
|
911 tree_matrix::accept (tree_walker& tw) |
1741
|
912 { |
2124
|
913 tw.visit_matrix (*this); |
1741
|
914 } |
|
915 |
2172
|
916 static int |
4460
|
917 warn_empty_list_elements (void) |
2172
|
918 { |
4460
|
919 Vwarn_empty_list_elements = check_preference ("warn_empty_list_elements"); |
2172
|
920 |
|
921 return 0; |
|
922 } |
|
923 |
2254
|
924 static int |
5280
|
925 warn_string_concat (void) |
|
926 { |
|
927 Vwarn_string_concat = check_preference ("warn_string_concat"); |
|
928 |
|
929 return 0; |
|
930 } |
|
931 |
|
932 static int |
2254
|
933 string_fill_char (void) |
|
934 { |
|
935 int status = 0; |
|
936 |
3523
|
937 std::string s = builtin_string_variable ("string_fill_char"); |
2254
|
938 |
|
939 switch (s.length ()) |
|
940 { |
|
941 case 1: |
|
942 Vstring_fill_char = s[0]; |
|
943 break; |
|
944 |
|
945 case 0: |
|
946 Vstring_fill_char = '\0'; |
|
947 break; |
|
948 |
|
949 default: |
|
950 warning ("string_fill_char must be a single character"); |
|
951 status = -1; |
|
952 break; |
|
953 } |
|
954 |
|
955 return status; |
|
956 } |
|
957 |
2172
|
958 void |
|
959 symbols_of_pt_mat (void) |
|
960 { |
3258
|
961 DEFVAR (string_fill_char, " ", string_fill_char, |
3361
|
962 "-*- texinfo -*-\n\ |
|
963 @defvr {Built-in Variable} string_fill_char\n\ |
|
964 The value of this variable is used to pad all strings in a string matrix\n\ |
|
965 to the same length. It should be a single character. The default value\n\ |
|
966 is @code{\" \"} (a single space). For example,\n\ |
|
967 \n\ |
|
968 @example\n\ |
|
969 @group\n\ |
|
970 string_fill_char = \"X\";\n\ |
|
971 [ \"these\"; \"are\"; \"strings\" ]\n\ |
|
972 @result{} \"theseXX\"\n\ |
|
973 \"areXXXX\"\n\ |
|
974 \"strings\"\n\ |
|
975 @end group\n\ |
|
976 @end example\n\ |
3363
|
977 @end defvr"); |
4460
|
978 |
|
979 DEFVAR (warn_empty_list_elements, false, warn_empty_list_elements, |
|
980 "-*- texinfo -*-\n\ |
|
981 @defvr {Built-in Variable} warn_empty_list_elements\n\ |
|
982 If the value of @code{warn_empty_list_elements} is nonzero, print a\n\ |
|
983 warning when an empty matrix is found in a matrix list. For example,\n\ |
|
984 \n\ |
|
985 @example\n\ |
|
986 a = [1, [], 3, [], 5]\n\ |
|
987 @end example\n\ |
|
988 \n\ |
|
989 @noindent\n\ |
|
990 The default value is 0.\n\ |
|
991 @end defvr"); |
|
992 |
5391
|
993 DEFVAR (warn_string_concat, false, warn_string_concat, |
5280
|
994 "-*- texinfo -*-\n\ |
|
995 @defvr {Built-in Variable} warn_string_concat\n\ |
|
996 If the value of @code{warn_string_concat} is nonzero, print a\n\ |
|
997 warning when concatenating a mixture of double and single quoted strings.\n\ |
|
998 The default value is 1.\n\ |
|
999 @end defvr"); |
|
1000 |
2172
|
1001 } |
|
1002 |
1741
|
1003 /* |
|
1004 ;;; Local Variables: *** |
|
1005 ;;; mode: C++ *** |
|
1006 ;;; End: *** |
|
1007 */ |