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 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
|
32 #include <strstream> |
3353
|
33 |
|
34 #include "lo-utils.h" |
|
35 |
|
36 #include "defun.h" |
|
37 #include "error.h" |
|
38 #include "ov-cell.h" |
3354
|
39 #include "oct-obj.h" |
3353
|
40 #include "unwind-prot.h" |
3354
|
41 #include "utils.h" |
3353
|
42 |
|
43 DEFINE_OCTAVE_ALLOCATOR (octave_cell); |
|
44 |
|
45 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell"); |
|
46 |
|
47 octave_value |
|
48 octave_cell::do_index_op (const octave_value_list& idx) |
|
49 { |
|
50 octave_value retval; |
|
51 |
3354
|
52 int len = idx.length (); |
|
53 |
|
54 switch (len) |
3353
|
55 { |
3354
|
56 case 2: |
|
57 { |
|
58 idx_vector i = idx (0).index_vector (); |
|
59 idx_vector j = idx (1).index_vector (); |
|
60 |
|
61 retval = cell_val.index (i, j); |
|
62 } |
|
63 break; |
3353
|
64 |
3354
|
65 case 1: |
|
66 { |
|
67 idx_vector i = idx (0).index_vector (); |
|
68 |
|
69 retval = cell_val.index (i); |
|
70 } |
|
71 break; |
|
72 |
|
73 default: |
|
74 { |
|
75 string n = type_name (); |
|
76 |
|
77 error ("invalid number of indices (%d) for %s value", |
|
78 len, n.c_str ()); |
|
79 } |
|
80 break; |
3353
|
81 } |
|
82 |
|
83 return retval; |
|
84 } |
|
85 |
|
86 void |
|
87 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) |
|
88 { |
|
89 #if 0 |
|
90 if (idx.length () == 1) |
|
91 { |
|
92 int i = idx(0).int_value (true); |
|
93 |
|
94 if (! error_state) |
|
95 { |
|
96 int n = lst.length (); |
|
97 |
|
98 if (i > 0 && (Vresize_on_range_error || i <= n)) |
|
99 lst(i-1) = rhs; |
|
100 else |
|
101 error ("list index = %d out of range", i); |
|
102 } |
|
103 else |
|
104 error ("list index must be an integer"); |
|
105 } |
|
106 else |
|
107 error ("lists may only be indexed by a single scalar"); |
|
108 #endif |
|
109 } |
|
110 |
|
111 void |
|
112 octave_cell::print (ostream& os, bool) const |
|
113 { |
|
114 print_raw (os); |
|
115 } |
|
116 |
|
117 void |
|
118 octave_cell::print_raw (ostream& os, bool) const |
|
119 { |
|
120 unwind_protect::begin_frame ("octave_cell_print"); |
|
121 |
|
122 int nr = cell_val.rows (); |
|
123 int nc = cell_val.columns(); |
|
124 |
|
125 if (nr > 0 && nc > 0) |
|
126 { |
|
127 indent (os); |
|
128 os << "{"; |
|
129 newline (os); |
|
130 |
|
131 increment_indent_level (); |
|
132 |
|
133 for (int j = 0; j < nc; j++) |
|
134 { |
|
135 for (int i = 0; i < nr; i++) |
|
136 { |
|
137 ostrstream buf; |
|
138 buf << "[" << i+1 << "," << j+1 << "]" << ends; |
|
139 const char *nm = buf.str (); |
|
140 |
|
141 octave_value val = cell_val(i,j); |
|
142 |
|
143 val.print_with_name (os, nm); |
|
144 |
|
145 delete [] nm; |
|
146 } |
|
147 } |
|
148 |
|
149 decrement_indent_level (); |
|
150 |
|
151 indent (os); |
|
152 |
|
153 os << "}"; |
|
154 } |
|
155 else |
|
156 os << "{}"; |
|
157 |
|
158 newline (os); |
|
159 |
|
160 unwind_protect::run_frame ("octave_cell_print"); |
|
161 } |
|
162 |
|
163 bool |
|
164 octave_cell::print_name_tag (ostream& os, const string& name) const |
|
165 { |
|
166 indent (os); |
|
167 if (is_empty ()) |
|
168 os << name << " = "; |
|
169 else |
|
170 { |
|
171 os << name << " ="; |
|
172 newline (os); |
|
173 } |
|
174 return false; |
|
175 } |
|
176 |
3354
|
177 DEFUN (iscell, args, , |
3448
|
178 "-*- texinfo -*-\n\ |
|
179 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ |
|
180 Return true if @var{x} is a cell array object. Otherwise, return\n\ |
|
181 false.\n\ |
|
182 @end deftypefn") |
3354
|
183 { |
|
184 octave_value retval; |
|
185 |
|
186 if (args.length () == 1) |
|
187 retval = args(0).is_cell (); |
|
188 else |
|
189 print_usage ("iscell"); |
|
190 |
|
191 return retval; |
|
192 } |
|
193 |
|
194 DEFUN (cell, args, , |
3448
|
195 "-*- texinfo -*-\n\ |
|
196 @deftypefn {Built-in Function} {} cell (@var{x})\n\ |
|
197 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ |
|
198 Create a new cell array object. If invoked with a single scalar\n\ |
|
199 argument, @code{cell} returns a square cell array with the dimension\n\ |
|
200 specified. If you supply two scalar arguments, @code{cell} takes\n\ |
|
201 them to be the number of rows and columns. If given a vector with two\n\ |
|
202 elements, @code{cell} uses the values of the elements as the number of\n\ |
|
203 rows and columns, respectively.\n\ |
|
204 @end deftypefn") |
3354
|
205 { |
|
206 octave_value retval; |
|
207 |
|
208 int nargin = args.length (); |
|
209 |
|
210 switch (nargin) |
|
211 { |
|
212 case 1: |
|
213 { |
|
214 int nr, nc; |
|
215 get_dimensions (args(0), "cell", nr, nc); |
|
216 |
|
217 if (! error_state) |
|
218 retval = Cell (nr, nc, Matrix ()); |
|
219 } |
|
220 break; |
|
221 |
|
222 case 2: |
|
223 { |
|
224 int nr, nc; |
|
225 get_dimensions (args(0), args(1), "cell", nr, nc); |
|
226 |
|
227 if (! error_state) |
|
228 retval = Cell (nr, nc, Matrix ()); |
|
229 } |
|
230 break; |
|
231 |
|
232 default: |
|
233 print_usage ("cell"); |
|
234 break; |
|
235 } |
|
236 |
|
237 return retval; |
|
238 } |
|
239 |
3353
|
240 /* |
|
241 ;;; Local Variables: *** |
|
242 ;;; mode: C++ *** |
|
243 ;;; End: *** |
|
244 */ |