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