Mercurial > hg > octave-lyh
annotate src/ov-cell.cc @ 8523:ad3afaaa19c1
implement non-copying contiguous range indexing
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 15 Jan 2009 07:22:24 +0100 |
parents | c72207960242 |
children | 3d8a914c580e |
rev | line source |
---|---|
3353 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007 |
4 John W. Eaton | |
3353 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
3353 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
3353 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
5850 | 28 #include <iomanip> |
3503 | 29 #include <iostream> |
5765 | 30 #include <sstream> |
5164 | 31 #include <vector> |
3353 | 32 |
5360 | 33 #include "Array-util.h" |
34 #include "byte-swap.h" | |
3353 | 35 #include "lo-utils.h" |
4153 | 36 #include "quit.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
37 #include "oct-locbuf.h" |
3353 | 38 |
39 #include "defun.h" | |
40 #include "error.h" | |
41 #include "ov-cell.h" | |
3354 | 42 #include "oct-obj.h" |
3353 | 43 #include "unwind-prot.h" |
3354 | 44 #include "utils.h" |
3928 | 45 #include "ov-base-mat.h" |
46 #include "ov-base-mat.cc" | |
47 #include "ov-re-mat.h" | |
48 #include "ov-scalar.h" | |
5360 | 49 #include "pr-output.h" |
50 #include "ov-scalar.h" | |
3928 | 51 |
4687 | 52 #include "ls-oct-ascii.h" |
53 #include "ls-oct-binary.h" | |
54 #include "ls-hdf5.h" | |
55 #include "ls-utils.h" | |
56 | |
3928 | 57 template class octave_base_matrix<Cell>; |
3353 | 58 |
59 DEFINE_OCTAVE_ALLOCATOR (octave_cell); | |
60 | |
4612 | 61 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell", "cell"); |
3353 | 62 |
6833 | 63 static void |
64 gripe_failed_assignment (void) | |
65 { | |
66 error ("assignment to cell array failed"); | |
67 } | |
68 | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
69 octave_value_list |
4247 | 70 octave_cell::subsref (const std::string& type, |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
71 const std::list<octave_value_list>& idx, |
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
72 int nargout) |
3933 | 73 { |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
74 octave_value_list retval; |
3933 | 75 |
76 switch (type[0]) | |
77 { | |
78 case '(': | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
79 retval(0) = do_index_op (idx.front ()); |
3933 | 80 break; |
81 | |
82 case '{': | |
83 { | |
84 octave_value tmp = do_index_op (idx.front ()); | |
85 | |
4582 | 86 if (! error_state) |
3933 | 87 { |
4582 | 88 Cell tcell = tmp.cell_value (); |
89 | |
90 if (tcell.length () == 1) | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
91 retval(0) = tcell(0,0); |
4582 | 92 else |
93 { | |
5275 | 94 octave_idx_type n = tcell.numel (); |
4705 | 95 |
96 octave_value_list lst (n, octave_value ()); | |
97 | |
5275 | 98 for (octave_idx_type i = 0; i < n; i++) |
4705 | 99 { |
100 OCTAVE_QUIT; | |
101 lst(i) = tcell(i); | |
102 } | |
103 | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
104 retval(0) = octave_value (lst, true); |
4582 | 105 } |
3933 | 106 } |
107 } | |
108 break; | |
109 | |
110 case '.': | |
111 { | |
112 std::string nm = type_name (); | |
113 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
114 } | |
115 break; | |
116 | |
117 default: | |
118 panic_impossible (); | |
119 } | |
120 | |
5775 | 121 // FIXME -- perhaps there should be an |
4994 | 122 // octave_value_list::next_subsref member function? See also |
123 // octave_user_function::subsref. | |
124 | |
125 if (idx.size () > 1) | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
126 retval = retval(0).next_subsref (nargout, type, idx); |
4994 | 127 |
128 return retval; | |
3933 | 129 } |
130 | |
131 octave_value | |
4247 | 132 octave_cell::subsasgn (const std::string& type, |
4219 | 133 const std::list<octave_value_list>& idx, |
3933 | 134 const octave_value& rhs) |
135 { | |
136 octave_value retval; | |
137 | |
138 int n = type.length (); | |
139 | |
140 octave_value t_rhs = rhs; | |
141 | |
142 if (n > 1) | |
143 { | |
144 switch (type[0]) | |
145 { | |
146 case '(': | |
147 { | |
6767 | 148 if (is_empty () && type[1] == '.') |
149 { | |
150 // Allow conversion of empty cell array to some other | |
151 // type in cases like | |
152 // | |
153 // x = []; x(i).f = rhs | |
3933 | 154 |
6767 | 155 octave_value tmp = octave_value::empty_conv (type, rhs); |
3933 | 156 |
6767 | 157 return tmp.subsasgn (type, idx, rhs); |
158 } | |
159 else | |
3933 | 160 { |
6767 | 161 octave_value tmp = do_index_op (idx.front (), true); |
3933 | 162 |
6767 | 163 if (! tmp.is_defined ()) |
164 tmp = octave_value::empty_conv (type.substr (1), rhs); | |
3933 | 165 |
6767 | 166 if (! error_state) |
167 { | |
168 std::list<octave_value_list> next_idx (idx); | |
4362 | 169 |
6767 | 170 next_idx.erase (next_idx.begin ()); |
171 | |
172 tmp.make_unique (); | |
173 | |
174 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | |
175 } | |
3933 | 176 } |
177 } | |
178 break; | |
179 | |
180 case '{': | |
181 { | |
182 octave_value tmp = do_index_op (idx.front (), true); | |
183 | |
4362 | 184 if (! error_state) |
185 { | |
8457
c72207960242
minor simplifications to latest patches
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
186 if (tmp.numel () == 1) |
8446
7b25349b32e6
avoid redundant copying in {} assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
187 { |
7b25349b32e6
avoid redundant copying in {} assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
188 tmp = tmp.cell_value ()(0,0); |
3933 | 189 |
4362 | 190 std::list<octave_value_list> next_idx (idx); |
191 | |
192 next_idx.erase (next_idx.begin ()); | |
3933 | 193 |
5927 | 194 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
4519 | 195 tmp = octave_value::empty_conv (type.substr (1), rhs); |
8452
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
196 else |
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
197 { |
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
198 // This is a bit of black magic. tmp is a shallow copy |
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
199 // of an element inside this cell, and maybe more. To |
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
200 // prevent make_unique from always forcing a copy, we |
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
201 // temporarily delete the stored value. |
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
202 assign (idx.front (), octave_value ()); |
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
203 tmp.make_unique (); |
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
204 assign (idx.front (), Cell (tmp)); |
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
205 } |
4519 | 206 |
207 if (! error_state) | |
208 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | |
4362 | 209 } |
8446
7b25349b32e6
avoid redundant copying in {} assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
210 else |
7b25349b32e6
avoid redundant copying in {} assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
211 error ("scalar indices required for {} in assignment."); |
3933 | 212 } |
213 } | |
214 break; | |
215 | |
216 case '.': | |
217 { | |
218 std::string nm = type_name (); | |
219 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
220 } | |
221 break; | |
222 | |
223 default: | |
224 panic_impossible (); | |
225 } | |
226 } | |
227 | |
3940 | 228 if (! error_state) |
3933 | 229 { |
3940 | 230 switch (type[0]) |
231 { | |
232 case '(': | |
233 { | |
234 octave_value_list i = idx.front (); | |
3933 | 235 |
3940 | 236 if (t_rhs.is_cell ()) |
237 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); | |
238 else | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7813
diff
changeset
|
239 if (t_rhs.is_null_value ()) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7813
diff
changeset
|
240 octave_base_matrix<Cell>::delete_elements (i); |
4941 | 241 else |
242 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); | |
3933 | 243 |
6833 | 244 if (! error_state) |
245 { | |
246 count++; | |
247 retval = octave_value (this); | |
248 } | |
249 else | |
250 gripe_failed_assignment (); | |
3940 | 251 } |
252 break; | |
3933 | 253 |
3940 | 254 case '{': |
255 { | |
256 octave_value_list i = idx.front (); | |
3933 | 257 |
5846 | 258 if (t_rhs.is_cs_list ()) |
259 { | |
260 Cell tmp_cell = Cell (t_rhs.list_value ()); | |
261 | |
7040 | 262 // The shape of the RHS is irrelevant, we just want |
263 // the number of elements to agree and to preserve the | |
264 // shape of the left hand side of the assignment. | |
265 | |
266 if (numel () == tmp_cell.numel ()) | |
267 tmp_cell = tmp_cell.reshape (dims ()); | |
5846 | 268 |
269 octave_base_matrix<Cell>::assign (i, tmp_cell); | |
270 } | |
8457
c72207960242
minor simplifications to latest patches
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
271 else if (i.all_scalars () || do_index_op (i, true).numel () == 1) |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8452
diff
changeset
|
272 // Regularize a null matrix if stored into a cell. |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8457
diff
changeset
|
273 octave_base_matrix<Cell>::assign (i, Cell (t_rhs.storable_value ())); |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8452
diff
changeset
|
274 else if (! error_state) |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8452
diff
changeset
|
275 error ("scalar indices required for {} in assignment."); |
3933 | 276 |
6833 | 277 if (! error_state) |
278 { | |
279 count++; | |
280 retval = octave_value (this); | |
281 } | |
282 else | |
283 gripe_failed_assignment (); | |
3940 | 284 } |
285 break; | |
3933 | 286 |
3940 | 287 case '.': |
288 { | |
289 std::string nm = type_name (); | |
290 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
291 } | |
292 break; | |
3933 | 293 |
3940 | 294 default: |
295 panic_impossible (); | |
296 } | |
3933 | 297 } |
298 | |
299 return retval; | |
300 } | |
301 | |
3353 | 302 void |
303 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) | |
304 { | |
3928 | 305 if (rhs.is_cell ()) |
306 octave_base_matrix<Cell>::assign (idx, rhs.cell_value ()); | |
3353 | 307 else |
3928 | 308 octave_base_matrix<Cell>::assign (idx, Cell (rhs)); |
3353 | 309 } |
310 | |
4791 | 311 size_t |
312 octave_cell::byte_size (void) const | |
313 { | |
314 size_t retval = 0; | |
315 | |
5275 | 316 for (octave_idx_type i = 0; i < numel (); i++) |
4791 | 317 retval += matrix(i).byte_size (); |
318 | |
319 return retval; | |
320 } | |
321 | |
3933 | 322 octave_value_list |
323 octave_cell::list_value (void) const | |
324 { | |
325 octave_value_list retval; | |
326 | |
5275 | 327 octave_idx_type nr = rows (); |
328 octave_idx_type nc = columns (); | |
3933 | 329 |
330 if (nr == 1 && nc > 0) | |
331 { | |
332 retval.resize (nc); | |
333 | |
5275 | 334 for (octave_idx_type i = 0; i < nc; i++) |
3933 | 335 retval(i) = matrix(0,i); |
336 } | |
337 else if (nc == 1 && nr > 0) | |
338 { | |
339 retval.resize (nr); | |
340 | |
5275 | 341 for (octave_idx_type i = 0; i < nr; i++) |
3933 | 342 retval(i) = matrix(i,0); |
343 } | |
344 else | |
345 error ("invalid conversion from cell array to list"); | |
346 | |
347 return retval; | |
348 } | |
349 | |
4243 | 350 string_vector |
5715 | 351 octave_cell::all_strings (bool pad) const |
4243 | 352 { |
4358 | 353 string_vector retval; |
354 | |
7285 | 355 octave_idx_type nel = numel (); |
4243 | 356 |
4358 | 357 int n_elts = 0; |
358 | |
5275 | 359 octave_idx_type max_len = 0; |
4358 | 360 |
7285 | 361 for (octave_idx_type i = 0; i < nel; i++) |
4358 | 362 { |
7285 | 363 string_vector s = matrix(i).all_strings (); |
364 | |
365 if (error_state) | |
366 return retval; | |
4358 | 367 |
7285 | 368 octave_idx_type s_len = s.length (); |
4358 | 369 |
7285 | 370 n_elts += s_len ? s_len : 1; |
4358 | 371 |
7285 | 372 octave_idx_type s_max_len = s.max_length (); |
4358 | 373 |
7285 | 374 if (s_max_len > max_len) |
375 max_len = s_max_len; | |
4358 | 376 } |
377 | |
378 retval.resize (n_elts); | |
4243 | 379 |
5275 | 380 octave_idx_type k = 0; |
4243 | 381 |
7285 | 382 for (octave_idx_type i = 0; i < nel; i++) |
4243 | 383 { |
7285 | 384 string_vector s = matrix(i).all_strings (); |
385 | |
386 octave_idx_type s_len = s.length (); | |
4358 | 387 |
7285 | 388 if (s_len) |
389 { | |
390 for (octave_idx_type j = 0; j < s_len; j++) | |
5715 | 391 { |
7285 | 392 std::string t = s[j]; |
5715 | 393 int t_len = t.length (); |
394 | |
395 if (pad && max_len > t_len) | |
396 t += std::string (max_len - t_len, ' '); | |
397 | |
398 retval[k++] = t; | |
399 } | |
4243 | 400 } |
7285 | 401 else if (pad) |
402 retval[k++] = std::string (max_len, ' '); | |
403 else | |
404 retval[k++] = std::string (); | |
4243 | 405 } |
406 | |
407 return retval; | |
408 } | |
409 | |
4604 | 410 bool |
411 octave_cell::print_as_scalar (void) const | |
412 { | |
413 return (ndims () > 2 || numel () == 0); | |
414 } | |
415 | |
3933 | 416 void |
417 octave_cell::print (std::ostream& os, bool) const | |
418 { | |
419 print_raw (os); | |
420 } | |
421 | |
422 void | |
423 octave_cell::print_raw (std::ostream& os, bool) const | |
424 { | |
4587 | 425 int nd = matrix.ndims (); |
4513 | 426 |
4587 | 427 if (nd == 2) |
4513 | 428 { |
5275 | 429 octave_idx_type nr = rows (); |
430 octave_idx_type nc = columns (); | |
4513 | 431 |
432 if (nr > 0 && nc > 0) | |
433 { | |
434 indent (os); | |
435 os << "{"; | |
436 newline (os); | |
437 | |
438 increment_indent_level (); | |
439 | |
5275 | 440 for (octave_idx_type j = 0; j < nc; j++) |
4513 | 441 { |
5275 | 442 for (octave_idx_type i = 0; i < nr; i++) |
4513 | 443 { |
444 OCTAVE_QUIT; | |
445 | |
5765 | 446 std::ostringstream buf; |
447 buf << "[" << i+1 << "," << j+1 << "]"; | |
3933 | 448 |
4513 | 449 octave_value val = matrix(i,j); |
450 | |
5765 | 451 val.print_with_name (os, buf.str ()); |
4513 | 452 } |
453 } | |
454 | |
455 decrement_indent_level (); | |
456 | |
457 indent (os); | |
458 os << "}"; | |
459 newline (os); | |
460 } | |
461 else | |
462 { | |
463 os << "{}"; | |
5360 | 464 if (Vprint_empty_dimensions) |
4513 | 465 os << "(" << nr << "x" << nc << ")"; |
466 os << "\n"; | |
467 } | |
468 } | |
469 else | |
3933 | 470 { |
471 indent (os); | |
4513 | 472 dim_vector dv = matrix.dims (); |
4587 | 473 os << "{" << dv.str () << " Cell Array}"; |
3933 | 474 newline (os); |
475 } | |
476 } | |
477 | |
4687 | 478 #define CELL_ELT_TAG "<cell-element>" |
479 | |
480 bool | |
6974 | 481 octave_cell::save_ascii (std::ostream& os) |
4687 | 482 { |
483 dim_vector d = dims (); | |
484 if (d.length () > 2) | |
485 { | |
486 os << "# ndims: " << d.length () << "\n"; | |
487 | |
5275 | 488 for (int i = 0; i < d.length (); i++) |
4687 | 489 os << " " << d (i); |
490 os << "\n"; | |
491 | |
492 Cell tmp = cell_value (); | |
493 | |
5275 | 494 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687 | 495 { |
496 octave_value o_val = tmp.elem (i); | |
497 | |
498 // Recurse to print sub-value. | |
6974 | 499 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
4687 | 500 |
501 if (! b) | |
502 return os; | |
503 } | |
504 } | |
505 else | |
506 { | |
507 // Keep this case, rather than use generic code above for backward | |
508 // compatiability. Makes load_ascii much more complex!! | |
509 os << "# rows: " << rows () << "\n" | |
510 << "# columns: " << columns () << "\n"; | |
511 | |
512 Cell tmp = cell_value (); | |
513 | |
5275 | 514 for (octave_idx_type j = 0; j < tmp.cols (); j++) |
4687 | 515 { |
5275 | 516 for (octave_idx_type i = 0; i < tmp.rows (); i++) |
4687 | 517 { |
518 octave_value o_val = tmp.elem (i, j); | |
519 | |
520 // Recurse to print sub-value. | |
6974 | 521 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
4687 | 522 |
523 if (! b) | |
524 return os; | |
525 } | |
526 | |
527 os << "\n"; | |
528 } | |
529 } | |
530 | |
531 return true; | |
532 } | |
533 | |
534 bool | |
535 octave_cell::load_ascii (std::istream& is) | |
536 { | |
537 bool success = true; | |
5099 | 538 |
539 string_vector keywords(2); | |
4687 | 540 |
5099 | 541 keywords[0] = "ndims"; |
542 keywords[1] = "rows"; | |
543 | |
544 std::string kw; | |
5275 | 545 octave_idx_type val = 0; |
5099 | 546 |
547 if (extract_keyword (is, keywords, kw, val, true)) | |
4687 | 548 { |
5099 | 549 if (kw == "ndims") |
4687 | 550 { |
5275 | 551 int mdims = static_cast<int> (val); |
4687 | 552 |
5099 | 553 if (mdims >= 0) |
554 { | |
555 dim_vector dv; | |
556 dv.resize (mdims); | |
557 | |
558 for (int i = 0; i < mdims; i++) | |
559 is >> dv(i); | |
4687 | 560 |
5099 | 561 Cell tmp(dv); |
4687 | 562 |
5275 | 563 for (octave_idx_type i = 0; i < dv.numel (); i++) |
5099 | 564 { |
565 octave_value t2; | |
566 bool dummy; | |
567 | |
568 // recurse to read cell elements | |
569 std::string nm = read_ascii_data (is, std::string (), | |
5759 | 570 dummy, t2, i); |
4687 | 571 |
5099 | 572 if (nm == CELL_ELT_TAG) |
573 { | |
574 if (is) | |
575 tmp.elem (i) = t2; | |
576 } | |
577 else | |
578 { | |
579 error ("load: cell array element had unexpected name"); | |
580 success = false; | |
581 break; | |
582 } | |
583 } | |
4687 | 584 |
5099 | 585 if (is) |
586 matrix = tmp; | |
4687 | 587 else |
588 { | |
5099 | 589 error ("load: failed to load matrix constant"); |
4687 | 590 success = false; |
591 } | |
592 } | |
593 else | |
594 { | |
5099 | 595 error ("load: failed to extract number of rows and columns"); |
596 success = false; | |
597 } | |
598 } | |
599 else if (kw == "rows") | |
600 { | |
5275 | 601 octave_idx_type nr = val; |
602 octave_idx_type nc = 0; | |
5099 | 603 |
604 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) | |
605 { | |
606 if (nr > 0 && nc > 0) | |
607 { | |
608 Cell tmp (nr, nc); | |
609 | |
5275 | 610 for (octave_idx_type j = 0; j < nc; j++) |
5099 | 611 { |
5275 | 612 for (octave_idx_type i = 0; i < nr; i++) |
5099 | 613 { |
614 octave_value t2; | |
615 bool dummy; | |
616 | |
617 // recurse to read cell elements | |
618 std::string nm = read_ascii_data (is, std::string (), | |
5759 | 619 dummy, t2, i); |
5099 | 620 |
621 if (nm == CELL_ELT_TAG) | |
622 { | |
623 if (is) | |
624 tmp.elem (i, j) = t2; | |
625 } | |
626 else | |
627 { | |
628 error ("load: cell array element had unexpected name"); | |
629 success = false; | |
630 goto cell_read_error; | |
631 } | |
632 } | |
633 } | |
634 | |
635 cell_read_error: | |
636 | |
637 if (is) | |
638 matrix = tmp; | |
639 else | |
640 { | |
641 error ("load: failed to load cell element"); | |
642 success = false; | |
643 } | |
644 } | |
645 else if (nr == 0 || nc == 0) | |
646 matrix = Cell (nr, nc); | |
647 else | |
648 panic_impossible (); | |
649 } | |
650 else | |
651 { | |
652 error ("load: failed to extract number of rows and columns for cell array"); | |
4687 | 653 success = false; |
654 } | |
655 } | |
656 else | |
5099 | 657 panic_impossible (); |
4687 | 658 } |
659 else | |
660 { | |
5099 | 661 error ("load: failed to extract number of rows and columns"); |
662 success = false; | |
4687 | 663 } |
664 | |
665 return success; | |
666 } | |
667 | |
668 bool | |
669 octave_cell::save_binary (std::ostream& os, bool& save_as_floats) | |
670 { | |
671 dim_vector d = dims (); | |
672 if (d.length () < 1) | |
673 return false; | |
674 | |
675 // Use negative value for ndims | |
5828 | 676 int32_t di = - d.length(); |
5760 | 677 os.write (reinterpret_cast<char *> (&di), 4); |
5275 | 678 for (int i = 0; i < d.length (); i++) |
4687 | 679 { |
680 di = d(i); | |
5760 | 681 os.write (reinterpret_cast<char *> (&di), 4); |
4687 | 682 } |
683 | |
684 Cell tmp = cell_value (); | |
685 | |
5275 | 686 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687 | 687 { |
688 octave_value o_val = tmp.elem (i); | |
689 | |
690 // Recurse to print sub-value. | |
691 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, | |
692 save_as_floats); | |
693 | |
694 if (! b) | |
695 return false; | |
696 } | |
697 | |
698 return true; | |
699 } | |
700 | |
701 bool | |
702 octave_cell::load_binary (std::istream& is, bool swap, | |
703 oct_mach_info::float_format fmt) | |
704 { | |
705 bool success = true; | |
5828 | 706 int32_t mdims; |
5760 | 707 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4687 | 708 return false; |
709 if (swap) | |
4944 | 710 swap_bytes<4> (&mdims); |
4687 | 711 if (mdims >= 0) |
712 return false; | |
713 | |
714 mdims = -mdims; | |
5828 | 715 int32_t di; |
4687 | 716 dim_vector dv; |
717 dv.resize (mdims); | |
718 | |
719 for (int i = 0; i < mdims; i++) | |
720 { | |
5760 | 721 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4687 | 722 return false; |
723 if (swap) | |
4944 | 724 swap_bytes<4> (&di); |
4687 | 725 dv(i) = di; |
726 } | |
727 | |
5157 | 728 // Convert an array with a single dimension to be a row vector. |
729 // Octave should never write files like this, other software | |
730 // might. | |
731 | |
732 if (mdims == 1) | |
733 { | |
734 mdims = 2; | |
735 dv.resize (mdims); | |
736 dv(1) = dv(0); | |
737 dv(0) = 1; | |
738 } | |
739 | |
5275 | 740 octave_idx_type nel = dv.numel (); |
4687 | 741 Cell tmp(dv); |
742 | |
5275 | 743 for (octave_idx_type i = 0; i < nel; i++) |
4687 | 744 { |
745 octave_value t2; | |
746 bool dummy; | |
747 std::string doc; | |
748 | |
749 // recurse to read cell elements | |
750 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
751 dummy, t2, doc); | |
752 | |
753 if (nm == CELL_ELT_TAG) | |
754 { | |
755 if (is) | |
756 tmp.elem (i) = t2; | |
757 } | |
758 else | |
759 { | |
760 error ("load: cell array element had unexpected name"); | |
761 success = false; | |
762 break; | |
763 } | |
764 } | |
765 | |
766 if (is) | |
767 matrix = tmp; | |
768 else | |
769 { | |
770 error ("load: failed to load matrix constant"); | |
771 success = false; | |
772 } | |
773 | |
774 return success; | |
775 } | |
776 | |
777 #if defined (HAVE_HDF5) | |
4815 | 778 |
4687 | 779 bool |
780 octave_cell::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
781 { | |
4814 | 782 dim_vector dv = dims (); |
4837 | 783 int empty = save_hdf5_empty (loc_id, name, dv); |
784 if (empty) | |
785 return (empty > 0); | |
786 | |
4815 | 787 hsize_t rank = dv.length (); |
4687 | 788 hid_t space_hid = -1, data_hid = -1, size_hid = -1; |
789 | |
790 data_hid = H5Gcreate (loc_id, name, 0); | |
4815 | 791 |
792 if (data_hid < 0) | |
793 return false; | |
4687 | 794 |
4814 | 795 // Have to save cell array shape, since can't have a |
796 // dataset of groups.... | |
4815 | 797 |
798 space_hid = H5Screate_simple (1, &rank, 0); | |
799 | |
4687 | 800 if (space_hid < 0) |
801 { | |
802 H5Gclose (data_hid); | |
803 return false; | |
804 } | |
805 | |
5351 | 806 OCTAVE_LOCAL_BUFFER (octave_idx_type, hdims, rank); |
4814 | 807 |
808 // Octave uses column-major, while HDF5 uses row-major ordering | |
4933 | 809 for (hsize_t i = 0; i < rank; i++) |
4815 | 810 hdims[i] = dv(rank-i-1); |
4814 | 811 |
5351 | 812 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, |
4687 | 813 H5P_DEFAULT); |
814 if (size_hid < 0) | |
815 { | |
816 H5Sclose (space_hid); | |
817 H5Gclose (data_hid); | |
818 return false; | |
819 } | |
820 | |
6276 | 821 if (H5Dwrite (size_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
822 H5P_DEFAULT, hdims) < 0) | |
4687 | 823 { |
824 H5Dclose (size_hid); | |
825 H5Sclose (space_hid); | |
826 H5Gclose (data_hid); | |
827 return false; | |
828 } | |
4815 | 829 |
4687 | 830 H5Dclose (size_hid); |
831 H5Sclose (space_hid); | |
832 | |
4815 | 833 // Recursively add each element of the cell to this group. |
834 | |
4687 | 835 Cell tmp = cell_value (); |
5850 | 836 |
837 octave_idx_type nel = dv.numel (); | |
838 | |
839 for (octave_idx_type i = 0; i < nel; i++) | |
4687 | 840 { |
5765 | 841 std::ostringstream buf; |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7285
diff
changeset
|
842 int digits = static_cast<int> (::floor (::log10 (static_cast<double> (nel)) + 1.0)); |
5850 | 843 buf << "_" << std::setw (digits) << std::setfill ('0') << i; |
5765 | 844 std::string s = buf.str (); |
4687 | 845 |
5850 | 846 if (! add_hdf5_data (data_hid, tmp.elem (i), s.c_str (), "", false, |
847 save_as_floats)) | |
4814 | 848 { |
849 H5Gclose (data_hid); | |
850 return false; | |
4687 | 851 } |
852 } | |
853 | |
854 H5Gclose (data_hid); | |
4815 | 855 |
4687 | 856 return true; |
857 } | |
858 | |
859 bool | |
860 octave_cell::load_hdf5 (hid_t loc_id, const char *name, | |
861 bool have_h5giterate_bug) | |
862 { | |
863 bool retval = false; | |
4837 | 864 |
865 dim_vector dv; | |
866 int empty = load_hdf5_empty (loc_id, name, dv); | |
867 if (empty > 0) | |
868 matrix.resize(dv); | |
869 if (empty) | |
870 return (empty > 0); | |
871 | |
4687 | 872 hid_t group_id = H5Gopen (loc_id, name); |
873 | |
874 if (group_id < 0) | |
875 return false; | |
876 | |
4814 | 877 hid_t data_hid = H5Dopen (group_id, "dims"); |
4687 | 878 hid_t space_hid = H5Dget_space (data_hid); |
879 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); | |
4814 | 880 if (rank != 1) |
4687 | 881 { |
4837 | 882 H5Dclose (data_hid); |
883 H5Gclose (group_id); | |
4687 | 884 return false; |
885 } | |
886 | |
4814 | 887 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
888 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
4815 | 889 |
4814 | 890 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
4687 | 891 |
4815 | 892 // Octave uses column-major, while HDF5 uses row-major ordering. |
893 | |
4814 | 894 dv.resize (hdims[0]); |
4815 | 895 |
5351 | 896 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); |
4814 | 897 |
5351 | 898 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
4815 | 899 H5P_DEFAULT, tmp) < 0) |
4687 | 900 { |
4837 | 901 H5Dclose (data_hid); |
902 H5Gclose (group_id); | |
4687 | 903 return false; |
904 } | |
4815 | 905 |
4687 | 906 H5Dclose (data_hid); |
907 H5Gclose (group_id); | |
908 | |
4815 | 909 for (hsize_t i = 0, j = hdims[0] - 1; i < hdims[0]; i++, j--) |
4814 | 910 dv(j) = tmp[i]; |
911 | |
4687 | 912 hdf5_callback_data dsub; |
913 | |
914 herr_t retval2 = -1; | |
4815 | 915 |
4814 | 916 Cell m (dv); |
4815 | 917 |
4687 | 918 int current_item = 0; |
4815 | 919 |
4687 | 920 if (have_h5giterate_bug) |
4815 | 921 current_item = 1; // Skip dims items in group. |
4687 | 922 |
4696 | 923 #ifdef HAVE_H5GGET_NUM_OBJS |
924 hsize_t num_obj = 0; | |
5060 | 925 group_id = H5Gopen (loc_id, name); |
926 H5Gget_num_objs (group_id, &num_obj); | |
927 H5Gclose (group_id); | |
4696 | 928 #endif |
929 | |
5275 | 930 for (octave_idx_type i = 0; i < dv.numel (); i++) |
4687 | 931 { |
4696 | 932 |
933 #ifdef HAVE_H5GGET_NUM_OBJS | |
4814 | 934 if (current_item >= static_cast<int> (num_obj)) |
935 retval2 = -1; | |
936 else | |
4696 | 937 #endif |
4814 | 938 retval2 = H5Giterate (loc_id, name, ¤t_item, |
939 hdf5_read_next_data, &dsub); | |
940 | |
4687 | 941 if (retval2 <= 0) |
942 break; | |
4814 | 943 |
944 octave_value ov = dsub.tc; | |
945 m.elem (i) = ov; | |
946 | |
947 if (have_h5giterate_bug) | |
4815 | 948 current_item++; // H5Giterate returned the last index processed. |
4814 | 949 |
4687 | 950 } |
951 | |
952 if (retval2 >= 0) | |
953 { | |
954 matrix = m; | |
955 retval = true; | |
956 } | |
957 | |
958 return retval; | |
959 } | |
4815 | 960 |
4687 | 961 #endif |
962 | |
3354 | 963 DEFUN (iscell, args, , |
3448 | 964 "-*- texinfo -*-\n\ |
965 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ | |
966 Return true if @var{x} is a cell array object. Otherwise, return\n\ | |
967 false.\n\ | |
968 @end deftypefn") | |
3354 | 969 { |
970 octave_value retval; | |
971 | |
972 if (args.length () == 1) | |
973 retval = args(0).is_cell (); | |
974 else | |
5823 | 975 print_usage (); |
3354 | 976 |
977 return retval; | |
978 } | |
979 | |
980 DEFUN (cell, args, , | |
3448 | 981 "-*- texinfo -*-\n\ |
982 @deftypefn {Built-in Function} {} cell (@var{x})\n\ | |
983 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ | |
984 Create a new cell array object. If invoked with a single scalar\n\ | |
985 argument, @code{cell} returns a square cell array with the dimension\n\ | |
986 specified. If you supply two scalar arguments, @code{cell} takes\n\ | |
987 them to be the number of rows and columns. If given a vector with two\n\ | |
988 elements, @code{cell} uses the values of the elements as the number of\n\ | |
989 rows and columns, respectively.\n\ | |
990 @end deftypefn") | |
3354 | 991 { |
992 octave_value retval; | |
993 | |
994 int nargin = args.length (); | |
995 | |
4563 | 996 dim_vector dims; |
997 | |
3354 | 998 switch (nargin) |
999 { | |
4563 | 1000 case 0: |
1001 dims = dim_vector (0, 0); | |
3354 | 1002 break; |
1003 | |
4563 | 1004 case 1: |
1005 get_dimensions (args(0), "cell", dims); | |
3354 | 1006 break; |
1007 | |
1008 default: | |
4563 | 1009 { |
1010 dims.resize (nargin); | |
1011 | |
1012 for (int i = 0; i < nargin; i++) | |
1013 { | |
1014 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); | |
1015 | |
1016 if (error_state) | |
1017 { | |
1018 error ("cell: expecting scalar arguments"); | |
1019 break; | |
1020 } | |
1021 } | |
1022 } | |
3354 | 1023 break; |
1024 } | |
1025 | |
4563 | 1026 if (! error_state) |
1027 { | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
1028 dims.chop_trailing_singletons (); |
4563 | 1029 |
1030 check_dimensions (dims, "cell"); | |
1031 | |
1032 if (! error_state) | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
1033 retval = Cell (dims, Matrix ()); |
4563 | 1034 } |
1035 | |
3354 | 1036 return retval; |
1037 } | |
1038 | |
4610 | 1039 DEFUN (iscellstr, args, , |
1040 "-*- texinfo -*-\n\ | |
1041 @deftypefn {Built-in Function} {} iscellstr (@var{cell})\n\ | |
1042 Return true if every element of the cell array @var{cell} is a\n\ | |
1043 character string\n\ | |
1044 @end deftypefn") | |
1045 { | |
4699 | 1046 octave_value retval; |
4610 | 1047 |
1048 if (args.length () == 1) | |
6116 | 1049 retval = args(0).is_cellstr (); |
4610 | 1050 else |
5823 | 1051 print_usage (); |
4610 | 1052 |
1053 return retval; | |
1054 } | |
1055 | |
4817 | 1056 // Note that since Fcellstr calls Fiscellstr, we need to have |
1057 // Fiscellstr defined first (to provide a declaration) and also we | |
1058 // should keep it in the same file (so we don't have to provide a | |
1059 // declaration) and so we don't have to use feval to call it. | |
1060 | |
1061 DEFUN (cellstr, args, , | |
1062 "-*- texinfo -*-\n\ | |
1063 @deftypefn {Built-in Function} {} cellstr (@var{string})\n\ | |
1064 Create a new cell array object from the elements of the string\n\ | |
1065 array @var{string}.\n\ | |
1066 @end deftypefn") | |
1067 { | |
1068 octave_value retval; | |
1069 | |
1070 if (args.length () == 1) | |
1071 { | |
1072 octave_value_list tmp = Fiscellstr (args, 1); | |
1073 | |
1074 if (tmp(0).is_true ()) | |
1075 retval = args(0); | |
1076 else | |
1077 { | |
1078 string_vector s = args(0).all_strings (); | |
1079 | |
1080 if (! error_state) | |
7813 | 1081 retval = (s.is_empty () |
1082 ? Cell (octave_value (std::string ())) | |
1083 : Cell (s, true)); | |
4817 | 1084 else |
1085 error ("cellstr: expecting argument to be a 2-d character array"); | |
1086 } | |
1087 } | |
1088 else | |
5823 | 1089 print_usage (); |
4817 | 1090 |
1091 return retval; | |
1092 } | |
1093 | |
4762 | 1094 DEFUN (struct2cell, args, , |
1095 "-*- texinfo -*-\n\ | |
1096 @deftypefn {Built-in Function} {} struct2cell (@var{S})\n\ | |
1097 Create a new cell array from the objects stored in the struct object.\n\ | |
4764 | 1098 If @var{f} is the number of fields in the structure, the resulting\n\ |
1099 cell array will have a dimension vector corresponding to\n\ | |
1100 @code{[@var{F} size(@var{S})]}.\n\ | |
5642 | 1101 @seealso{cell2struct, fieldnames}\n\ |
1102 @end deftypefn") | |
4762 | 1103 { |
1104 octave_value retval; | |
4764 | 1105 |
4762 | 1106 int nargin = args.length (); |
4764 | 1107 |
4762 | 1108 if (nargin == 1) |
1109 { | |
4764 | 1110 Octave_map m = args(0).map_value (); |
1111 | |
4762 | 1112 if (! error_state) |
1113 { | |
1114 dim_vector m_dv = m.dims (); | |
4764 | 1115 |
4762 | 1116 string_vector keys = m.keys (); |
4764 | 1117 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1118 octave_idx_type num_fields = keys.length (); |
4764 | 1119 |
4762 | 1120 // The resulting dim_vector should have dimensions: |
1121 // [numel(fields) size(struct)] | |
4764 | 1122 |
4762 | 1123 dim_vector result_dv; |
4764 | 1124 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields. |
1125 | |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1126 result_dv(0) = num_fields; |
4762 | 1127 |
1128 for (int i = 1; i < result_dv.length (); i++) | |
1129 result_dv(i) = m_dv(i-1); | |
4764 | 1130 |
4762 | 1131 Cell c (result_dv); |
4764 | 1132 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1133 octave_idx_type n_elts = m.numel (); |
4764 | 1134 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1135 for (octave_idx_type j = 0; j < num_fields; j++) |
4762 | 1136 { |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1137 octave_idx_type k = j; |
4764 | 1138 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1139 const Cell vals = m.contents (keys(j)); |
4764 | 1140 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1141 for (octave_idx_type i = 0; i < n_elts; i++) |
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1142 { |
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1143 c(k) = vals(i); |
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1144 k += num_fields; |
4762 | 1145 } |
1146 } | |
1147 | |
1148 retval = c; | |
1149 } | |
1150 else | |
1151 error ("struct2cell: expecting argument to be a cell array"); | |
1152 } | |
1153 else | |
5823 | 1154 print_usage (); |
4764 | 1155 |
4762 | 1156 return retval; |
1157 } | |
1158 | |
5900 | 1159 mxArray * |
1160 octave_cell::as_mxArray (void) const | |
1161 { | |
1162 mxArray *retval = new mxArray (dims ()); | |
1163 | |
1164 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); | |
1165 | |
6686 | 1166 mwSize nel = numel (); |
5900 | 1167 |
1168 const octave_value *p = matrix.data (); | |
1169 | |
6686 | 1170 for (mwIndex i = 0; i < nel; i++) |
5900 | 1171 elts[i] = new mxArray (p[i]); |
1172 | |
1173 return retval; | |
1174 } | |
1175 | |
3353 | 1176 /* |
1177 ;;; Local Variables: *** | |
1178 ;;; mode: C++ *** | |
1179 ;;; End: *** | |
1180 */ |