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