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