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 |
|
252 octave_cell::all_strings (void) const |
|
253 { |
|
254 int nr = rows (); |
|
255 int nc = columns (); |
|
256 |
|
257 string_vector retval (nr * nc); |
|
258 |
|
259 int k = 0; |
|
260 |
|
261 for (int j = 0; j < nc; j++) |
|
262 { |
|
263 for (int i = 0; i < nr; i++) |
|
264 { |
|
265 retval[k++] = matrix(i,j).string_value (); |
|
266 |
|
267 if (error_state) |
|
268 return string_vector (); |
|
269 } |
|
270 } |
|
271 |
|
272 return retval; |
|
273 } |
|
274 |
3933
|
275 void |
|
276 octave_cell::print (std::ostream& os, bool) const |
|
277 { |
|
278 print_raw (os); |
|
279 } |
|
280 |
|
281 void |
|
282 octave_cell::print_raw (std::ostream& os, bool) const |
|
283 { |
|
284 int nr = rows (); |
|
285 int nc = columns (); |
|
286 |
|
287 if (nr > 0 && nc > 0) |
|
288 { |
|
289 indent (os); |
|
290 os << "{"; |
|
291 newline (os); |
|
292 |
|
293 increment_indent_level (); |
|
294 |
|
295 for (int j = 0; j < nc; j++) |
|
296 { |
|
297 for (int i = 0; i < nr; i++) |
|
298 { |
4153
|
299 OCTAVE_QUIT; |
|
300 |
4051
|
301 OSSTREAM buf; |
|
302 buf << "[" << i+1 << "," << j+1 << "]" << OSSTREAM_ENDS; |
3933
|
303 |
|
304 octave_value val = matrix(i,j); |
|
305 |
4051
|
306 val.print_with_name (os, OSSTREAM_STR (buf)); |
3933
|
307 |
4051
|
308 OSSTREAM_FREEZE (buf); |
3933
|
309 } |
|
310 } |
|
311 |
|
312 decrement_indent_level (); |
|
313 |
|
314 indent (os); |
|
315 os << "}"; |
|
316 newline (os); |
|
317 } |
|
318 else |
4011
|
319 { |
|
320 os << "{}"; |
|
321 if (nr > 0 || nc > 0) |
|
322 os << "(" << nr << "x" << nc << ")"; |
|
323 os << "\n"; |
|
324 } |
3933
|
325 } |
|
326 |
|
327 bool |
|
328 octave_cell::print_name_tag (std::ostream& os, const std::string& name) const |
|
329 { |
|
330 indent (os); |
4011
|
331 |
|
332 int nr = rows (); |
|
333 int nc = columns (); |
|
334 |
|
335 if (nr > 0 && nc > 0) |
|
336 { |
|
337 os << name << " ="; |
|
338 newline (os); |
|
339 } |
|
340 else |
|
341 os << name << " = "; |
|
342 |
3933
|
343 return false; |
|
344 } |
|
345 |
3354
|
346 DEFUN (iscell, args, , |
3448
|
347 "-*- texinfo -*-\n\ |
|
348 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ |
|
349 Return true if @var{x} is a cell array object. Otherwise, return\n\ |
|
350 false.\n\ |
|
351 @end deftypefn") |
3354
|
352 { |
|
353 octave_value retval; |
|
354 |
|
355 if (args.length () == 1) |
|
356 retval = args(0).is_cell (); |
|
357 else |
|
358 print_usage ("iscell"); |
|
359 |
|
360 return retval; |
|
361 } |
|
362 |
|
363 DEFUN (cell, args, , |
3448
|
364 "-*- texinfo -*-\n\ |
|
365 @deftypefn {Built-in Function} {} cell (@var{x})\n\ |
|
366 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ |
|
367 Create a new cell array object. If invoked with a single scalar\n\ |
|
368 argument, @code{cell} returns a square cell array with the dimension\n\ |
|
369 specified. If you supply two scalar arguments, @code{cell} takes\n\ |
|
370 them to be the number of rows and columns. If given a vector with two\n\ |
|
371 elements, @code{cell} uses the values of the elements as the number of\n\ |
|
372 rows and columns, respectively.\n\ |
|
373 @end deftypefn") |
3354
|
374 { |
|
375 octave_value retval; |
|
376 |
|
377 int nargin = args.length (); |
|
378 |
|
379 switch (nargin) |
|
380 { |
|
381 case 1: |
|
382 { |
|
383 int nr, nc; |
|
384 get_dimensions (args(0), "cell", nr, nc); |
|
385 |
|
386 if (! error_state) |
|
387 retval = Cell (nr, nc, Matrix ()); |
|
388 } |
|
389 break; |
|
390 |
|
391 case 2: |
|
392 { |
|
393 int nr, nc; |
|
394 get_dimensions (args(0), args(1), "cell", nr, nc); |
|
395 |
|
396 if (! error_state) |
|
397 retval = Cell (nr, nc, Matrix ()); |
|
398 } |
|
399 break; |
|
400 |
|
401 default: |
|
402 print_usage ("cell"); |
|
403 break; |
|
404 } |
|
405 |
|
406 return retval; |
|
407 } |
|
408 |
3353
|
409 /* |
|
410 ;;; Local Variables: *** |
|
411 ;;; mode: C++ *** |
|
412 ;;; End: *** |
|
413 */ |