comparison libinterp/octave-value/ov-cell.cc @ 20979:0963ed389012

maint: invert if/else/error instances. * ov-bool-mat.cc, ov-cell.cc, ov-class.cc, ov-classdef.cc, ov-cx-mat.cc, ov-flt-cx-mat.cc, ov-flt-re-mat.cc, ov-perm.cc, ov-re-mat.cc, ov-str-mat.cc, ov-struct.cc, ov-usr-fcn.cc: Invert if/else/error instances.
author John W. Eaton <jwe@octave.org>
date Thu, 24 Dec 2015 12:50:28 -0500
parents 3aa293be0e8d
children fc9cca99b2de
comparison
equal deleted inserted replaced
20978:a5b500efca9f 20979:0963ed389012
308 } 308 }
309 break; 309 break;
310 310
311 case '.': 311 case '.':
312 { 312 {
313 if (is_empty ()) 313 if (! is_empty ())
314 {
315 // Do nothing; the next branch will handle it.
316 }
317 else
318 { 314 {
319 std::string nm = type_name (); 315 std::string nm = type_name ();
320 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); 316 error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
321 } 317 }
318
319 // Do nothing; the next branch will handle it.
322 } 320 }
323 break; 321 break;
324 322
325 default: 323 default:
326 panic_impossible (); 324 panic_impossible ();
378 } 376 }
379 break; 377 break;
380 378
381 case '.': 379 case '.':
382 { 380 {
383 if (is_empty ()) 381 if (! is_empty ())
384 {
385 // Allow conversion of empty cell array to some other
386 // type in cases like
387 //
388 // x = {}; x.f = rhs
389
390 octave_value tmp = octave_value::empty_conv (type, rhs);
391
392 return tmp.subsasgn (type, idx, rhs);
393 }
394 else
395 { 382 {
396 std::string nm = type_name (); 383 std::string nm = type_name ();
397 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); 384 error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
398 } 385 }
386
387 // Allow conversion of empty cell array to some other
388 // type in cases like
389 //
390 // x = {}; x.f = rhs
391
392 octave_value tmp = octave_value::empty_conv (type, rhs);
393
394 return tmp.subsasgn (type, idx, rhs);
399 } 395 }
400 break; 396 break;
401 397
402 default: 398 default:
403 panic_impossible (); 399 panic_impossible ();
779 775
780 if (kw == "ndims") 776 if (kw == "ndims")
781 { 777 {
782 int mdims = static_cast<int> (val); 778 int mdims = static_cast<int> (val);
783 779
784 if (mdims >= 0) 780 if (mdims < 0)
781 error ("load: failed to extract number of rows and columns");
782
783 dim_vector dv;
784 dv.resize (mdims);
785
786 for (int i = 0; i < mdims; i++)
787 is >> dv(i);
788
789 Cell tmp(dv);
790
791 for (octave_idx_type i = 0; i < dv.numel (); i++)
785 { 792 {
786 dim_vector dv; 793 octave_value t2;
787 dv.resize (mdims); 794 bool dummy;
788 795
789 for (int i = 0; i < mdims; i++) 796 // recurse to read cell elements
790 is >> dv(i); 797 std::string nm = read_text_data (is, std::string (),
791 798 dummy, t2, i);
792 Cell tmp(dv); 799
793 800 if (nm != CELL_ELT_TAG)
794 for (octave_idx_type i = 0; i < dv.numel (); i++) 801 error ("load: cell array element had unexpected name");
795 {
796 octave_value t2;
797 bool dummy;
798
799 // recurse to read cell elements
800 std::string nm = read_text_data (is, std::string (),
801 dummy, t2, i);
802
803 if (nm == CELL_ELT_TAG)
804 {
805 if (is)
806 tmp.elem (i) = t2;
807 }
808 else
809 error ("load: cell array element had unexpected name");
810 }
811 802
812 if (is) 803 if (is)
813 matrix = tmp; 804 tmp.elem (i) = t2;
814 else
815 error ("load: failed to load matrix constant");
816 } 805 }
817 else 806
818 error ("load: failed to extract number of rows and columns"); 807 if (! is)
808 error ("load: failed to load matrix constant");
809
810 matrix = tmp;
819 } 811 }
820 else if (kw == "rows") 812 else if (kw == "rows")
821 { 813 {
822 octave_idx_type nr = val; 814 octave_idx_type nr = val;
823 octave_idx_type nc = 0; 815 octave_idx_type nc = 0;
824 816
825 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) 817 if (nr < 0 || ! extract_keyword (is, "columns", nc) || nc < 0)
818 error ("load: failed to extract number of rows and columns for cell array");
819
820 if (nr > 0 && nc > 0)
826 { 821 {
827 if (nr > 0 && nc > 0) 822 Cell tmp (nr, nc);
823
824 for (octave_idx_type j = 0; j < nc; j++)
828 { 825 {
829 Cell tmp (nr, nc); 826 for (octave_idx_type i = 0; i < nr; i++)
830
831 for (octave_idx_type j = 0; j < nc; j++)
832 { 827 {
833 for (octave_idx_type i = 0; i < nr; i++) 828 octave_value t2;
834 { 829 bool dummy;
835 octave_value t2; 830
836 bool dummy; 831 // recurse to read cell elements
837 832 std::string nm = read_text_data (is, std::string (),
838 // recurse to read cell elements 833 dummy, t2, i);
839 std::string nm = read_text_data (is, std::string (), 834
840 dummy, t2, i); 835 if (nm != CELL_ELT_TAG)
841 836 error ("load: cell array element had unexpected name");
842 if (nm == CELL_ELT_TAG) 837
843 { 838 if (is)
844 if (is) 839 tmp.elem (i, j) = t2;
845 tmp.elem (i, j) = t2;
846 }
847 else
848 error ("load: cell array element had unexpected name");
849 }
850 } 840 }
851
852 if (is)
853 matrix = tmp;
854 else
855 error ("load: failed to load cell element");
856 } 841 }
857 else if (nr == 0 || nc == 0) 842
858 matrix = Cell (nr, nc); 843 if (! is)
859 else 844 error ("load: failed to load cell element");
860 panic_impossible (); 845
846 matrix = tmp;
861 } 847 }
848 else if (nr == 0 || nc == 0)
849 matrix = Cell (nr, nc);
862 else 850 else
863 error ("load: failed to extract number of rows and columns for cell array"); 851 panic_impossible ();
864 } 852 }
865 else 853 else
866 panic_impossible (); 854 panic_impossible ();
867 855
868 return true; 856 return true;
952 940
953 // recurse to read cell elements 941 // recurse to read cell elements
954 std::string nm = read_binary_data (is, swap, fmt, std::string (), 942 std::string nm = read_binary_data (is, swap, fmt, std::string (),
955 dummy, t2, doc); 943 dummy, t2, doc);
956 944
957 if (nm == CELL_ELT_TAG) 945 if (nm != CELL_ELT_TAG)
958 {
959 if (is)
960 tmp.elem (i) = t2;
961 }
962 else
963 error ("load: cell array element had unexpected name"); 946 error ("load: cell array element had unexpected name");
964 } 947
965 948 if (is)
966 if (is) 949 tmp.elem (i) = t2;
967 matrix = tmp; 950 }
968 else 951
952 if (! is)
969 error ("load: failed to load matrix constant"); 953 error ("load: failed to load matrix constant");
954
955 matrix = tmp;
970 956
971 return true; 957 return true;
972 } 958 }
973 959
974 void * 960 void *