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