comparison src/pt-mat.cc @ 8147:9a5ef4f632a3

Add class dispatch for [] operator to vertcat/horzcat methods
author David Bateman <dbateman@free.fr>
date Thu, 25 Sep 2008 13:03:17 -0400
parents 8655dc0906e6
children 73c4516fae10
comparison
equal deleted inserted replaced
8146:a9ec011ead94 8147:9a5ef4f632a3
65 65
66 tm_row_const_rep (void) 66 tm_row_const_rep (void)
67 : count (1), dv (0, 0), all_str (false), 67 : count (1), dv (0, 0), all_str (false),
68 all_sq_str (false), all_dq_str (false), 68 all_sq_str (false), all_dq_str (false),
69 some_str (false), all_real (false), all_cmplx (false), 69 some_str (false), all_real (false), all_cmplx (false),
70 all_mt (true), any_sparse (false), 70 all_mt (true), any_sparse (false), any_class (false),
71 class_nm (octave_base_value::static_class_name ()), ok (false) 71 class_nm (octave_base_value::static_class_name ()), ok (false)
72 { } 72 { }
73 73
74 tm_row_const_rep (const tree_argument_list& row) 74 tm_row_const_rep (const tree_argument_list& row)
75 : count (1), dv (0, 0), all_str (false), all_sq_str (false), 75 : count (1), dv (0, 0), all_str (false), all_sq_str (false),
76 some_str (false), all_real (false), all_cmplx (false), 76 some_str (false), all_real (false), all_cmplx (false),
77 all_mt (true), any_sparse (false), 77 all_mt (true), any_sparse (false), any_class (false),
78 class_nm (octave_base_value::static_class_name ()), ok (false) 78 class_nm (octave_base_value::static_class_name ()), ok (false)
79 { init (row); } 79 { init (row); }
80 80
81 ~tm_row_const_rep (void) { } 81 ~tm_row_const_rep (void) { }
82 82
90 bool some_str; 90 bool some_str;
91 bool all_real; 91 bool all_real;
92 bool all_cmplx; 92 bool all_cmplx;
93 bool all_mt; 93 bool all_mt;
94 bool any_sparse; 94 bool any_sparse;
95 bool any_class;
95 96
96 std::string class_nm; 97 std::string class_nm;
97 98
98 bool ok; 99 bool ok;
99 100
155 156
156 octave_idx_type rows (void) { return rep->dv(0); } 157 octave_idx_type rows (void) { return rep->dv(0); }
157 octave_idx_type cols (void) { return rep->dv(1); } 158 octave_idx_type cols (void) { return rep->dv(1); }
158 159
159 bool empty (void) const { return rep->empty (); } 160 bool empty (void) const { return rep->empty (); }
161
162 size_t length (void) const { return rep->length (); }
160 163
161 dim_vector dims (void) { return rep->dv; } 164 dim_vector dims (void) { return rep->dv; }
162 165
163 bool all_strings_p (void) const { return rep->all_str; } 166 bool all_strings_p (void) const { return rep->all_str; }
164 bool all_sq_strings_p (void) const { return rep->all_sq_str; } 167 bool all_sq_strings_p (void) const { return rep->all_sq_str; }
166 bool some_strings_p (void) const { return rep->some_str; } 169 bool some_strings_p (void) const { return rep->some_str; }
167 bool all_real_p (void) const { return rep->all_real; } 170 bool all_real_p (void) const { return rep->all_real; }
168 bool all_complex_p (void) const { return rep->all_cmplx; } 171 bool all_complex_p (void) const { return rep->all_cmplx; }
169 bool all_empty_p (void) const { return rep->all_mt; } 172 bool all_empty_p (void) const { return rep->all_mt; }
170 bool any_sparse_p (void) const { return rep->any_sparse; } 173 bool any_sparse_p (void) const { return rep->any_sparse; }
174 bool any_class_p (void) const { return rep->any_class; }
171 175
172 std::string class_name (void) const { return rep->class_nm; } 176 std::string class_name (void) const { return rep->class_nm; }
173 177
174 operator bool () const { return (rep && rep->ok); } 178 operator bool () const { return (rep && rep->ok); }
175 179
343 all_cmplx = false; 347 all_cmplx = false;
344 348
345 if (!any_sparse && val.is_sparse_type ()) 349 if (!any_sparse && val.is_sparse_type ())
346 any_sparse = true; 350 any_sparse = true;
347 351
352 if (!any_class && val.is_object ())
353 any_class = true;
354
348 return true; 355 return true;
349 } 356 }
350 357
351 void 358 void
352 tm_row_const::tm_row_const_rep::init (const tree_argument_list& row) 359 tm_row_const::tm_row_const_rep::init (const tree_argument_list& row)
355 all_sq_str = true; 362 all_sq_str = true;
356 all_dq_str = true; 363 all_dq_str = true;
357 all_real = true; 364 all_real = true;
358 all_cmplx = true; 365 all_cmplx = true;
359 any_sparse = false; 366 any_sparse = false;
367 any_class = false;
360 368
361 bool first_elem = true; 369 bool first_elem = true;
362 370
363 for (tree_argument_list::const_iterator p = row.begin (); 371 for (tree_argument_list::const_iterator p = row.begin ();
364 p != row.end (); 372 p != row.end ();
436 public: 444 public:
437 445
438 tm_const (const tree_matrix& tm) 446 tm_const (const tree_matrix& tm)
439 : dv (0, 0), all_str (false), all_sq_str (false), all_dq_str (false), 447 : dv (0, 0), all_str (false), all_sq_str (false), all_dq_str (false),
440 some_str (false), all_real (false), all_cmplx (false), 448 some_str (false), all_real (false), all_cmplx (false),
441 all_mt (true), any_sparse (false), 449 all_mt (true), any_sparse (false), any_class (false),
442 class_nm (octave_base_value::static_class_name ()), ok (false) 450 class_nm (octave_base_value::static_class_name ()), ok (false)
443 { init (tm); } 451 { init (tm); }
444 452
445 ~tm_const (void) { } 453 ~tm_const (void) { }
446 454
455 bool some_strings_p (void) const { return some_str; } 463 bool some_strings_p (void) const { return some_str; }
456 bool all_real_p (void) const { return all_real; } 464 bool all_real_p (void) const { return all_real; }
457 bool all_complex_p (void) const { return all_cmplx; } 465 bool all_complex_p (void) const { return all_cmplx; }
458 bool all_empty_p (void) const { return all_mt; } 466 bool all_empty_p (void) const { return all_mt; }
459 bool any_sparse_p (void) const { return any_sparse; } 467 bool any_sparse_p (void) const { return any_sparse; }
468 bool any_class_p (void) const { return any_class; }
460 469
461 std::string class_name (void) const { return class_nm; } 470 std::string class_name (void) const { return class_nm; }
462 471
463 operator bool () const { return ok; } 472 operator bool () const { return ok; }
464 473
472 bool some_str; 481 bool some_str;
473 bool all_real; 482 bool all_real;
474 bool all_cmplx; 483 bool all_cmplx;
475 bool all_mt; 484 bool all_mt;
476 bool any_sparse; 485 bool any_sparse;
486 bool any_class;
477 487
478 std::string class_nm; 488 std::string class_nm;
479 489
480 bool ok; 490 bool ok;
481 491
495 all_sq_str = true; 505 all_sq_str = true;
496 all_dq_str = true; 506 all_dq_str = true;
497 all_real = true; 507 all_real = true;
498 all_cmplx = true; 508 all_cmplx = true;
499 any_sparse = false; 509 any_sparse = false;
510 any_class = false;
500 511
501 bool first_elem = true; 512 bool first_elem = true;
502 513
503 // Just eval and figure out if what we have is complex or all 514 // Just eval and figure out if what we have is complex or all
504 // strings. We can't check columns until we know that this is a 515 // strings. We can't check columns until we know that this is a
536 if (all_mt && ! tmp.all_empty_p ()) 547 if (all_mt && ! tmp.all_empty_p ())
537 all_mt = false; 548 all_mt = false;
538 549
539 if (!any_sparse && tmp.any_sparse_p ()) 550 if (!any_sparse && tmp.any_sparse_p ())
540 any_sparse = true; 551 any_sparse = true;
552
553 if (!any_class && tmp.any_class_p ())
554 any_class = true;
541 555
542 append (tmp); 556 append (tmp);
543 } 557 }
544 else 558 else
545 break; 559 break;
767 bool all_dq_strings_p = false; 781 bool all_dq_strings_p = false;
768 bool all_empty_p = false; 782 bool all_empty_p = false;
769 bool all_real_p = false; 783 bool all_real_p = false;
770 bool all_complex_p = false; 784 bool all_complex_p = false;
771 bool any_sparse_p = false; 785 bool any_sparse_p = false;
786 bool any_class_p = false;
772 bool frc_str_conv = false; 787 bool frc_str_conv = false;
773 788
774 tm_const tmp (*this); 789 tm_const tmp (*this);
775 790
776 if (tmp && ! tmp.empty ()) 791 if (tmp && ! tmp.empty ())
781 all_dq_strings_p = tmp.all_dq_strings_p (); 796 all_dq_strings_p = tmp.all_dq_strings_p ();
782 all_empty_p = tmp.all_empty_p (); 797 all_empty_p = tmp.all_empty_p ();
783 all_real_p = tmp.all_real_p (); 798 all_real_p = tmp.all_real_p ();
784 all_complex_p = tmp.all_complex_p (); 799 all_complex_p = tmp.all_complex_p ();
785 any_sparse_p = tmp.any_sparse_p (); 800 any_sparse_p = tmp.any_sparse_p ();
801 any_class_p = tmp.any_class_p ();
786 frc_str_conv = tmp.some_strings_p (); 802 frc_str_conv = tmp.some_strings_p ();
787 803
788 // Try to speed up the common cases. 804 // Try to speed up the common cases.
789 805
790 std::string result_type = tmp.class_name (); 806 std::string result_type = tmp.class_name ();
791 807
792 if (result_type == "double") 808 if (any_class_p)
809 {
810 octave_value_list tmp3 (tmp.length (), octave_value ());
811
812 int j = 0;
813 for (tm_const::iterator p = tmp.begin (); p != tmp.end (); p++)
814 {
815 OCTAVE_QUIT;
816
817 tm_row_const row = *p;
818
819 if (row.length () == 1)
820 tmp3 (j++) = *(row.begin ());
821 else
822 {
823 octave_value_list tmp1 (row.length (), octave_value ());
824
825 int i = 0;
826 for (tm_row_const::iterator q = row.begin ();
827 q != row.end (); q++)
828 tmp1 (i++) = *q;
829
830 octave_value_list tmp2;
831 octave_value fcn =
832 symbol_table::find_function ("horzcat", tmp1);
833
834 if (fcn.is_defined ())
835 {
836 tmp2 = fcn.do_multi_index_op (1, tmp1);
837
838 if (error_state)
839 goto done;
840
841 tmp3 (j++) = tmp2 (0);
842 }
843 else
844 {
845 ::error ("cat not find overloaded horzcat function");
846 goto done;
847 }
848 }
849 }
850
851 if (tmp.length () == 1)
852 retval = tmp3 (0);
853 else
854 {
855 octave_value_list tmp2;
856 octave_value fcn = symbol_table::find_function ("vertcat", tmp3);
857
858 if (fcn.is_defined ())
859 {
860 tmp2 = fcn.do_multi_index_op (1, tmp3);
861
862 if (! error_state)
863 retval = tmp2 (0);
864 }
865 else
866 ::error ("cat not find overloaded vertcat function");
867 }
868 }
869 else if (result_type == "double")
793 { 870 {
794 if (any_sparse_p) 871 if (any_sparse_p)
795 { 872 {
796 if (all_real_p) 873 if (all_real_p)
797 DO_SINGLE_TYPE_CONCAT (SparseMatrix, sparse_matrix_value); 874 DO_SINGLE_TYPE_CONCAT (SparseMatrix, sparse_matrix_value);