Mercurial > hg > octave-lyh
annotate src/ov-struct.cc @ 8523:ad3afaaa19c1
implement non-copying contiguous range indexing
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 15 Jan 2009 07:22:24 +0100 |
parents | c1709a45b45b |
children | 3d8a914c580e |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
4 2007 John W. Eaton | |
2376 | 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. | |
2376 | 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/>. | |
2376 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3503 | 28 #include <iostream> |
2376 | 29 |
3933 | 30 #include "Cell.h" |
4358 | 31 #include "defun.h" |
2376 | 32 #include "error.h" |
4358 | 33 #include "gripes.h" |
2979 | 34 #include "oct-lvalue.h" |
3932 | 35 #include "ov-list.h" |
2376 | 36 #include "ov-struct.h" |
37 #include "unwind-prot.h" | |
6811 | 38 #include "utils.h" |
2948 | 39 #include "variables.h" |
2376 | 40 |
4750 | 41 #include "Array-util.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
42 #include "oct-locbuf.h" |
4750 | 43 |
4687 | 44 #include "byte-swap.h" |
45 #include "ls-oct-ascii.h" | |
46 #include "ls-oct-binary.h" | |
47 #include "ls-hdf5.h" | |
48 #include "ls-utils.h" | |
5759 | 49 #include "pr-output.h" |
4687 | 50 |
3219 | 51 DEFINE_OCTAVE_ALLOCATOR(octave_struct); |
2376 | 52 |
4612 | 53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct"); |
2376 | 54 |
4513 | 55 Cell |
3933 | 56 octave_struct::dotref (const octave_value_list& idx) |
2962 | 57 { |
4513 | 58 Cell retval; |
3933 | 59 |
60 assert (idx.length () == 1); | |
2962 | 61 |
3933 | 62 std::string nm = idx(0).string_value (); |
63 | |
4219 | 64 Octave_map::const_iterator p = map.seek (nm); |
2376 | 65 |
4219 | 66 if (p != map.end ()) |
3933 | 67 retval = map.contents (p); |
68 else | |
2376 | 69 error ("structure has no member `%s'", nm.c_str ()); |
70 | |
71 return retval; | |
72 } | |
73 | |
4513 | 74 #if 0 |
3933 | 75 static void |
76 gripe_invalid_index (void) | |
77 { | |
78 error ("invalid index for structure array"); | |
79 } | |
4513 | 80 #endif |
3933 | 81 |
82 static void | |
83 gripe_invalid_index_for_assignment (void) | |
84 { | |
85 error ("invalid index for structure array assignment"); | |
86 } | |
87 | |
88 static void | |
89 gripe_invalid_index_type (const std::string& nm, char t) | |
90 { | |
91 error ("%s cannot be indexed with %c", nm.c_str (), t); | |
92 } | |
93 | |
94 static void | |
95 gripe_failed_assignment (void) | |
96 { | |
97 error ("assignment to structure element failed"); | |
98 } | |
99 | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
100 octave_value_list |
4247 | 101 octave_struct::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
|
102 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
|
103 int nargout) |
3933 | 104 { |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
105 octave_value_list retval; |
3933 | 106 |
107 int skip = 1; | |
108 | |
109 switch (type[0]) | |
110 { | |
111 case '(': | |
112 { | |
113 if (type.length () > 1 && type[1] == '.') | |
114 { | |
4219 | 115 std::list<octave_value_list>::const_iterator p = idx.begin (); |
116 octave_value_list key_idx = *++p; | |
3933 | 117 |
4513 | 118 Cell tmp = dotref (key_idx); |
3933 | 119 |
120 if (! error_state) | |
121 { | |
7460 | 122 Cell t = tmp.index (idx.front (), true); |
3933 | 123 |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
124 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); |
3933 | 125 |
4513 | 126 // We handled two index elements, so tell |
127 // next_subsref to skip both of them. | |
3933 | 128 |
4513 | 129 skip++; |
3933 | 130 } |
131 } | |
132 else | |
8031
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
133 retval(0) = map.index (idx.front (), false); |
3933 | 134 } |
135 break; | |
136 | |
137 case '.': | |
138 { | |
5592 | 139 if (map.numel() > 0) |
140 { | |
141 Cell t = dotref (idx.front ()); | |
3933 | 142 |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
143 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); |
5592 | 144 } |
3933 | 145 } |
146 break; | |
147 | |
148 case '{': | |
149 gripe_invalid_index_type (type_name (), type[0]); | |
150 break; | |
151 | |
152 default: | |
153 panic_impossible (); | |
154 } | |
155 | |
5775 | 156 // FIXME -- perhaps there should be an |
4994 | 157 // octave_value_list::next_subsref member function? See also |
158 // octave_user_function::subsref. | |
159 | |
160 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
|
161 retval = retval(0).next_subsref (nargout, type, idx, skip); |
3933 | 162 |
163 return retval; | |
164 } | |
165 | |
8031
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
166 /* |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
167 %!test |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
168 %! x(1).a.a = 1; x(2).a.a = 2; |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
169 %! assert (size (x), [1, 2]); |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
170 %! assert (x(1).a.a, 1); |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
171 %! assert (x(2).a.a, 2); |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
172 */ |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
173 |
3933 | 174 octave_value |
4513 | 175 octave_struct::numeric_conv (const Cell& val, |
3933 | 176 const std::string& type) |
177 { | |
178 octave_value retval; | |
179 | |
180 if (val.length () == 1) | |
181 { | |
182 retval = val(0); | |
183 | |
184 if (type.length () > 0 && type[0] == '.' && ! retval.is_map ()) | |
185 retval = Octave_map (); | |
186 } | |
187 else | |
188 gripe_invalid_index_for_assignment (); | |
189 | |
190 return retval; | |
191 } | |
192 | |
193 octave_value | |
4247 | 194 octave_struct::subsasgn (const std::string& type, |
4219 | 195 const std::list<octave_value_list>& idx, |
3933 | 196 const octave_value& rhs) |
2376 | 197 { |
3933 | 198 octave_value retval; |
199 | |
200 int n = type.length (); | |
201 | |
202 octave_value t_rhs = rhs; | |
203 | |
204 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) | |
205 { | |
206 switch (type[0]) | |
207 { | |
208 case '(': | |
209 { | |
210 if (type.length () > 1 && type[1] == '.') | |
211 { | |
4219 | 212 std::list<octave_value_list>::const_iterator p = idx.begin (); |
213 octave_value_list t_idx = *p; | |
3933 | 214 |
4513 | 215 octave_value_list key_idx = *++p; |
216 | |
217 assert (key_idx.length () == 1); | |
3933 | 218 |
4513 | 219 std::string key = key_idx(0).string_value (); |
3933 | 220 |
4513 | 221 octave_value u; |
3933 | 222 |
4513 | 223 if (! map.contains (key)) |
224 u = octave_value::empty_conv (type.substr (2), rhs); | |
225 else | |
226 { | |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
227 Cell& cell_ref = map.contents (key); |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
228 |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
229 octave_value u1 = cell_ref.index (idx.front (), true); |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
230 u = numeric_conv (u1, type.substr (2)); |
3933 | 231 |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
232 if (u.is_defined () && u.is_copy_of (u1)) |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
233 { |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
234 // This is a bit of black magic. u is a shallow copy |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
235 // of an element inside this struct, and maybe more. To |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
236 // prevent make_unique from always forcing a copy, we |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
237 // temporarily delete the stored value. |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
238 u1 = octave_value (); |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
239 cell_ref.assign (idx.front (), Cell (octave_value ())); |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
240 u.make_unique (); |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
241 cell_ref.assign (idx.front (), Cell (u)); |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
242 } |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
243 else |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
244 // Safe is safe. |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
245 u.make_unique (); |
3933 | 246 |
4513 | 247 } |
3933 | 248 |
4513 | 249 if (! error_state) |
250 { | |
251 std::list<octave_value_list> next_idx (idx); | |
3933 | 252 |
4513 | 253 // We handled two index elements, so subsasgn to |
254 // needs to skip both of them. | |
3933 | 255 |
4513 | 256 next_idx.erase (next_idx.begin ()); |
257 next_idx.erase (next_idx.begin ()); | |
4059 | 258 |
4513 | 259 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); |
3933 | 260 } |
261 } | |
262 else | |
263 gripe_invalid_index_for_assignment (); | |
264 } | |
265 break; | |
266 | |
267 case '.': | |
268 { | |
269 octave_value_list key_idx = idx.front (); | |
270 | |
271 assert (key_idx.length () == 1); | |
272 | |
273 std::string key = key_idx(0).string_value (); | |
274 | |
275 octave_value u; | |
276 | |
277 if (! map.contains (key)) | |
278 u = octave_value::empty_conv (type.substr (1), rhs); | |
279 else | |
280 { | |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
281 Cell& cell_ref = map.contents (key); |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
282 |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
283 u = numeric_conv (cell_ref, type.substr (2)); |
3933 | 284 |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
285 if (u.is_defined () && u.is_copy_of (cell_ref(0))) |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
286 { |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
287 // This is a bit of black magic. u is a shallow copy |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
288 // of an element inside this struct, and maybe more. To |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
289 // prevent make_unique from always forcing a copy, we |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
290 // temporarily delete the stored value. |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
291 cell_ref(0) = octave_value (); |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
292 u.make_unique (); |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
293 cell_ref(0) = u; |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
294 } |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
295 else |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
296 // Safe is safe. |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
297 u.make_unique (); |
3933 | 298 } |
299 | |
300 if (! error_state) | |
301 { | |
4219 | 302 std::list<octave_value_list> next_idx (idx); |
3933 | 303 |
4219 | 304 next_idx.erase (next_idx.begin ()); |
3933 | 305 |
306 t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); | |
307 } | |
308 } | |
309 break; | |
310 | |
311 case '{': | |
312 gripe_invalid_index_type (type_name (), type[0]); | |
313 break; | |
314 | |
315 default: | |
316 panic_impossible (); | |
317 } | |
318 } | |
319 | |
320 if (! error_state) | |
321 { | |
322 switch (type[0]) | |
323 { | |
324 case '(': | |
325 { | |
326 if (n > 1 && type[1] == '.') | |
327 { | |
4219 | 328 std::list<octave_value_list>::const_iterator p = idx.begin (); |
329 octave_value_list key_idx = *++p; | |
3933 | 330 |
331 assert (key_idx.length () == 1); | |
332 | |
333 std::string key = key_idx(0).string_value (); | |
334 | |
335 if (! error_state) | |
336 { | |
4513 | 337 map.assign (idx.front (), key, t_rhs); |
3933 | 338 |
4513 | 339 if (! error_state) |
5759 | 340 { |
341 count++; | |
342 retval = octave_value (this); | |
343 } | |
3933 | 344 else |
4513 | 345 gripe_failed_assignment (); |
3933 | 346 } |
347 else | |
348 gripe_failed_assignment (); | |
349 } | |
350 else | |
4197 | 351 { |
5592 | 352 if (t_rhs.is_map()) |
4197 | 353 { |
5592 | 354 Octave_map rhs_map = t_rhs.map_value (); |
4197 | 355 |
356 if (! error_state) | |
5592 | 357 { |
358 map.assign (idx.front (), rhs_map); | |
359 | |
360 if (! error_state) | |
5759 | 361 { |
362 count++; | |
363 retval = octave_value (this); | |
364 } | |
5592 | 365 else |
366 gripe_failed_assignment (); | |
367 } | |
4197 | 368 else |
5592 | 369 error ("invalid structure assignment"); |
4197 | 370 } |
4513 | 371 else |
5592 | 372 { |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8031
diff
changeset
|
373 if (t_rhs.is_null_value()) |
5592 | 374 { |
375 map.maybe_delete_elements (idx.front()); | |
376 | |
377 if (! error_state) | |
5759 | 378 { |
379 count++; | |
380 retval = octave_value (this); | |
381 } | |
5592 | 382 else |
383 gripe_failed_assignment (); | |
384 } | |
385 else | |
386 error ("invalid structure assignment"); | |
387 } | |
4197 | 388 } |
3933 | 389 } |
390 break; | |
391 | |
392 case '.': | |
393 { | |
394 octave_value_list key_idx = idx.front (); | |
395 | |
396 assert (key_idx.length () == 1); | |
397 | |
398 std::string key = key_idx(0).string_value (); | |
399 | |
6833 | 400 if (t_rhs.is_cs_list ()) |
401 { | |
402 Cell tmp_cell = Cell (t_rhs.list_value ()); | |
403 | |
7040 | 404 // The shape of the RHS is irrelevant, we just want |
405 // the number of elements to agree and to preserve the | |
406 // shape of the left hand side of the assignment. | |
407 | |
408 if (numel () == tmp_cell.numel ()) | |
409 tmp_cell = tmp_cell.reshape (dims ()); | |
6833 | 410 |
411 map.assign (key, tmp_cell); | |
412 } | |
413 else | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8031
diff
changeset
|
414 // Regularize a null matrix if stored into a struct component. |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8456
diff
changeset
|
415 map.assign (key, t_rhs.storable_value ()); |
3933 | 416 |
417 if (! error_state) | |
5759 | 418 { |
419 count++; | |
420 retval = octave_value (this); | |
421 } | |
3933 | 422 else |
423 gripe_failed_assignment (); | |
424 } | |
425 break; | |
426 | |
427 case '{': | |
428 gripe_invalid_index_type (type_name (), type[0]); | |
429 break; | |
430 | |
431 default: | |
432 panic_impossible (); | |
433 } | |
434 } | |
435 else | |
436 gripe_failed_assignment (); | |
437 | |
438 return retval; | |
2376 | 439 } |
440 | |
7046 | 441 octave_value |
442 octave_struct::do_index_op (const octave_value_list& idx, bool resize_ok) | |
443 { | |
444 octave_value retval; | |
445 | |
446 octave_idx_type n_idx = idx.length (); | |
447 | |
448 int nd = map.ndims (); | |
449 | |
450 switch (n_idx) | |
451 { | |
452 case 0: | |
453 retval = map; | |
454 break; | |
455 | |
456 case 1: | |
457 { | |
458 idx_vector i = idx (0).index_vector (); | |
459 | |
460 if (! error_state) | |
461 retval = map.index (i, resize_ok, Cell::resize_fill_value ()); | |
462 } | |
463 break; | |
464 | |
465 default: | |
466 { | |
467 if (n_idx == 2 && nd == 2) | |
468 { | |
469 idx_vector i = idx (0).index_vector (); | |
470 | |
471 if (! error_state) | |
472 { | |
473 idx_vector j = idx (1).index_vector (); | |
474 | |
475 if (! error_state) | |
476 retval = map.index (i, j, resize_ok, | |
477 Cell::resize_fill_value ()); | |
478 } | |
479 } | |
480 else | |
481 { | |
482 Array<idx_vector> idx_vec (n_idx); | |
483 | |
484 for (octave_idx_type i = 0; i < n_idx; i++) | |
485 { | |
486 idx_vec(i) = idx(i).index_vector (); | |
487 | |
488 if (error_state) | |
489 break; | |
490 } | |
491 | |
492 if (! error_state) | |
493 retval = map.index (idx_vec, resize_ok, | |
494 Cell::resize_fill_value ()); | |
495 } | |
496 } | |
497 break; | |
498 } | |
499 | |
500 return retval; | |
501 } | |
502 | |
4791 | 503 size_t |
504 octave_struct::byte_size (void) const | |
505 { | |
506 // Neglect the size of the fieldnames. | |
507 | |
508 size_t retval = 0; | |
509 | |
510 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) | |
511 { | |
512 std::string key = map.key (p); | |
513 | |
514 octave_value val = octave_value (map.contents (p)); | |
515 | |
516 retval += val.byte_size (); | |
517 } | |
518 | |
519 return retval; | |
520 } | |
521 | |
2376 | 522 void |
3523 | 523 octave_struct::print (std::ostream& os, bool) const |
2901 | 524 { |
525 print_raw (os); | |
526 } | |
527 | |
528 void | |
3523 | 529 octave_struct::print_raw (std::ostream& os, bool) const |
2376 | 530 { |
2985 | 531 unwind_protect::begin_frame ("octave_struct_print"); |
2376 | 532 |
533 unwind_protect_int (Vstruct_levels_to_print); | |
534 | |
3961 | 535 if (Vstruct_levels_to_print >= 0) |
2376 | 536 { |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
537 bool print_keys_only = Vstruct_levels_to_print-- == 0; |
3961 | 538 |
2901 | 539 indent (os); |
540 os << "{"; | |
541 newline (os); | |
2376 | 542 |
2901 | 543 increment_indent_level (); |
2376 | 544 |
5598 | 545 octave_idx_type n = map.numel (); |
3932 | 546 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
547 if (n != 1 || print_keys_only) |
4604 | 548 { |
549 indent (os); | |
550 dim_vector dv = dims (); | |
551 os << dv.str () << " struct array containing the fields:"; | |
552 newline (os); | |
553 newline (os); | |
554 | |
555 increment_indent_level (); | |
556 } | |
557 | |
5880 | 558 string_vector key_list = map.keys (); |
559 | |
560 for (octave_idx_type i = 0; i < key_list.length (); i++) | |
2376 | 561 { |
5880 | 562 std::string key = key_list[i]; |
563 | |
564 Cell val = map.contents (key); | |
2376 | 565 |
4499 | 566 octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); |
3961 | 567 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
568 if (n != 1 || print_keys_only) |
3932 | 569 { |
3961 | 570 indent (os); |
4604 | 571 os << key; |
572 if (n == 1) | |
573 { | |
574 dim_vector dv = tmp.dims (); | |
575 os << ": " << dv.str () << " " << tmp.type_name (); | |
576 } | |
3961 | 577 newline (os); |
3932 | 578 } |
3961 | 579 else |
4121 | 580 tmp.print_with_name (os, key); |
2376 | 581 } |
582 | |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
583 if (n != 1 || print_keys_only) |
4604 | 584 decrement_indent_level (); |
585 | |
2901 | 586 decrement_indent_level (); |
2376 | 587 |
2901 | 588 indent (os); |
589 os << "}"; | |
590 newline (os); | |
2376 | 591 } |
592 else | |
2901 | 593 { |
3961 | 594 indent (os); |
595 os << "<structure>"; | |
2901 | 596 newline (os); |
597 } | |
2376 | 598 |
2985 | 599 unwind_protect::run_frame ("octave_struct_print"); |
2376 | 600 } |
601 | |
2901 | 602 bool |
3523 | 603 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901 | 604 { |
3961 | 605 bool retval = false; |
606 | |
2901 | 607 indent (os); |
3961 | 608 |
609 if (Vstruct_levels_to_print < 0) | |
610 os << name << " = "; | |
611 else | |
612 { | |
613 os << name << " ="; | |
614 newline (os); | |
615 retval = true; | |
616 } | |
617 | |
618 return retval; | |
2901 | 619 } |
620 | |
4744 | 621 static bool |
622 scalar (const dim_vector& dims) | |
623 { | |
624 return dims.length () == 2 && dims (0) == 1 && dims (1) == 1; | |
625 } | |
626 | |
627 /* | |
628 %!shared x | |
629 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3; | |
630 %!assert(struct('a',1,'b',3),x(1)) | |
5592 | 631 %!assert(isempty(x([]))) |
632 %!assert(isempty(struct('a',{},'b',{}))) | |
4744 | 633 %!assert(struct('a',{1,2},'b',{3,3}),x) |
634 %!assert(struct('a',{1,2},'b',3),x) | |
635 %!assert(struct('a',{1,2},'b',{3}),x) | |
636 %!assert(struct('b',3,'a',{1,2}),x) | |
637 %!assert(struct('b',{3},'a',{1,2}),x) | |
638 %!test x=struct([]); | |
639 %!assert(size(x),[0,0]); | |
640 %!assert(isstruct(x)); | |
641 %!assert(isempty(fieldnames(x))); | |
642 %!fail("struct('a',{1,2},'b',{1,2,3})","dimensions of parameter 2 do not match those of parameter 4") | |
5582 | 643 %!fail("struct(1,2,3,4)","struct expects alternating \"field\", VALUE pairs"); |
644 %!fail("struct('1',2,'3')","struct expects alternating \"field\", VALUE pairs"); | |
4744 | 645 */ |
646 | |
647 DEFUN (struct, args, , | |
648 "-*- texinfo -*-\n\ | |
649 @deftypefn {Built-in Function} {} struct (\"field\", @var{value}, \"field\", @var{value}, @dots{})\n\ | |
650 \n\ | |
651 Create a structure and initialize its value.\n\ | |
652 \n\ | |
653 If the values are cell arrays, create a structure array and initialize\n\ | |
654 its values. The dimensions of each cell array of values must match.\n\ | |
655 Singleton cells and non-cell values are repeated so that they fill\n\ | |
656 the entire array. If the cells are empty, create an empty structure\n\ | |
4911 | 657 array with the specified field names.\n\ |
658 @end deftypefn") | |
4744 | 659 { |
6946 | 660 octave_value retval; |
4744 | 661 |
662 int nargin = args.length (); | |
663 | |
5444 | 664 // struct ([]) returns an empty struct. |
665 | |
666 // struct (empty_matrix) returns an empty struct with the same | |
667 // dimensions as the empty matrix. | |
668 | |
669 // Note that struct () creates a 1x1 struct with no fields for | |
670 // compatibility with Matlab. | |
4744 | 671 |
6946 | 672 if ((nargin == 1 || nargin == 2) |
673 && args(0).is_empty () && args(0).is_real_matrix ()) | |
674 { | |
675 Cell fields; | |
676 | |
677 if (nargin == 2) | |
678 { | |
679 if (args(1).is_cellstr ()) | |
680 retval = Octave_map (args(0).dims (), args(1).cell_value ()); | |
681 else | |
682 error ("struct: expecting cell array of field names as second argument"); | |
683 } | |
684 else | |
685 retval = Octave_map (args(0).dims ()); | |
686 | |
687 return retval; | |
688 } | |
4744 | 689 |
690 // Check for "field", VALUE pairs. | |
691 | |
692 for (int i = 0; i < nargin; i += 2) | |
693 { | |
694 if (! args(i).is_string () || i + 1 >= nargin) | |
695 { | |
696 error ("struct expects alternating \"field\", VALUE pairs"); | |
697 return retval; | |
698 } | |
699 } | |
700 | |
701 // Check that the dimensions of the values correspond. | |
702 | |
703 dim_vector dims (1, 1); | |
704 | |
705 int first_dimensioned_value = 0; | |
706 | |
707 for (int i = 1; i < nargin; i += 2) | |
708 { | |
709 if (args(i).is_cell ()) | |
710 { | |
711 dim_vector argdims (args(i).dims ()); | |
712 | |
713 if (! scalar (argdims)) | |
714 { | |
715 if (! first_dimensioned_value) | |
716 { | |
717 dims = argdims; | |
718 first_dimensioned_value = i + 1; | |
719 } | |
720 else if (dims != argdims) | |
721 { | |
722 error ("struct: dimensions of parameter %d do not match those of parameter %d", | |
723 first_dimensioned_value, i+1); | |
724 return retval; | |
725 } | |
726 } | |
727 } | |
728 } | |
729 | |
730 // Create the return value. | |
731 | |
732 Octave_map map (dims); | |
733 | |
734 for (int i = 0; i < nargin; i+= 2) | |
735 { | |
736 // Get key. | |
737 | |
738 std::string key (args(i).string_value ()); | |
739 | |
740 if (error_state) | |
741 return retval; | |
742 | |
6811 | 743 if (! valid_identifier (key)) |
744 { | |
745 error ("struct: invalid structure field name `%s'", key.c_str ()); | |
746 return retval; | |
747 } | |
748 | |
4744 | 749 // Value may be v, { v }, or { v1, v2, ... } |
750 // In the first two cases, we need to create a cell array of | |
751 // the appropriate dimensions filled with v. In the last case, | |
752 // the cell array has already been determined to be of the | |
753 // correct dimensions. | |
754 | |
755 if (args(i+1).is_cell ()) | |
756 { | |
757 const Cell c (args(i+1).cell_value ()); | |
758 | |
759 if (error_state) | |
760 return retval; | |
761 | |
762 if (scalar (c.dims ())) | |
763 map.assign (key, Cell (dims, c(0))); | |
764 else | |
765 map.assign (key, c); | |
766 } | |
767 else | |
768 map.assign (key, Cell (dims, args(i+1))); | |
769 | |
770 if (error_state) | |
771 return retval; | |
772 } | |
6946 | 773 |
4744 | 774 return octave_value (map); |
775 } | |
776 | |
4358 | 777 DEFUN (isstruct, args, , |
778 "-*- texinfo -*-\n\ | |
779 @deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ | |
780 Return 1 if the value of the expression @var{expr} is a structure.\n\ | |
781 @end deftypefn") | |
782 { | |
783 octave_value retval; | |
784 | |
785 if (args.length () == 1) | |
786 retval = args(0).is_map (); | |
787 else | |
5823 | 788 print_usage (); |
4358 | 789 |
790 return retval; | |
791 } | |
792 | |
793 DEFUN (fieldnames, args, , | |
794 "-*- texinfo -*-\n\ | |
795 @deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\ | |
796 Return a cell array of strings naming the elements of the structure\n\ | |
797 @var{struct}. It is an error to call @code{fieldnames} with an\n\ | |
798 argument that is not a structure.\n\ | |
799 @end deftypefn") | |
800 { | |
801 octave_value retval; | |
802 | |
803 int nargin = args.length (); | |
804 | |
805 if (nargin == 1) | |
806 { | |
7336 | 807 octave_value arg = args(0); |
808 | |
809 if (arg.is_map () || arg.is_object ()) | |
4358 | 810 { |
7336 | 811 Octave_map m = arg.map_value (); |
812 | |
4744 | 813 string_vector keys = m.keys (); |
7336 | 814 |
4744 | 815 if (keys.length () == 0) |
816 retval = Cell (0, 1); | |
817 else | |
818 retval = Cell (m.keys ()); | |
4358 | 819 } |
820 else | |
821 gripe_wrong_type_arg ("fieldnames", args(0)); | |
822 } | |
823 else | |
5823 | 824 print_usage (); |
4358 | 825 |
826 return retval; | |
827 } | |
828 | |
829 DEFUN (isfield, args, , | |
830 "-*- texinfo -*-\n\ | |
831 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ | |
832 Return true if the expression @var{expr} is a structure and it includes an\n\ | |
833 element named @var{name}. The first argument must be a structure and\n\ | |
834 the second must be a string.\n\ | |
835 @end deftypefn") | |
836 { | |
837 octave_value retval; | |
838 | |
839 int nargin = args.length (); | |
840 | |
841 if (nargin == 2) | |
842 { | |
843 retval = false; | |
844 | |
5775 | 845 // FIXME -- should this work for all types that can do |
4358 | 846 // structure reference operations? |
847 | |
848 if (args(0).is_map () && args(1).is_string ()) | |
849 { | |
850 std::string key = args(1).string_value (); | |
851 | |
852 Octave_map m = args(0).map_value (); | |
853 | |
854 retval = m.contains (key) != 0; | |
855 } | |
856 } | |
857 else | |
5823 | 858 print_usage (); |
4358 | 859 |
860 return retval; | |
861 } | |
862 | |
4750 | 863 // Check that the dimensions of the input arguments are correct. |
864 | |
865 static bool | |
866 cell2struct_check_args (const dim_vector& c_dv, const dim_vector& f_dv, | |
867 bool is_cell, int dim) | |
868 { | |
4751 | 869 bool retval = true; |
4750 | 870 |
871 if (dim >= 0 && dim < c_dv.length ()) | |
872 { | |
873 if (is_cell) | |
874 { | |
4752 | 875 if (f_dv.numel () != c_dv(dim)) |
4750 | 876 { |
4751 | 877 error ("cell2struct: numel (FIELD) != size (CELL, DIM)"); |
4750 | 878 |
879 retval = false; | |
880 } | |
881 } | |
882 else | |
883 { | |
884 if (f_dv.length () > 2) | |
885 { | |
4751 | 886 error ("cell2struct: field array must be a 2-d matrix"); |
4750 | 887 |
888 retval = false; | |
889 } | |
890 else if (f_dv(0) != c_dv(dim)) | |
891 { | |
4751 | 892 error ("cell2struct: size (FIELD, 1) != length (C, DIM)"); |
4750 | 893 |
894 retval = false; | |
895 } | |
896 } | |
897 } | |
898 else | |
899 { | |
900 error ("cell2struct: DIM out of range"); | |
901 | |
902 retval = false; | |
903 } | |
904 | |
905 return retval; | |
906 } | |
907 | |
908 static void | |
5275 | 909 cell2struct_construct_idx (Array<octave_idx_type>& ra_idx1, |
910 const Array<octave_idx_type>& ra_idx2, | |
911 octave_idx_type dim, octave_idx_type fill_value) | |
4750 | 912 { |
5275 | 913 octave_idx_type iidx = 0; |
4750 | 914 |
5275 | 915 for (octave_idx_type idx = 0; idx < ra_idx1.length (); idx++) |
4750 | 916 { |
917 if (idx == dim) | |
918 ra_idx1.elem (idx) = fill_value; | |
919 else | |
920 ra_idx1.elem (idx) = ra_idx2(iidx++); | |
921 } | |
922 } | |
923 | |
924 DEFUN (cell2struct, args, , | |
925 "-*- texinfo -*-\n\ | |
4817 | 926 @deftypefn {Built-in Function} {} cell2struct (@var{cell}, @var{fields}, @var{dim})\n\ |
927 Convert @var{cell} to a structure. The number of fields in @var{fields}\n\ | |
928 must match the number of elements in @var{cell} along dimension @var{dim},\n\ | |
929 that is @code{numel (@var{fields}) == size (@var{cell}, @var{dim})}.\n\ | |
4750 | 930 \n\ |
931 @example\n\ | |
932 @group\n\ | |
7031 | 933 A = cell2struct (@{'Peter', 'Hannah', 'Robert';\n\ |
934 185, 170, 168@},\n\ | |
6519 | 935 @{'Name','Height'@}, 1);\n\ |
4750 | 936 A(1)\n\ |
937 @result{} ans =\n\ | |
4780 | 938 @{\n\ |
939 Height = 185\n\ | |
940 Name = Peter\n\ | |
941 @}\n\ | |
4750 | 942 \n\ |
943 @end group\n\ | |
944 @end example\n\ | |
945 @end deftypefn") | |
946 { | |
947 octave_value retval; | |
948 | |
4751 | 949 if (args.length () == 3) |
4750 | 950 { |
4751 | 951 Cell c = args(0).cell_value (); |
4750 | 952 |
4751 | 953 if (! error_state) |
4750 | 954 { |
4751 | 955 octave_value field = args(1); |
4750 | 956 |
4751 | 957 // Field is either cell or character matrix. |
4750 | 958 |
5775 | 959 // FIXME -- this could be simplified if we had |
4752 | 960 // cellstr and iscellstr functions available. |
961 | |
4751 | 962 bool field_is_cell = field.is_cell (); |
4750 | 963 |
4751 | 964 Cell field_cell; |
965 charMatrix field_char; | |
4750 | 966 |
967 if (field_is_cell) | |
4751 | 968 field_cell = field.cell_value (); |
969 else | |
970 field_char = field.char_matrix_value (); | |
971 | |
972 if (! error_state) | |
4750 | 973 { |
4751 | 974 // Retrieve the dimension value. |
975 | |
5775 | 976 // FIXME -- int_value () should print out the |
4751 | 977 // conversions it does to be Matlab compatible. |
978 | |
5275 | 979 octave_idx_type dim = args(2).int_value () - 1; |
4751 | 980 |
981 if (! error_state) | |
982 { | |
983 dim_vector c_dv = c.dims (); | |
984 dim_vector field_dv = field.dims (); | |
985 | |
986 if (cell2struct_check_args (c_dv, field_dv, field_is_cell, | |
987 dim)) | |
988 { | |
5275 | 989 octave_idx_type c_dv_length = c_dv.length (); |
4751 | 990 |
991 // Dimension vector for the Cell arrays to be | |
992 // put into the structure. | |
993 | |
994 dim_vector value_dv; | |
995 | |
996 // Initialize c_value_dv. | |
4750 | 997 |
4751 | 998 if (c_dv_length == 2) |
999 value_dv = dim_vector (1, 1); | |
1000 else | |
1001 value_dv.resize (c_dv_length - 1); | |
1002 | |
5275 | 1003 octave_idx_type idx_tmp = 0; |
4751 | 1004 |
5275 | 1005 for (octave_idx_type i = 0; i < c_dv_length; i++) |
4751 | 1006 { |
1007 if (i != dim) | |
1008 value_dv.elem (idx_tmp++) = c_dv.elem (i); | |
1009 } | |
1010 | |
1011 // All initializing is done, we can start moving | |
1012 // values. | |
1013 | |
1014 Octave_map map; | |
1015 | |
1016 // If field is a cell array then we use all | |
1017 // elements in array, on the other hand when | |
1018 // field is a character array the number of | |
1019 // elements is equals the number of rows. | |
1020 | |
5275 | 1021 octave_idx_type field_numel |
4751 | 1022 = field_is_cell ? field_dv.numel (): field_dv(0); |
1023 | |
1024 // For matlab compatibility. | |
1025 | |
1026 if (field_numel == 0) | |
1027 map.reshape (dim_vector (0, 1)); | |
4750 | 1028 |
5275 | 1029 for (octave_idx_type i = 0; i < field_numel; i++) |
4751 | 1030 { |
1031 // Construct cell array which goes into the | |
1032 // structure together with the appropriate | |
1033 // field name. | |
1034 | |
1035 Cell c_value (value_dv); | |
1036 | |
5275 | 1037 Array<octave_idx_type> value_idx (value_dv.length (), 0); |
1038 Array<octave_idx_type> c_idx (c_dv_length, 0); | |
4751 | 1039 |
5275 | 1040 for (octave_idx_type j = 0; j < value_dv.numel (); j++) |
4751 | 1041 { |
1042 // Need to do this to construct the | |
1043 // appropriate idx for getting elements | |
1044 // from the original cell array. | |
1045 | |
1046 cell2struct_construct_idx (c_idx, value_idx, | |
1047 dim, i); | |
1048 | |
1049 c_value.elem (value_idx) = c.elem (c_idx); | |
1050 | |
1051 increment_index (value_idx, value_dv); | |
1052 } | |
1053 | |
1054 std::string field_str; | |
4750 | 1055 |
4751 | 1056 if (field_is_cell) |
1057 { | |
1058 // Matlab retrieves the field values | |
1059 // column by column. | |
1060 | |
1061 octave_value field_tmp = field_cell.elem (i); | |
1062 | |
1063 field_str = field_tmp.string_value (); | |
1064 | |
1065 if (error_state) | |
1066 { | |
1067 error ("cell2struct: fields have to be of type string"); | |
1068 break; | |
1069 } | |
1070 } | |
1071 else | |
1072 { | |
1073 field_str = field_char.row_as_string (i); | |
1074 | |
1075 if (error_state) | |
1076 return retval; | |
1077 } | |
1078 | |
6811 | 1079 if (! valid_identifier (field_str)) |
1080 { | |
1081 error ("cell2struct: invalid field name `%s'", | |
1082 field_str.c_str ()); | |
1083 break; | |
1084 } | |
1085 | |
4751 | 1086 map.reshape (value_dv); |
1087 | |
1088 map.assign (field_str, c_value); | |
1089 } | |
1090 | |
1091 if (! error_state) | |
1092 retval = map; | |
1093 } | |
4750 | 1094 } |
4751 | 1095 else |
1096 error ("cell2struct: expecting third argument to be an integer"); | |
4750 | 1097 } |
1098 else | |
4751 | 1099 error ("cell2struct: expecting second argument to be a cell or character array"); |
4750 | 1100 } |
4751 | 1101 else |
1102 error ("cell2struct: expecting first argument to be a cell array"); | |
4750 | 1103 } |
4751 | 1104 else |
5823 | 1105 print_usage (); |
4750 | 1106 |
1107 return retval; | |
1108 } | |
1109 | |
4817 | 1110 // So we can call Fcellstr directly. |
1111 extern octave_value_list Fcellstr (const octave_value_list& args, int); | |
1112 | |
1113 DEFUN (rmfield, args, , | |
1114 "-*- texinfo -*-\n\ | |
1115 @deftypefn {Built-in Function} {} rmfield (@var{s}, @var{f})\n\ | |
1116 Remove field @var{f} from the structure @var{s}. If @var{f} is a\n\ | |
1117 cell array of character strings or a character array, remove the\n\ | |
1118 named fields.\n\ | |
5642 | 1119 @seealso{cellstr, iscellstr, setfield}\n\ |
1120 @end deftypefn") | |
4817 | 1121 { |
1122 octave_value retval; | |
1123 | |
1124 int nargin = args.length (); | |
1125 | |
1126 if (nargin == 2) | |
1127 { | |
1128 Octave_map m = args(0).map_value (); | |
1129 | |
1130 octave_value_list fval = Fcellstr (args(1), 1); | |
1131 | |
1132 if (! error_state) | |
1133 { | |
1134 Cell fcell = fval(0).cell_value (); | |
1135 | |
1136 for (int i = 0; i < fcell.numel (); i++) | |
1137 { | |
1138 std::string key = fcell(i).string_value (); | |
1139 | |
1140 if (m.contains (key)) | |
1141 m.del (key); | |
1142 else | |
1143 { | |
1144 error ("rmfield: structure does not contain field %s", | |
1145 key.c_str ()); | |
1146 | |
1147 break; | |
1148 } | |
1149 } | |
1150 | |
1151 if (! error_state) | |
1152 retval = m; | |
1153 } | |
1154 } | |
1155 else | |
5823 | 1156 print_usage (); |
4817 | 1157 |
1158 return retval; | |
1159 } | |
1160 | |
1161 bool | |
6974 | 1162 octave_struct::save_ascii (std::ostream& os) |
4817 | 1163 { |
1164 Octave_map m = map_value (); | |
6639 | 1165 os << "# length: " << m.nfields () << "\n"; |
4817 | 1166 |
1167 Octave_map::iterator i = m.begin (); | |
1168 while (i != m.end ()) | |
1169 { | |
5341 | 1170 octave_value val = map.contents (i); |
4817 | 1171 |
6974 | 1172 bool b = save_ascii_data (os, val, m.key (i), false, 0); |
4817 | 1173 |
1174 if (! b) | |
1175 return os; | |
1176 | |
1177 i++; | |
1178 } | |
1179 | |
1180 return true; | |
1181 } | |
1182 | |
1183 bool | |
1184 octave_struct::load_ascii (std::istream& is) | |
1185 { | |
5275 | 1186 octave_idx_type len = 0; |
4817 | 1187 bool success = true; |
1188 | |
1189 if (extract_keyword (is, "length", len) && len >= 0) | |
1190 { | |
1191 if (len > 0) | |
1192 { | |
1193 Octave_map m (map); | |
1194 | |
5275 | 1195 for (octave_idx_type j = 0; j < len; j++) |
4817 | 1196 { |
1197 octave_value t2; | |
1198 bool dummy; | |
1199 | |
1200 // recurse to read cell elements | |
1201 std::string nm | |
5756 | 1202 = read_ascii_data (is, std::string (), dummy, t2, j); |
4817 | 1203 |
1204 if (!is) | |
1205 break; | |
1206 | |
6293 | 1207 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1208 |
6293 | 1209 if (error_state) |
1210 { | |
1211 error ("load: internal error loading struct elements"); | |
1212 return false; | |
1213 } | |
5433 | 1214 |
6293 | 1215 m.assign (nm, tcell); |
4817 | 1216 } |
1217 | |
1218 if (is) | |
1219 map = m; | |
1220 else | |
1221 { | |
1222 error ("load: failed to load structure"); | |
1223 success = false; | |
1224 } | |
1225 } | |
1226 else if (len == 0 ) | |
6292 | 1227 map = Octave_map (dim_vector (1, 1)); |
4817 | 1228 else |
1229 panic_impossible (); | |
1230 } | |
1231 else { | |
1232 error ("load: failed to extract number of elements in structure"); | |
1233 success = false; | |
1234 } | |
1235 | |
1236 return success; | |
1237 } | |
1238 | |
1239 bool | |
1240 octave_struct::save_binary (std::ostream& os, bool& save_as_floats) | |
1241 { | |
1242 Octave_map m = map_value (); | |
1243 | |
6639 | 1244 int32_t len = m.nfields (); |
5760 | 1245 os.write (reinterpret_cast<char *> (&len), 4); |
4817 | 1246 |
1247 Octave_map::iterator i = m.begin (); | |
1248 while (i != m.end ()) | |
1249 { | |
5341 | 1250 octave_value val = map.contents (i); |
4817 | 1251 |
5341 | 1252 bool b = save_binary_data (os, val, m.key (i), "", 0, save_as_floats); |
4817 | 1253 |
1254 if (! b) | |
1255 return os; | |
1256 | |
1257 i++; | |
1258 } | |
1259 | |
1260 return true; | |
1261 } | |
1262 | |
1263 bool | |
1264 octave_struct::load_binary (std::istream& is, bool swap, | |
5760 | 1265 oct_mach_info::float_format fmt) |
4817 | 1266 { |
1267 bool success = true; | |
5828 | 1268 int32_t len; |
5760 | 1269 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4817 | 1270 return false; |
1271 if (swap) | |
4944 | 1272 swap_bytes<4> (&len); |
4817 | 1273 |
1274 if (len > 0) | |
1275 { | |
1276 Octave_map m (map); | |
1277 | |
5275 | 1278 for (octave_idx_type j = 0; j < len; j++) |
4817 | 1279 { |
1280 octave_value t2; | |
1281 bool dummy; | |
1282 std::string doc; | |
1283 | |
1284 // recurse to read cell elements | |
1285 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
1286 dummy, t2, doc); | |
1287 | |
1288 if (!is) | |
1289 break; | |
1290 | |
6293 | 1291 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1292 |
6293 | 1293 if (error_state) |
1294 { | |
1295 error ("load: internal error loading struct elements"); | |
1296 return false; | |
1297 } | |
5433 | 1298 |
6293 | 1299 m.assign (nm, tcell); |
4817 | 1300 } |
1301 | |
1302 if (is) | |
1303 map = m; | |
1304 else | |
1305 { | |
1306 error ("load: failed to load structure"); | |
1307 success = false; | |
1308 } | |
1309 } | |
1310 else if (len == 0 ) | |
6292 | 1311 map = Octave_map (dim_vector (1, 1)); |
4817 | 1312 else |
1313 panic_impossible (); | |
1314 | |
1315 return success; | |
1316 } | |
1317 | |
1318 #if defined (HAVE_HDF5) | |
1319 | |
1320 bool | |
1321 octave_struct::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
1322 { | |
1323 hid_t data_hid = -1; | |
1324 | |
1325 data_hid = H5Gcreate (loc_id, name, 0); | |
1326 if (data_hid < 0) return false; | |
1327 | |
1328 // recursively add each element of the structure to this group | |
1329 Octave_map m = map_value (); | |
1330 Octave_map::iterator i = m.begin (); | |
1331 while (i != m.end ()) | |
1332 { | |
5341 | 1333 octave_value val = map.contents (i); |
4817 | 1334 |
5341 | 1335 bool retval2 = add_hdf5_data (data_hid, val, m.key (i), "", false, |
4817 | 1336 save_as_floats); |
1337 | |
1338 if (! retval2) | |
1339 break; | |
1340 | |
1341 i++; | |
1342 } | |
1343 | |
1344 H5Gclose (data_hid); | |
4837 | 1345 |
4817 | 1346 return true; |
1347 } | |
1348 | |
1349 bool | |
1350 octave_struct::load_hdf5 (hid_t loc_id, const char *name, | |
1351 bool have_h5giterate_bug) | |
1352 { | |
1353 bool retval = false; | |
1354 | |
1355 hdf5_callback_data dsub; | |
1356 | |
1357 herr_t retval2 = 0; | |
6292 | 1358 Octave_map m (dim_vector (1, 1)); |
4817 | 1359 int current_item = 0; |
1360 #ifdef HAVE_H5GGET_NUM_OBJS | |
1361 hsize_t num_obj = 0; | |
5060 | 1362 hid_t group_id = H5Gopen (loc_id, name); |
1363 H5Gget_num_objs (group_id, &num_obj); | |
1364 H5Gclose (group_id); | |
4817 | 1365 |
1366 while (current_item < static_cast<int> (num_obj) | |
1367 && (retval2 = H5Giterate (loc_id, name, ¤t_item, | |
1368 hdf5_read_next_data, &dsub)) > 0) | |
1369 #else | |
1370 while ((retval2 = H5Giterate (loc_id, name, ¤t_item, | |
1371 hdf5_read_next_data, &dsub)) > 0) | |
1372 #endif | |
1373 { | |
5342 | 1374 octave_value t2 = dsub.tc; |
1375 | |
6293 | 1376 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1377 |
6293 | 1378 if (error_state) |
1379 { | |
1380 error ("load: internal error loading struct elements"); | |
1381 return false; | |
1382 } | |
5433 | 1383 |
6293 | 1384 m.assign (dsub.name, tcell); |
5336 | 1385 |
4817 | 1386 if (have_h5giterate_bug) |
1387 current_item++; // H5Giterate returned the last index processed | |
1388 } | |
1389 | |
1390 if (retval2 >= 0) | |
1391 { | |
1392 map = m; | |
1393 retval = true; | |
1394 } | |
1395 | |
1396 return retval; | |
1397 } | |
1398 | |
4687 | 1399 #endif |
1400 | |
5900 | 1401 mxArray * |
1402 octave_struct::as_mxArray (void) const | |
1403 { | |
1404 int nf = nfields (); | |
1405 string_vector kv = map_keys (); | |
6065 | 1406 |
1407 OCTAVE_LOCAL_BUFFER (const char *, f, nf); | |
1408 | |
5900 | 1409 for (int i = 0; i < nf; i++) |
6065 | 1410 f[i] = kv[i].c_str (); |
5900 | 1411 |
1412 mxArray *retval = new mxArray (dims (), nf, f); | |
1413 | |
1414 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); | |
1415 | |
6686 | 1416 mwSize nel = numel (); |
5900 | 1417 |
6686 | 1418 mwSize ntot = nf * nel; |
5900 | 1419 |
1420 for (int i = 0; i < nf; i++) | |
1421 { | |
1422 Cell c = map.contents (kv[i]); | |
1423 | |
1424 const octave_value *p = c.data (); | |
1425 | |
6686 | 1426 mwIndex k = 0; |
1427 for (mwIndex j = i; j < ntot; j += nf) | |
5900 | 1428 elts[j] = new mxArray (p[k++]); |
1429 } | |
1430 | |
1431 return retval; | |
1432 } | |
1433 | |
2376 | 1434 /* |
1435 ;;; Local Variables: *** | |
1436 ;;; mode: C++ *** | |
1437 ;;; End: *** | |
1438 */ |