comparison src/pt-mat.cc @ 5533:667ad2becb63

[project @ 2005-11-10 21:40:48 by jwe]
author jwe
date Thu, 10 Nov 2005 21:40:49 +0000
parents e0011660696c
children 512d0d11ae39
comparison
equal deleted inserted replaced
5532:8ad54ce6a831 5533:667ad2becb63
68 68
69 tm_row_const_rep (void) 69 tm_row_const_rep (void)
70 : count (1), dv (0, 0), all_str (false), 70 : count (1), dv (0, 0), all_str (false),
71 all_sq_str (false), all_dq_str (false), 71 all_sq_str (false), all_dq_str (false),
72 some_str (false), all_real (false), all_cmplx (false), 72 some_str (false), all_real (false), all_cmplx (false),
73 all_mt (true), ok (false) { } 73 all_mt (true), class_nm (octave_base_value::static_class_name ()),
74 ok (false)
75 { }
74 76
75 tm_row_const_rep (const tree_argument_list& row) 77 tm_row_const_rep (const tree_argument_list& row)
76 : count (1), dv (0, 0), all_str (false), all_sq_str (false), 78 : count (1), dv (0, 0), all_str (false), all_sq_str (false),
77 some_str (false), all_real (false), all_cmplx (false), 79 some_str (false), all_real (false), all_cmplx (false),
78 all_mt (true), ok (false) 80 all_mt (true), class_nm (octave_base_value::static_class_name ()),
81 ok (false)
79 { init (row); } 82 { init (row); }
80 83
81 ~tm_row_const_rep (void) { } 84 ~tm_row_const_rep (void) { }
82 85
83 int count; 86 int count;
90 bool some_str; 93 bool some_str;
91 bool all_real; 94 bool all_real;
92 bool all_cmplx; 95 bool all_cmplx;
93 bool all_mt; 96 bool all_mt;
94 97
98 std::string class_nm;
99
95 bool ok; 100 bool ok;
96 101
97 bool do_init_element (tree_expression *, const octave_value&, bool&); 102 bool do_init_element (tree_expression *, const octave_value&, bool&);
98 103
99 void init (const tree_argument_list&); 104 void init (const tree_argument_list&);
161 bool some_strings_p (void) const { return rep->some_str; } 166 bool some_strings_p (void) const { return rep->some_str; }
162 bool all_real_p (void) const { return rep->all_real; } 167 bool all_real_p (void) const { return rep->all_real; }
163 bool all_complex_p (void) const { return rep->all_cmplx; } 168 bool all_complex_p (void) const { return rep->all_cmplx; }
164 bool all_empty_p (void) const { return rep->all_mt; } 169 bool all_empty_p (void) const { return rep->all_mt; }
165 170
171 std::string class_name (void) const { return rep->class_nm; }
172
166 operator bool () const { return (rep && rep->ok); } 173 operator bool () const { return (rep && rep->ok); }
167 174
168 iterator begin (void) { return rep->begin (); } 175 iterator begin (void) { return rep->begin (); }
169 const_iterator begin (void) const { return rep->begin (); } 176 const_iterator begin (void) const { return rep->begin (); }
170 177
173 180
174 private: 181 private:
175 182
176 tm_row_const_rep *rep; 183 tm_row_const_rep *rep;
177 }; 184 };
185
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 }
178 248
179 bool 249 bool
180 tm_row_const::tm_row_const_rep::do_init_element (tree_expression *elt, 250 tm_row_const::tm_row_const_rep::do_init_element (tree_expression *elt,
181 const octave_value& val, 251 const octave_value& val,
182 bool& first_elem) 252 bool& first_elem)
183 { 253 {
184 octave_idx_type this_elt_nr = val.rows (); 254 octave_idx_type this_elt_nr = val.rows ();
185 octave_idx_type this_elt_nc = val.columns (); 255 octave_idx_type this_elt_nc = val.columns ();
186 256
257 std::string this_elt_class_nm = val.class_name ();
258
187 dim_vector this_elt_dv = val.dims (); 259 dim_vector this_elt_dv = val.dims ();
188 260
189 if (! this_elt_dv.all_zero ()) 261 if (! this_elt_dv.all_zero ())
190 { 262 {
191 all_mt = false; 263 all_mt = false;
192 264
193 if (first_elem) 265 if (first_elem)
194 { 266 {
195 first_elem = false; 267 first_elem = false;
268
269 class_nm = this_elt_class_nm;
196 270
197 dv.resize (this_elt_dv.length ()); 271 dv.resize (this_elt_dv.length ());
198 for (int i = 2; i < dv.length (); i++) 272 for (int i = 2; i < dv.length (); i++)
199 dv.elem (i) = this_elt_dv.elem (i); 273 dv.elem (i) = this_elt_dv.elem (i);
200 274
202 276
203 dv.elem (1) = 0; 277 dv.elem (1) = 0;
204 } 278 }
205 else 279 else
206 { 280 {
281 class_nm = get_concat_class (class_nm, this_elt_class_nm);
282
207 int len = (this_elt_dv.length () < dv.length () 283 int len = (this_elt_dv.length () < dv.length ()
208 ? this_elt_dv.length () : dv.length ()); 284 ? this_elt_dv.length () : dv.length ());
209 285
210 if (this_elt_nr != dv (0)) 286 if (this_elt_nr != dv (0))
211 { 287 {
354 public: 430 public:
355 431
356 tm_const (const tree_matrix& tm) 432 tm_const (const tree_matrix& tm)
357 : dv (0, 0), all_str (false), all_sq_str (false), all_dq_str (false), 433 : dv (0, 0), all_str (false), all_sq_str (false), all_dq_str (false),
358 some_str (false), all_real (false), all_cmplx (false), 434 some_str (false), all_real (false), all_cmplx (false),
359 all_mt (true), ok (false) 435 all_mt (true), class_nm (octave_base_value::static_class_name ()),
360 { init (tm); } 436 ok (false)
437 { init (tm); }
361 438
362 ~tm_const (void) { } 439 ~tm_const (void) { }
363 440
364 octave_idx_type rows (void) const { return dv.elem (0); } 441 octave_idx_type rows (void) const { return dv.elem (0); }
365 octave_idx_type cols (void) const { return dv.elem (1); } 442 octave_idx_type cols (void) const { return dv.elem (1); }
372 bool some_strings_p (void) const { return some_str; } 449 bool some_strings_p (void) const { return some_str; }
373 bool all_real_p (void) const { return all_real; } 450 bool all_real_p (void) const { return all_real; }
374 bool all_complex_p (void) const { return all_cmplx; } 451 bool all_complex_p (void) const { return all_cmplx; }
375 bool all_empty_p (void) const { return all_mt; } 452 bool all_empty_p (void) const { return all_mt; }
376 453
454 std::string class_name (void) const { return class_nm; }
455
377 operator bool () const { return ok; } 456 operator bool () const { return ok; }
378 457
379 private: 458 private:
380 459
381 dim_vector dv; 460 dim_vector dv;
386 bool some_str; 465 bool some_str;
387 bool all_real; 466 bool all_real;
388 bool all_cmplx; 467 bool all_cmplx;
389 bool all_mt; 468 bool all_mt;
390 469
470 std::string class_nm;
471
391 bool ok; 472 bool ok;
392 473
393 tm_const (void); 474 tm_const (void);
394 475
395 tm_const (const tm_const&); 476 tm_const (const tm_const&);
461 tm_row_const elt = *p; 542 tm_row_const elt = *p;
462 543
463 octave_idx_type this_elt_nr = elt.rows (); 544 octave_idx_type this_elt_nr = elt.rows ();
464 octave_idx_type this_elt_nc = elt.cols (); 545 octave_idx_type this_elt_nc = elt.cols ();
465 546
547 std::string this_elt_class_nm = elt.class_name ();
548
466 dim_vector this_elt_dv = elt.dims (); 549 dim_vector this_elt_dv = elt.dims ();
467 550
468 if (!this_elt_dv.all_zero ()) 551 if (!this_elt_dv.all_zero ())
469 { 552 {
470 all_mt = false; 553 all_mt = false;
471 554
472 if (first_elem) 555 if (first_elem)
473 { 556 {
474 first_elem = false; 557 first_elem = false;
558
559 class_nm = this_elt_class_nm;
475 560
476 dv.resize (this_elt_dv.length ()); 561 dv.resize (this_elt_dv.length ());
477 for (int i = 2; i < dv.length (); i++) 562 for (int i = 2; i < dv.length (); i++)
478 dv.elem (i) = this_elt_dv.elem (i); 563 dv.elem (i) = this_elt_dv.elem (i);
479 564
481 566
482 dv.elem (1) = this_elt_nc; 567 dv.elem (1) = this_elt_nc;
483 } 568 }
484 else if (all_str) 569 else if (all_str)
485 { 570 {
571 class_nm = get_concat_class (class_nm, this_elt_class_nm);
572
486 if (this_elt_nc > cols ()) 573 if (this_elt_nc > cols ())
487 dv.elem (1) = this_elt_nc; 574 dv.elem (1) = this_elt_nc;
488 } 575 }
489 else 576 else
490 { 577 {
578 class_nm = get_concat_class (class_nm, this_elt_class_nm);
579
491 bool get_out = false; 580 bool get_out = false;
492 int len = (this_elt_dv.length () < dv.length () 581 int len = (this_elt_dv.length () < dv.length ()
493 ? this_elt_dv.length () : dv.length ()); 582 ? this_elt_dv.length () : dv.length ());
494 583
495 for (int i = 1; i < len; i++) 584 for (int i = 1; i < len; i++)
641 ra_idx(1) = 0; \ 730 ra_idx(1) = 0; \
642 } \ 731 } \
643 } \ 732 } \
644 while (0) 733 while (0)
645 734
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
646 octave_value 746 octave_value
647 tree_matrix::rvalue (void) 747 tree_matrix::rvalue (void)
648 { 748 {
649 octave_value retval; 749 octave_value retval;
650 750
669 all_complex_p = tmp.all_complex_p (); 769 all_complex_p = tmp.all_complex_p ();
670 frc_str_conv = tmp.some_strings_p (); 770 frc_str_conv = tmp.some_strings_p ();
671 771
672 // Try to speed up the common cases. 772 // Try to speed up the common cases.
673 773
674 if (all_strings_p) 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")
675 { 787 {
676 char type = all_sq_strings_p ? '\'' : '"'; 788 char type = all_sq_strings_p ? '\'' : '"';
677 789
678 maybe_warn_string_concat (all_dq_strings_p, all_sq_strings_p); 790 maybe_warn_string_concat (all_dq_strings_p, all_sq_strings_p);
679 791
681 793
682 SINGLE_TYPE_CONCAT (charNDArray, char_array_value); 794 SINGLE_TYPE_CONCAT (charNDArray, char_array_value);
683 795
684 retval = octave_value (result, true, type); 796 retval = octave_value (result, true, type);
685 } 797 }
686 else if (all_real_p) 798 else if (result_type == "logical")
687 { 799 DO_SINGLE_TYPE_CONCAT (boolNDArray, bool_array_value);
688 NDArray result (dv); 800 else if (result_type == "int8")
689 801 DO_SINGLE_TYPE_CONCAT (int8NDArray, int8_array_value);
690 SINGLE_TYPE_CONCAT (NDArray, array_value); 802 else if (result_type == "int16")
691 803 DO_SINGLE_TYPE_CONCAT (int16NDArray, int16_array_value);
692 retval = result; 804 else if (result_type == "int32")
693 } 805 DO_SINGLE_TYPE_CONCAT (int32NDArray, int32_array_value);
694 else if (all_complex_p) 806 else if (result_type == "int64")
695 { 807 DO_SINGLE_TYPE_CONCAT (int64NDArray, int64_array_value);
696 ComplexNDArray result (dv); 808 else if (result_type == "uint8")
697 809 DO_SINGLE_TYPE_CONCAT (uint8NDArray, uint8_array_value);
698 SINGLE_TYPE_CONCAT (ComplexNDArray, complex_array_value); 810 else if (result_type == "uint16")
699 811 DO_SINGLE_TYPE_CONCAT (uint16NDArray, uint16_array_value);
700 retval = result; 812 else if (result_type == "uint32")
701 } 813 DO_SINGLE_TYPE_CONCAT (uint32NDArray, uint32_array_value);
814 else if (result_type == "uint64")
815 DO_SINGLE_TYPE_CONCAT (uint64NDArray, uint64_array_value);
702 else 816 else
703 { 817 {
704 // The line below might seem crazy, since we take a copy of 818 // The line below might seem crazy, since we take a copy of
705 // the first argument, resize it to be empty and then resize 819 // the first argument, resize it to be empty and then resize
706 // it to be full. This is done since it means that there is 820 // it to be full. This is done since it means that there is