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