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