Mercurial > octave-nkf
annotate src/ov-list.cc @ 12119:e320928eeb3a release-3-2-x release-3-2-4
version 3.2.4
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 22 Jan 2010 12:43:12 +0100 |
parents | c2248cc4821a |
children | 610bf90fce2a |
rev | line source |
---|---|
2882 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
8920 | 4 2007, 2008, 2009 John W. Eaton |
2882 | 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. | |
2882 | 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/>. | |
2882 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3503 | 28 #include <iostream> |
5765 | 29 #include <sstream> |
2882 | 30 |
31 #include "lo-utils.h" | |
32 | |
4513 | 33 #include "Cell.h" |
2882 | 34 #include "defun.h" |
35 #include "error.h" | |
4994 | 36 #include "ov.h" |
2882 | 37 #include "ov-list.h" |
38 #include "unwind-prot.h" | |
39 | |
4687 | 40 #include "byte-swap.h" |
41 #include "ls-oct-ascii.h" | |
42 #include "ls-oct-binary.h" | |
43 #include "ls-hdf5.h" | |
44 #include "ls-utils.h" | |
45 | |
3219 | 46 DEFINE_OCTAVE_ALLOCATOR (octave_list); |
2882 | 47 |
4612 | 48 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_list, "list", "list"); |
2882 | 49 |
4513 | 50 octave_list::octave_list (const Cell& c) |
4591 | 51 : octave_base_value (), data () |
4513 | 52 { |
5275 | 53 octave_idx_type n = c.length (); |
4513 | 54 |
4591 | 55 data.resize (dim_vector (1, n)); |
4513 | 56 |
5275 | 57 for (octave_idx_type i = 0; i < n; i++) |
4591 | 58 data(i) = c(i); |
4513 | 59 } |
60 | |
4994 | 61 octave_value_list |
4247 | 62 octave_list::subsref (const std::string& type, |
4994 | 63 const std::list<octave_value_list>& idx, int nargout) |
3933 | 64 { |
4994 | 65 octave_value_list retval; |
3933 | 66 |
67 switch (type[0]) | |
68 { | |
69 case '(': | |
70 { | |
71 octave_value_list tmp_idx = idx.front (); | |
72 | |
73 if (tmp_idx.length () == 1) | |
74 { | |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
75 Cell tmp = data.index (tmp_idx (0)); |
4842 | 76 |
4994 | 77 retval(0) = octave_value (new octave_list (tmp)); |
3933 | 78 } |
79 else | |
80 error ("only one index allowed for lists"); | |
81 } | |
82 break; | |
83 | |
84 case '{': | |
85 { | |
86 octave_value_list tmp_idx = idx.front (); | |
87 | |
88 if (tmp_idx.length () == 1) | |
89 { | |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
90 Cell tmp = data.index (tmp_idx (0)); |
3933 | 91 |
92 if (tmp.length () == 1) | |
4994 | 93 retval(0) = tmp(0); |
4842 | 94 else |
4994 | 95 retval(0) = octave_value (tmp, true); |
3933 | 96 } |
97 else | |
98 error ("only one index allowed for lists"); | |
99 } | |
100 break; | |
101 | |
102 case '.': | |
103 { | |
104 std::string nm = type_name (); | |
105 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
106 } | |
107 break; | |
108 | |
109 default: | |
110 panic_impossible (); | |
111 } | |
112 | |
5775 | 113 // FIXME -- perhaps there should be an |
4994 | 114 // octave_value_list::next_subsref member function? See also |
115 // octave_user_function::subsref. | |
116 | |
117 if (idx.size () > 1) | |
118 retval = retval(0).next_subsref (nargout, type, idx); | |
119 | |
120 return retval; | |
3933 | 121 } |
122 | |
123 octave_value | |
5885 | 124 octave_list::do_index_op (const octave_value_list& idx, bool resize_ok) |
2882 | 125 { |
126 octave_value retval; | |
127 | |
128 if (idx.length () == 1) | |
129 { | |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
130 Cell tcell = data.index (idx (0), resize_ok); |
5757 | 131 |
132 octave_value_list result; | |
133 | |
134 octave_idx_type n = tcell.numel (); | |
135 | |
136 result.resize (n); | |
137 | |
138 for (octave_idx_type i = 0; i < n; i++) | |
139 result(i) = tcell(i); | |
140 | |
141 retval = result; | |
2882 | 142 } |
143 else | |
5757 | 144 error ("only one index allowed for lists"); |
2882 | 145 |
146 return retval; | |
147 } | |
148 | |
3933 | 149 octave_value |
4247 | 150 octave_list::subsasgn (const std::string& type, |
4219 | 151 const std::list<octave_value_list>& idx, |
3933 | 152 const octave_value& rhs) |
153 { | |
154 octave_value retval; | |
155 | |
5275 | 156 octave_idx_type n = type.length (); |
3933 | 157 |
158 octave_value t_rhs = rhs; | |
159 | |
9286
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9064
diff
changeset
|
160 if (idx.front ().empty ()) |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9064
diff
changeset
|
161 { |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9064
diff
changeset
|
162 error ("missing index in indexed assignment"); |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9064
diff
changeset
|
163 return retval; |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9064
diff
changeset
|
164 } |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9064
diff
changeset
|
165 |
3933 | 166 if (n > 1) |
167 { | |
168 switch (type[0]) | |
169 { | |
170 case '(': | |
171 { | |
172 octave_value tmp = do_index_op (idx.front (), true); | |
173 | |
174 if (! tmp.is_defined ()) | |
175 tmp = octave_value::empty_conv (type.substr (1), rhs); | |
176 | |
177 if (! error_state) | |
178 { | |
4219 | 179 std::list<octave_value_list> next_idx (idx); |
3933 | 180 |
4219 | 181 next_idx.erase (next_idx.begin ()); |
3933 | 182 |
183 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | |
184 } | |
185 } | |
186 break; | |
187 | |
188 case '{': | |
189 case '.': | |
190 { | |
191 std::string nm = type_name (); | |
192 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
193 } | |
194 break; | |
195 | |
196 default: | |
197 panic_impossible (); | |
198 } | |
199 } | |
200 | |
3940 | 201 if (! error_state) |
3933 | 202 { |
3940 | 203 switch (type[0]) |
204 { | |
205 case '(': | |
206 { | |
207 octave_value_list i = idx.front (); | |
3933 | 208 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
209 data.assign (i, Cell (t_rhs), Cell::resize_fill_value ()); |
3933 | 210 |
5759 | 211 count++; |
212 retval = octave_value (this); | |
3940 | 213 } |
214 break; | |
3933 | 215 |
3940 | 216 case '{': |
217 case '.': | |
218 { | |
219 std::string nm = type_name (); | |
220 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
221 } | |
222 break; | |
3933 | 223 |
3940 | 224 default: |
225 panic_impossible (); | |
226 } | |
3933 | 227 } |
228 | |
229 return retval; | |
230 } | |
231 | |
2882 | 232 void |
3196 | 233 octave_list::assign (const octave_value_list& idx, const octave_value& rhs) |
234 { | |
235 if (idx.length () == 1) | |
236 { | |
3202 | 237 int i = idx(0).int_value (true); |
3196 | 238 |
239 if (! error_state) | |
240 { | |
5275 | 241 octave_idx_type n = data.length (); |
3196 | 242 |
4461 | 243 if (i > 0) |
244 { | |
5781 | 245 if (i > n) |
246 warning_with_id ("Octave:resize-on-range-error", | |
247 "list index = %d out of range", i); | |
4461 | 248 |
4591 | 249 data(i-1) = rhs; |
4461 | 250 } |
3196 | 251 else |
3202 | 252 error ("list index = %d out of range", i); |
3196 | 253 } |
3202 | 254 else |
255 error ("list index must be an integer"); | |
3196 | 256 } |
257 else | |
258 error ("lists may only be indexed by a single scalar"); | |
259 } | |
260 | |
4791 | 261 size_t |
262 octave_list::byte_size (void) const | |
263 { | |
264 size_t retval = 0; | |
265 | |
5275 | 266 for (octave_idx_type i = 0; i < numel (); i++) |
4791 | 267 retval += data(i).byte_size (); |
268 | |
269 return retval; | |
270 } | |
271 | |
4591 | 272 octave_value_list |
273 octave_list::list_value (void) const | |
274 { | |
275 octave_value_list retval; | |
276 | |
5275 | 277 octave_idx_type n = data.length (); |
4591 | 278 |
279 retval.resize (n); | |
280 | |
5275 | 281 for (octave_idx_type i = 0; i < n; i++) |
4591 | 282 retval(i) = data(i); |
283 | |
284 return retval; | |
285 } | |
286 | |
3196 | 287 void |
3523 | 288 octave_list::print (std::ostream& os, bool) const |
2901 | 289 { |
290 print_raw (os); | |
291 } | |
292 | |
293 void | |
3523 | 294 octave_list::print_raw (std::ostream& os, bool) const |
2882 | 295 { |
2985 | 296 unwind_protect::begin_frame ("octave_list_print"); |
2882 | 297 |
5275 | 298 octave_idx_type n = data.length (); |
2882 | 299 |
3196 | 300 if (n > 0) |
2882 | 301 { |
3196 | 302 indent (os); |
303 os << "("; | |
304 newline (os); | |
305 | |
306 increment_indent_level (); | |
2916 | 307 |
5275 | 308 for (octave_idx_type i = 0; i < n; i++) |
3196 | 309 { |
5765 | 310 std::ostringstream buf; |
4051 | 311 |
5765 | 312 buf << "[" << i+1 << "]"; |
2882 | 313 |
4591 | 314 octave_value val = data(i); |
2916 | 315 |
5765 | 316 val.print_with_name (os, buf.str ()); |
3196 | 317 } |
2882 | 318 |
3196 | 319 decrement_indent_level (); |
2882 | 320 |
3196 | 321 indent (os); |
322 os << ")"; | |
323 } | |
324 else | |
325 os << "()"; | |
326 | |
2901 | 327 newline (os); |
2882 | 328 |
2985 | 329 unwind_protect::run_frame ("octave_list_print"); |
2882 | 330 } |
331 | |
2901 | 332 bool |
3523 | 333 octave_list::print_name_tag (std::ostream& os, const std::string& name) const |
2901 | 334 { |
335 indent (os); | |
4591 | 336 if (data.length () == 0) |
3196 | 337 os << name << " = "; |
338 else | |
339 { | |
340 os << name << " ="; | |
341 newline (os); | |
342 } | |
2901 | 343 return false; |
344 } | |
345 | |
2993 | 346 DEFUN (list, args, , |
3447 | 347 "-*- texinfo -*-\n\ |
3448 | 348 @deftypefn {Built-in Function} {} list (@var{a1}, @var{a2}, @dots{})\n\ |
3447 | 349 Create a new list with elements given by the arguments @var{a1},\n\ |
350 @var{a2}, @dots{}.\n\ | |
351 @end deftypefn") | |
2882 | 352 { |
4591 | 353 static bool warned = false; |
354 | |
355 if (! warned) | |
356 { | |
357 warning ("list objects are deprecated; use cell arrays instead"); | |
358 warned = true; | |
359 } | |
360 | |
2882 | 361 return octave_value (args); |
362 } | |
363 | |
3219 | 364 DEFUN (nth, args, , |
3447 | 365 "-*- texinfo -*-\n\ |
366 @deftypefn {Built-in Function} {} nth (@var{list}, @var{n})\n\ | |
367 Return the @var{n}-th element of @var{list}.\n\ | |
368 @end deftypefn") | |
3219 | 369 { |
370 octave_value retval; | |
371 | |
372 if (args.length () == 2) | |
373 { | |
374 octave_value_list lst = args(0).list_value (); | |
375 | |
376 if (! error_state) | |
377 { | |
378 int n = args(1).int_value (true); | |
379 | |
380 if (! error_state) | |
381 { | |
382 if (n > 0 && n <= lst.length ()) | |
383 retval = lst(n-1); | |
384 else | |
385 error ("nth: index = %d out of range", n); | |
386 } | |
387 else | |
388 error ("nth: second argument must be an integer"); | |
389 } | |
390 else | |
391 error ("nth: first argument must be a list"); | |
392 } | |
393 else | |
5823 | 394 print_usage (); |
3219 | 395 |
396 return retval; | |
397 } | |
398 | |
2882 | 399 DEFUN (append, args, , |
3447 | 400 "-*- texinfo -*-\n\ |
401 @deftypefn {Built-in Function} {} append (@var{list}, @var{a1}, @var{a2}, @dots{})\n\ | |
6666 | 402 Return a new list created by appending @var{a1}, @var{a2}, @dots{}, to\n\ |
3447 | 403 @var{list}. If any of the arguments to be appended is a list, its\n\ |
404 elements are appended individually. For example,\n\ | |
2882 | 405 \n\ |
3447 | 406 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
407 @group\n\ |
3447 | 408 x = list (1, 2);\n\ |
409 y = list (3, 4);\n\ | |
410 append (x, y);\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
411 @end group\n\ |
3447 | 412 @end example\n\ |
3219 | 413 \n\ |
3447 | 414 @noindent\n\ |
415 results in the list containing the four elements @samp{(1 2 3 4)}, not\n\ | |
416 a list containing the three elements @samp{(1 2 (3 4))}.\n\ | |
417 @end deftypefn") | |
2882 | 418 { |
419 octave_value retval; | |
420 | |
421 int nargin = args.length (); | |
422 | |
423 if (nargin > 1) | |
424 { | |
425 octave_value_list tmp = args(0).list_value (); | |
426 | |
427 if (! error_state) | |
428 { | |
429 for (int i = 1; i < nargin; i++) | |
3219 | 430 { |
431 octave_value ov = args(i); | |
432 | |
433 if (ov.is_list ()) | |
434 tmp.append (ov.list_value ()); | |
435 else | |
436 tmp.append (ov); | |
437 } | |
2882 | 438 |
4233 | 439 retval = octave_value (tmp); |
2882 | 440 } |
441 } | |
442 else | |
5823 | 443 print_usage (); |
2882 | 444 |
445 return retval; | |
446 } | |
447 | |
448 DEFUN (reverse, args, , | |
3447 | 449 "-*- texinfo -*-\n\ |
450 @deftypefn {Built-in Function} {} reverse (@var{list})\n\ | |
451 Return a new list created by reversing the elements of @var{list}.\n\ | |
452 @end deftypefn") | |
2882 | 453 { |
454 octave_value retval; | |
455 | |
456 int nargin = args.length (); | |
457 | |
458 if (nargin == 1) | |
459 { | |
460 octave_value_list tmp = args(0).list_value (); | |
461 | |
462 if (! error_state) | |
4233 | 463 retval = octave_value (tmp.reverse ()); |
2882 | 464 } |
465 else | |
5823 | 466 print_usage (); |
2882 | 467 |
468 return retval; | |
469 } | |
470 | |
3196 | 471 DEFUN (splice, args, , |
3447 | 472 "-*- texinfo -*-\n\ |
473 @deftypefn {Built-in Function} {} splice (@var{list_1}, @var{offset}, @var{length}, @var{list_2})\n\ | |
474 Replace @var{length} elements of @var{list_1} beginning at\n\ | |
475 @var{offset} with the contents of @var{list_2} (if any). If\n\ | |
476 @var{length} is omitted, all elements from @var{offset} to the end of\n\ | |
477 @var{list_1} are replaced. As a special case, if @var{offset} is one\n\ | |
478 greater than the length of @var{list_1} and @var{length} is 0, splice\n\ | |
3448 | 479 is equivalent to @code{append (@var{list_1}, @var{list_2})}.\n\ |
3447 | 480 @end deftypefn") |
3196 | 481 { |
482 octave_value retval; | |
483 | |
484 int nargin = args.length (); | |
485 | |
486 if (nargin > 1 && nargin < 5) | |
487 { | |
488 octave_value_list list_1 = args(0).list_value (); | |
489 | |
490 if (! error_state) | |
491 { | |
3202 | 492 int offset = args(1).int_value (true); |
3196 | 493 |
494 if (! error_state) | |
495 { | |
3202 | 496 offset--; |
497 | |
498 int length = 0; | |
499 | |
500 octave_value_list list_2; | |
3196 | 501 |
3202 | 502 if (nargin < 3) |
503 length = list_1.length () - offset; | |
504 else | |
505 { | |
506 length = args(2).int_value (true); | |
3196 | 507 |
3202 | 508 if (! error_state) |
3196 | 509 { |
3202 | 510 if (nargin == 4) |
511 { | |
512 list_2 = args(3).list_value (); | |
3196 | 513 |
3202 | 514 if (error_state) |
515 error ("splice: fourth argument must be a list"); | |
3196 | 516 } |
517 } | |
3202 | 518 else |
519 error ("splice: LENGTH must be an integer"); | |
520 } | |
3196 | 521 |
3202 | 522 if (! error_state) |
4233 | 523 retval = octave_value (list_1.splice (offset, length, list_2)); |
3196 | 524 } |
525 else | |
526 error ("splice: OFFSET must be an integer"); | |
527 } | |
528 else | |
529 error ("splice: first argument must be a list"); | |
530 } | |
531 else | |
5823 | 532 print_usage (); |
3196 | 533 |
534 return retval; | |
535 } | |
536 | |
4687 | 537 bool |
6974 | 538 octave_list::save_ascii (std::ostream& os) |
4687 | 539 { |
540 octave_value_list lst = list_value (); | |
541 os << "# length: " << lst.length () << "\n"; | |
542 | |
543 for (int i = 0; i < lst.length (); ++i) | |
544 { | |
545 // should we use lst.name_tags () to label the elements? | |
5717 | 546 |
5765 | 547 std::ostringstream buf; |
548 buf << "_" << i; | |
549 std::string s = buf.str (); | |
5717 | 550 |
6974 | 551 bool b = save_ascii_data (os, lst (i), s.c_str (), false, 0); |
4687 | 552 |
553 if (! b) | |
554 return false; | |
555 } | |
556 | |
557 return true; | |
558 } | |
559 | |
560 bool | |
561 octave_list::load_ascii (std::istream& is) | |
562 { | |
563 int len = 0; | |
564 bool success = true; | |
565 | |
566 if (extract_keyword (is, "length", len) && len >= 0) | |
567 { | |
568 if (len > 0) | |
569 { | |
570 octave_value_list lst; | |
571 | |
572 for (int j = 0; j < len; j++) | |
573 { | |
574 octave_value t2; | |
575 bool dummy; | |
576 | |
577 // recurse to read list elements | |
578 std::string nm | |
5757 | 579 = read_ascii_data (is, std::string (), dummy, t2, j); |
4687 | 580 |
581 if (!is) | |
582 break; | |
583 | |
584 lst.append (t2); | |
585 } | |
586 | |
587 if (is) | |
588 data = lst; | |
589 else | |
590 { | |
591 error ("load: failed to load list"); | |
592 success = false; | |
593 } | |
594 } | |
595 else if (len == 0 ) | |
596 data = Cell (0, 0); | |
597 else | |
598 panic_impossible (); | |
599 } | |
600 else { | |
601 error ("load: failed to extract number of elements in list"); | |
602 success = false; | |
603 } | |
604 | |
605 return success; | |
606 } | |
607 | |
608 bool | |
609 octave_list::save_binary (std::ostream& os, bool& save_as_floats) | |
610 { | |
611 octave_value_list lst = list_value (); | |
612 | |
5828 | 613 int32_t len = lst.length(); |
5760 | 614 os.write (reinterpret_cast<char *> (&len), 4); |
4687 | 615 |
616 for (int i = 0; i < lst.length (); i++) | |
617 { | |
618 // should we use lst.name_tags () to label the elements? | |
5717 | 619 |
5765 | 620 std::ostringstream buf; |
621 buf << "_" << i; | |
622 std::string s = buf.str (); | |
4687 | 623 |
624 // Recurse to print sub-value. | |
5717 | 625 bool b = save_binary_data (os, lst(i), s.c_str (), "", 0, |
626 save_as_floats); | |
4687 | 627 |
628 if (! b) | |
629 return false; | |
630 } | |
631 | |
632 return true; | |
633 } | |
634 | |
635 bool | |
636 octave_list::load_binary (std::istream& is, bool swap, | |
5760 | 637 oct_mach_info::float_format fmt) |
4687 | 638 { |
5828 | 639 int32_t len; |
5760 | 640 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4687 | 641 return false; |
642 if (swap) | |
4944 | 643 swap_bytes<4> (&len); |
4687 | 644 |
645 if (len > 0) | |
646 { | |
647 octave_value_list lst; | |
648 | |
5275 | 649 for (octave_idx_type i = 0; i < len; i++) |
4687 | 650 { |
651 octave_value t2; | |
652 bool dummy; | |
653 std::string doc; | |
654 | |
655 // recurse to read list elements | |
656 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
657 dummy, t2, doc); | |
658 | |
659 if (!is) | |
660 return false; | |
661 | |
662 lst.append(t2); | |
663 } | |
664 | |
665 if (is) | |
666 data = lst; | |
667 else | |
668 { | |
669 error ("load: failed to load list"); | |
670 return false; | |
671 } | |
672 | |
673 } | |
674 else if (len == 0 ) | |
675 data = Cell (0, 0); | |
676 else | |
677 return false; | |
678 | |
679 return true; | |
680 } | |
681 | |
682 #if defined (HAVE_HDF5) | |
4944 | 683 |
4687 | 684 bool |
685 octave_list::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
686 { | |
687 hid_t data_hid = -1; | |
688 | |
689 data_hid = H5Gcreate (loc_id, name, 0); | |
690 if (data_hid < 0) return false; | |
691 | |
692 // recursively add each element of the list to this group | |
693 octave_value_list lst = list_value (); | |
694 | |
5275 | 695 for (octave_idx_type i = 0; i < lst.length (); ++i) |
4687 | 696 { |
697 // should we use lst.name_tags () to label the elements? | |
5717 | 698 |
5765 | 699 std::ostringstream buf; |
700 buf << "_" << i; | |
701 std::string s = buf.str (); | |
5717 | 702 |
703 bool retval2 = add_hdf5_data (data_hid, lst (i), s.c_str (), "", | |
4687 | 704 false, save_as_floats); |
705 if (! retval2) | |
706 break; | |
707 } | |
708 | |
709 H5Gclose (data_hid); | |
4837 | 710 |
4687 | 711 return true; |
712 } | |
713 | |
714 bool | |
715 octave_list::load_hdf5 (hid_t loc_id, const char *name, | |
716 bool have_h5giterate_bug) | |
717 { | |
718 bool retval = false; | |
719 | |
720 hdf5_callback_data dsub; | |
721 | |
4696 | 722 herr_t retval2 = -1; |
4687 | 723 octave_value_list lst; |
724 int current_item = 0; | |
4696 | 725 #ifdef HAVE_H5GGET_NUM_OBJS |
726 hsize_t num_obj = 0; | |
5060 | 727 hid_t group_id = H5Gopen (loc_id, name); |
728 H5Gget_num_objs (group_id, &num_obj); | |
729 H5Gclose (group_id); | |
4696 | 730 |
731 while (current_item < static_cast<int> (num_obj) | |
732 && (retval2 = H5Giterate (loc_id, name, ¤t_item, | |
733 hdf5_read_next_data, &dsub)) > 0) | |
734 #else | |
4687 | 735 while ((retval2 = H5Giterate (loc_id, name, ¤t_item, |
736 hdf5_read_next_data, &dsub)) > 0) | |
4696 | 737 #endif |
4687 | 738 { |
739 lst.append (dsub.tc); | |
740 | |
741 if (have_h5giterate_bug) | |
742 current_item++; // H5Giterate returned the last index processed | |
743 } | |
744 | |
745 if (retval2 >= 0) | |
746 { | |
747 data = lst; | |
748 retval = true; | |
749 } | |
750 | |
751 return retval; | |
752 } | |
4944 | 753 |
4687 | 754 #endif |
755 | |
2882 | 756 /* |
757 ;;; Local Variables: *** | |
758 ;;; mode: C++ *** | |
759 ;;; End: *** | |
760 */ |