Mercurial > hg > octave-nkf
comparison src/ov-cell.cc @ 3933:f9ea3dcf58ee
[project @ 2002-05-15 03:21:00 by jwe]
author | jwe |
---|---|
date | Wed, 15 May 2002 03:21:01 +0000 |
parents | e8627dc4bdf2 |
children | 1b58576bdaa6 |
comparison
equal
deleted
inserted
replaced
3932:2e2e32198722 | 3933:f9ea3dcf58ee |
---|---|
48 | 48 |
49 DEFINE_OCTAVE_ALLOCATOR (octave_cell); | 49 DEFINE_OCTAVE_ALLOCATOR (octave_cell); |
50 | 50 |
51 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell"); | 51 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell"); |
52 | 52 |
53 octave_value | |
54 octave_cell::subsref (const std::string type, | |
55 const SLList<octave_value_list>& idx) | |
56 { | |
57 octave_value retval; | |
58 | |
59 switch (type[0]) | |
60 { | |
61 case '(': | |
62 retval = do_index_op (idx.front ()); | |
63 break; | |
64 | |
65 case '{': | |
66 { | |
67 octave_value tmp = do_index_op (idx.front ()); | |
68 | |
69 Cell tcell = tmp.cell_value (); | |
70 | |
71 if (tcell.length () == 1) | |
72 retval = tcell(0,0); | |
73 else | |
74 { | |
75 int nr = tcell.rows (); | |
76 int nc = tcell.columns (); | |
77 octave_value_list lst (nr * nc, octave_value ()); | |
78 int k = 0; | |
79 for (int j = 0; j < nc; j++) | |
80 for (int i = 0; i < nr; i++) | |
81 lst(k++) = tcell(i,j); | |
82 retval = lst; | |
83 } | |
84 } | |
85 break; | |
86 | |
87 case '.': | |
88 { | |
89 std::string nm = type_name (); | |
90 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
91 } | |
92 break; | |
93 | |
94 default: | |
95 panic_impossible (); | |
96 } | |
97 | |
98 return retval.next_subsref (type, idx); | |
99 } | |
100 | |
101 octave_value | |
102 octave_cell::subsasgn (const std::string type, | |
103 const SLList<octave_value_list>& idx, | |
104 const octave_value& rhs) | |
105 { | |
106 octave_value retval; | |
107 | |
108 int n = type.length (); | |
109 | |
110 octave_value t_rhs = rhs; | |
111 | |
112 if (n > 1) | |
113 { | |
114 switch (type[0]) | |
115 { | |
116 case '(': | |
117 { | |
118 octave_value tmp = do_index_op (idx.front (), true); | |
119 | |
120 if (! tmp.is_defined ()) | |
121 tmp = octave_value::empty_conv (type.substr (1), rhs); | |
122 | |
123 if (! error_state) | |
124 { | |
125 SLList<octave_value_list> next_idx (idx); | |
126 | |
127 next_idx.remove_front (); | |
128 | |
129 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | |
130 } | |
131 } | |
132 break; | |
133 | |
134 case '{': | |
135 { | |
136 octave_value tmp = do_index_op (idx.front (), true); | |
137 | |
138 if (! tmp.is_defined ()) | |
139 tmp = octave_value::empty_conv (type.substr (1), rhs); | |
140 | |
141 Cell tcell = tmp.cell_value (); | |
142 | |
143 if (! error_state && tcell.length () == 1) | |
144 { | |
145 tmp = tcell(0,0); | |
146 | |
147 SLList<octave_value_list> next_idx (idx); | |
148 | |
149 next_idx.remove_front (); | |
150 | |
151 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | |
152 } | |
153 } | |
154 break; | |
155 | |
156 case '.': | |
157 { | |
158 std::string nm = type_name (); | |
159 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
160 } | |
161 break; | |
162 | |
163 default: | |
164 panic_impossible (); | |
165 } | |
166 } | |
167 | |
168 switch (type[0]) | |
169 { | |
170 case '(': | |
171 { | |
172 octave_value_list i = idx.front (); | |
173 | |
174 if (t_rhs.is_cell ()) | |
175 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); | |
176 else | |
177 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); | |
178 | |
179 retval = octave_value (this, count + 1); | |
180 } | |
181 break; | |
182 | |
183 case '{': | |
184 { | |
185 octave_value_list i = idx.front (); | |
186 | |
187 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); | |
188 | |
189 retval = octave_value (this, count + 1); | |
190 } | |
191 break; | |
192 | |
193 case '.': | |
194 { | |
195 std::string nm = type_name (); | |
196 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
197 } | |
198 break; | |
199 | |
200 default: | |
201 panic_impossible (); | |
202 } | |
203 | |
204 return retval; | |
205 } | |
206 | |
53 void | 207 void |
54 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) | 208 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) |
55 { | 209 { |
56 if (rhs.is_cell ()) | 210 if (rhs.is_cell ()) |
57 octave_base_matrix<Cell>::assign (idx, rhs.cell_value ()); | 211 octave_base_matrix<Cell>::assign (idx, rhs.cell_value ()); |
58 else | 212 else |
59 octave_base_matrix<Cell>::assign (idx, Cell (rhs)); | 213 octave_base_matrix<Cell>::assign (idx, Cell (rhs)); |
214 } | |
215 | |
216 octave_value_list | |
217 octave_cell::list_value (void) const | |
218 { | |
219 octave_value_list retval; | |
220 | |
221 int nr = rows (); | |
222 int nc = columns (); | |
223 | |
224 if (nr == 1 && nc > 0) | |
225 { | |
226 retval.resize (nc); | |
227 | |
228 for (int i = 0; i < nc; i++) | |
229 retval(i) = matrix(0,i); | |
230 } | |
231 else if (nc == 1 && nr > 0) | |
232 { | |
233 retval.resize (nr); | |
234 | |
235 for (int i = 0; i < nr; i++) | |
236 retval(i) = matrix(i,0); | |
237 } | |
238 else | |
239 error ("invalid conversion from cell array to list"); | |
240 | |
241 return retval; | |
242 } | |
243 | |
244 void | |
245 octave_cell::print (std::ostream& os, bool) const | |
246 { | |
247 print_raw (os); | |
248 } | |
249 | |
250 void | |
251 octave_cell::print_raw (std::ostream& os, bool) const | |
252 { | |
253 int nr = rows (); | |
254 int nc = columns (); | |
255 | |
256 if (nr > 0 && nc > 0) | |
257 { | |
258 indent (os); | |
259 os << "{"; | |
260 newline (os); | |
261 | |
262 increment_indent_level (); | |
263 | |
264 for (int j = 0; j < nc; j++) | |
265 { | |
266 for (int i = 0; i < nr; i++) | |
267 { | |
268 std::ostrstream buf; | |
269 buf << "[" << i+1 << "," << j+1 << "]" << std::ends; | |
270 const char *nm = buf.str (); | |
271 | |
272 octave_value val = matrix(i,j); | |
273 | |
274 val.print_with_name (os, nm); | |
275 | |
276 delete [] nm; | |
277 } | |
278 } | |
279 | |
280 decrement_indent_level (); | |
281 | |
282 indent (os); | |
283 os << "}"; | |
284 newline (os); | |
285 } | |
286 else | |
287 os << "{}"; | |
288 } | |
289 | |
290 bool | |
291 octave_cell::print_name_tag (std::ostream& os, const std::string& name) const | |
292 { | |
293 indent (os); | |
294 os << name << " ="; | |
295 newline (os); | |
296 return false; | |
60 } | 297 } |
61 | 298 |
62 DEFUN (iscell, args, , | 299 DEFUN (iscell, args, , |
63 "-*- texinfo -*-\n\ | 300 "-*- texinfo -*-\n\ |
64 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ | 301 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ |