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