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) |
|
47 : octave_base_value (), lst () |
|
48 { |
|
49 int n = c.length (); |
|
50 |
|
51 lst.resize (n); |
|
52 |
|
53 for (int i = 0; i < n; i++) |
|
54 lst(i) = c(i); |
|
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 |
4233
|
73 retval = octave_value (octave_value_list (lst.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 |
|
88 octave_value_list tmp = lst.index (i); |
|
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 |
4233
|
121 retval = octave_value (octave_value_list (lst.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 { |
3202
|
214 int n = lst.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 |
|
221 lst(i-1) = rhs; |
|
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 |
|
233 void |
3523
|
234 octave_list::print (std::ostream& os, bool) const |
2901
|
235 { |
|
236 print_raw (os); |
|
237 } |
|
238 |
|
239 void |
3523
|
240 octave_list::print_raw (std::ostream& os, bool) const |
2882
|
241 { |
2985
|
242 unwind_protect::begin_frame ("octave_list_print"); |
2882
|
243 |
|
244 int n = lst.length (); |
|
245 |
3196
|
246 if (n > 0) |
2882
|
247 { |
3196
|
248 indent (os); |
|
249 os << "("; |
|
250 newline (os); |
|
251 |
|
252 increment_indent_level (); |
2916
|
253 |
3196
|
254 for (int i = 0; i < n; i++) |
|
255 { |
4051
|
256 OSSTREAM buf; |
|
257 |
|
258 buf << "[" << i+1 << "]" << OSSTREAM_ENDS; |
2882
|
259 |
3196
|
260 octave_value val = lst(i); |
2916
|
261 |
4051
|
262 val.print_with_name (os, OSSTREAM_STR (buf)); |
3196
|
263 |
4051
|
264 OSSTREAM_FREEZE (buf); |
3196
|
265 } |
2882
|
266 |
3196
|
267 decrement_indent_level (); |
2882
|
268 |
3196
|
269 indent (os); |
|
270 os << ")"; |
|
271 } |
|
272 else |
|
273 os << "()"; |
|
274 |
2901
|
275 newline (os); |
2882
|
276 |
2985
|
277 unwind_protect::run_frame ("octave_list_print"); |
2882
|
278 } |
|
279 |
2901
|
280 bool |
3523
|
281 octave_list::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
282 { |
|
283 indent (os); |
3196
|
284 if (lst.length () == 0) |
|
285 os << name << " = "; |
|
286 else |
|
287 { |
|
288 os << name << " ="; |
|
289 newline (os); |
|
290 } |
2901
|
291 return false; |
|
292 } |
|
293 |
2993
|
294 DEFUN (list, args, , |
3447
|
295 "-*- texinfo -*-\n\ |
3448
|
296 @deftypefn {Built-in Function} {} list (@var{a1}, @var{a2}, @dots{})\n\ |
3447
|
297 Create a new list with elements given by the arguments @var{a1},\n\ |
|
298 @var{a2}, @dots{}.\n\ |
|
299 @end deftypefn") |
2882
|
300 { |
|
301 return octave_value (args); |
|
302 } |
|
303 |
3219
|
304 DEFUN (nth, args, , |
3447
|
305 "-*- texinfo -*-\n\ |
|
306 @deftypefn {Built-in Function} {} nth (@var{list}, @var{n})\n\ |
|
307 Return the @var{n}-th element of @var{list}.\n\ |
|
308 @end deftypefn") |
3219
|
309 { |
|
310 octave_value retval; |
|
311 |
|
312 if (args.length () == 2) |
|
313 { |
|
314 octave_value_list lst = args(0).list_value (); |
|
315 |
|
316 if (! error_state) |
|
317 { |
|
318 int n = args(1).int_value (true); |
|
319 |
|
320 if (! error_state) |
|
321 { |
|
322 if (n > 0 && n <= lst.length ()) |
|
323 retval = lst(n-1); |
|
324 else |
|
325 error ("nth: index = %d out of range", n); |
|
326 } |
|
327 else |
|
328 error ("nth: second argument must be an integer"); |
|
329 } |
|
330 else |
|
331 error ("nth: first argument must be a list"); |
|
332 } |
|
333 else |
|
334 print_usage ("nth"); |
|
335 |
|
336 return retval; |
|
337 } |
|
338 |
2882
|
339 DEFUN (append, args, , |
3447
|
340 "-*- texinfo -*-\n\ |
|
341 @deftypefn {Built-in Function} {} append (@var{list}, @var{a1}, @var{a2}, @dots{})\n\ |
|
342 Return a new list created by appending @var{a1}, @var{a1}, @dots{}, to\n\ |
|
343 @var{list}. If any of the arguments to be appended is a list, its\n\ |
|
344 elements are appended individually. For example,\n\ |
2882
|
345 \n\ |
3447
|
346 @example\n\ |
|
347 x = list (1, 2);\n\ |
|
348 y = list (3, 4);\n\ |
|
349 append (x, y);\n\ |
|
350 @end example\n\ |
3219
|
351 \n\ |
3447
|
352 @noindent\n\ |
|
353 results in the list containing the four elements @samp{(1 2 3 4)}, not\n\ |
|
354 a list containing the three elements @samp{(1 2 (3 4))}.\n\ |
|
355 @end deftypefn") |
2882
|
356 { |
|
357 octave_value retval; |
|
358 |
|
359 int nargin = args.length (); |
|
360 |
|
361 if (nargin > 1) |
|
362 { |
|
363 octave_value_list tmp = args(0).list_value (); |
|
364 |
|
365 if (! error_state) |
|
366 { |
|
367 for (int i = 1; i < nargin; i++) |
3219
|
368 { |
|
369 octave_value ov = args(i); |
|
370 |
|
371 if (ov.is_list ()) |
|
372 tmp.append (ov.list_value ()); |
|
373 else |
|
374 tmp.append (ov); |
|
375 } |
2882
|
376 |
4233
|
377 retval = octave_value (tmp); |
2882
|
378 } |
|
379 } |
|
380 else |
|
381 print_usage ("append"); |
|
382 |
|
383 return retval; |
|
384 } |
|
385 |
|
386 DEFUN (reverse, args, , |
3447
|
387 "-*- texinfo -*-\n\ |
|
388 @deftypefn {Built-in Function} {} reverse (@var{list})\n\ |
|
389 Return a new list created by reversing the elements of @var{list}.\n\ |
|
390 @end deftypefn") |
2882
|
391 { |
|
392 octave_value retval; |
|
393 |
|
394 int nargin = args.length (); |
|
395 |
|
396 if (nargin == 1) |
|
397 { |
|
398 octave_value_list tmp = args(0).list_value (); |
|
399 |
|
400 if (! error_state) |
4233
|
401 retval = octave_value (tmp.reverse ()); |
2882
|
402 } |
|
403 else |
|
404 print_usage ("reverse"); |
|
405 |
|
406 return retval; |
|
407 } |
|
408 |
3196
|
409 DEFUN (splice, args, , |
3447
|
410 "-*- texinfo -*-\n\ |
|
411 @deftypefn {Built-in Function} {} splice (@var{list_1}, @var{offset}, @var{length}, @var{list_2})\n\ |
|
412 Replace @var{length} elements of @var{list_1} beginning at\n\ |
|
413 @var{offset} with the contents of @var{list_2} (if any). If\n\ |
|
414 @var{length} is omitted, all elements from @var{offset} to the end of\n\ |
|
415 @var{list_1} are replaced. As a special case, if @var{offset} is one\n\ |
|
416 greater than the length of @var{list_1} and @var{length} is 0, splice\n\ |
3448
|
417 is equivalent to @code{append (@var{list_1}, @var{list_2})}.\n\ |
3447
|
418 @end deftypefn") |
3196
|
419 { |
|
420 octave_value retval; |
|
421 |
|
422 int nargin = args.length (); |
|
423 |
|
424 if (nargin > 1 && nargin < 5) |
|
425 { |
|
426 octave_value_list list_1 = args(0).list_value (); |
|
427 |
|
428 if (! error_state) |
|
429 { |
3202
|
430 int offset = args(1).int_value (true); |
3196
|
431 |
|
432 if (! error_state) |
|
433 { |
3202
|
434 offset--; |
|
435 |
|
436 int length = 0; |
|
437 |
|
438 octave_value_list list_2; |
3196
|
439 |
3202
|
440 if (nargin < 3) |
|
441 length = list_1.length () - offset; |
|
442 else |
|
443 { |
|
444 length = args(2).int_value (true); |
3196
|
445 |
3202
|
446 if (! error_state) |
3196
|
447 { |
3202
|
448 if (nargin == 4) |
|
449 { |
|
450 list_2 = args(3).list_value (); |
3196
|
451 |
3202
|
452 if (error_state) |
|
453 error ("splice: fourth argument must be a list"); |
3196
|
454 } |
|
455 } |
3202
|
456 else |
|
457 error ("splice: LENGTH must be an integer"); |
|
458 } |
3196
|
459 |
3202
|
460 if (! error_state) |
4233
|
461 retval = octave_value (list_1.splice (offset, length, list_2)); |
3196
|
462 } |
|
463 else |
|
464 error ("splice: OFFSET must be an integer"); |
|
465 } |
|
466 else |
|
467 error ("splice: first argument must be a list"); |
|
468 } |
|
469 else |
|
470 print_usage ("splice"); |
|
471 |
|
472 return retval; |
|
473 } |
|
474 |
2882
|
475 /* |
|
476 ;;; Local Variables: *** |
|
477 ;;; mode: C++ *** |
|
478 ;;; End: *** |
|
479 */ |