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 { |
4513
|
330 int ndims = matrix.ndims (); |
|
331 |
|
332 if (ndims == 2) |
|
333 { |
|
334 int nr = rows (); |
|
335 int nc = columns (); |
|
336 |
|
337 if (nr > 0 && nc > 0) |
|
338 { |
|
339 indent (os); |
|
340 os << "{"; |
|
341 newline (os); |
|
342 |
|
343 increment_indent_level (); |
|
344 |
|
345 for (int j = 0; j < nc; j++) |
|
346 { |
|
347 for (int i = 0; i < nr; i++) |
|
348 { |
|
349 OCTAVE_QUIT; |
|
350 |
|
351 OSSTREAM buf; |
|
352 buf << "[" << i+1 << "," << j+1 << "]" << OSSTREAM_ENDS; |
3933
|
353 |
4513
|
354 octave_value val = matrix(i,j); |
|
355 |
|
356 val.print_with_name (os, OSSTREAM_STR (buf)); |
|
357 |
|
358 OSSTREAM_FREEZE (buf); |
|
359 } |
|
360 } |
|
361 |
|
362 decrement_indent_level (); |
|
363 |
|
364 indent (os); |
|
365 os << "}"; |
|
366 newline (os); |
|
367 } |
|
368 else |
|
369 { |
|
370 os << "{}"; |
|
371 if (nr > 0 || nc > 0) |
|
372 os << "(" << nr << "x" << nc << ")"; |
|
373 os << "\n"; |
|
374 } |
|
375 } |
|
376 else |
3933
|
377 { |
|
378 indent (os); |
|
379 os << "{"; |
4513
|
380 dim_vector dv = matrix.dims (); |
|
381 for (int i = 0; i < ndims; i++) |
3933
|
382 { |
4513
|
383 if (i > 0) |
|
384 os << "x"; |
|
385 os << dv(i); |
3933
|
386 } |
4513
|
387 os << " 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 |
|
444 switch (nargin) |
|
445 { |
|
446 case 1: |
|
447 { |
|
448 int nr, nc; |
|
449 get_dimensions (args(0), "cell", nr, nc); |
|
450 |
|
451 if (! error_state) |
|
452 retval = Cell (nr, nc, Matrix ()); |
|
453 } |
|
454 break; |
|
455 |
|
456 case 2: |
|
457 { |
|
458 int nr, nc; |
|
459 get_dimensions (args(0), args(1), "cell", nr, nc); |
|
460 |
|
461 if (! error_state) |
|
462 retval = Cell (nr, nc, Matrix ()); |
|
463 } |
|
464 break; |
|
465 |
|
466 default: |
|
467 print_usage ("cell"); |
|
468 break; |
|
469 } |
|
470 |
|
471 return retval; |
|
472 } |
|
473 |
4358
|
474 DEFUN (cellstr, args, , |
|
475 "-*- texinfo -*-\n\ |
|
476 @deftypefn {Built-in Function} {} cellstr (@var{string})\n\ |
|
477 Create a new cell array object from the elements of the string\n\ |
|
478 array @var{string}.\n\ |
|
479 @end deftypefn") |
|
480 { |
|
481 octave_value retval; |
|
482 |
|
483 if (args.length () == 1) |
|
484 { |
|
485 string_vector s = args(0).all_strings (); |
|
486 |
|
487 if (! error_state) |
|
488 retval = Cell (s); |
|
489 else |
|
490 error ("cellstr: expecting argument to be a string"); |
|
491 } |
|
492 else |
|
493 print_usage ("cellstr"); |
|
494 |
|
495 return retval; |
|
496 } |
|
497 |
3353
|
498 /* |
|
499 ;;; Local Variables: *** |
|
500 ;;; mode: C++ *** |
|
501 ;;; End: *** |
|
502 */ |