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 |
2974
|
46 octave_list::do_index_op (const octave_value_list& idx) |
2882
|
47 { |
|
48 octave_value retval; |
|
49 |
|
50 if (idx.length () == 1) |
|
51 { |
3219
|
52 idx_vector i = idx (0).index_vector (); |
2882
|
53 |
3219
|
54 retval = octave_value_list (lst.index (i)); |
2882
|
55 } |
|
56 else |
|
57 error ("lists may only be indexed by a single scalar"); |
|
58 |
|
59 return retval; |
|
60 } |
|
61 |
|
62 void |
3196
|
63 octave_list::assign (const octave_value_list& idx, const octave_value& rhs) |
|
64 { |
|
65 if (idx.length () == 1) |
|
66 { |
3202
|
67 int i = idx(0).int_value (true); |
3196
|
68 |
|
69 if (! error_state) |
|
70 { |
3202
|
71 int n = lst.length (); |
3196
|
72 |
3202
|
73 if (i > 0 && (Vresize_on_range_error || i <= n)) |
|
74 lst(i-1) = rhs; |
3196
|
75 else |
3202
|
76 error ("list index = %d out of range", i); |
3196
|
77 } |
3202
|
78 else |
|
79 error ("list index must be an integer"); |
3196
|
80 } |
|
81 else |
|
82 error ("lists may only be indexed by a single scalar"); |
|
83 } |
|
84 |
|
85 void |
3523
|
86 octave_list::print (std::ostream& os, bool) const |
2901
|
87 { |
|
88 print_raw (os); |
|
89 } |
|
90 |
|
91 void |
3523
|
92 octave_list::print_raw (std::ostream& os, bool) const |
2882
|
93 { |
2985
|
94 unwind_protect::begin_frame ("octave_list_print"); |
2882
|
95 |
|
96 int n = lst.length (); |
|
97 |
3196
|
98 if (n > 0) |
2882
|
99 { |
3196
|
100 indent (os); |
|
101 os << "("; |
|
102 newline (os); |
|
103 |
|
104 increment_indent_level (); |
2916
|
105 |
3196
|
106 for (int i = 0; i < n; i++) |
|
107 { |
3523
|
108 std::ostrstream buf; |
3538
|
109 buf << "[" << i+1 << "]" << std::ends; |
3196
|
110 const char *nm = buf.str (); |
2882
|
111 |
3196
|
112 octave_value val = lst(i); |
2916
|
113 |
3196
|
114 val.print_with_name (os, nm); |
|
115 |
|
116 delete [] nm; |
|
117 } |
2882
|
118 |
3196
|
119 decrement_indent_level (); |
2882
|
120 |
3196
|
121 indent (os); |
|
122 os << ")"; |
|
123 } |
|
124 else |
|
125 os << "()"; |
|
126 |
2901
|
127 newline (os); |
2882
|
128 |
2985
|
129 unwind_protect::run_frame ("octave_list_print"); |
2882
|
130 } |
|
131 |
2901
|
132 bool |
3523
|
133 octave_list::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
134 { |
|
135 indent (os); |
3196
|
136 if (lst.length () == 0) |
|
137 os << name << " = "; |
|
138 else |
|
139 { |
|
140 os << name << " ="; |
|
141 newline (os); |
|
142 } |
2901
|
143 return false; |
|
144 } |
|
145 |
2993
|
146 DEFUN (list, args, , |
3447
|
147 "-*- texinfo -*-\n\ |
3448
|
148 @deftypefn {Built-in Function} {} list (@var{a1}, @var{a2}, @dots{})\n\ |
3447
|
149 Create a new list with elements given by the arguments @var{a1},\n\ |
|
150 @var{a2}, @dots{}.\n\ |
|
151 @end deftypefn") |
2882
|
152 { |
|
153 return octave_value (args); |
|
154 } |
|
155 |
3219
|
156 DEFUN (nth, args, , |
3447
|
157 "-*- texinfo -*-\n\ |
|
158 @deftypefn {Built-in Function} {} nth (@var{list}, @var{n})\n\ |
|
159 Return the @var{n}-th element of @var{list}.\n\ |
|
160 @end deftypefn") |
3219
|
161 { |
|
162 octave_value retval; |
|
163 |
|
164 if (args.length () == 2) |
|
165 { |
|
166 octave_value_list lst = args(0).list_value (); |
|
167 |
|
168 if (! error_state) |
|
169 { |
|
170 int n = args(1).int_value (true); |
|
171 |
|
172 if (! error_state) |
|
173 { |
|
174 if (n > 0 && n <= lst.length ()) |
|
175 retval = lst(n-1); |
|
176 else |
|
177 error ("nth: index = %d out of range", n); |
|
178 } |
|
179 else |
|
180 error ("nth: second argument must be an integer"); |
|
181 } |
|
182 else |
|
183 error ("nth: first argument must be a list"); |
|
184 } |
|
185 else |
|
186 print_usage ("nth"); |
|
187 |
|
188 return retval; |
|
189 } |
|
190 |
2882
|
191 DEFUN (append, args, , |
3447
|
192 "-*- texinfo -*-\n\ |
|
193 @deftypefn {Built-in Function} {} append (@var{list}, @var{a1}, @var{a2}, @dots{})\n\ |
|
194 Return a new list created by appending @var{a1}, @var{a1}, @dots{}, to\n\ |
|
195 @var{list}. If any of the arguments to be appended is a list, its\n\ |
|
196 elements are appended individually. For example,\n\ |
2882
|
197 \n\ |
3447
|
198 @example\n\ |
|
199 x = list (1, 2);\n\ |
|
200 y = list (3, 4);\n\ |
|
201 append (x, y);\n\ |
|
202 @end example\n\ |
3219
|
203 \n\ |
3447
|
204 @noindent\n\ |
|
205 results in the list containing the four elements @samp{(1 2 3 4)}, not\n\ |
|
206 a list containing the three elements @samp{(1 2 (3 4))}.\n\ |
|
207 @end deftypefn") |
2882
|
208 { |
|
209 octave_value retval; |
|
210 |
|
211 int nargin = args.length (); |
|
212 |
|
213 if (nargin > 1) |
|
214 { |
|
215 octave_value_list tmp = args(0).list_value (); |
|
216 |
|
217 if (! error_state) |
|
218 { |
|
219 for (int i = 1; i < nargin; i++) |
3219
|
220 { |
|
221 octave_value ov = args(i); |
|
222 |
|
223 if (ov.is_list ()) |
|
224 tmp.append (ov.list_value ()); |
|
225 else |
|
226 tmp.append (ov); |
|
227 } |
2882
|
228 |
|
229 retval = tmp; |
|
230 } |
|
231 } |
|
232 else |
|
233 print_usage ("append"); |
|
234 |
|
235 return retval; |
|
236 } |
|
237 |
|
238 DEFUN (reverse, args, , |
3447
|
239 "-*- texinfo -*-\n\ |
|
240 @deftypefn {Built-in Function} {} reverse (@var{list})\n\ |
|
241 Return a new list created by reversing the elements of @var{list}.\n\ |
|
242 @end deftypefn") |
2882
|
243 { |
|
244 octave_value retval; |
|
245 |
|
246 int nargin = args.length (); |
|
247 |
|
248 if (nargin == 1) |
|
249 { |
|
250 octave_value_list tmp = args(0).list_value (); |
|
251 |
|
252 if (! error_state) |
|
253 retval = tmp.reverse (); |
|
254 } |
|
255 else |
|
256 print_usage ("reverse"); |
|
257 |
|
258 return retval; |
|
259 } |
|
260 |
3196
|
261 DEFUN (splice, args, , |
3447
|
262 "-*- texinfo -*-\n\ |
|
263 @deftypefn {Built-in Function} {} splice (@var{list_1}, @var{offset}, @var{length}, @var{list_2})\n\ |
|
264 Replace @var{length} elements of @var{list_1} beginning at\n\ |
|
265 @var{offset} with the contents of @var{list_2} (if any). If\n\ |
|
266 @var{length} is omitted, all elements from @var{offset} to the end of\n\ |
|
267 @var{list_1} are replaced. As a special case, if @var{offset} is one\n\ |
|
268 greater than the length of @var{list_1} and @var{length} is 0, splice\n\ |
3448
|
269 is equivalent to @code{append (@var{list_1}, @var{list_2})}.\n\ |
3447
|
270 @end deftypefn") |
3196
|
271 { |
|
272 octave_value retval; |
|
273 |
|
274 int nargin = args.length (); |
|
275 |
|
276 if (nargin > 1 && nargin < 5) |
|
277 { |
|
278 octave_value_list list_1 = args(0).list_value (); |
|
279 |
|
280 if (! error_state) |
|
281 { |
3202
|
282 int offset = args(1).int_value (true); |
3196
|
283 |
|
284 if (! error_state) |
|
285 { |
3202
|
286 offset--; |
|
287 |
|
288 int length = 0; |
|
289 |
|
290 octave_value_list list_2; |
3196
|
291 |
3202
|
292 if (nargin < 3) |
|
293 length = list_1.length () - offset; |
|
294 else |
|
295 { |
|
296 length = args(2).int_value (true); |
3196
|
297 |
3202
|
298 if (! error_state) |
3196
|
299 { |
3202
|
300 if (nargin == 4) |
|
301 { |
|
302 list_2 = args(3).list_value (); |
3196
|
303 |
3202
|
304 if (error_state) |
|
305 error ("splice: fourth argument must be a list"); |
3196
|
306 } |
|
307 } |
3202
|
308 else |
|
309 error ("splice: LENGTH must be an integer"); |
|
310 } |
3196
|
311 |
3202
|
312 if (! error_state) |
|
313 retval = list_1.splice (offset, length, list_2); |
3196
|
314 } |
|
315 else |
|
316 error ("splice: OFFSET must be an integer"); |
|
317 } |
|
318 else |
|
319 error ("splice: first argument must be a list"); |
|
320 } |
|
321 else |
|
322 print_usage ("splice"); |
|
323 |
|
324 return retval; |
|
325 } |
|
326 |
2882
|
327 /* |
|
328 ;;; Local Variables: *** |
|
329 ;;; mode: C++ *** |
|
330 ;;; End: *** |
|
331 */ |