Mercurial > jwe > octave
annotate src/ov-cell.cc @ 8546:3d8a914c580e
improve parser indexed assigment code
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 20 Jan 2009 21:15:17 +0100 |
parents | ad3afaaa19c1 |
children | 906f976d35a8 |
rev | line source |
---|---|
3353 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007 |
4 John W. Eaton | |
3353 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
3353 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
3353 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
5850 | 28 #include <iomanip> |
3503 | 29 #include <iostream> |
5765 | 30 #include <sstream> |
5164 | 31 #include <vector> |
3353 | 32 |
5360 | 33 #include "Array-util.h" |
34 #include "byte-swap.h" | |
3353 | 35 #include "lo-utils.h" |
4153 | 36 #include "quit.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
37 #include "oct-locbuf.h" |
3353 | 38 |
39 #include "defun.h" | |
40 #include "error.h" | |
41 #include "ov-cell.h" | |
3354 | 42 #include "oct-obj.h" |
3353 | 43 #include "unwind-prot.h" |
3354 | 44 #include "utils.h" |
3928 | 45 #include "ov-base-mat.h" |
46 #include "ov-base-mat.cc" | |
47 #include "ov-re-mat.h" | |
48 #include "ov-scalar.h" | |
5360 | 49 #include "pr-output.h" |
50 #include "ov-scalar.h" | |
3928 | 51 |
4687 | 52 #include "ls-oct-ascii.h" |
53 #include "ls-oct-binary.h" | |
54 #include "ls-hdf5.h" | |
55 #include "ls-utils.h" | |
56 | |
3928 | 57 template class octave_base_matrix<Cell>; |
3353 | 58 |
59 DEFINE_OCTAVE_ALLOCATOR (octave_cell); | |
60 | |
4612 | 61 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell", "cell"); |
3353 | 62 |
6833 | 63 static void |
64 gripe_failed_assignment (void) | |
65 { | |
66 error ("assignment to cell array failed"); | |
67 } | |
68 | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
69 octave_value_list |
4247 | 70 octave_cell::subsref (const std::string& type, |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
71 const std::list<octave_value_list>& idx, |
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
72 int nargout) |
3933 | 73 { |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
74 octave_value_list retval; |
3933 | 75 |
76 switch (type[0]) | |
77 { | |
78 case '(': | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
79 retval(0) = do_index_op (idx.front ()); |
3933 | 80 break; |
81 | |
82 case '{': | |
83 { | |
84 octave_value tmp = do_index_op (idx.front ()); | |
85 | |
4582 | 86 if (! error_state) |
3933 | 87 { |
4582 | 88 Cell tcell = tmp.cell_value (); |
89 | |
90 if (tcell.length () == 1) | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
91 retval(0) = tcell(0,0); |
4582 | 92 else |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
93 retval = octave_value (octave_value_list (tcell), true); |
3933 | 94 } |
95 } | |
96 break; | |
97 | |
98 case '.': | |
99 { | |
100 std::string nm = type_name (); | |
101 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
102 } | |
103 break; | |
104 | |
105 default: | |
106 panic_impossible (); | |
107 } | |
108 | |
5775 | 109 // FIXME -- perhaps there should be an |
4994 | 110 // octave_value_list::next_subsref member function? See also |
111 // octave_user_function::subsref. | |
112 | |
113 if (idx.size () > 1) | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
114 retval = retval(0).next_subsref (nargout, type, idx); |
4994 | 115 |
116 return retval; | |
3933 | 117 } |
118 | |
119 octave_value | |
4247 | 120 octave_cell::subsasgn (const std::string& type, |
4219 | 121 const std::list<octave_value_list>& idx, |
3933 | 122 const octave_value& rhs) |
123 { | |
124 octave_value retval; | |
125 | |
126 int n = type.length (); | |
127 | |
128 octave_value t_rhs = rhs; | |
129 | |
130 if (n > 1) | |
131 { | |
132 switch (type[0]) | |
133 { | |
134 case '(': | |
135 { | |
6767 | 136 if (is_empty () && type[1] == '.') |
137 { | |
138 // Allow conversion of empty cell array to some other | |
139 // type in cases like | |
140 // | |
141 // x = []; x(i).f = rhs | |
3933 | 142 |
6767 | 143 octave_value tmp = octave_value::empty_conv (type, rhs); |
3933 | 144 |
6767 | 145 return tmp.subsasgn (type, idx, rhs); |
146 } | |
147 else | |
3933 | 148 { |
6767 | 149 octave_value tmp = do_index_op (idx.front (), true); |
3933 | 150 |
6767 | 151 if (! tmp.is_defined ()) |
152 tmp = octave_value::empty_conv (type.substr (1), rhs); | |
3933 | 153 |
6767 | 154 if (! error_state) |
155 { | |
156 std::list<octave_value_list> next_idx (idx); | |
4362 | 157 |
6767 | 158 next_idx.erase (next_idx.begin ()); |
159 | |
160 tmp.make_unique (); | |
161 | |
162 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | |
163 } | |
3933 | 164 } |
165 } | |
166 break; | |
167 | |
168 case '{': | |
169 { | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
170 Cell tmpc = matrix.index (idx.front (), true); |
3933 | 171 |
4362 | 172 if (! error_state) |
173 { | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
174 std::list<octave_value_list> next_idx (idx); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
175 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
176 next_idx.erase (next_idx.begin ()); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
177 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
178 std::string next_type = type.substr (1); |
3933 | 179 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
180 if (rhs.is_cs_list ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
181 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
182 const octave_value_list rhsl = rhs.list_value (); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
183 if (tmpc.numel () == rhsl.length ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
184 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
185 for (octave_idx_type k = 0; k < tmpc.numel () && ! error_state; k++) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
186 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
187 octave_value tmp = tmpc (k); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
188 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
189 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
190 tmp = octave_value::empty_conv (next_type, rhs); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
191 tmp.make_unique (); // probably a no-op. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
192 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
193 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
194 // optimization: ignore the copy still stored inside our array and in tmpc. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
195 tmp.make_unique (2); |
4362 | 196 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
197 tmpc(k) = tmp.subsasgn (next_type, next_idx, rhsl(k)); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
198 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
199 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
200 t_rhs = octave_value (octave_value_list (tmpc), true); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
201 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
202 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
203 error ("invalid cs-list length in assignment"); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
204 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
205 else if (tmpc.numel () == 1) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
206 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
207 octave_value tmp = tmpc(0); |
3933 | 208 |
5927 | 209 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
210 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
211 tmp = octave_value::empty_conv (type.substr (1), rhs); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
212 tmp.make_unique (); // probably a no-op. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
213 } |
8452
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
214 else |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
215 // optimization: ignore the copy still stored inside our array and in tmpc. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
216 tmp.make_unique (2); |
4519 | 217 |
218 if (! error_state) | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
219 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); |
4362 | 220 } |
8446
7b25349b32e6
avoid redundant copying in {} assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
221 else |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
222 error ("invalid assignment to cs-list outside multiple assignment."); |
3933 | 223 } |
224 } | |
225 break; | |
226 | |
227 case '.': | |
228 { | |
229 std::string nm = type_name (); | |
230 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
231 } | |
232 break; | |
233 | |
234 default: | |
235 panic_impossible (); | |
236 } | |
237 } | |
238 | |
3940 | 239 if (! error_state) |
3933 | 240 { |
3940 | 241 switch (type[0]) |
242 { | |
243 case '(': | |
244 { | |
245 octave_value_list i = idx.front (); | |
3933 | 246 |
3940 | 247 if (t_rhs.is_cell ()) |
248 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); | |
249 else | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7813
diff
changeset
|
250 if (t_rhs.is_null_value ()) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7813
diff
changeset
|
251 octave_base_matrix<Cell>::delete_elements (i); |
4941 | 252 else |
253 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); | |
3933 | 254 |
6833 | 255 if (! error_state) |
256 { | |
257 count++; | |
258 retval = octave_value (this); | |
259 } | |
260 else | |
261 gripe_failed_assignment (); | |
3940 | 262 } |
263 break; | |
3933 | 264 |
3940 | 265 case '{': |
266 { | |
267 octave_value_list i = idx.front (); | |
3933 | 268 |
5846 | 269 if (t_rhs.is_cs_list ()) |
270 { | |
271 Cell tmp_cell = Cell (t_rhs.list_value ()); | |
272 | |
7040 | 273 // The shape of the RHS is irrelevant, we just want |
274 // the number of elements to agree and to preserve the | |
275 // shape of the left hand side of the assignment. | |
276 | |
277 if (numel () == tmp_cell.numel ()) | |
278 tmp_cell = tmp_cell.reshape (dims ()); | |
5846 | 279 |
280 octave_base_matrix<Cell>::assign (i, tmp_cell); | |
281 } | |
8457
c72207960242
minor simplifications to latest patches
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
282 else if (i.all_scalars () || do_index_op (i, true).numel () == 1) |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8452
diff
changeset
|
283 // Regularize a null matrix if stored into a cell. |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8457
diff
changeset
|
284 octave_base_matrix<Cell>::assign (i, Cell (t_rhs.storable_value ())); |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8452
diff
changeset
|
285 else if (! error_state) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
286 error ("invalid assignment to cs-list outside multiple assignment."); |
3933 | 287 |
6833 | 288 if (! error_state) |
289 { | |
290 count++; | |
291 retval = octave_value (this); | |
292 } | |
293 else | |
294 gripe_failed_assignment (); | |
3940 | 295 } |
296 break; | |
3933 | 297 |
3940 | 298 case '.': |
299 { | |
300 std::string nm = type_name (); | |
301 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
302 } | |
303 break; | |
3933 | 304 |
3940 | 305 default: |
306 panic_impossible (); | |
307 } | |
3933 | 308 } |
309 | |
310 return retval; | |
311 } | |
312 | |
3353 | 313 void |
314 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) | |
315 { | |
3928 | 316 if (rhs.is_cell ()) |
317 octave_base_matrix<Cell>::assign (idx, rhs.cell_value ()); | |
3353 | 318 else |
3928 | 319 octave_base_matrix<Cell>::assign (idx, Cell (rhs)); |
3353 | 320 } |
321 | |
4791 | 322 size_t |
323 octave_cell::byte_size (void) const | |
324 { | |
325 size_t retval = 0; | |
326 | |
5275 | 327 for (octave_idx_type i = 0; i < numel (); i++) |
4791 | 328 retval += matrix(i).byte_size (); |
329 | |
330 return retval; | |
331 } | |
332 | |
3933 | 333 octave_value_list |
334 octave_cell::list_value (void) const | |
335 { | |
336 octave_value_list retval; | |
337 | |
5275 | 338 octave_idx_type nr = rows (); |
339 octave_idx_type nc = columns (); | |
3933 | 340 |
341 if (nr == 1 && nc > 0) | |
342 { | |
343 retval.resize (nc); | |
344 | |
5275 | 345 for (octave_idx_type i = 0; i < nc; i++) |
3933 | 346 retval(i) = matrix(0,i); |
347 } | |
348 else if (nc == 1 && nr > 0) | |
349 { | |
350 retval.resize (nr); | |
351 | |
5275 | 352 for (octave_idx_type i = 0; i < nr; i++) |
3933 | 353 retval(i) = matrix(i,0); |
354 } | |
355 else | |
356 error ("invalid conversion from cell array to list"); | |
357 | |
358 return retval; | |
359 } | |
360 | |
4243 | 361 string_vector |
5715 | 362 octave_cell::all_strings (bool pad) const |
4243 | 363 { |
4358 | 364 string_vector retval; |
365 | |
7285 | 366 octave_idx_type nel = numel (); |
4243 | 367 |
4358 | 368 int n_elts = 0; |
369 | |
5275 | 370 octave_idx_type max_len = 0; |
4358 | 371 |
7285 | 372 for (octave_idx_type i = 0; i < nel; i++) |
4358 | 373 { |
7285 | 374 string_vector s = matrix(i).all_strings (); |
375 | |
376 if (error_state) | |
377 return retval; | |
4358 | 378 |
7285 | 379 octave_idx_type s_len = s.length (); |
4358 | 380 |
7285 | 381 n_elts += s_len ? s_len : 1; |
4358 | 382 |
7285 | 383 octave_idx_type s_max_len = s.max_length (); |
4358 | 384 |
7285 | 385 if (s_max_len > max_len) |
386 max_len = s_max_len; | |
4358 | 387 } |
388 | |
389 retval.resize (n_elts); | |
4243 | 390 |
5275 | 391 octave_idx_type k = 0; |
4243 | 392 |
7285 | 393 for (octave_idx_type i = 0; i < nel; i++) |
4243 | 394 { |
7285 | 395 string_vector s = matrix(i).all_strings (); |
396 | |
397 octave_idx_type s_len = s.length (); | |
4358 | 398 |
7285 | 399 if (s_len) |
400 { | |
401 for (octave_idx_type j = 0; j < s_len; j++) | |
5715 | 402 { |
7285 | 403 std::string t = s[j]; |
5715 | 404 int t_len = t.length (); |
405 | |
406 if (pad && max_len > t_len) | |
407 t += std::string (max_len - t_len, ' '); | |
408 | |
409 retval[k++] = t; | |
410 } | |
4243 | 411 } |
7285 | 412 else if (pad) |
413 retval[k++] = std::string (max_len, ' '); | |
414 else | |
415 retval[k++] = std::string (); | |
4243 | 416 } |
417 | |
418 return retval; | |
419 } | |
420 | |
4604 | 421 bool |
422 octave_cell::print_as_scalar (void) const | |
423 { | |
424 return (ndims () > 2 || numel () == 0); | |
425 } | |
426 | |
3933 | 427 void |
428 octave_cell::print (std::ostream& os, bool) const | |
429 { | |
430 print_raw (os); | |
431 } | |
432 | |
433 void | |
434 octave_cell::print_raw (std::ostream& os, bool) const | |
435 { | |
4587 | 436 int nd = matrix.ndims (); |
4513 | 437 |
4587 | 438 if (nd == 2) |
4513 | 439 { |
5275 | 440 octave_idx_type nr = rows (); |
441 octave_idx_type nc = columns (); | |
4513 | 442 |
443 if (nr > 0 && nc > 0) | |
444 { | |
445 indent (os); | |
446 os << "{"; | |
447 newline (os); | |
448 | |
449 increment_indent_level (); | |
450 | |
5275 | 451 for (octave_idx_type j = 0; j < nc; j++) |
4513 | 452 { |
5275 | 453 for (octave_idx_type i = 0; i < nr; i++) |
4513 | 454 { |
455 OCTAVE_QUIT; | |
456 | |
5765 | 457 std::ostringstream buf; |
458 buf << "[" << i+1 << "," << j+1 << "]"; | |
3933 | 459 |
4513 | 460 octave_value val = matrix(i,j); |
461 | |
5765 | 462 val.print_with_name (os, buf.str ()); |
4513 | 463 } |
464 } | |
465 | |
466 decrement_indent_level (); | |
467 | |
468 indent (os); | |
469 os << "}"; | |
470 newline (os); | |
471 } | |
472 else | |
473 { | |
474 os << "{}"; | |
5360 | 475 if (Vprint_empty_dimensions) |
4513 | 476 os << "(" << nr << "x" << nc << ")"; |
477 os << "\n"; | |
478 } | |
479 } | |
480 else | |
3933 | 481 { |
482 indent (os); | |
4513 | 483 dim_vector dv = matrix.dims (); |
4587 | 484 os << "{" << dv.str () << " Cell Array}"; |
3933 | 485 newline (os); |
486 } | |
487 } | |
488 | |
4687 | 489 #define CELL_ELT_TAG "<cell-element>" |
490 | |
491 bool | |
6974 | 492 octave_cell::save_ascii (std::ostream& os) |
4687 | 493 { |
494 dim_vector d = dims (); | |
495 if (d.length () > 2) | |
496 { | |
497 os << "# ndims: " << d.length () << "\n"; | |
498 | |
5275 | 499 for (int i = 0; i < d.length (); i++) |
4687 | 500 os << " " << d (i); |
501 os << "\n"; | |
502 | |
503 Cell tmp = cell_value (); | |
504 | |
5275 | 505 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687 | 506 { |
507 octave_value o_val = tmp.elem (i); | |
508 | |
509 // Recurse to print sub-value. | |
6974 | 510 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
4687 | 511 |
512 if (! b) | |
513 return os; | |
514 } | |
515 } | |
516 else | |
517 { | |
518 // Keep this case, rather than use generic code above for backward | |
519 // compatiability. Makes load_ascii much more complex!! | |
520 os << "# rows: " << rows () << "\n" | |
521 << "# columns: " << columns () << "\n"; | |
522 | |
523 Cell tmp = cell_value (); | |
524 | |
5275 | 525 for (octave_idx_type j = 0; j < tmp.cols (); j++) |
4687 | 526 { |
5275 | 527 for (octave_idx_type i = 0; i < tmp.rows (); i++) |
4687 | 528 { |
529 octave_value o_val = tmp.elem (i, j); | |
530 | |
531 // Recurse to print sub-value. | |
6974 | 532 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
4687 | 533 |
534 if (! b) | |
535 return os; | |
536 } | |
537 | |
538 os << "\n"; | |
539 } | |
540 } | |
541 | |
542 return true; | |
543 } | |
544 | |
545 bool | |
546 octave_cell::load_ascii (std::istream& is) | |
547 { | |
548 bool success = true; | |
5099 | 549 |
550 string_vector keywords(2); | |
4687 | 551 |
5099 | 552 keywords[0] = "ndims"; |
553 keywords[1] = "rows"; | |
554 | |
555 std::string kw; | |
5275 | 556 octave_idx_type val = 0; |
5099 | 557 |
558 if (extract_keyword (is, keywords, kw, val, true)) | |
4687 | 559 { |
5099 | 560 if (kw == "ndims") |
4687 | 561 { |
5275 | 562 int mdims = static_cast<int> (val); |
4687 | 563 |
5099 | 564 if (mdims >= 0) |
565 { | |
566 dim_vector dv; | |
567 dv.resize (mdims); | |
568 | |
569 for (int i = 0; i < mdims; i++) | |
570 is >> dv(i); | |
4687 | 571 |
5099 | 572 Cell tmp(dv); |
4687 | 573 |
5275 | 574 for (octave_idx_type i = 0; i < dv.numel (); i++) |
5099 | 575 { |
576 octave_value t2; | |
577 bool dummy; | |
578 | |
579 // recurse to read cell elements | |
580 std::string nm = read_ascii_data (is, std::string (), | |
5759 | 581 dummy, t2, i); |
4687 | 582 |
5099 | 583 if (nm == CELL_ELT_TAG) |
584 { | |
585 if (is) | |
586 tmp.elem (i) = t2; | |
587 } | |
588 else | |
589 { | |
590 error ("load: cell array element had unexpected name"); | |
591 success = false; | |
592 break; | |
593 } | |
594 } | |
4687 | 595 |
5099 | 596 if (is) |
597 matrix = tmp; | |
4687 | 598 else |
599 { | |
5099 | 600 error ("load: failed to load matrix constant"); |
4687 | 601 success = false; |
602 } | |
603 } | |
604 else | |
605 { | |
5099 | 606 error ("load: failed to extract number of rows and columns"); |
607 success = false; | |
608 } | |
609 } | |
610 else if (kw == "rows") | |
611 { | |
5275 | 612 octave_idx_type nr = val; |
613 octave_idx_type nc = 0; | |
5099 | 614 |
615 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) | |
616 { | |
617 if (nr > 0 && nc > 0) | |
618 { | |
619 Cell tmp (nr, nc); | |
620 | |
5275 | 621 for (octave_idx_type j = 0; j < nc; j++) |
5099 | 622 { |
5275 | 623 for (octave_idx_type i = 0; i < nr; i++) |
5099 | 624 { |
625 octave_value t2; | |
626 bool dummy; | |
627 | |
628 // recurse to read cell elements | |
629 std::string nm = read_ascii_data (is, std::string (), | |
5759 | 630 dummy, t2, i); |
5099 | 631 |
632 if (nm == CELL_ELT_TAG) | |
633 { | |
634 if (is) | |
635 tmp.elem (i, j) = t2; | |
636 } | |
637 else | |
638 { | |
639 error ("load: cell array element had unexpected name"); | |
640 success = false; | |
641 goto cell_read_error; | |
642 } | |
643 } | |
644 } | |
645 | |
646 cell_read_error: | |
647 | |
648 if (is) | |
649 matrix = tmp; | |
650 else | |
651 { | |
652 error ("load: failed to load cell element"); | |
653 success = false; | |
654 } | |
655 } | |
656 else if (nr == 0 || nc == 0) | |
657 matrix = Cell (nr, nc); | |
658 else | |
659 panic_impossible (); | |
660 } | |
661 else | |
662 { | |
663 error ("load: failed to extract number of rows and columns for cell array"); | |
4687 | 664 success = false; |
665 } | |
666 } | |
667 else | |
5099 | 668 panic_impossible (); |
4687 | 669 } |
670 else | |
671 { | |
5099 | 672 error ("load: failed to extract number of rows and columns"); |
673 success = false; | |
4687 | 674 } |
675 | |
676 return success; | |
677 } | |
678 | |
679 bool | |
680 octave_cell::save_binary (std::ostream& os, bool& save_as_floats) | |
681 { | |
682 dim_vector d = dims (); | |
683 if (d.length () < 1) | |
684 return false; | |
685 | |
686 // Use negative value for ndims | |
5828 | 687 int32_t di = - d.length(); |
5760 | 688 os.write (reinterpret_cast<char *> (&di), 4); |
5275 | 689 for (int i = 0; i < d.length (); i++) |
4687 | 690 { |
691 di = d(i); | |
5760 | 692 os.write (reinterpret_cast<char *> (&di), 4); |
4687 | 693 } |
694 | |
695 Cell tmp = cell_value (); | |
696 | |
5275 | 697 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687 | 698 { |
699 octave_value o_val = tmp.elem (i); | |
700 | |
701 // Recurse to print sub-value. | |
702 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, | |
703 save_as_floats); | |
704 | |
705 if (! b) | |
706 return false; | |
707 } | |
708 | |
709 return true; | |
710 } | |
711 | |
712 bool | |
713 octave_cell::load_binary (std::istream& is, bool swap, | |
714 oct_mach_info::float_format fmt) | |
715 { | |
716 bool success = true; | |
5828 | 717 int32_t mdims; |
5760 | 718 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4687 | 719 return false; |
720 if (swap) | |
4944 | 721 swap_bytes<4> (&mdims); |
4687 | 722 if (mdims >= 0) |
723 return false; | |
724 | |
725 mdims = -mdims; | |
5828 | 726 int32_t di; |
4687 | 727 dim_vector dv; |
728 dv.resize (mdims); | |
729 | |
730 for (int i = 0; i < mdims; i++) | |
731 { | |
5760 | 732 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4687 | 733 return false; |
734 if (swap) | |
4944 | 735 swap_bytes<4> (&di); |
4687 | 736 dv(i) = di; |
737 } | |
738 | |
5157 | 739 // Convert an array with a single dimension to be a row vector. |
740 // Octave should never write files like this, other software | |
741 // might. | |
742 | |
743 if (mdims == 1) | |
744 { | |
745 mdims = 2; | |
746 dv.resize (mdims); | |
747 dv(1) = dv(0); | |
748 dv(0) = 1; | |
749 } | |
750 | |
5275 | 751 octave_idx_type nel = dv.numel (); |
4687 | 752 Cell tmp(dv); |
753 | |
5275 | 754 for (octave_idx_type i = 0; i < nel; i++) |
4687 | 755 { |
756 octave_value t2; | |
757 bool dummy; | |
758 std::string doc; | |
759 | |
760 // recurse to read cell elements | |
761 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
762 dummy, t2, doc); | |
763 | |
764 if (nm == CELL_ELT_TAG) | |
765 { | |
766 if (is) | |
767 tmp.elem (i) = t2; | |
768 } | |
769 else | |
770 { | |
771 error ("load: cell array element had unexpected name"); | |
772 success = false; | |
773 break; | |
774 } | |
775 } | |
776 | |
777 if (is) | |
778 matrix = tmp; | |
779 else | |
780 { | |
781 error ("load: failed to load matrix constant"); | |
782 success = false; | |
783 } | |
784 | |
785 return success; | |
786 } | |
787 | |
788 #if defined (HAVE_HDF5) | |
4815 | 789 |
4687 | 790 bool |
791 octave_cell::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
792 { | |
4814 | 793 dim_vector dv = dims (); |
4837 | 794 int empty = save_hdf5_empty (loc_id, name, dv); |
795 if (empty) | |
796 return (empty > 0); | |
797 | |
4815 | 798 hsize_t rank = dv.length (); |
4687 | 799 hid_t space_hid = -1, data_hid = -1, size_hid = -1; |
800 | |
801 data_hid = H5Gcreate (loc_id, name, 0); | |
4815 | 802 |
803 if (data_hid < 0) | |
804 return false; | |
4687 | 805 |
4814 | 806 // Have to save cell array shape, since can't have a |
807 // dataset of groups.... | |
4815 | 808 |
809 space_hid = H5Screate_simple (1, &rank, 0); | |
810 | |
4687 | 811 if (space_hid < 0) |
812 { | |
813 H5Gclose (data_hid); | |
814 return false; | |
815 } | |
816 | |
5351 | 817 OCTAVE_LOCAL_BUFFER (octave_idx_type, hdims, rank); |
4814 | 818 |
819 // Octave uses column-major, while HDF5 uses row-major ordering | |
4933 | 820 for (hsize_t i = 0; i < rank; i++) |
4815 | 821 hdims[i] = dv(rank-i-1); |
4814 | 822 |
5351 | 823 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, |
4687 | 824 H5P_DEFAULT); |
825 if (size_hid < 0) | |
826 { | |
827 H5Sclose (space_hid); | |
828 H5Gclose (data_hid); | |
829 return false; | |
830 } | |
831 | |
6276 | 832 if (H5Dwrite (size_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
833 H5P_DEFAULT, hdims) < 0) | |
4687 | 834 { |
835 H5Dclose (size_hid); | |
836 H5Sclose (space_hid); | |
837 H5Gclose (data_hid); | |
838 return false; | |
839 } | |
4815 | 840 |
4687 | 841 H5Dclose (size_hid); |
842 H5Sclose (space_hid); | |
843 | |
4815 | 844 // Recursively add each element of the cell to this group. |
845 | |
4687 | 846 Cell tmp = cell_value (); |
5850 | 847 |
848 octave_idx_type nel = dv.numel (); | |
849 | |
850 for (octave_idx_type i = 0; i < nel; i++) | |
4687 | 851 { |
5765 | 852 std::ostringstream buf; |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7285
diff
changeset
|
853 int digits = static_cast<int> (::floor (::log10 (static_cast<double> (nel)) + 1.0)); |
5850 | 854 buf << "_" << std::setw (digits) << std::setfill ('0') << i; |
5765 | 855 std::string s = buf.str (); |
4687 | 856 |
5850 | 857 if (! add_hdf5_data (data_hid, tmp.elem (i), s.c_str (), "", false, |
858 save_as_floats)) | |
4814 | 859 { |
860 H5Gclose (data_hid); | |
861 return false; | |
4687 | 862 } |
863 } | |
864 | |
865 H5Gclose (data_hid); | |
4815 | 866 |
4687 | 867 return true; |
868 } | |
869 | |
870 bool | |
871 octave_cell::load_hdf5 (hid_t loc_id, const char *name, | |
872 bool have_h5giterate_bug) | |
873 { | |
874 bool retval = false; | |
4837 | 875 |
876 dim_vector dv; | |
877 int empty = load_hdf5_empty (loc_id, name, dv); | |
878 if (empty > 0) | |
879 matrix.resize(dv); | |
880 if (empty) | |
881 return (empty > 0); | |
882 | |
4687 | 883 hid_t group_id = H5Gopen (loc_id, name); |
884 | |
885 if (group_id < 0) | |
886 return false; | |
887 | |
4814 | 888 hid_t data_hid = H5Dopen (group_id, "dims"); |
4687 | 889 hid_t space_hid = H5Dget_space (data_hid); |
890 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); | |
4814 | 891 if (rank != 1) |
4687 | 892 { |
4837 | 893 H5Dclose (data_hid); |
894 H5Gclose (group_id); | |
4687 | 895 return false; |
896 } | |
897 | |
4814 | 898 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
899 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
4815 | 900 |
4814 | 901 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
4687 | 902 |
4815 | 903 // Octave uses column-major, while HDF5 uses row-major ordering. |
904 | |
4814 | 905 dv.resize (hdims[0]); |
4815 | 906 |
5351 | 907 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); |
4814 | 908 |
5351 | 909 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
4815 | 910 H5P_DEFAULT, tmp) < 0) |
4687 | 911 { |
4837 | 912 H5Dclose (data_hid); |
913 H5Gclose (group_id); | |
4687 | 914 return false; |
915 } | |
4815 | 916 |
4687 | 917 H5Dclose (data_hid); |
918 H5Gclose (group_id); | |
919 | |
4815 | 920 for (hsize_t i = 0, j = hdims[0] - 1; i < hdims[0]; i++, j--) |
4814 | 921 dv(j) = tmp[i]; |
922 | |
4687 | 923 hdf5_callback_data dsub; |
924 | |
925 herr_t retval2 = -1; | |
4815 | 926 |
4814 | 927 Cell m (dv); |
4815 | 928 |
4687 | 929 int current_item = 0; |
4815 | 930 |
4687 | 931 if (have_h5giterate_bug) |
4815 | 932 current_item = 1; // Skip dims items in group. |
4687 | 933 |
4696 | 934 #ifdef HAVE_H5GGET_NUM_OBJS |
935 hsize_t num_obj = 0; | |
5060 | 936 group_id = H5Gopen (loc_id, name); |
937 H5Gget_num_objs (group_id, &num_obj); | |
938 H5Gclose (group_id); | |
4696 | 939 #endif |
940 | |
5275 | 941 for (octave_idx_type i = 0; i < dv.numel (); i++) |
4687 | 942 { |
4696 | 943 |
944 #ifdef HAVE_H5GGET_NUM_OBJS | |
4814 | 945 if (current_item >= static_cast<int> (num_obj)) |
946 retval2 = -1; | |
947 else | |
4696 | 948 #endif |
4814 | 949 retval2 = H5Giterate (loc_id, name, ¤t_item, |
950 hdf5_read_next_data, &dsub); | |
951 | |
4687 | 952 if (retval2 <= 0) |
953 break; | |
4814 | 954 |
955 octave_value ov = dsub.tc; | |
956 m.elem (i) = ov; | |
957 | |
958 if (have_h5giterate_bug) | |
4815 | 959 current_item++; // H5Giterate returned the last index processed. |
4814 | 960 |
4687 | 961 } |
962 | |
963 if (retval2 >= 0) | |
964 { | |
965 matrix = m; | |
966 retval = true; | |
967 } | |
968 | |
969 return retval; | |
970 } | |
4815 | 971 |
4687 | 972 #endif |
973 | |
3354 | 974 DEFUN (iscell, args, , |
3448 | 975 "-*- texinfo -*-\n\ |
976 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ | |
977 Return true if @var{x} is a cell array object. Otherwise, return\n\ | |
978 false.\n\ | |
979 @end deftypefn") | |
3354 | 980 { |
981 octave_value retval; | |
982 | |
983 if (args.length () == 1) | |
984 retval = args(0).is_cell (); | |
985 else | |
5823 | 986 print_usage (); |
3354 | 987 |
988 return retval; | |
989 } | |
990 | |
991 DEFUN (cell, args, , | |
3448 | 992 "-*- texinfo -*-\n\ |
993 @deftypefn {Built-in Function} {} cell (@var{x})\n\ | |
994 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ | |
995 Create a new cell array object. If invoked with a single scalar\n\ | |
996 argument, @code{cell} returns a square cell array with the dimension\n\ | |
997 specified. If you supply two scalar arguments, @code{cell} takes\n\ | |
998 them to be the number of rows and columns. If given a vector with two\n\ | |
999 elements, @code{cell} uses the values of the elements as the number of\n\ | |
1000 rows and columns, respectively.\n\ | |
1001 @end deftypefn") | |
3354 | 1002 { |
1003 octave_value retval; | |
1004 | |
1005 int nargin = args.length (); | |
1006 | |
4563 | 1007 dim_vector dims; |
1008 | |
3354 | 1009 switch (nargin) |
1010 { | |
4563 | 1011 case 0: |
1012 dims = dim_vector (0, 0); | |
3354 | 1013 break; |
1014 | |
4563 | 1015 case 1: |
1016 get_dimensions (args(0), "cell", dims); | |
3354 | 1017 break; |
1018 | |
1019 default: | |
4563 | 1020 { |
1021 dims.resize (nargin); | |
1022 | |
1023 for (int i = 0; i < nargin; i++) | |
1024 { | |
1025 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); | |
1026 | |
1027 if (error_state) | |
1028 { | |
1029 error ("cell: expecting scalar arguments"); | |
1030 break; | |
1031 } | |
1032 } | |
1033 } | |
3354 | 1034 break; |
1035 } | |
1036 | |
4563 | 1037 if (! error_state) |
1038 { | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
1039 dims.chop_trailing_singletons (); |
4563 | 1040 |
1041 check_dimensions (dims, "cell"); | |
1042 | |
1043 if (! error_state) | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
1044 retval = Cell (dims, Matrix ()); |
4563 | 1045 } |
1046 | |
3354 | 1047 return retval; |
1048 } | |
1049 | |
4610 | 1050 DEFUN (iscellstr, args, , |
1051 "-*- texinfo -*-\n\ | |
1052 @deftypefn {Built-in Function} {} iscellstr (@var{cell})\n\ | |
1053 Return true if every element of the cell array @var{cell} is a\n\ | |
1054 character string\n\ | |
1055 @end deftypefn") | |
1056 { | |
4699 | 1057 octave_value retval; |
4610 | 1058 |
1059 if (args.length () == 1) | |
6116 | 1060 retval = args(0).is_cellstr (); |
4610 | 1061 else |
5823 | 1062 print_usage (); |
4610 | 1063 |
1064 return retval; | |
1065 } | |
1066 | |
4817 | 1067 // Note that since Fcellstr calls Fiscellstr, we need to have |
1068 // Fiscellstr defined first (to provide a declaration) and also we | |
1069 // should keep it in the same file (so we don't have to provide a | |
1070 // declaration) and so we don't have to use feval to call it. | |
1071 | |
1072 DEFUN (cellstr, args, , | |
1073 "-*- texinfo -*-\n\ | |
1074 @deftypefn {Built-in Function} {} cellstr (@var{string})\n\ | |
1075 Create a new cell array object from the elements of the string\n\ | |
1076 array @var{string}.\n\ | |
1077 @end deftypefn") | |
1078 { | |
1079 octave_value retval; | |
1080 | |
1081 if (args.length () == 1) | |
1082 { | |
1083 octave_value_list tmp = Fiscellstr (args, 1); | |
1084 | |
1085 if (tmp(0).is_true ()) | |
1086 retval = args(0); | |
1087 else | |
1088 { | |
1089 string_vector s = args(0).all_strings (); | |
1090 | |
1091 if (! error_state) | |
7813 | 1092 retval = (s.is_empty () |
1093 ? Cell (octave_value (std::string ())) | |
1094 : Cell (s, true)); | |
4817 | 1095 else |
1096 error ("cellstr: expecting argument to be a 2-d character array"); | |
1097 } | |
1098 } | |
1099 else | |
5823 | 1100 print_usage (); |
4817 | 1101 |
1102 return retval; | |
1103 } | |
1104 | |
4762 | 1105 DEFUN (struct2cell, args, , |
1106 "-*- texinfo -*-\n\ | |
1107 @deftypefn {Built-in Function} {} struct2cell (@var{S})\n\ | |
1108 Create a new cell array from the objects stored in the struct object.\n\ | |
4764 | 1109 If @var{f} is the number of fields in the structure, the resulting\n\ |
1110 cell array will have a dimension vector corresponding to\n\ | |
1111 @code{[@var{F} size(@var{S})]}.\n\ | |
5642 | 1112 @seealso{cell2struct, fieldnames}\n\ |
1113 @end deftypefn") | |
4762 | 1114 { |
1115 octave_value retval; | |
4764 | 1116 |
4762 | 1117 int nargin = args.length (); |
4764 | 1118 |
4762 | 1119 if (nargin == 1) |
1120 { | |
4764 | 1121 Octave_map m = args(0).map_value (); |
1122 | |
4762 | 1123 if (! error_state) |
1124 { | |
1125 dim_vector m_dv = m.dims (); | |
4764 | 1126 |
4762 | 1127 string_vector keys = m.keys (); |
4764 | 1128 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1129 octave_idx_type num_fields = keys.length (); |
4764 | 1130 |
4762 | 1131 // The resulting dim_vector should have dimensions: |
1132 // [numel(fields) size(struct)] | |
4764 | 1133 |
4762 | 1134 dim_vector result_dv; |
4764 | 1135 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields. |
1136 | |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1137 result_dv(0) = num_fields; |
4762 | 1138 |
1139 for (int i = 1; i < result_dv.length (); i++) | |
1140 result_dv(i) = m_dv(i-1); | |
4764 | 1141 |
4762 | 1142 Cell c (result_dv); |
4764 | 1143 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1144 octave_idx_type n_elts = m.numel (); |
4764 | 1145 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1146 for (octave_idx_type j = 0; j < num_fields; j++) |
4762 | 1147 { |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1148 octave_idx_type k = j; |
4764 | 1149 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1150 const Cell vals = m.contents (keys(j)); |
4764 | 1151 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1152 for (octave_idx_type i = 0; i < n_elts; i++) |
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1153 { |
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1154 c(k) = vals(i); |
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1155 k += num_fields; |
4762 | 1156 } |
1157 } | |
1158 | |
1159 retval = c; | |
1160 } | |
1161 else | |
1162 error ("struct2cell: expecting argument to be a cell array"); | |
1163 } | |
1164 else | |
5823 | 1165 print_usage (); |
4764 | 1166 |
4762 | 1167 return retval; |
1168 } | |
1169 | |
5900 | 1170 mxArray * |
1171 octave_cell::as_mxArray (void) const | |
1172 { | |
1173 mxArray *retval = new mxArray (dims ()); | |
1174 | |
1175 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); | |
1176 | |
6686 | 1177 mwSize nel = numel (); |
5900 | 1178 |
1179 const octave_value *p = matrix.data (); | |
1180 | |
6686 | 1181 for (mwIndex i = 0; i < nel; i++) |
5900 | 1182 elts[i] = new mxArray (p[i]); |
1183 | |
1184 return retval; | |
1185 } | |
1186 | |
3353 | 1187 /* |
1188 ;;; Local Variables: *** | |
1189 ;;; mode: C++ *** | |
1190 ;;; End: *** | |
1191 */ |