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