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 |
3940
|
189 assign (i, t_rhs); |
3933
|
190 |
3940
|
191 retval = octave_value (this, count + 1); |
|
192 } |
|
193 break; |
3933
|
194 |
3940
|
195 case '{': |
|
196 case '.': |
|
197 { |
|
198 std::string nm = type_name (); |
|
199 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
200 } |
|
201 break; |
3933
|
202 |
3940
|
203 default: |
|
204 panic_impossible (); |
|
205 } |
3933
|
206 } |
|
207 |
|
208 return retval; |
|
209 } |
|
210 |
2882
|
211 void |
3196
|
212 octave_list::assign (const octave_value_list& idx, const octave_value& rhs) |
|
213 { |
|
214 if (idx.length () == 1) |
|
215 { |
3202
|
216 int i = idx(0).int_value (true); |
3196
|
217 |
|
218 if (! error_state) |
|
219 { |
4591
|
220 int n = data.length (); |
3196
|
221 |
4461
|
222 if (i > 0) |
|
223 { |
|
224 if (Vwarn_resize_on_range_error && i > n) |
|
225 warning ("list index = %d out of range", i); |
|
226 |
4591
|
227 data(i-1) = rhs; |
4461
|
228 } |
3196
|
229 else |
3202
|
230 error ("list index = %d out of range", i); |
3196
|
231 } |
3202
|
232 else |
|
233 error ("list index must be an integer"); |
3196
|
234 } |
|
235 else |
|
236 error ("lists may only be indexed by a single scalar"); |
|
237 } |
|
238 |
4591
|
239 octave_value_list |
|
240 octave_list::list_value (void) const |
|
241 { |
|
242 octave_value_list retval; |
|
243 |
|
244 int n = data.length (); |
|
245 |
|
246 retval.resize (n); |
|
247 |
|
248 for (int i = 0; i < n; i++) |
|
249 retval(i) = data(i); |
|
250 |
|
251 return retval; |
|
252 } |
|
253 |
3196
|
254 void |
3523
|
255 octave_list::print (std::ostream& os, bool) const |
2901
|
256 { |
|
257 print_raw (os); |
|
258 } |
|
259 |
|
260 void |
3523
|
261 octave_list::print_raw (std::ostream& os, bool) const |
2882
|
262 { |
2985
|
263 unwind_protect::begin_frame ("octave_list_print"); |
2882
|
264 |
4591
|
265 int n = data.length (); |
2882
|
266 |
3196
|
267 if (n > 0) |
2882
|
268 { |
3196
|
269 indent (os); |
|
270 os << "("; |
|
271 newline (os); |
|
272 |
|
273 increment_indent_level (); |
2916
|
274 |
3196
|
275 for (int i = 0; i < n; i++) |
|
276 { |
4051
|
277 OSSTREAM buf; |
|
278 |
|
279 buf << "[" << i+1 << "]" << OSSTREAM_ENDS; |
2882
|
280 |
4591
|
281 octave_value val = data(i); |
2916
|
282 |
4051
|
283 val.print_with_name (os, OSSTREAM_STR (buf)); |
3196
|
284 |
4051
|
285 OSSTREAM_FREEZE (buf); |
3196
|
286 } |
2882
|
287 |
3196
|
288 decrement_indent_level (); |
2882
|
289 |
3196
|
290 indent (os); |
|
291 os << ")"; |
|
292 } |
|
293 else |
|
294 os << "()"; |
|
295 |
2901
|
296 newline (os); |
2882
|
297 |
2985
|
298 unwind_protect::run_frame ("octave_list_print"); |
2882
|
299 } |
|
300 |
2901
|
301 bool |
3523
|
302 octave_list::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
303 { |
|
304 indent (os); |
4591
|
305 if (data.length () == 0) |
3196
|
306 os << name << " = "; |
|
307 else |
|
308 { |
|
309 os << name << " ="; |
|
310 newline (os); |
|
311 } |
2901
|
312 return false; |
|
313 } |
|
314 |
2993
|
315 DEFUN (list, args, , |
3447
|
316 "-*- texinfo -*-\n\ |
3448
|
317 @deftypefn {Built-in Function} {} list (@var{a1}, @var{a2}, @dots{})\n\ |
3447
|
318 Create a new list with elements given by the arguments @var{a1},\n\ |
|
319 @var{a2}, @dots{}.\n\ |
|
320 @end deftypefn") |
2882
|
321 { |
4591
|
322 static bool warned = false; |
|
323 |
|
324 if (! warned) |
|
325 { |
|
326 warning ("list objects are deprecated; use cell arrays instead"); |
|
327 warned = true; |
|
328 } |
|
329 |
2882
|
330 return octave_value (args); |
|
331 } |
|
332 |
3219
|
333 DEFUN (nth, args, , |
3447
|
334 "-*- texinfo -*-\n\ |
|
335 @deftypefn {Built-in Function} {} nth (@var{list}, @var{n})\n\ |
|
336 Return the @var{n}-th element of @var{list}.\n\ |
|
337 @end deftypefn") |
3219
|
338 { |
|
339 octave_value retval; |
|
340 |
|
341 if (args.length () == 2) |
|
342 { |
|
343 octave_value_list lst = args(0).list_value (); |
|
344 |
|
345 if (! error_state) |
|
346 { |
|
347 int n = args(1).int_value (true); |
|
348 |
|
349 if (! error_state) |
|
350 { |
|
351 if (n > 0 && n <= lst.length ()) |
|
352 retval = lst(n-1); |
|
353 else |
|
354 error ("nth: index = %d out of range", n); |
|
355 } |
|
356 else |
|
357 error ("nth: second argument must be an integer"); |
|
358 } |
|
359 else |
|
360 error ("nth: first argument must be a list"); |
|
361 } |
|
362 else |
|
363 print_usage ("nth"); |
|
364 |
|
365 return retval; |
|
366 } |
|
367 |
2882
|
368 DEFUN (append, args, , |
3447
|
369 "-*- texinfo -*-\n\ |
|
370 @deftypefn {Built-in Function} {} append (@var{list}, @var{a1}, @var{a2}, @dots{})\n\ |
|
371 Return a new list created by appending @var{a1}, @var{a1}, @dots{}, to\n\ |
|
372 @var{list}. If any of the arguments to be appended is a list, its\n\ |
|
373 elements are appended individually. For example,\n\ |
2882
|
374 \n\ |
3447
|
375 @example\n\ |
|
376 x = list (1, 2);\n\ |
|
377 y = list (3, 4);\n\ |
|
378 append (x, y);\n\ |
|
379 @end example\n\ |
3219
|
380 \n\ |
3447
|
381 @noindent\n\ |
|
382 results in the list containing the four elements @samp{(1 2 3 4)}, not\n\ |
|
383 a list containing the three elements @samp{(1 2 (3 4))}.\n\ |
|
384 @end deftypefn") |
2882
|
385 { |
|
386 octave_value retval; |
|
387 |
|
388 int nargin = args.length (); |
|
389 |
|
390 if (nargin > 1) |
|
391 { |
|
392 octave_value_list tmp = args(0).list_value (); |
|
393 |
|
394 if (! error_state) |
|
395 { |
|
396 for (int i = 1; i < nargin; i++) |
3219
|
397 { |
|
398 octave_value ov = args(i); |
|
399 |
|
400 if (ov.is_list ()) |
|
401 tmp.append (ov.list_value ()); |
|
402 else |
|
403 tmp.append (ov); |
|
404 } |
2882
|
405 |
4233
|
406 retval = octave_value (tmp); |
2882
|
407 } |
|
408 } |
|
409 else |
|
410 print_usage ("append"); |
|
411 |
|
412 return retval; |
|
413 } |
|
414 |
|
415 DEFUN (reverse, args, , |
3447
|
416 "-*- texinfo -*-\n\ |
|
417 @deftypefn {Built-in Function} {} reverse (@var{list})\n\ |
|
418 Return a new list created by reversing the elements of @var{list}.\n\ |
|
419 @end deftypefn") |
2882
|
420 { |
|
421 octave_value retval; |
|
422 |
|
423 int nargin = args.length (); |
|
424 |
|
425 if (nargin == 1) |
|
426 { |
|
427 octave_value_list tmp = args(0).list_value (); |
|
428 |
|
429 if (! error_state) |
4233
|
430 retval = octave_value (tmp.reverse ()); |
2882
|
431 } |
|
432 else |
|
433 print_usage ("reverse"); |
|
434 |
|
435 return retval; |
|
436 } |
|
437 |
3196
|
438 DEFUN (splice, args, , |
3447
|
439 "-*- texinfo -*-\n\ |
|
440 @deftypefn {Built-in Function} {} splice (@var{list_1}, @var{offset}, @var{length}, @var{list_2})\n\ |
|
441 Replace @var{length} elements of @var{list_1} beginning at\n\ |
|
442 @var{offset} with the contents of @var{list_2} (if any). If\n\ |
|
443 @var{length} is omitted, all elements from @var{offset} to the end of\n\ |
|
444 @var{list_1} are replaced. As a special case, if @var{offset} is one\n\ |
|
445 greater than the length of @var{list_1} and @var{length} is 0, splice\n\ |
3448
|
446 is equivalent to @code{append (@var{list_1}, @var{list_2})}.\n\ |
3447
|
447 @end deftypefn") |
3196
|
448 { |
|
449 octave_value retval; |
|
450 |
|
451 int nargin = args.length (); |
|
452 |
|
453 if (nargin > 1 && nargin < 5) |
|
454 { |
|
455 octave_value_list list_1 = args(0).list_value (); |
|
456 |
|
457 if (! error_state) |
|
458 { |
3202
|
459 int offset = args(1).int_value (true); |
3196
|
460 |
|
461 if (! error_state) |
|
462 { |
3202
|
463 offset--; |
|
464 |
|
465 int length = 0; |
|
466 |
|
467 octave_value_list list_2; |
3196
|
468 |
3202
|
469 if (nargin < 3) |
|
470 length = list_1.length () - offset; |
|
471 else |
|
472 { |
|
473 length = args(2).int_value (true); |
3196
|
474 |
3202
|
475 if (! error_state) |
3196
|
476 { |
3202
|
477 if (nargin == 4) |
|
478 { |
|
479 list_2 = args(3).list_value (); |
3196
|
480 |
3202
|
481 if (error_state) |
|
482 error ("splice: fourth argument must be a list"); |
3196
|
483 } |
|
484 } |
3202
|
485 else |
|
486 error ("splice: LENGTH must be an integer"); |
|
487 } |
3196
|
488 |
3202
|
489 if (! error_state) |
4233
|
490 retval = octave_value (list_1.splice (offset, length, list_2)); |
3196
|
491 } |
|
492 else |
|
493 error ("splice: OFFSET must be an integer"); |
|
494 } |
|
495 else |
|
496 error ("splice: first argument must be a list"); |
|
497 } |
|
498 else |
|
499 print_usage ("splice"); |
|
500 |
|
501 return retval; |
|
502 } |
|
503 |
4687
|
504 bool |
|
505 octave_list::save_ascii (std::ostream& os, bool& infnan_warned, |
|
506 bool strip_nan_and_inf) |
|
507 { |
|
508 octave_value_list lst = list_value (); |
|
509 os << "# length: " << lst.length () << "\n"; |
|
510 |
|
511 for (int i = 0; i < lst.length (); ++i) |
|
512 { |
|
513 // should we use lst.name_tags () to label the elements? |
|
514 char s[20]; |
|
515 sprintf (s, "_%d", i); |
|
516 bool b = save_ascii_data (os, lst (i), s, infnan_warned, |
|
517 strip_nan_and_inf, 0, 0); |
|
518 |
|
519 if (! b) |
|
520 return false; |
|
521 } |
|
522 |
|
523 return true; |
|
524 } |
|
525 |
|
526 bool |
|
527 octave_list::load_ascii (std::istream& is) |
|
528 { |
|
529 int len = 0; |
|
530 bool success = true; |
|
531 |
|
532 if (extract_keyword (is, "length", len) && len >= 0) |
|
533 { |
|
534 if (len > 0) |
|
535 { |
|
536 octave_value_list lst; |
|
537 |
|
538 for (int j = 0; j < len; j++) |
|
539 { |
|
540 octave_value t2; |
|
541 bool dummy; |
|
542 |
|
543 // recurse to read list elements |
|
544 std::string nm |
|
545 = read_ascii_data (is, std::string (), dummy, t2, count); |
|
546 |
|
547 if (!is) |
|
548 break; |
|
549 |
|
550 lst.append (t2); |
|
551 } |
|
552 |
|
553 if (is) |
|
554 data = lst; |
|
555 else |
|
556 { |
|
557 error ("load: failed to load list"); |
|
558 success = false; |
|
559 } |
|
560 } |
|
561 else if (len == 0 ) |
|
562 data = Cell (0, 0); |
|
563 else |
|
564 panic_impossible (); |
|
565 } |
|
566 else { |
|
567 error ("load: failed to extract number of elements in list"); |
|
568 success = false; |
|
569 } |
|
570 |
|
571 return success; |
|
572 } |
|
573 |
|
574 bool |
|
575 octave_list::save_binary (std::ostream& os, bool& save_as_floats) |
|
576 { |
|
577 octave_value_list lst = list_value (); |
|
578 |
|
579 FOUR_BYTE_INT len = lst.length(); |
|
580 os.write (X_CAST (char *, &len), 4); |
|
581 |
|
582 for (int i = 0; i < lst.length (); i++) |
|
583 { |
|
584 // should we use lst.name_tags () to label the elements? |
|
585 char s[20]; |
|
586 sprintf (s, "_%d", i); |
|
587 |
|
588 // Recurse to print sub-value. |
|
589 bool b = save_binary_data (os, lst(i), s, "", 0, save_as_floats); |
|
590 |
|
591 if (! b) |
|
592 return false; |
|
593 } |
|
594 |
|
595 return true; |
|
596 } |
|
597 |
|
598 bool |
|
599 octave_list::load_binary (std::istream& is, bool swap, |
|
600 oct_mach_info::float_format fmt) |
|
601 { |
|
602 FOUR_BYTE_INT len; |
|
603 if (! is.read (X_CAST (char *, &len), 4)) |
|
604 return false; |
|
605 if (swap) |
|
606 swap_4_bytes (X_CAST (char *, &len)); |
|
607 |
|
608 if (len > 0) |
|
609 { |
|
610 octave_value_list lst; |
|
611 |
|
612 for (int i = 0; i < len; i++) |
|
613 { |
|
614 octave_value t2; |
|
615 bool dummy; |
|
616 std::string doc; |
|
617 |
|
618 // recurse to read list elements |
|
619 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
|
620 dummy, t2, doc); |
|
621 |
|
622 if (!is) |
|
623 return false; |
|
624 |
|
625 lst.append(t2); |
|
626 } |
|
627 |
|
628 if (is) |
|
629 data = lst; |
|
630 else |
|
631 { |
|
632 error ("load: failed to load list"); |
|
633 return false; |
|
634 } |
|
635 |
|
636 } |
|
637 else if (len == 0 ) |
|
638 data = Cell (0, 0); |
|
639 else |
|
640 return false; |
|
641 |
|
642 return true; |
|
643 } |
|
644 |
|
645 #if defined (HAVE_HDF5) |
|
646 bool |
|
647 octave_list::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) |
|
648 { |
|
649 hid_t data_hid = -1; |
|
650 |
|
651 data_hid = H5Gcreate (loc_id, name, 0); |
|
652 if (data_hid < 0) return false; |
|
653 |
|
654 // recursively add each element of the list to this group |
|
655 octave_value_list lst = list_value (); |
|
656 |
|
657 for (int i = 0; i < lst.length (); ++i) |
|
658 { |
|
659 // should we use lst.name_tags () to label the elements? |
|
660 char s[20]; |
|
661 sprintf (s, "_%d", i); |
|
662 bool retval2 = add_hdf5_data (data_hid, lst (i), s, "", |
|
663 false, save_as_floats); |
|
664 if (! retval2) |
|
665 break; |
|
666 } |
|
667 |
|
668 H5Gclose (data_hid); |
|
669 return true; |
|
670 } |
|
671 |
|
672 bool |
|
673 octave_list::load_hdf5 (hid_t loc_id, const char *name, |
|
674 bool have_h5giterate_bug) |
|
675 { |
|
676 bool retval = false; |
|
677 |
|
678 hdf5_callback_data dsub; |
|
679 |
|
680 herr_t retval2; |
|
681 octave_value_list lst; |
|
682 int current_item = 0; |
|
683 while ((retval2 = H5Giterate (loc_id, name, ¤t_item, |
|
684 hdf5_read_next_data, &dsub)) > 0) |
|
685 { |
|
686 lst.append (dsub.tc); |
|
687 |
|
688 if (have_h5giterate_bug) |
|
689 current_item++; // H5Giterate returned the last index processed |
|
690 } |
|
691 |
|
692 if (retval2 >= 0) |
|
693 { |
|
694 data = lst; |
|
695 retval = true; |
|
696 } |
|
697 |
|
698 return retval; |
|
699 } |
|
700 #endif |
|
701 |
2882
|
702 /* |
|
703 ;;; Local Variables: *** |
|
704 ;;; mode: C++ *** |
|
705 ;;; End: *** |
|
706 */ |