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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
3353
|
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" |
4153
|
35 #include "quit.h" |
3353
|
36 |
|
37 #include "defun.h" |
|
38 #include "error.h" |
|
39 #include "ov-cell.h" |
3354
|
40 #include "oct-obj.h" |
3353
|
41 #include "unwind-prot.h" |
3354
|
42 #include "utils.h" |
3928
|
43 #include "ov-base-mat.h" |
|
44 #include "ov-base-mat.cc" |
|
45 #include "ov-re-mat.h" |
|
46 #include "ov-scalar.h" |
|
47 |
|
48 template class octave_base_matrix<Cell>; |
3353
|
49 |
|
50 DEFINE_OCTAVE_ALLOCATOR (octave_cell); |
|
51 |
|
52 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell"); |
|
53 |
3933
|
54 octave_value |
4247
|
55 octave_cell::subsref (const std::string& type, |
4219
|
56 const std::list<octave_value_list>& idx) |
3933
|
57 { |
|
58 octave_value retval; |
|
59 |
|
60 switch (type[0]) |
|
61 { |
|
62 case '(': |
|
63 retval = do_index_op (idx.front ()); |
|
64 break; |
|
65 |
|
66 case '{': |
|
67 { |
|
68 octave_value tmp = do_index_op (idx.front ()); |
|
69 |
|
70 Cell tcell = tmp.cell_value (); |
|
71 |
|
72 if (tcell.length () == 1) |
|
73 retval = tcell(0,0); |
|
74 else |
|
75 { |
|
76 int nr = tcell.rows (); |
|
77 int nc = tcell.columns (); |
|
78 octave_value_list lst (nr * nc, octave_value ()); |
|
79 int k = 0; |
|
80 for (int j = 0; j < nc; j++) |
|
81 for (int i = 0; i < nr; i++) |
4153
|
82 { |
|
83 OCTAVE_QUIT; |
|
84 lst(k++) = tcell(i,j); |
|
85 } |
3977
|
86 retval = octave_value (lst, true); |
3933
|
87 } |
|
88 } |
|
89 break; |
|
90 |
|
91 case '.': |
|
92 { |
|
93 std::string nm = type_name (); |
|
94 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
95 } |
|
96 break; |
|
97 |
|
98 default: |
|
99 panic_impossible (); |
|
100 } |
|
101 |
|
102 return retval.next_subsref (type, idx); |
|
103 } |
|
104 |
|
105 octave_value |
4247
|
106 octave_cell::subsasgn (const std::string& type, |
4219
|
107 const std::list<octave_value_list>& idx, |
3933
|
108 const octave_value& rhs) |
|
109 { |
|
110 octave_value retval; |
|
111 |
|
112 int n = type.length (); |
|
113 |
|
114 octave_value t_rhs = rhs; |
|
115 |
|
116 if (n > 1) |
|
117 { |
|
118 switch (type[0]) |
|
119 { |
|
120 case '(': |
|
121 { |
|
122 octave_value tmp = do_index_op (idx.front (), true); |
|
123 |
|
124 if (! tmp.is_defined ()) |
|
125 tmp = octave_value::empty_conv (type.substr (1), rhs); |
|
126 |
|
127 if (! error_state) |
|
128 { |
4219
|
129 std::list<octave_value_list> next_idx (idx); |
3933
|
130 |
4219
|
131 next_idx.erase (next_idx.begin ()); |
3933
|
132 |
4362
|
133 tmp.make_unique (); |
|
134 |
3933
|
135 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); |
|
136 } |
|
137 } |
|
138 break; |
|
139 |
|
140 case '{': |
|
141 { |
|
142 octave_value tmp = do_index_op (idx.front (), true); |
|
143 |
|
144 if (! tmp.is_defined ()) |
|
145 tmp = octave_value::empty_conv (type.substr (1), rhs); |
|
146 |
4362
|
147 if (! error_state) |
|
148 { |
|
149 Cell tcell = tmp.cell_value (); |
3933
|
150 |
4362
|
151 if (! error_state && tcell.length () == 1) |
|
152 { |
|
153 tmp = tcell(0,0); |
3933
|
154 |
4362
|
155 std::list<octave_value_list> next_idx (idx); |
|
156 |
|
157 next_idx.erase (next_idx.begin ()); |
3933
|
158 |
4362
|
159 tmp.make_unique (); |
3933
|
160 |
4362
|
161 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); |
|
162 } |
3933
|
163 } |
|
164 } |
|
165 break; |
|
166 |
|
167 case '.': |
|
168 { |
|
169 std::string nm = type_name (); |
|
170 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
171 } |
|
172 break; |
|
173 |
|
174 default: |
|
175 panic_impossible (); |
|
176 } |
|
177 } |
|
178 |
3940
|
179 if (! error_state) |
3933
|
180 { |
3940
|
181 switch (type[0]) |
|
182 { |
|
183 case '(': |
|
184 { |
|
185 octave_value_list i = idx.front (); |
3933
|
186 |
3940
|
187 if (t_rhs.is_cell ()) |
|
188 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); |
|
189 else |
|
190 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); |
3933
|
191 |
3940
|
192 retval = octave_value (this, count + 1); |
|
193 } |
|
194 break; |
3933
|
195 |
3940
|
196 case '{': |
|
197 { |
|
198 octave_value_list i = idx.front (); |
3933
|
199 |
3940
|
200 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); |
3933
|
201 |
3940
|
202 retval = octave_value (this, count + 1); |
|
203 } |
|
204 break; |
3933
|
205 |
3940
|
206 case '.': |
|
207 { |
|
208 std::string nm = type_name (); |
|
209 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
210 } |
|
211 break; |
3933
|
212 |
3940
|
213 default: |
|
214 panic_impossible (); |
|
215 } |
3933
|
216 } |
|
217 |
|
218 return retval; |
|
219 } |
|
220 |
3353
|
221 void |
|
222 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) |
|
223 { |
3928
|
224 if (rhs.is_cell ()) |
|
225 octave_base_matrix<Cell>::assign (idx, rhs.cell_value ()); |
3353
|
226 else |
3928
|
227 octave_base_matrix<Cell>::assign (idx, Cell (rhs)); |
3353
|
228 } |
|
229 |
3933
|
230 octave_value_list |
|
231 octave_cell::list_value (void) const |
|
232 { |
|
233 octave_value_list retval; |
|
234 |
|
235 int nr = rows (); |
|
236 int nc = columns (); |
|
237 |
|
238 if (nr == 1 && nc > 0) |
|
239 { |
|
240 retval.resize (nc); |
|
241 |
|
242 for (int i = 0; i < nc; i++) |
|
243 retval(i) = matrix(0,i); |
|
244 } |
|
245 else if (nc == 1 && nr > 0) |
|
246 { |
|
247 retval.resize (nr); |
|
248 |
|
249 for (int i = 0; i < nr; i++) |
|
250 retval(i) = matrix(i,0); |
|
251 } |
|
252 else |
|
253 error ("invalid conversion from cell array to list"); |
|
254 |
|
255 return retval; |
|
256 } |
|
257 |
4243
|
258 string_vector |
4457
|
259 octave_cell::all_strings (bool pad, bool) const |
4243
|
260 { |
4358
|
261 string_vector retval; |
|
262 |
4243
|
263 int nr = rows (); |
|
264 int nc = columns (); |
|
265 |
4358
|
266 int n_elts = 0; |
|
267 |
|
268 int max_len = 0; |
|
269 |
|
270 if (pad) |
|
271 { |
|
272 for (int j = 0; j < nc; j++) |
|
273 { |
|
274 for (int i = 0; i < nr; i++) |
|
275 { |
|
276 string_vector s = matrix(i,j).all_strings (); |
|
277 |
|
278 if (error_state) |
|
279 return retval; |
|
280 |
|
281 n_elts += s.length (); |
|
282 |
|
283 int s_max_len = s.max_length (); |
|
284 |
|
285 if (s_max_len > max_len) |
|
286 max_len = s_max_len; |
|
287 } |
|
288 } |
|
289 } |
|
290 |
|
291 retval.resize (n_elts); |
4243
|
292 |
|
293 int k = 0; |
|
294 |
|
295 for (int j = 0; j < nc; j++) |
|
296 { |
|
297 for (int i = 0; i < nr; i++) |
|
298 { |
4358
|
299 string_vector s = matrix(i,j).all_strings (); |
|
300 |
|
301 int n = s.length (); |
4243
|
302 |
4358
|
303 for (int ii = 0; ii < n; ii++) |
|
304 { |
|
305 std::string t = s[ii]; |
|
306 int t_len = t.length (); |
|
307 |
|
308 if (pad && max_len > t_len) |
|
309 t += std::string (max_len - t_len, ' '); |
|
310 |
|
311 retval[k++] = t; |
|
312 } |
4243
|
313 } |
|
314 } |
|
315 |
4358
|
316 |
|
317 |
4243
|
318 return retval; |
|
319 } |
|
320 |
3933
|
321 void |
|
322 octave_cell::print (std::ostream& os, bool) const |
|
323 { |
|
324 print_raw (os); |
|
325 } |
|
326 |
|
327 void |
|
328 octave_cell::print_raw (std::ostream& os, bool) const |
|
329 { |
|
330 int nr = rows (); |
|
331 int nc = columns (); |
|
332 |
|
333 if (nr > 0 && nc > 0) |
|
334 { |
|
335 indent (os); |
|
336 os << "{"; |
|
337 newline (os); |
|
338 |
|
339 increment_indent_level (); |
|
340 |
|
341 for (int j = 0; j < nc; j++) |
|
342 { |
|
343 for (int i = 0; i < nr; i++) |
|
344 { |
4153
|
345 OCTAVE_QUIT; |
|
346 |
4051
|
347 OSSTREAM buf; |
|
348 buf << "[" << i+1 << "," << j+1 << "]" << OSSTREAM_ENDS; |
3933
|
349 |
|
350 octave_value val = matrix(i,j); |
|
351 |
4051
|
352 val.print_with_name (os, OSSTREAM_STR (buf)); |
3933
|
353 |
4051
|
354 OSSTREAM_FREEZE (buf); |
3933
|
355 } |
|
356 } |
|
357 |
|
358 decrement_indent_level (); |
|
359 |
|
360 indent (os); |
|
361 os << "}"; |
|
362 newline (os); |
|
363 } |
|
364 else |
4011
|
365 { |
|
366 os << "{}"; |
|
367 if (nr > 0 || nc > 0) |
|
368 os << "(" << nr << "x" << nc << ")"; |
|
369 os << "\n"; |
|
370 } |
3933
|
371 } |
|
372 |
|
373 bool |
|
374 octave_cell::print_name_tag (std::ostream& os, const std::string& name) const |
|
375 { |
|
376 indent (os); |
4011
|
377 |
|
378 int nr = rows (); |
|
379 int nc = columns (); |
|
380 |
|
381 if (nr > 0 && nc > 0) |
|
382 { |
|
383 os << name << " ="; |
|
384 newline (os); |
|
385 } |
|
386 else |
|
387 os << name << " = "; |
|
388 |
3933
|
389 return false; |
|
390 } |
|
391 |
3354
|
392 DEFUN (iscell, args, , |
3448
|
393 "-*- texinfo -*-\n\ |
|
394 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ |
|
395 Return true if @var{x} is a cell array object. Otherwise, return\n\ |
|
396 false.\n\ |
|
397 @end deftypefn") |
3354
|
398 { |
|
399 octave_value retval; |
|
400 |
|
401 if (args.length () == 1) |
|
402 retval = args(0).is_cell (); |
|
403 else |
|
404 print_usage ("iscell"); |
|
405 |
|
406 return retval; |
|
407 } |
|
408 |
|
409 DEFUN (cell, args, , |
3448
|
410 "-*- texinfo -*-\n\ |
|
411 @deftypefn {Built-in Function} {} cell (@var{x})\n\ |
|
412 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ |
|
413 Create a new cell array object. If invoked with a single scalar\n\ |
|
414 argument, @code{cell} returns a square cell array with the dimension\n\ |
|
415 specified. If you supply two scalar arguments, @code{cell} takes\n\ |
|
416 them to be the number of rows and columns. If given a vector with two\n\ |
|
417 elements, @code{cell} uses the values of the elements as the number of\n\ |
|
418 rows and columns, respectively.\n\ |
|
419 @end deftypefn") |
3354
|
420 { |
|
421 octave_value retval; |
|
422 |
|
423 int nargin = args.length (); |
|
424 |
|
425 switch (nargin) |
|
426 { |
|
427 case 1: |
|
428 { |
|
429 int nr, nc; |
|
430 get_dimensions (args(0), "cell", nr, nc); |
|
431 |
|
432 if (! error_state) |
|
433 retval = Cell (nr, nc, Matrix ()); |
|
434 } |
|
435 break; |
|
436 |
|
437 case 2: |
|
438 { |
|
439 int nr, nc; |
|
440 get_dimensions (args(0), args(1), "cell", nr, nc); |
|
441 |
|
442 if (! error_state) |
|
443 retval = Cell (nr, nc, Matrix ()); |
|
444 } |
|
445 break; |
|
446 |
|
447 default: |
|
448 print_usage ("cell"); |
|
449 break; |
|
450 } |
|
451 |
|
452 return retval; |
|
453 } |
|
454 |
4358
|
455 DEFUN (cellstr, args, , |
|
456 "-*- texinfo -*-\n\ |
|
457 @deftypefn {Built-in Function} {} cellstr (@var{string})\n\ |
|
458 Create a new cell array object from the elements of the string\n\ |
|
459 array @var{string}.\n\ |
|
460 @end deftypefn") |
|
461 { |
|
462 octave_value retval; |
|
463 |
|
464 if (args.length () == 1) |
|
465 { |
|
466 string_vector s = args(0).all_strings (); |
|
467 |
|
468 if (! error_state) |
|
469 retval = Cell (s); |
|
470 else |
|
471 error ("cellstr: expecting argument to be a string"); |
|
472 } |
|
473 else |
|
474 print_usage ("cellstr"); |
|
475 |
|
476 return retval; |
|
477 } |
|
478 |
3353
|
479 /* |
|
480 ;;; Local Variables: *** |
|
481 ;;; mode: C++ *** |
|
482 ;;; End: *** |
|
483 */ |