comparison src/ov-cell.cc @ 11770:1a7ef7a48be1 release-3-0-x

struct2cell: handle structure arrays properly
author John W. Eaton <jwe@octave.org>
date Tue, 06 May 2008 03:23:12 -0400
parents c8d362c69013
children 65e41465c46b
comparison
equal deleted inserted replaced
11769:20aba78035b3 11770:1a7ef7a48be1
1112 { 1112 {
1113 dim_vector m_dv = m.dims (); 1113 dim_vector m_dv = m.dims ();
1114 1114
1115 string_vector keys = m.keys (); 1115 string_vector keys = m.keys ();
1116 1116
1117 octave_idx_type fields_numel = keys.length (); 1117 octave_idx_type num_fields = keys.length ();
1118 1118
1119 // The resulting dim_vector should have dimensions: 1119 // The resulting dim_vector should have dimensions:
1120 // [numel(fields) size(struct)] 1120 // [numel(fields) size(struct)]
1121 1121
1122 dim_vector result_dv; 1122 dim_vector result_dv;
1123 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields. 1123 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields.
1124 1124
1125 result_dv(0) = fields_numel; 1125 result_dv(0) = num_fields;
1126 1126
1127 for (int i = 1; i < result_dv.length (); i++) 1127 for (int i = 1; i < result_dv.length (); i++)
1128 result_dv(i) = m_dv(i-1); 1128 result_dv(i) = m_dv(i-1);
1129 1129
1130 // Squeeze to be sure that a (3,1) vector doesn't get turned
1131 // into a (3,3,1) vector.
1132
1133 result_dv = result_dv.squeeze ();
1134
1135 Cell c (result_dv); 1130 Cell c (result_dv);
1136 1131
1137 // Use ra_idx both for counting and for assignments, so 1132 octave_idx_type n_elts = m.numel ();
1138 // ra_idx(0) will both contain fields_numel for each call to 1133
1139 // increment_index and j for each assignment. 1134 for (octave_idx_type j = 0; j < num_fields; j++)
1140
1141 Array<octave_idx_type> ra_idx (result_dv.length (), 0);
1142 ra_idx(0) = fields_numel;
1143
1144 for (octave_idx_type i = 0; i < m_dv.numel (); i++)
1145 { 1135 {
1146 for (octave_idx_type j = 0; j < fields_numel; j++) 1136 octave_idx_type k = j;
1137
1138 const Cell vals = m.contents (keys(j));
1139
1140 for (octave_idx_type i = 0; i < n_elts; i++)
1147 { 1141 {
1148 ra_idx(0) = j; 1142 c(k) = vals(i);
1149 1143 k += num_fields;
1150 Cell c_tmp = m.contents (keys(j));
1151
1152 if (c_tmp.length () > 1) // Is a cell.
1153 c(ra_idx) = c_tmp;
1154 else if (c_tmp.length () == 1) // Get octave_value.
1155 c(ra_idx) = c_tmp(0);
1156 else
1157 c(ra_idx) = Cell ();
1158
1159 ra_idx(0) = fields_numel;
1160 } 1144 }
1161
1162 increment_index (ra_idx, result_dv);
1163 } 1145 }
1164 1146
1165 retval = c; 1147 retval = c;
1166 } 1148 }
1167 else 1149 else