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 |
3219
|
42 DEFINE_OCTAVE_ALLOCATOR (octave_list); |
2882
|
43 |
3219
|
44 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_list, "list"); |
2882
|
45 |
4513
|
46 octave_list::octave_list (const Cell& c) |
4591
|
47 : octave_base_value (), data () |
4513
|
48 { |
|
49 int n = c.length (); |
|
50 |
4591
|
51 data.resize (dim_vector (1, n)); |
4513
|
52 |
|
53 for (int i = 0; i < n; i++) |
4591
|
54 data(i) = c(i); |
4513
|
55 } |
|
56 |
2882
|
57 octave_value |
4247
|
58 octave_list::subsref (const std::string& type, |
4219
|
59 const std::list<octave_value_list>& idx) |
3933
|
60 { |
|
61 octave_value retval; |
|
62 |
|
63 switch (type[0]) |
|
64 { |
|
65 case '(': |
|
66 { |
|
67 octave_value_list tmp_idx = idx.front (); |
|
68 |
|
69 if (tmp_idx.length () == 1) |
|
70 { |
|
71 idx_vector i = tmp_idx (0).index_vector (); |
|
72 |
4591
|
73 retval = octave_list (data.index (i)); |
3933
|
74 } |
|
75 else |
|
76 error ("only one index allowed for lists"); |
|
77 } |
|
78 break; |
|
79 |
|
80 case '{': |
|
81 { |
|
82 octave_value_list tmp_idx = idx.front (); |
|
83 |
|
84 if (tmp_idx.length () == 1) |
|
85 { |
|
86 idx_vector i = tmp_idx (0).index_vector (); |
|
87 |
4591
|
88 Cell tmp = data.index (i); |
3933
|
89 |
|
90 if (tmp.length () == 1) |
|
91 retval = tmp(0); |
|
92 } |
|
93 else |
|
94 error ("only one index allowed for lists"); |
|
95 } |
|
96 break; |
|
97 |
|
98 case '.': |
|
99 { |
|
100 std::string nm = type_name (); |
|
101 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
102 } |
|
103 break; |
|
104 |
|
105 default: |
|
106 panic_impossible (); |
|
107 } |
|
108 |
|
109 return retval.next_subsref (type, idx); |
|
110 } |
|
111 |
|
112 octave_value |
|
113 octave_list::do_index_op (const octave_value_list& idx, int resize_ok) |
2882
|
114 { |
|
115 octave_value retval; |
|
116 |
|
117 if (idx.length () == 1) |
|
118 { |
3219
|
119 idx_vector i = idx (0).index_vector (); |
2882
|
120 |
4591
|
121 retval = octave_list (data.index (i, resize_ok)); |
2882
|
122 } |
|
123 else |
|
124 error ("lists may only be indexed by a single scalar"); |
|
125 |
|
126 return retval; |
|
127 } |
|
128 |
3933
|
129 octave_value |
4247
|
130 octave_list::subsasgn (const std::string& type, |
4219
|
131 const std::list<octave_value_list>& idx, |
3933
|
132 const octave_value& rhs) |
|
133 { |
|
134 octave_value retval; |
|
135 |
|
136 int n = type.length (); |
|
137 |
|
138 octave_value t_rhs = rhs; |
|
139 |
|
140 if (n > 1) |
|
141 { |
|
142 switch (type[0]) |
|
143 { |
|
144 case '(': |
|
145 { |
|
146 octave_value tmp = do_index_op (idx.front (), true); |
|
147 |
|
148 if (! tmp.is_defined ()) |
|
149 tmp = octave_value::empty_conv (type.substr (1), rhs); |
|
150 |
|
151 if (! error_state) |
|
152 { |
4219
|
153 std::list<octave_value_list> next_idx (idx); |
3933
|
154 |
4219
|
155 next_idx.erase (next_idx.begin ()); |
3933
|
156 |
|
157 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); |
|
158 } |
|
159 } |
|
160 break; |
|
161 |
|
162 case '{': |
|
163 case '.': |
|
164 { |
|
165 std::string nm = type_name (); |
|
166 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
167 } |
|
168 break; |
|
169 |
|
170 default: |
|
171 panic_impossible (); |
|
172 } |
|
173 } |
|
174 |
3940
|
175 if (! error_state) |
3933
|
176 { |
3940
|
177 switch (type[0]) |
|
178 { |
|
179 case '(': |
|
180 { |
|
181 octave_value_list i = idx.front (); |
3933
|
182 |
3940
|
183 assign (i, t_rhs); |
3933
|
184 |
3940
|
185 retval = octave_value (this, count + 1); |
|
186 } |
|
187 break; |
3933
|
188 |
3940
|
189 case '{': |
|
190 case '.': |
|
191 { |
|
192 std::string nm = type_name (); |
|
193 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
194 } |
|
195 break; |
3933
|
196 |
3940
|
197 default: |
|
198 panic_impossible (); |
|
199 } |
3933
|
200 } |
|
201 |
|
202 return retval; |
|
203 } |
|
204 |
2882
|
205 void |
3196
|
206 octave_list::assign (const octave_value_list& idx, const octave_value& rhs) |
|
207 { |
|
208 if (idx.length () == 1) |
|
209 { |
3202
|
210 int i = idx(0).int_value (true); |
3196
|
211 |
|
212 if (! error_state) |
|
213 { |
4591
|
214 int n = data.length (); |
3196
|
215 |
4461
|
216 if (i > 0) |
|
217 { |
|
218 if (Vwarn_resize_on_range_error && i > n) |
|
219 warning ("list index = %d out of range", i); |
|
220 |
4591
|
221 data(i-1) = rhs; |
4461
|
222 } |
3196
|
223 else |
3202
|
224 error ("list index = %d out of range", i); |
3196
|
225 } |
3202
|
226 else |
|
227 error ("list index must be an integer"); |
3196
|
228 } |
|
229 else |
|
230 error ("lists may only be indexed by a single scalar"); |
|
231 } |
|
232 |
4591
|
233 octave_value_list |
|
234 octave_list::list_value (void) const |
|
235 { |
|
236 octave_value_list retval; |
|
237 |
|
238 int n = data.length (); |
|
239 |
|
240 retval.resize (n); |
|
241 |
|
242 for (int i = 0; i < n; i++) |
|
243 retval(i) = data(i); |
|
244 |
|
245 return retval; |
|
246 } |
|
247 |
3196
|
248 void |
3523
|
249 octave_list::print (std::ostream& os, bool) const |
2901
|
250 { |
|
251 print_raw (os); |
|
252 } |
|
253 |
|
254 void |
3523
|
255 octave_list::print_raw (std::ostream& os, bool) const |
2882
|
256 { |
2985
|
257 unwind_protect::begin_frame ("octave_list_print"); |
2882
|
258 |
4591
|
259 int n = data.length (); |
2882
|
260 |
3196
|
261 if (n > 0) |
2882
|
262 { |
3196
|
263 indent (os); |
|
264 os << "("; |
|
265 newline (os); |
|
266 |
|
267 increment_indent_level (); |
2916
|
268 |
3196
|
269 for (int i = 0; i < n; i++) |
|
270 { |
4051
|
271 OSSTREAM buf; |
|
272 |
|
273 buf << "[" << i+1 << "]" << OSSTREAM_ENDS; |
2882
|
274 |
4591
|
275 octave_value val = data(i); |
2916
|
276 |
4051
|
277 val.print_with_name (os, OSSTREAM_STR (buf)); |
3196
|
278 |
4051
|
279 OSSTREAM_FREEZE (buf); |
3196
|
280 } |
2882
|
281 |
3196
|
282 decrement_indent_level (); |
2882
|
283 |
3196
|
284 indent (os); |
|
285 os << ")"; |
|
286 } |
|
287 else |
|
288 os << "()"; |
|
289 |
2901
|
290 newline (os); |
2882
|
291 |
2985
|
292 unwind_protect::run_frame ("octave_list_print"); |
2882
|
293 } |
|
294 |
2901
|
295 bool |
3523
|
296 octave_list::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
297 { |
|
298 indent (os); |
4591
|
299 if (data.length () == 0) |
3196
|
300 os << name << " = "; |
|
301 else |
|
302 { |
|
303 os << name << " ="; |
|
304 newline (os); |
|
305 } |
2901
|
306 return false; |
|
307 } |
|
308 |
2993
|
309 DEFUN (list, args, , |
3447
|
310 "-*- texinfo -*-\n\ |
3448
|
311 @deftypefn {Built-in Function} {} list (@var{a1}, @var{a2}, @dots{})\n\ |
3447
|
312 Create a new list with elements given by the arguments @var{a1},\n\ |
|
313 @var{a2}, @dots{}.\n\ |
|
314 @end deftypefn") |
2882
|
315 { |
4591
|
316 static bool warned = false; |
|
317 |
|
318 if (! warned) |
|
319 { |
|
320 warning ("list objects are deprecated; use cell arrays instead"); |
|
321 warned = true; |
|
322 } |
|
323 |
2882
|
324 return octave_value (args); |
|
325 } |
|
326 |
3219
|
327 DEFUN (nth, args, , |
3447
|
328 "-*- texinfo -*-\n\ |
|
329 @deftypefn {Built-in Function} {} nth (@var{list}, @var{n})\n\ |
|
330 Return the @var{n}-th element of @var{list}.\n\ |
|
331 @end deftypefn") |
3219
|
332 { |
|
333 octave_value retval; |
|
334 |
|
335 if (args.length () == 2) |
|
336 { |
|
337 octave_value_list lst = args(0).list_value (); |
|
338 |
|
339 if (! error_state) |
|
340 { |
|
341 int n = args(1).int_value (true); |
|
342 |
|
343 if (! error_state) |
|
344 { |
|
345 if (n > 0 && n <= lst.length ()) |
|
346 retval = lst(n-1); |
|
347 else |
|
348 error ("nth: index = %d out of range", n); |
|
349 } |
|
350 else |
|
351 error ("nth: second argument must be an integer"); |
|
352 } |
|
353 else |
|
354 error ("nth: first argument must be a list"); |
|
355 } |
|
356 else |
|
357 print_usage ("nth"); |
|
358 |
|
359 return retval; |
|
360 } |
|
361 |
2882
|
362 DEFUN (append, args, , |
3447
|
363 "-*- texinfo -*-\n\ |
|
364 @deftypefn {Built-in Function} {} append (@var{list}, @var{a1}, @var{a2}, @dots{})\n\ |
|
365 Return a new list created by appending @var{a1}, @var{a1}, @dots{}, to\n\ |
|
366 @var{list}. If any of the arguments to be appended is a list, its\n\ |
|
367 elements are appended individually. For example,\n\ |
2882
|
368 \n\ |
3447
|
369 @example\n\ |
|
370 x = list (1, 2);\n\ |
|
371 y = list (3, 4);\n\ |
|
372 append (x, y);\n\ |
|
373 @end example\n\ |
3219
|
374 \n\ |
3447
|
375 @noindent\n\ |
|
376 results in the list containing the four elements @samp{(1 2 3 4)}, not\n\ |
|
377 a list containing the three elements @samp{(1 2 (3 4))}.\n\ |
|
378 @end deftypefn") |
2882
|
379 { |
|
380 octave_value retval; |
|
381 |
|
382 int nargin = args.length (); |
|
383 |
|
384 if (nargin > 1) |
|
385 { |
|
386 octave_value_list tmp = args(0).list_value (); |
|
387 |
|
388 if (! error_state) |
|
389 { |
|
390 for (int i = 1; i < nargin; i++) |
3219
|
391 { |
|
392 octave_value ov = args(i); |
|
393 |
|
394 if (ov.is_list ()) |
|
395 tmp.append (ov.list_value ()); |
|
396 else |
|
397 tmp.append (ov); |
|
398 } |
2882
|
399 |
4233
|
400 retval = octave_value (tmp); |
2882
|
401 } |
|
402 } |
|
403 else |
|
404 print_usage ("append"); |
|
405 |
|
406 return retval; |
|
407 } |
|
408 |
|
409 DEFUN (reverse, args, , |
3447
|
410 "-*- texinfo -*-\n\ |
|
411 @deftypefn {Built-in Function} {} reverse (@var{list})\n\ |
|
412 Return a new list created by reversing the elements of @var{list}.\n\ |
|
413 @end deftypefn") |
2882
|
414 { |
|
415 octave_value retval; |
|
416 |
|
417 int nargin = args.length (); |
|
418 |
|
419 if (nargin == 1) |
|
420 { |
|
421 octave_value_list tmp = args(0).list_value (); |
|
422 |
|
423 if (! error_state) |
4233
|
424 retval = octave_value (tmp.reverse ()); |
2882
|
425 } |
|
426 else |
|
427 print_usage ("reverse"); |
|
428 |
|
429 return retval; |
|
430 } |
|
431 |
3196
|
432 DEFUN (splice, args, , |
3447
|
433 "-*- texinfo -*-\n\ |
|
434 @deftypefn {Built-in Function} {} splice (@var{list_1}, @var{offset}, @var{length}, @var{list_2})\n\ |
|
435 Replace @var{length} elements of @var{list_1} beginning at\n\ |
|
436 @var{offset} with the contents of @var{list_2} (if any). If\n\ |
|
437 @var{length} is omitted, all elements from @var{offset} to the end of\n\ |
|
438 @var{list_1} are replaced. As a special case, if @var{offset} is one\n\ |
|
439 greater than the length of @var{list_1} and @var{length} is 0, splice\n\ |
3448
|
440 is equivalent to @code{append (@var{list_1}, @var{list_2})}.\n\ |
3447
|
441 @end deftypefn") |
3196
|
442 { |
|
443 octave_value retval; |
|
444 |
|
445 int nargin = args.length (); |
|
446 |
|
447 if (nargin > 1 && nargin < 5) |
|
448 { |
|
449 octave_value_list list_1 = args(0).list_value (); |
|
450 |
|
451 if (! error_state) |
|
452 { |
3202
|
453 int offset = args(1).int_value (true); |
3196
|
454 |
|
455 if (! error_state) |
|
456 { |
3202
|
457 offset--; |
|
458 |
|
459 int length = 0; |
|
460 |
|
461 octave_value_list list_2; |
3196
|
462 |
3202
|
463 if (nargin < 3) |
|
464 length = list_1.length () - offset; |
|
465 else |
|
466 { |
|
467 length = args(2).int_value (true); |
3196
|
468 |
3202
|
469 if (! error_state) |
3196
|
470 { |
3202
|
471 if (nargin == 4) |
|
472 { |
|
473 list_2 = args(3).list_value (); |
3196
|
474 |
3202
|
475 if (error_state) |
|
476 error ("splice: fourth argument must be a list"); |
3196
|
477 } |
|
478 } |
3202
|
479 else |
|
480 error ("splice: LENGTH must be an integer"); |
|
481 } |
3196
|
482 |
3202
|
483 if (! error_state) |
4233
|
484 retval = octave_value (list_1.splice (offset, length, list_2)); |
3196
|
485 } |
|
486 else |
|
487 error ("splice: OFFSET must be an integer"); |
|
488 } |
|
489 else |
|
490 error ("splice: first argument must be a list"); |
|
491 } |
|
492 else |
|
493 print_usage ("splice"); |
|
494 |
|
495 return retval; |
|
496 } |
|
497 |
2882
|
498 /* |
|
499 ;;; Local Variables: *** |
|
500 ;;; mode: C++ *** |
|
501 ;;; End: *** |
|
502 */ |