Mercurial > octave-nkf
comparison src/ov-cell.cc @ 11586:12df7854fa7c
strip trailing whitespace from source files
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:24:59 -0500 |
parents | fd0a3ac60b0e |
children | 7a5aacf65f81 |
comparison
equal
deleted
inserted
replaced
11585:1473d0cf86d2 | 11586:12df7854fa7c |
---|---|
105 return octave_value (); | 105 return octave_value (); |
106 } | 106 } |
107 | 107 |
108 template <> | 108 template <> |
109 bool | 109 bool |
110 octave_base_matrix<Cell>::fast_elem_insert (octave_idx_type n, | 110 octave_base_matrix<Cell>::fast_elem_insert (octave_idx_type n, |
111 const octave_value& x) | 111 const octave_value& x) |
112 { | 112 { |
113 const octave_cell *xrep = | 113 const octave_cell *xrep = |
114 dynamic_cast<const octave_cell *> (&x.get_rep ()); | 114 dynamic_cast<const octave_cell *> (&x.get_rep ()); |
115 | 115 |
116 bool retval = xrep && xrep->matrix.numel () == 1 && n < matrix.numel (); | 116 bool retval = xrep && xrep->matrix.numel () == 1 && n < matrix.numel (); |
117 if (retval) | 117 if (retval) |
118 matrix(n) = xrep->matrix(0); | 118 matrix(n) = xrep->matrix(0); |
311 { | 311 { |
312 tmp = octave_value::empty_conv (type.substr (1), rhs); | 312 tmp = octave_value::empty_conv (type.substr (1), rhs); |
313 tmp.make_unique (); // probably a no-op. | 313 tmp.make_unique (); // probably a no-op. |
314 } | 314 } |
315 else | 315 else |
316 // optimization: ignore the copy still stored inside our array. | 316 // optimization: ignore the copy still stored inside our array. |
317 tmp.make_unique (1); | 317 tmp.make_unique (1); |
318 | 318 |
319 if (! error_state) | 319 if (! error_state) |
320 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); | 320 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); |
321 } | 321 } |
433 } | 433 } |
434 | 434 |
435 return retval; | 435 return retval; |
436 } | 436 } |
437 | 437 |
438 bool | 438 bool |
439 octave_cell::is_cellstr (void) const | 439 octave_cell::is_cellstr (void) const |
440 { | 440 { |
441 bool retval; | 441 bool retval; |
442 if (cellstr_cache.get ()) | 442 if (cellstr_cache.get ()) |
443 retval = true; | 443 retval = true; |
450 } | 450 } |
451 | 451 |
452 return retval; | 452 return retval; |
453 } | 453 } |
454 | 454 |
455 void | 455 void |
456 octave_cell::assign (const octave_value_list& idx, const Cell& rhs) | 456 octave_cell::assign (const octave_value_list& idx, const Cell& rhs) |
457 { | 457 { |
458 clear_cellstr_cache (); | 458 clear_cellstr_cache (); |
459 octave_base_matrix<Cell>::assign (idx, rhs); | 459 octave_base_matrix<Cell>::assign (idx, rhs); |
460 } | 460 } |
461 | 461 |
462 void | 462 void |
463 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) | 463 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) |
464 { | 464 { |
465 clear_cellstr_cache (); | 465 clear_cellstr_cache (); |
466 octave_base_matrix<Cell>::assign (idx, rhs); | 466 octave_base_matrix<Cell>::assign (idx, rhs); |
467 } | 467 } |
468 | 468 |
469 | 469 |
470 void | 470 void |
471 octave_cell::delete_elements (const octave_value_list& idx) | 471 octave_cell::delete_elements (const octave_value_list& idx) |
472 { | 472 { |
473 clear_cellstr_cache (); | 473 clear_cellstr_cache (); |
474 octave_base_matrix<Cell>::delete_elements (idx); | 474 octave_base_matrix<Cell>::delete_elements (idx); |
475 } | 475 } |
499 // We already have the cache. | 499 // We already have the cache. |
500 retval = new octave_cell (tmp); | 500 retval = new octave_cell (tmp); |
501 } | 501 } |
502 else | 502 else |
503 error ("sort: only cell arrays of character strings may be sorted"); | 503 error ("sort: only cell arrays of character strings may be sorted"); |
504 | 504 |
505 return retval; | 505 return retval; |
506 } | 506 } |
507 | 507 |
508 octave_value | 508 octave_value |
509 octave_cell::sort (Array<octave_idx_type> &sidx, octave_idx_type dim, | 509 octave_cell::sort (Array<octave_idx_type> &sidx, octave_idx_type dim, |
520 // We already have the cache. | 520 // We already have the cache. |
521 retval = new octave_cell (tmp); | 521 retval = new octave_cell (tmp); |
522 } | 522 } |
523 else | 523 else |
524 error ("sort: only cell arrays of character strings may be sorted"); | 524 error ("sort: only cell arrays of character strings may be sorted"); |
525 | 525 |
526 return retval; | 526 return retval; |
527 } | 527 } |
528 | 528 |
529 sortmode | 529 sortmode |
530 octave_cell::is_sorted (sortmode mode) const | 530 octave_cell::is_sorted (sortmode mode) const |
531 { | 531 { |
532 sortmode retval = UNSORTED; | 532 sortmode retval = UNSORTED; |
533 | 533 |
534 if (is_cellstr ()) | 534 if (is_cellstr ()) |
537 | 537 |
538 retval = tmp.is_sorted (mode); | 538 retval = tmp.is_sorted (mode); |
539 } | 539 } |
540 else | 540 else |
541 error ("issorted: not a cell array of strings"); | 541 error ("issorted: not a cell array of strings"); |
542 | 542 |
543 return retval; | 543 return retval; |
544 } | 544 } |
545 | 545 |
546 | 546 |
547 Array<octave_idx_type> | 547 Array<octave_idx_type> |
555 | 555 |
556 retval = tmp.sort_rows_idx (mode); | 556 retval = tmp.sort_rows_idx (mode); |
557 } | 557 } |
558 else | 558 else |
559 error ("sortrows: only cell arrays of character strings may be sorted"); | 559 error ("sortrows: only cell arrays of character strings may be sorted"); |
560 | 560 |
561 return retval; | 561 return retval; |
562 } | 562 } |
563 | 563 |
564 sortmode | 564 sortmode |
565 octave_cell::is_sorted_rows (sortmode mode) const | 565 octave_cell::is_sorted_rows (sortmode mode) const |
566 { | 566 { |
567 sortmode retval = UNSORTED; | 567 sortmode retval = UNSORTED; |
568 | 568 |
569 if (is_cellstr ()) | 569 if (is_cellstr ()) |
572 | 572 |
573 retval = tmp.is_sorted_rows (mode); | 573 retval = tmp.is_sorted_rows (mode); |
574 } | 574 } |
575 else | 575 else |
576 error ("issorted: not a cell array of strings"); | 576 error ("issorted: not a cell array of strings"); |
577 | 577 |
578 return retval; | 578 return retval; |
579 } | 579 } |
580 | 580 |
581 bool | 581 bool |
582 octave_cell::is_true (void) const | 582 octave_cell::is_true (void) const |
744 } | 744 } |
745 } | 745 } |
746 | 746 |
747 #define CELL_ELT_TAG "<cell-element>" | 747 #define CELL_ELT_TAG "<cell-element>" |
748 | 748 |
749 bool | 749 bool |
750 octave_cell::save_ascii (std::ostream& os) | 750 octave_cell::save_ascii (std::ostream& os) |
751 { | 751 { |
752 dim_vector d = dims (); | 752 dim_vector d = dims (); |
753 if (d.length () > 2) | 753 if (d.length () > 2) |
754 { | 754 { |
755 os << "# ndims: " << d.length () << "\n"; | 755 os << "# ndims: " << d.length () << "\n"; |
756 | 756 |
757 for (int i = 0; i < d.length (); i++) | 757 for (int i = 0; i < d.length (); i++) |
758 os << " " << d (i); | 758 os << " " << d (i); |
759 os << "\n"; | 759 os << "\n"; |
760 | 760 |
761 Cell tmp = cell_value (); | 761 Cell tmp = cell_value (); |
762 | 762 |
763 for (octave_idx_type i = 0; i < d.numel (); i++) | 763 for (octave_idx_type i = 0; i < d.numel (); i++) |
764 { | 764 { |
765 octave_value o_val = tmp.elem (i); | 765 octave_value o_val = tmp.elem (i); |
766 | 766 |
767 // Recurse to print sub-value. | 767 // Recurse to print sub-value. |
768 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); | 768 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
769 | 769 |
770 if (! b) | 770 if (! b) |
771 return os; | 771 return os; |
772 } | 772 } |
773 } | 773 } |
774 else | 774 else |
775 { | 775 { |
776 // Keep this case, rather than use generic code above for backward | 776 // Keep this case, rather than use generic code above for backward |
777 // compatiability. Makes load_ascii much more complex!! | 777 // compatiability. Makes load_ascii much more complex!! |
778 os << "# rows: " << rows () << "\n" | 778 os << "# rows: " << rows () << "\n" |
779 << "# columns: " << columns () << "\n"; | 779 << "# columns: " << columns () << "\n"; |
780 | 780 |
781 Cell tmp = cell_value (); | 781 Cell tmp = cell_value (); |
782 | 782 |
783 for (octave_idx_type j = 0; j < tmp.cols (); j++) | 783 for (octave_idx_type j = 0; j < tmp.cols (); j++) |
784 { | 784 { |
785 for (octave_idx_type i = 0; i < tmp.rows (); i++) | 785 for (octave_idx_type i = 0; i < tmp.rows (); i++) |
786 { | 786 { |
787 octave_value o_val = tmp.elem (i, j); | 787 octave_value o_val = tmp.elem (i, j); |
788 | 788 |
789 // Recurse to print sub-value. | 789 // Recurse to print sub-value. |
790 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); | 790 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
791 | 791 |
792 if (! b) | 792 if (! b) |
793 return os; | 793 return os; |
794 } | 794 } |
795 | 795 |
796 os << "\n"; | 796 os << "\n"; |
797 } | 797 } |
798 } | 798 } |
799 | 799 |
800 return true; | 800 return true; |
801 } | 801 } |
802 | 802 |
803 bool | 803 bool |
804 octave_cell::load_ascii (std::istream& is) | 804 octave_cell::load_ascii (std::istream& is) |
805 { | 805 { |
806 bool success = true; | 806 bool success = true; |
807 | 807 |
808 clear_cellstr_cache (); | 808 clear_cellstr_cache (); |
835 { | 835 { |
836 octave_value t2; | 836 octave_value t2; |
837 bool dummy; | 837 bool dummy; |
838 | 838 |
839 // recurse to read cell elements | 839 // recurse to read cell elements |
840 std::string nm = read_ascii_data (is, std::string (), | 840 std::string nm = read_ascii_data (is, std::string (), |
841 dummy, t2, i); | 841 dummy, t2, i); |
842 | 842 |
843 if (nm == CELL_ELT_TAG) | 843 if (nm == CELL_ELT_TAG) |
844 { | 844 { |
845 if (is) | 845 if (is) |
900 success = false; | 900 success = false; |
901 goto cell_read_error; | 901 goto cell_read_error; |
902 } | 902 } |
903 } | 903 } |
904 } | 904 } |
905 | 905 |
906 cell_read_error: | 906 cell_read_error: |
907 | 907 |
908 if (is) | 908 if (is) |
909 matrix = tmp; | 909 matrix = tmp; |
910 else | 910 else |
934 } | 934 } |
935 | 935 |
936 return success; | 936 return success; |
937 } | 937 } |
938 | 938 |
939 bool | 939 bool |
940 octave_cell::save_binary (std::ostream& os, bool& save_as_floats) | 940 octave_cell::save_binary (std::ostream& os, bool& save_as_floats) |
941 { | 941 { |
942 dim_vector d = dims (); | 942 dim_vector d = dims (); |
943 if (d.length () < 1) | 943 if (d.length () < 1) |
944 return false; | 944 return false; |
949 for (int i = 0; i < d.length (); i++) | 949 for (int i = 0; i < d.length (); i++) |
950 { | 950 { |
951 di = d(i); | 951 di = d(i); |
952 os.write (reinterpret_cast<char *> (&di), 4); | 952 os.write (reinterpret_cast<char *> (&di), 4); |
953 } | 953 } |
954 | 954 |
955 Cell tmp = cell_value (); | 955 Cell tmp = cell_value (); |
956 | 956 |
957 for (octave_idx_type i = 0; i < d.numel (); i++) | 957 for (octave_idx_type i = 0; i < d.numel (); i++) |
958 { | 958 { |
959 octave_value o_val = tmp.elem (i); | 959 octave_value o_val = tmp.elem (i); |
960 | 960 |
961 // Recurse to print sub-value. | 961 // Recurse to print sub-value. |
962 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, | 962 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, |
963 save_as_floats); | 963 save_as_floats); |
964 | 964 |
965 if (! b) | 965 if (! b) |
966 return false; | 966 return false; |
967 } | 967 } |
968 | 968 |
969 return true; | 969 return true; |
970 } | 970 } |
971 | 971 |
972 bool | 972 bool |
973 octave_cell::load_binary (std::istream& is, bool swap, | 973 octave_cell::load_binary (std::istream& is, bool swap, |
974 oct_mach_info::float_format fmt) | 974 oct_mach_info::float_format fmt) |
975 { | 975 { |
976 clear_cellstr_cache (); | 976 clear_cellstr_cache (); |
977 | 977 |
995 return false; | 995 return false; |
996 if (swap) | 996 if (swap) |
997 swap_bytes<4> (&di); | 997 swap_bytes<4> (&di); |
998 dv(i) = di; | 998 dv(i) = di; |
999 } | 999 } |
1000 | 1000 |
1001 // Convert an array with a single dimension to be a row vector. | 1001 // Convert an array with a single dimension to be a row vector. |
1002 // Octave should never write files like this, other software | 1002 // Octave should never write files like this, other software |
1003 // might. | 1003 // might. |
1004 | 1004 |
1005 if (mdims == 1) | 1005 if (mdims == 1) |
1018 octave_value t2; | 1018 octave_value t2; |
1019 bool dummy; | 1019 bool dummy; |
1020 std::string doc; | 1020 std::string doc; |
1021 | 1021 |
1022 // recurse to read cell elements | 1022 // recurse to read cell elements |
1023 std::string nm = read_binary_data (is, swap, fmt, std::string (), | 1023 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
1024 dummy, t2, doc); | 1024 dummy, t2, doc); |
1025 | 1025 |
1026 if (nm == CELL_ELT_TAG) | 1026 if (nm == CELL_ELT_TAG) |
1027 { | 1027 { |
1028 if (is) | 1028 if (is) |
1062 dim_vector dv = dims (); | 1062 dim_vector dv = dims (); |
1063 int empty = save_hdf5_empty (loc_id, name, dv); | 1063 int empty = save_hdf5_empty (loc_id, name, dv); |
1064 if (empty) | 1064 if (empty) |
1065 return (empty > 0); | 1065 return (empty > 0); |
1066 | 1066 |
1067 hsize_t rank = dv.length (); | 1067 hsize_t rank = dv.length (); |
1068 hid_t space_hid = -1, data_hid = -1, size_hid = -1; | 1068 hid_t space_hid = -1, data_hid = -1, size_hid = -1; |
1069 | 1069 |
1070 #if HAVE_HDF5_18 | 1070 #if HAVE_HDF5_18 |
1071 data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); | 1071 data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
1072 #else | 1072 #else |
1074 #endif | 1074 #endif |
1075 | 1075 |
1076 if (data_hid < 0) | 1076 if (data_hid < 0) |
1077 return false; | 1077 return false; |
1078 | 1078 |
1079 // Have to save cell array shape, since can't have a | 1079 // Have to save cell array shape, since can't have a |
1080 // dataset of groups.... | 1080 // dataset of groups.... |
1081 | 1081 |
1082 space_hid = H5Screate_simple (1, &rank, 0); | 1082 space_hid = H5Screate_simple (1, &rank, 0); |
1083 | 1083 |
1084 if (space_hid < 0) | 1084 if (space_hid < 0) |
1085 { | 1085 { |
1086 H5Gclose (data_hid); | 1086 H5Gclose (data_hid); |
1087 return false; | 1087 return false; |
1088 } | 1088 } |
1089 | 1089 |
1092 // Octave uses column-major, while HDF5 uses row-major ordering | 1092 // Octave uses column-major, while HDF5 uses row-major ordering |
1093 for (hsize_t i = 0; i < rank; i++) | 1093 for (hsize_t i = 0; i < rank; i++) |
1094 hdims[i] = dv(rank-i-1); | 1094 hdims[i] = dv(rank-i-1); |
1095 | 1095 |
1096 #if HAVE_HDF5_18 | 1096 #if HAVE_HDF5_18 |
1097 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, | 1097 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, |
1098 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); | 1098 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
1099 #else | 1099 #else |
1100 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, | 1100 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, |
1101 H5P_DEFAULT); | 1101 H5P_DEFAULT); |
1102 #endif | 1102 #endif |
1103 if (size_hid < 0) | 1103 if (size_hid < 0) |
1104 { | 1104 { |
1105 H5Sclose (space_hid); | 1105 H5Sclose (space_hid); |
1106 H5Gclose (data_hid); | 1106 H5Gclose (data_hid); |
1107 return false; | 1107 return false; |
1108 } | 1108 } |
1173 #else | 1173 #else |
1174 hid_t data_hid = H5Dopen (group_id, "dims"); | 1174 hid_t data_hid = H5Dopen (group_id, "dims"); |
1175 #endif | 1175 #endif |
1176 hid_t space_hid = H5Dget_space (data_hid); | 1176 hid_t space_hid = H5Dget_space (data_hid); |
1177 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); | 1177 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
1178 if (rank != 1) | 1178 if (rank != 1) |
1179 { | 1179 { |
1180 H5Dclose (data_hid); | 1180 H5Dclose (data_hid); |
1181 H5Gclose (group_id); | 1181 H5Gclose (group_id); |
1182 return false; | 1182 return false; |
1183 } | 1183 } |
1190 // Octave uses column-major, while HDF5 uses row-major ordering. | 1190 // Octave uses column-major, while HDF5 uses row-major ordering. |
1191 | 1191 |
1192 dv.resize (hdims[0]); | 1192 dv.resize (hdims[0]); |
1193 | 1193 |
1194 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); | 1194 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); |
1195 | 1195 |
1196 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, | 1196 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
1197 H5P_DEFAULT, tmp) < 0) | 1197 H5P_DEFAULT, tmp) < 0) |
1198 { | 1198 { |
1199 H5Dclose (data_hid); | 1199 H5Dclose (data_hid); |
1200 H5Gclose (group_id); | 1200 H5Gclose (group_id); |
1201 return false; | 1201 return false; |
1215 | 1215 |
1216 int current_item = 0; | 1216 int current_item = 0; |
1217 | 1217 |
1218 hsize_t num_obj = 0; | 1218 hsize_t num_obj = 0; |
1219 #if HAVE_HDF5_18 | 1219 #if HAVE_HDF5_18 |
1220 group_id = H5Gopen (loc_id, name, H5P_DEFAULT); | 1220 group_id = H5Gopen (loc_id, name, H5P_DEFAULT); |
1221 #else | 1221 #else |
1222 group_id = H5Gopen (loc_id, name); | 1222 group_id = H5Gopen (loc_id, name); |
1223 #endif | 1223 #endif |
1224 H5Gget_num_objs (group_id, &num_obj); | 1224 H5Gget_num_objs (group_id, &num_obj); |
1225 H5Gclose (group_id); | 1225 H5Gclose (group_id); |
1226 | 1226 |
1227 for (octave_idx_type i = 0; i < dv.numel (); i++) | 1227 for (octave_idx_type i = 0; i < dv.numel (); i++) |
1230 if (current_item >= static_cast<int> (num_obj)) | 1230 if (current_item >= static_cast<int> (num_obj)) |
1231 retval2 = -1; | 1231 retval2 = -1; |
1232 else | 1232 else |
1233 retval2 = H5Giterate (loc_id, name, ¤t_item, | 1233 retval2 = H5Giterate (loc_id, name, ¤t_item, |
1234 hdf5_read_next_data, &dsub); | 1234 hdf5_read_next_data, &dsub); |
1235 | 1235 |
1236 if (retval2 <= 0) | 1236 if (retval2 <= 0) |
1237 break; | 1237 break; |
1238 | 1238 |
1239 octave_value ov = dsub.tc; | 1239 octave_value ov = dsub.tc; |
1240 m.elem (i) = ov; | 1240 m.elem (i) = ov; |
1244 if (retval2 >= 0) | 1244 if (retval2 >= 0) |
1245 { | 1245 { |
1246 matrix = m; | 1246 matrix = m; |
1247 retval = true; | 1247 retval = true; |
1248 } | 1248 } |
1249 | 1249 |
1250 return retval; | 1250 return retval; |
1251 } | 1251 } |
1252 | 1252 |
1253 #endif | 1253 #endif |
1254 | 1254 |
1518 FORWARD_MAPPER (xisupper); | 1518 FORWARD_MAPPER (xisupper); |
1519 FORWARD_MAPPER (xisxdigit); | 1519 FORWARD_MAPPER (xisxdigit); |
1520 FORWARD_MAPPER (xtoascii); | 1520 FORWARD_MAPPER (xtoascii); |
1521 FORWARD_MAPPER (xtolower); | 1521 FORWARD_MAPPER (xtolower); |
1522 FORWARD_MAPPER (xtoupper); | 1522 FORWARD_MAPPER (xtoupper); |
1523 | 1523 |
1524 default: | 1524 default: |
1525 return octave_base_value::map (umap); | 1525 return octave_base_value::map (umap); |
1526 } | 1526 } |
1527 } | 1527 } |