3353
|
1 /* |
|
2 |
|
3 Copyright (C) 1999 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> |
3353
|
32 |
4051
|
33 #include "lo-sstream.h" |
3353
|
34 #include "lo-utils.h" |
|
35 |
|
36 #include "defun.h" |
|
37 #include "error.h" |
|
38 #include "ov-cell.h" |
3354
|
39 #include "oct-obj.h" |
3353
|
40 #include "unwind-prot.h" |
3354
|
41 #include "utils.h" |
3928
|
42 #include "ov-base-mat.h" |
|
43 #include "ov-base-mat.cc" |
|
44 #include "ov-re-mat.h" |
|
45 #include "ov-scalar.h" |
|
46 |
|
47 template class octave_base_matrix<Cell>; |
3353
|
48 |
|
49 DEFINE_OCTAVE_ALLOCATOR (octave_cell); |
|
50 |
|
51 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell"); |
|
52 |
3933
|
53 octave_value |
|
54 octave_cell::subsref (const std::string type, |
|
55 const SLList<octave_value_list>& idx) |
|
56 { |
|
57 octave_value retval; |
|
58 |
|
59 switch (type[0]) |
|
60 { |
|
61 case '(': |
|
62 retval = do_index_op (idx.front ()); |
|
63 break; |
|
64 |
|
65 case '{': |
|
66 { |
|
67 octave_value tmp = do_index_op (idx.front ()); |
|
68 |
|
69 Cell tcell = tmp.cell_value (); |
|
70 |
|
71 if (tcell.length () == 1) |
|
72 retval = tcell(0,0); |
|
73 else |
|
74 { |
|
75 int nr = tcell.rows (); |
|
76 int nc = tcell.columns (); |
|
77 octave_value_list lst (nr * nc, octave_value ()); |
|
78 int k = 0; |
|
79 for (int j = 0; j < nc; j++) |
|
80 for (int i = 0; i < nr; i++) |
|
81 lst(k++) = tcell(i,j); |
3977
|
82 retval = octave_value (lst, true); |
3933
|
83 } |
|
84 } |
|
85 break; |
|
86 |
|
87 case '.': |
|
88 { |
|
89 std::string nm = type_name (); |
|
90 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
91 } |
|
92 break; |
|
93 |
|
94 default: |
|
95 panic_impossible (); |
|
96 } |
|
97 |
|
98 return retval.next_subsref (type, idx); |
|
99 } |
|
100 |
|
101 octave_value |
|
102 octave_cell::subsasgn (const std::string type, |
|
103 const SLList<octave_value_list>& idx, |
|
104 const octave_value& rhs) |
|
105 { |
|
106 octave_value retval; |
|
107 |
|
108 int n = type.length (); |
|
109 |
|
110 octave_value t_rhs = rhs; |
|
111 |
|
112 if (n > 1) |
|
113 { |
|
114 switch (type[0]) |
|
115 { |
|
116 case '(': |
|
117 { |
|
118 octave_value tmp = do_index_op (idx.front (), true); |
|
119 |
|
120 if (! tmp.is_defined ()) |
|
121 tmp = octave_value::empty_conv (type.substr (1), rhs); |
|
122 |
|
123 if (! error_state) |
|
124 { |
|
125 SLList<octave_value_list> next_idx (idx); |
|
126 |
|
127 next_idx.remove_front (); |
|
128 |
|
129 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); |
|
130 } |
|
131 } |
|
132 break; |
|
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 Cell tcell = tmp.cell_value (); |
|
142 |
|
143 if (! error_state && tcell.length () == 1) |
|
144 { |
|
145 tmp = tcell(0,0); |
|
146 |
|
147 SLList<octave_value_list> next_idx (idx); |
|
148 |
|
149 next_idx.remove_front (); |
|
150 |
|
151 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); |
|
152 } |
|
153 } |
|
154 break; |
|
155 |
|
156 case '.': |
|
157 { |
|
158 std::string nm = type_name (); |
|
159 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
160 } |
|
161 break; |
|
162 |
|
163 default: |
|
164 panic_impossible (); |
|
165 } |
|
166 } |
|
167 |
3940
|
168 if (! error_state) |
3933
|
169 { |
3940
|
170 switch (type[0]) |
|
171 { |
|
172 case '(': |
|
173 { |
|
174 octave_value_list i = idx.front (); |
3933
|
175 |
3940
|
176 if (t_rhs.is_cell ()) |
|
177 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); |
|
178 else |
|
179 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); |
3933
|
180 |
3940
|
181 retval = octave_value (this, count + 1); |
|
182 } |
|
183 break; |
3933
|
184 |
3940
|
185 case '{': |
|
186 { |
|
187 octave_value_list i = idx.front (); |
3933
|
188 |
3940
|
189 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); |
3933
|
190 |
3940
|
191 retval = octave_value (this, count + 1); |
|
192 } |
|
193 break; |
3933
|
194 |
3940
|
195 case '.': |
|
196 { |
|
197 std::string nm = type_name (); |
|
198 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
199 } |
|
200 break; |
3933
|
201 |
3940
|
202 default: |
|
203 panic_impossible (); |
|
204 } |
3933
|
205 } |
|
206 |
|
207 return retval; |
|
208 } |
|
209 |
3353
|
210 void |
|
211 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) |
|
212 { |
3928
|
213 if (rhs.is_cell ()) |
|
214 octave_base_matrix<Cell>::assign (idx, rhs.cell_value ()); |
3353
|
215 else |
3928
|
216 octave_base_matrix<Cell>::assign (idx, Cell (rhs)); |
3353
|
217 } |
|
218 |
3933
|
219 octave_value_list |
|
220 octave_cell::list_value (void) const |
|
221 { |
|
222 octave_value_list retval; |
|
223 |
|
224 int nr = rows (); |
|
225 int nc = columns (); |
|
226 |
|
227 if (nr == 1 && nc > 0) |
|
228 { |
|
229 retval.resize (nc); |
|
230 |
|
231 for (int i = 0; i < nc; i++) |
|
232 retval(i) = matrix(0,i); |
|
233 } |
|
234 else if (nc == 1 && nr > 0) |
|
235 { |
|
236 retval.resize (nr); |
|
237 |
|
238 for (int i = 0; i < nr; i++) |
|
239 retval(i) = matrix(i,0); |
|
240 } |
|
241 else |
|
242 error ("invalid conversion from cell array to list"); |
|
243 |
|
244 return retval; |
|
245 } |
|
246 |
|
247 void |
|
248 octave_cell::print (std::ostream& os, bool) const |
|
249 { |
|
250 print_raw (os); |
|
251 } |
|
252 |
|
253 void |
|
254 octave_cell::print_raw (std::ostream& os, bool) const |
|
255 { |
|
256 int nr = rows (); |
|
257 int nc = columns (); |
|
258 |
|
259 if (nr > 0 && nc > 0) |
|
260 { |
|
261 indent (os); |
|
262 os << "{"; |
|
263 newline (os); |
|
264 |
|
265 increment_indent_level (); |
|
266 |
|
267 for (int j = 0; j < nc; j++) |
|
268 { |
|
269 for (int i = 0; i < nr; i++) |
|
270 { |
4051
|
271 OSSTREAM buf; |
|
272 buf << "[" << i+1 << "," << j+1 << "]" << OSSTREAM_ENDS; |
3933
|
273 |
|
274 octave_value val = matrix(i,j); |
|
275 |
4051
|
276 val.print_with_name (os, OSSTREAM_STR (buf)); |
3933
|
277 |
4051
|
278 OSSTREAM_FREEZE (buf); |
3933
|
279 } |
|
280 } |
|
281 |
|
282 decrement_indent_level (); |
|
283 |
|
284 indent (os); |
|
285 os << "}"; |
|
286 newline (os); |
|
287 } |
|
288 else |
4011
|
289 { |
|
290 os << "{}"; |
|
291 if (nr > 0 || nc > 0) |
|
292 os << "(" << nr << "x" << nc << ")"; |
|
293 os << "\n"; |
|
294 } |
3933
|
295 } |
|
296 |
|
297 bool |
|
298 octave_cell::print_name_tag (std::ostream& os, const std::string& name) const |
|
299 { |
|
300 indent (os); |
4011
|
301 |
|
302 int nr = rows (); |
|
303 int nc = columns (); |
|
304 |
|
305 if (nr > 0 && nc > 0) |
|
306 { |
|
307 os << name << " ="; |
|
308 newline (os); |
|
309 } |
|
310 else |
|
311 os << name << " = "; |
|
312 |
3933
|
313 return false; |
|
314 } |
|
315 |
3354
|
316 DEFUN (iscell, args, , |
3448
|
317 "-*- texinfo -*-\n\ |
|
318 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ |
|
319 Return true if @var{x} is a cell array object. Otherwise, return\n\ |
|
320 false.\n\ |
|
321 @end deftypefn") |
3354
|
322 { |
|
323 octave_value retval; |
|
324 |
|
325 if (args.length () == 1) |
|
326 retval = args(0).is_cell (); |
|
327 else |
|
328 print_usage ("iscell"); |
|
329 |
|
330 return retval; |
|
331 } |
|
332 |
|
333 DEFUN (cell, args, , |
3448
|
334 "-*- texinfo -*-\n\ |
|
335 @deftypefn {Built-in Function} {} cell (@var{x})\n\ |
|
336 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ |
|
337 Create a new cell array object. If invoked with a single scalar\n\ |
|
338 argument, @code{cell} returns a square cell array with the dimension\n\ |
|
339 specified. If you supply two scalar arguments, @code{cell} takes\n\ |
|
340 them to be the number of rows and columns. If given a vector with two\n\ |
|
341 elements, @code{cell} uses the values of the elements as the number of\n\ |
|
342 rows and columns, respectively.\n\ |
|
343 @end deftypefn") |
3354
|
344 { |
|
345 octave_value retval; |
|
346 |
|
347 int nargin = args.length (); |
|
348 |
|
349 switch (nargin) |
|
350 { |
|
351 case 1: |
|
352 { |
|
353 int nr, nc; |
|
354 get_dimensions (args(0), "cell", nr, nc); |
|
355 |
|
356 if (! error_state) |
|
357 retval = Cell (nr, nc, Matrix ()); |
|
358 } |
|
359 break; |
|
360 |
|
361 case 2: |
|
362 { |
|
363 int nr, nc; |
|
364 get_dimensions (args(0), args(1), "cell", nr, nc); |
|
365 |
|
366 if (! error_state) |
|
367 retval = Cell (nr, nc, Matrix ()); |
|
368 } |
|
369 break; |
|
370 |
|
371 default: |
|
372 print_usage ("cell"); |
|
373 break; |
|
374 } |
|
375 |
|
376 return retval; |
|
377 } |
|
378 |
3353
|
379 /* |
|
380 ;;; Local Variables: *** |
|
381 ;;; mode: C++ *** |
|
382 ;;; End: *** |
|
383 */ |