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 |
|
31 #include <iostream.h> |
2916
|
32 #include <strstream.h> |
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 |
|
41 octave_allocator |
|
42 octave_list::allocator (sizeof (octave_list)); |
|
43 |
|
44 int |
|
45 octave_list::t_id (-1); |
|
46 |
|
47 const string |
|
48 octave_list::t_name ("list"); |
|
49 |
|
50 octave_value |
2974
|
51 octave_list::do_index_op (const octave_value_list& idx) |
2882
|
52 { |
|
53 octave_value retval; |
|
54 |
|
55 if (idx.length () == 1) |
|
56 { |
|
57 double d = idx(0).double_value (); |
|
58 |
|
59 if (! error_state) |
|
60 { |
|
61 if (D_NINT (d) == d) |
|
62 { |
|
63 int n = lst.length (); |
|
64 |
|
65 int i = static_cast<int> (d); |
|
66 |
|
67 if (i > 0 && i <= n) |
|
68 retval = lst(i-1); |
|
69 else |
|
70 error ("list index = %d out of range", i); |
|
71 } |
|
72 else |
|
73 error ("list index must be an integer"); |
|
74 } |
|
75 } |
|
76 else |
|
77 error ("lists may only be indexed by a single scalar"); |
|
78 |
|
79 return retval; |
|
80 } |
|
81 |
|
82 void |
3196
|
83 octave_list::assign (const octave_value_list& idx, const octave_value& rhs) |
|
84 { |
|
85 if (idx.length () == 1) |
|
86 { |
|
87 double d = idx(0).double_value (); |
|
88 |
|
89 if (! error_state) |
|
90 { |
|
91 if (D_NINT (d) == d) |
|
92 { |
|
93 int n = lst.length (); |
|
94 |
|
95 int i = static_cast<int> (d); |
|
96 |
|
97 if (i > 0 && (Vresize_on_range_error || i <= n)) |
|
98 lst(i) = rhs; |
|
99 else |
|
100 error ("list index = %d out of range", i); |
|
101 } |
|
102 else |
|
103 error ("list index must be an integer"); |
|
104 } |
|
105 } |
|
106 else |
|
107 error ("lists may only be indexed by a single scalar"); |
|
108 } |
|
109 |
|
110 void |
2901
|
111 octave_list::print (ostream& os, bool) const |
|
112 { |
|
113 print_raw (os); |
|
114 } |
|
115 |
|
116 void |
|
117 octave_list::print_raw (ostream& os, bool) const |
2882
|
118 { |
2985
|
119 unwind_protect::begin_frame ("octave_list_print"); |
2882
|
120 |
|
121 int n = lst.length (); |
|
122 |
3196
|
123 if (n > 0) |
2882
|
124 { |
3196
|
125 indent (os); |
|
126 os << "("; |
|
127 newline (os); |
|
128 |
|
129 increment_indent_level (); |
2916
|
130 |
3196
|
131 for (int i = 0; i < n; i++) |
|
132 { |
|
133 ostrstream buf; |
|
134 buf << "[" << i+1 << "]" << ends; |
|
135 const char *nm = buf.str (); |
2882
|
136 |
3196
|
137 octave_value val = lst(i); |
2916
|
138 |
3196
|
139 val.print_with_name (os, nm); |
|
140 |
|
141 delete [] nm; |
|
142 } |
2882
|
143 |
3196
|
144 decrement_indent_level (); |
2882
|
145 |
3196
|
146 indent (os); |
|
147 os << ")"; |
|
148 } |
|
149 else |
|
150 os << "()"; |
|
151 |
2901
|
152 newline (os); |
2882
|
153 |
2985
|
154 unwind_protect::run_frame ("octave_list_print"); |
2882
|
155 } |
|
156 |
2901
|
157 bool |
|
158 octave_list::print_name_tag (ostream& os, const string& name) const |
|
159 { |
|
160 indent (os); |
3196
|
161 if (lst.length () == 0) |
|
162 os << name << " = "; |
|
163 else |
|
164 { |
|
165 os << name << " ="; |
|
166 newline (os); |
|
167 } |
2901
|
168 return false; |
|
169 } |
|
170 |
2993
|
171 DEFUN (list, args, , |
|
172 "list (ARGS)\n\ |
2882
|
173 \n\ |
|
174 Create a new list from ARGS.") |
|
175 { |
|
176 return octave_value (args); |
|
177 } |
|
178 |
|
179 DEFUN (append, args, , |
|
180 "append (LIST, ARGS)\n\ |
|
181 \n\ |
|
182 Return a new list created by appending ARGS to LIST") |
|
183 { |
|
184 octave_value retval; |
|
185 |
|
186 int nargin = args.length (); |
|
187 |
|
188 if (nargin > 1) |
|
189 { |
|
190 octave_value_list tmp = args(0).list_value (); |
|
191 |
|
192 if (! error_state) |
|
193 { |
|
194 for (int i = 1; i < nargin; i++) |
|
195 tmp.append (args(i)); |
|
196 |
|
197 retval = tmp; |
|
198 } |
|
199 } |
|
200 else |
|
201 print_usage ("append"); |
|
202 |
|
203 return retval; |
|
204 } |
|
205 |
|
206 DEFUN (reverse, args, , |
|
207 "reverse (LIST)\n\ |
|
208 \n\ |
|
209 Return a new list created by reversing the elements of LIST") |
|
210 { |
|
211 octave_value retval; |
|
212 |
|
213 int nargin = args.length (); |
|
214 |
|
215 if (nargin == 1) |
|
216 { |
|
217 octave_value_list tmp = args(0).list_value (); |
|
218 |
|
219 if (! error_state) |
|
220 retval = tmp.reverse (); |
|
221 } |
|
222 else |
|
223 print_usage ("reverse"); |
|
224 |
|
225 return retval; |
|
226 } |
|
227 |
3196
|
228 DEFUN (splice, args, , |
|
229 "splice (LIST_1, OFFSET, LENGTH, LIST_2)\n\ |
|
230 splice (LIST_1, OFFSET, LENGTH, LIST_2)\n\ |
|
231 splice (LIST_1, OFFSET, LENGTH)\n\ |
|
232 splice (LIST_1, OFFSET)\n\ |
|
233 \n\ |
|
234 Replace LENGTH elements of LIST_1 beginning at OFFSET with the |
|
235 contents of LIST_2 (if any). If LENGTH is omitted, ") |
|
236 { |
|
237 octave_value retval; |
|
238 |
|
239 int nargin = args.length (); |
|
240 |
|
241 if (nargin > 1 && nargin < 5) |
|
242 { |
|
243 octave_value_list list_1 = args(0).list_value (); |
|
244 |
|
245 if (! error_state) |
|
246 { |
|
247 double d_offset = args(1).double_value (); |
|
248 |
|
249 if (! error_state) |
|
250 { |
|
251 if (D_NINT (d_offset) == d_offset) |
|
252 { |
|
253 int offset = static_cast<int> (d_offset) - 1; |
|
254 |
|
255 int length = 0; |
|
256 |
|
257 if (nargin < 3) |
|
258 length = list_1.length () - offset; |
|
259 else |
|
260 { |
|
261 double d_length = args(2).double_value (); |
|
262 |
|
263 if (error_state) |
|
264 return retval; |
|
265 else |
|
266 { |
|
267 if (D_NINT (d_length) == d_length) |
|
268 length = static_cast<int> (d_length); |
|
269 else |
|
270 error ("splice: LENGTH must be an integer"); |
|
271 } |
|
272 } |
|
273 |
|
274 octave_value_list list_2; |
|
275 |
|
276 if (nargin == 4) |
|
277 { |
|
278 list_2 = args(3).list_value (); |
|
279 |
|
280 if (error_state) |
|
281 { |
|
282 error ("splice: fourth argument must be a list"); |
|
283 return retval; |
|
284 } |
|
285 } |
|
286 |
|
287 retval = list_1.splice (offset, length, list_2); |
|
288 } |
|
289 else |
|
290 error ("splice: OFFSET must be an integer"); |
|
291 } |
|
292 else |
|
293 error ("splice: OFFSET must be an integer"); |
|
294 } |
|
295 else |
|
296 error ("splice: first argument must be a list"); |
|
297 } |
|
298 else |
|
299 print_usage ("splice"); |
|
300 |
|
301 return retval; |
|
302 } |
|
303 |
2882
|
304 /* |
|
305 ;;; Local Variables: *** |
|
306 ;;; mode: C++ *** |
|
307 ;;; End: *** |
|
308 */ |