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 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
|
32 #include <strstream> |
2882
|
33 |
|
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 |
3933
|
46 octave_list::subsref (const std::string type, |
|
47 const SLList<octave_value_list>& idx) |
|
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 |
|
61 retval = octave_value_list (lst.index (i)); |
|
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 |
3933
|
109 retval = 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 |
|
118 octave_list::subsasgn (const std::string type, |
|
119 const SLList<octave_value_list>& idx, |
|
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 { |
|
141 SLList<octave_value_list> next_idx (idx); |
|
142 |
|
143 next_idx.remove_front (); |
|
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 |
|
163 switch (type[0]) |
|
164 { |
|
165 case '(': |
|
166 { |
|
167 octave_value_list i = idx.front (); |
|
168 |
|
169 assign (i, t_rhs); |
|
170 |
|
171 retval = octave_value (this, count + 1); |
|
172 } |
|
173 break; |
|
174 |
|
175 case '{': |
|
176 case '.': |
|
177 { |
|
178 std::string nm = type_name (); |
|
179 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
180 } |
|
181 break; |
|
182 |
|
183 default: |
|
184 panic_impossible (); |
|
185 } |
|
186 |
|
187 return retval; |
|
188 } |
|
189 |
2882
|
190 void |
3196
|
191 octave_list::assign (const octave_value_list& idx, const octave_value& rhs) |
|
192 { |
|
193 if (idx.length () == 1) |
|
194 { |
3202
|
195 int i = idx(0).int_value (true); |
3196
|
196 |
|
197 if (! error_state) |
|
198 { |
3202
|
199 int n = lst.length (); |
3196
|
200 |
3202
|
201 if (i > 0 && (Vresize_on_range_error || i <= n)) |
|
202 lst(i-1) = rhs; |
3196
|
203 else |
3202
|
204 error ("list index = %d out of range", i); |
3196
|
205 } |
3202
|
206 else |
|
207 error ("list index must be an integer"); |
3196
|
208 } |
|
209 else |
|
210 error ("lists may only be indexed by a single scalar"); |
|
211 } |
|
212 |
|
213 void |
3523
|
214 octave_list::print (std::ostream& os, bool) const |
2901
|
215 { |
|
216 print_raw (os); |
|
217 } |
|
218 |
|
219 void |
3523
|
220 octave_list::print_raw (std::ostream& os, bool) const |
2882
|
221 { |
2985
|
222 unwind_protect::begin_frame ("octave_list_print"); |
2882
|
223 |
|
224 int n = lst.length (); |
|
225 |
3196
|
226 if (n > 0) |
2882
|
227 { |
3196
|
228 indent (os); |
|
229 os << "("; |
|
230 newline (os); |
|
231 |
|
232 increment_indent_level (); |
2916
|
233 |
3196
|
234 for (int i = 0; i < n; i++) |
|
235 { |
3523
|
236 std::ostrstream buf; |
3538
|
237 buf << "[" << i+1 << "]" << std::ends; |
3196
|
238 const char *nm = buf.str (); |
2882
|
239 |
3196
|
240 octave_value val = lst(i); |
2916
|
241 |
3196
|
242 val.print_with_name (os, nm); |
|
243 |
|
244 delete [] nm; |
|
245 } |
2882
|
246 |
3196
|
247 decrement_indent_level (); |
2882
|
248 |
3196
|
249 indent (os); |
|
250 os << ")"; |
|
251 } |
|
252 else |
|
253 os << "()"; |
|
254 |
2901
|
255 newline (os); |
2882
|
256 |
2985
|
257 unwind_protect::run_frame ("octave_list_print"); |
2882
|
258 } |
|
259 |
2901
|
260 bool |
3523
|
261 octave_list::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
262 { |
|
263 indent (os); |
3196
|
264 if (lst.length () == 0) |
|
265 os << name << " = "; |
|
266 else |
|
267 { |
|
268 os << name << " ="; |
|
269 newline (os); |
|
270 } |
2901
|
271 return false; |
|
272 } |
|
273 |
2993
|
274 DEFUN (list, args, , |
3447
|
275 "-*- texinfo -*-\n\ |
3448
|
276 @deftypefn {Built-in Function} {} list (@var{a1}, @var{a2}, @dots{})\n\ |
3447
|
277 Create a new list with elements given by the arguments @var{a1},\n\ |
|
278 @var{a2}, @dots{}.\n\ |
|
279 @end deftypefn") |
2882
|
280 { |
|
281 return octave_value (args); |
|
282 } |
|
283 |
3219
|
284 DEFUN (nth, args, , |
3447
|
285 "-*- texinfo -*-\n\ |
|
286 @deftypefn {Built-in Function} {} nth (@var{list}, @var{n})\n\ |
|
287 Return the @var{n}-th element of @var{list}.\n\ |
|
288 @end deftypefn") |
3219
|
289 { |
|
290 octave_value retval; |
|
291 |
|
292 if (args.length () == 2) |
|
293 { |
|
294 octave_value_list lst = args(0).list_value (); |
|
295 |
|
296 if (! error_state) |
|
297 { |
|
298 int n = args(1).int_value (true); |
|
299 |
|
300 if (! error_state) |
|
301 { |
|
302 if (n > 0 && n <= lst.length ()) |
|
303 retval = lst(n-1); |
|
304 else |
|
305 error ("nth: index = %d out of range", n); |
|
306 } |
|
307 else |
|
308 error ("nth: second argument must be an integer"); |
|
309 } |
|
310 else |
|
311 error ("nth: first argument must be a list"); |
|
312 } |
|
313 else |
|
314 print_usage ("nth"); |
|
315 |
|
316 return retval; |
|
317 } |
|
318 |
2882
|
319 DEFUN (append, args, , |
3447
|
320 "-*- texinfo -*-\n\ |
|
321 @deftypefn {Built-in Function} {} append (@var{list}, @var{a1}, @var{a2}, @dots{})\n\ |
|
322 Return a new list created by appending @var{a1}, @var{a1}, @dots{}, to\n\ |
|
323 @var{list}. If any of the arguments to be appended is a list, its\n\ |
|
324 elements are appended individually. For example,\n\ |
2882
|
325 \n\ |
3447
|
326 @example\n\ |
|
327 x = list (1, 2);\n\ |
|
328 y = list (3, 4);\n\ |
|
329 append (x, y);\n\ |
|
330 @end example\n\ |
3219
|
331 \n\ |
3447
|
332 @noindent\n\ |
|
333 results in the list containing the four elements @samp{(1 2 3 4)}, not\n\ |
|
334 a list containing the three elements @samp{(1 2 (3 4))}.\n\ |
|
335 @end deftypefn") |
2882
|
336 { |
|
337 octave_value retval; |
|
338 |
|
339 int nargin = args.length (); |
|
340 |
|
341 if (nargin > 1) |
|
342 { |
|
343 octave_value_list tmp = args(0).list_value (); |
|
344 |
|
345 if (! error_state) |
|
346 { |
|
347 for (int i = 1; i < nargin; i++) |
3219
|
348 { |
|
349 octave_value ov = args(i); |
|
350 |
|
351 if (ov.is_list ()) |
|
352 tmp.append (ov.list_value ()); |
|
353 else |
|
354 tmp.append (ov); |
|
355 } |
2882
|
356 |
|
357 retval = tmp; |
|
358 } |
|
359 } |
|
360 else |
|
361 print_usage ("append"); |
|
362 |
|
363 return retval; |
|
364 } |
|
365 |
|
366 DEFUN (reverse, args, , |
3447
|
367 "-*- texinfo -*-\n\ |
|
368 @deftypefn {Built-in Function} {} reverse (@var{list})\n\ |
|
369 Return a new list created by reversing the elements of @var{list}.\n\ |
|
370 @end deftypefn") |
2882
|
371 { |
|
372 octave_value retval; |
|
373 |
|
374 int nargin = args.length (); |
|
375 |
|
376 if (nargin == 1) |
|
377 { |
|
378 octave_value_list tmp = args(0).list_value (); |
|
379 |
|
380 if (! error_state) |
|
381 retval = tmp.reverse (); |
|
382 } |
|
383 else |
|
384 print_usage ("reverse"); |
|
385 |
|
386 return retval; |
|
387 } |
|
388 |
3196
|
389 DEFUN (splice, args, , |
3447
|
390 "-*- texinfo -*-\n\ |
|
391 @deftypefn {Built-in Function} {} splice (@var{list_1}, @var{offset}, @var{length}, @var{list_2})\n\ |
|
392 Replace @var{length} elements of @var{list_1} beginning at\n\ |
|
393 @var{offset} with the contents of @var{list_2} (if any). If\n\ |
|
394 @var{length} is omitted, all elements from @var{offset} to the end of\n\ |
|
395 @var{list_1} are replaced. As a special case, if @var{offset} is one\n\ |
|
396 greater than the length of @var{list_1} and @var{length} is 0, splice\n\ |
3448
|
397 is equivalent to @code{append (@var{list_1}, @var{list_2})}.\n\ |
3447
|
398 @end deftypefn") |
3196
|
399 { |
|
400 octave_value retval; |
|
401 |
|
402 int nargin = args.length (); |
|
403 |
|
404 if (nargin > 1 && nargin < 5) |
|
405 { |
|
406 octave_value_list list_1 = args(0).list_value (); |
|
407 |
|
408 if (! error_state) |
|
409 { |
3202
|
410 int offset = args(1).int_value (true); |
3196
|
411 |
|
412 if (! error_state) |
|
413 { |
3202
|
414 offset--; |
|
415 |
|
416 int length = 0; |
|
417 |
|
418 octave_value_list list_2; |
3196
|
419 |
3202
|
420 if (nargin < 3) |
|
421 length = list_1.length () - offset; |
|
422 else |
|
423 { |
|
424 length = args(2).int_value (true); |
3196
|
425 |
3202
|
426 if (! error_state) |
3196
|
427 { |
3202
|
428 if (nargin == 4) |
|
429 { |
|
430 list_2 = args(3).list_value (); |
3196
|
431 |
3202
|
432 if (error_state) |
|
433 error ("splice: fourth argument must be a list"); |
3196
|
434 } |
|
435 } |
3202
|
436 else |
|
437 error ("splice: LENGTH must be an integer"); |
|
438 } |
3196
|
439 |
3202
|
440 if (! error_state) |
|
441 retval = list_1.splice (offset, length, list_2); |
3196
|
442 } |
|
443 else |
|
444 error ("splice: OFFSET must be an integer"); |
|
445 } |
|
446 else |
|
447 error ("splice: first argument must be a list"); |
|
448 } |
|
449 else |
|
450 print_usage ("splice"); |
|
451 |
|
452 return retval; |
|
453 } |
|
454 |
2882
|
455 /* |
|
456 ;;; Local Variables: *** |
|
457 ;;; mode: C++ *** |
|
458 ;;; End: *** |
|
459 */ |