Mercurial > octave
view libinterp/octave-value/ov-cell.cc @ 22407:34ce5be04942
maint: Style check C++ code in libinterp/.
* build-env.h, build-env.in.cc, builtins.h, Cell.cc, Cell.h, __contourc__.cc,
__dispatch__.cc, __dsearchn__.cc, __ilu__.cc, __lin_interpn__.cc, __luinc__.cc,
__magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, balance.cc,
base-text-renderer.h, besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc,
c-file-ptr-stream.h, call-stack.cc, call-stack.h, cdisplay.c, cdisplay.h,
cellfun.cc, coct-hdf5-types.c, comment-list.cc, comment-list.h, conv2.cc,
daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc, debug.h, defaults.cc,
defaults.in.h, defun-dld.h, defun-int.h, defun.h, det.cc, dirfns.cc, dirfns.h,
display.cc, display.h, dlmread.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc,
error.cc, error.h, errwarn.h, event-queue.cc, event-queue.h, fft.cc, fft2.cc,
fftn.cc, file-io.cc, file-io.h, filter.cc, find.cc, ft-text-renderer.cc,
ft-text-renderer.h, gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, givens.cc,
gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h, graphics.cc,
graphics.in.h, gripes.h, gsvd.cc, hash.cc, help.cc, help.h, hess.cc,
hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h, interpreter.cc,
interpreter.h, inv.cc, jit-ir.cc, jit-ir.h, jit-typeinfo.cc, jit-typeinfo.h,
jit-util.cc, jit-util.h, kron.cc, load-path.cc, load-path.h, load-save.cc,
load-save.h, lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-hdf5.cc,
ls-hdf5.h, ls-mat-ascii.cc, ls-mat-ascii.h, ls-mat4.cc, ls-mat4.h, ls-mat5.h,
ls-oct-binary.cc, ls-oct-binary.h, ls-oct-text.cc, ls-oct-text.h, ls-utils.cc,
ls-utils.h, lsode.cc, lu.cc, matrix_type.cc, max.cc, mex.cc, mex.h, mexproto.h,
mgorth.cc, mxarray.in.h, nproc.cc, oct-errno.h, oct-errno.in.cc, oct-fstrm.cc,
oct-fstrm.h, oct-handle.h, oct-hdf5-types.cc, oct-hdf5-types.h, oct-hdf5.h,
oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h, oct-lvalue.cc,
oct-lvalue.h, oct-map.h, oct-obj.h, oct-opengl.h, oct-prcstrm.cc,
oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h, oct-stdstrm.h, oct-stream.cc,
oct-stream.h, oct-strstrm.cc, oct-strstrm.h, oct.h, octave-default-image.h,
octave-link.h, octave-preserve-stream-state.h, ordschur.cc, pager.cc, pager.h,
pinv.cc, pr-output.cc, pr-output.h, procstream.cc, procstream.h, profiler.h,
psi.cc, pt-jit.cc, pt-jit.h, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc,
regexp.cc, schur.cc, sighandlers.cc, sighandlers.h, sparse-xdiv.cc,
sparse-xdiv.h, sparse-xpow.cc, sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc,
str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc,
symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h, text-renderer.cc,
text-renderer.h, time.cc, toplev.cc, toplev.h, tril.cc, tsearch.cc, txt-eng.cc,
txt-eng.h, typecast.cc, urlwrite.cc, utils.cc, utils.h, variables.cc,
variables.h, workspace-element.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc,
xpow.h, zfstream.cc, zfstream.h, deprecated-config.h, __delaunayn__.cc,
__eigs__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc,
__init_gnuplot__.cc, __osmesa_print__.cc, __voronoi__.cc, amd.cc,
audiodevinfo.cc, audioread.cc, ccolamd.cc, chol.cc, colamd.cc, convhulln.cc,
dmperm.cc, fftw.cc, gzip.cc, oct-qhull.h, qr.cc, symbfact.cc, symrcm.cc,
liboctinterp-build-info.h, liboctinterp-build-info.in.cc, ov-base-diag.h,
ov-base-int.cc, ov-base-int.h, ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.h,
ov-base-sparse.cc, ov-base-sparse.h, ov-base.cc, ov-base.h, ov-bool-mat.cc,
ov-bool-mat.h, ov-bool-sparse.cc, ov-bool-sparse.h, ov-bool.cc, ov-bool.h,
ov-builtin.cc, ov-builtin.h, ov-cell.cc, ov-cell.h, ov-ch-mat.cc, ov-ch-mat.h,
ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-colon.cc,
ov-colon.h, ov-complex.cc, ov-complex.h, ov-cs-list.h, ov-cx-diag.cc,
ov-cx-diag.h, ov-cx-mat.cc, ov-cx-mat.h, ov-cx-sparse.cc, ov-cx-sparse.h,
ov-dld-fcn.cc, ov-dld-fcn.h, ov-fcn-handle.cc, ov-fcn-handle.h,
ov-fcn-inline.cc, ov-fcn-inline.h, ov-fcn.cc, ov-fcn.h, ov-float.cc,
ov-float.h, ov-flt-complex.cc, ov-flt-complex.h, ov-flt-cx-diag.cc,
ov-flt-cx-diag.h, ov-flt-cx-mat.cc, ov-flt-cx-mat.h, ov-flt-re-diag.cc,
ov-flt-re-diag.h, ov-flt-re-mat.cc, ov-flt-re-mat.h, ov-int-traits.h,
ov-int16.cc, ov-int16.h, ov-int32.cc, ov-int32.h, ov-int64.cc, ov-int64.h,
ov-int8.cc, ov-int8.h, ov-intx.h, ov-java.cc, ov-java.h, ov-lazy-idx.cc,
ov-lazy-idx.h, ov-mex-fcn.cc, ov-mex-fcn.h, ov-null-mat.cc, ov-null-mat.h,
ov-oncleanup.cc, ov-oncleanup.h, ov-perm.cc, ov-perm.h, ov-range.cc,
ov-range.h, ov-re-diag.cc, ov-re-diag.h, ov-re-mat.cc, ov-re-mat.h,
ov-re-sparse.cc, ov-re-sparse.h, ov-scalar.cc, ov-scalar.h, ov-str-mat.cc,
ov-str-mat.h, ov-struct.cc, ov-struct.h, ov-typeinfo.cc, ov-typeinfo.h,
ov-uint16.cc, ov-uint16.h, ov-uint32.cc, ov-uint32.h, ov-uint64.cc,
ov-uint64.h, ov-uint8.cc, ov-uint8.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.h,
ovl.cc, ovl.h, octave.cc, octave.h, op-b-b.cc, op-b-bm.cc, op-b-sbm.cc,
op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc, op-chm.cc,
op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-pm.cc, op-cm-s.cc,
op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc,
op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc,
op-dm-template.cc, op-dms-template.cc, op-fcdm-fcdm.cc, op-fcm-fcm.cc,
op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc, op-fcm-pm.cc, op-fcn.cc,
op-fcs-fcm.cc, op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc, op-fdm-fdm.cc,
op-fm-fcm.cc, op-fm-fcs.cc, op-fm-fm.cc, op-fm-fs.cc, op-fm-pm.cc,
op-fs-fcm.cc, op-fs-fcs.cc, op-fs-fm.cc, op-fs-fs.cc, op-i16-i16.cc,
op-i32-i32.cc, op-i64-i64.cc, op-i8-i8.cc, op-int-concat.cc, op-int.h,
op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-pm.cc, op-m-s.cc, op-m-scm.cc,
op-m-sm.cc, op-pm-cm.cc, op-pm-fcm.cc, op-pm-fm.cc, op-pm-m.cc, op-pm-pm.cc,
op-pm-scm.cc, op-pm-sm.cc, op-pm-template.cc, op-range.cc, op-s-cm.cc,
op-s-cs.cc, op-s-m.cc, op-s-s.cc, op-s-scm.cc, op-s-sm.cc, op-sbm-b.cc,
op-sbm-bm.cc, op-sbm-sbm.cc, op-scm-cm.cc, op-scm-cs.cc, op-scm-m.cc,
op-scm-s.cc, op-scm-scm.cc, op-scm-sm.cc, op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc,
op-sm-s.cc, op-sm-scm.cc, op-sm-sm.cc, op-str-m.cc, op-str-s.cc, op-str-str.cc,
op-struct.cc, op-ui16-ui16.cc, op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc,
ops.h, options-usage.h, lex.h, parse.h, pt-all.h, pt-arg-list.cc,
pt-arg-list.h, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc,
pt-binop.h, pt-bp.cc, pt-bp.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc,
pt-cell.h, pt-check.cc, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.cc,
pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc,
pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc,
pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-funcall.cc, pt-funcall.h,
pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.cc, pt-jump.h, pt-loop.cc,
pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc,
pt-pr-code.h, pt-select.cc, pt-select.h, pt-stmt.cc, pt-stmt.h, pt-unop.cc,
pt-unop.h, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Array-jit.cc,
Array-tc.cc, version.cc, version.in.h:
Style check C++ code in libinterp/
author | Rik <rik@octave.org> |
---|---|
date | Tue, 30 Aug 2016 21:46:47 -0700 |
parents | bac0d6f07a3e |
children | 3a2b891d0b33 e9a0469dedd9 |
line wrap: on
line source
/* Copyright (C) 1999-2016 John W. Eaton Copyright (C) 2009-2010 VZLU Prague This file is part of Octave. Octave is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. Octave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Octave; see the file COPYING. If not, see <http://www.gnu.org/licenses/>. */ #if defined (HAVE_CONFIG_H) # include "config.h" #endif #include <iomanip> #include <iostream> #include <sstream> #include <vector> #include <queue> #include "Array-util.h" #include "byte-swap.h" #include "lo-utils.h" #include "quit.h" #include "oct-locbuf.h" #include "defun.h" #include "error.h" #include "mxarray.h" #include "ov-cell.h" #include "ovl.h" #include "oct-hdf5.h" #include "unwind-prot.h" #include "utils.h" #include "ov-base-mat.h" #include "ov-base-mat.cc" #include "ov-re-mat.h" #include "ov-scalar.h" #include "pr-output.h" #include "ov-scalar.h" #include "errwarn.h" #include "ls-oct-text.h" #include "ls-oct-binary.h" #include "ls-hdf5.h" #include "ls-utils.h" // Cell is able to handle octave_value indexing by itself, so just forward // everything. template <> octave_value octave_base_matrix<Cell>::do_index_op (const octave_value_list& idx, bool resize_ok) { return matrix.index (idx, resize_ok); } template <> void octave_base_matrix<Cell>::assign (const octave_value_list& idx, const Cell& rhs) { matrix.assign (idx, rhs); } template <> void octave_base_matrix<Cell>::assign (const octave_value_list& idx, octave_value rhs) { // FIXME: Really? if (rhs.is_cell ()) matrix.assign (idx, rhs.cell_value ()); else matrix.assign (idx, Cell (rhs)); } template <> void octave_base_matrix<Cell>::delete_elements (const octave_value_list& idx) { matrix.delete_elements (idx); } // FIXME: this list of specializations is becoming so long that we should // really ask whether octave_cell should inherit from octave_base_matrix at all. template <> octave_value octave_base_matrix<Cell>::fast_elem_extract (octave_idx_type n) const { if (n < matrix.numel ()) return Cell (matrix(n)); else return octave_value (); } template <> bool octave_base_matrix<Cell>::fast_elem_insert (octave_idx_type n, const octave_value& x) { const octave_cell *xrep = dynamic_cast<const octave_cell *> (&x.get_rep ()); bool retval = xrep && xrep->matrix.numel () == 1 && n < matrix.numel (); if (retval) matrix(n) = xrep->matrix(0); return retval; } template class octave_base_matrix<Cell>; DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell", "cell"); octave_value_list octave_cell::subsref (const std::string& type, const std::list<octave_value_list>& idx, int nargout, const std::list<octave_lvalue> *lvalue_list) { octave_value_list retval; switch (type[0]) { case '(': retval(0) = do_index_op (idx.front ()); break; case '{': { octave_value tmp = do_index_op (idx.front ()); Cell tcell = tmp.cell_value (); if (tcell.numel () == 1) retval(0) = tcell(0,0); else retval = octave_value (octave_value_list (tcell), true); } break; case '.': { std::string nm = type_name (); error ("%s cannot be indexed with %c", nm.c_str (), type[0]); } break; default: panic_impossible (); } // FIXME: perhaps there should be an // octave_value_list::next_subsref member function? See also // octave_user_function::subsref. if (idx.size () > 1) retval = (lvalue_list ? retval(0).next_subsref (nargout, type, idx, lvalue_list) : retval(0).next_subsref (nargout, type, idx)); return retval; } octave_value octave_cell::subsref (const std::string& type, const std::list<octave_value_list>& idx, bool auto_add) { octave_value retval; switch (type[0]) { case '(': retval = do_index_op (idx.front (), auto_add); break; case '{': { octave_value tmp = do_index_op (idx.front (), auto_add); const Cell tcell = tmp.cell_value (); if (tcell.numel () == 1) retval = tcell(0,0); else retval = octave_value (octave_value_list (tcell), true); } break; case '.': { std::string nm = type_name (); error ("%s cannot be indexed with %c", nm.c_str (), type[0]); } break; default: panic_impossible (); } // FIXME: perhaps there should be an // octave_value_list::next_subsref member function? See also // octave_user_function::subsref. if (idx.size () > 1) retval = retval.next_subsref (auto_add, type, idx); return retval; } octave_value octave_cell::subsasgn (const std::string& type, const std::list<octave_value_list>& idx, const octave_value& rhs) { octave_value retval; int n = type.length (); octave_value t_rhs = rhs; clear_cellstr_cache (); if (idx.front ().empty ()) error ("missing index in indexed assignment"); if (n > 1) { switch (type[0]) { case '(': { if (is_empty () && type[1] == '.') { // Allow conversion of empty cell array to some other // type in cases like // // x = {}; x(i).f = rhs octave_value tmp = octave_value::empty_conv (type, rhs); return tmp.subsasgn (type, idx, rhs); } else { octave_value tmp = do_index_op (idx.front (), true); if (! tmp.is_defined ()) tmp = octave_value::empty_conv (type.substr (1), rhs); std::list<octave_value_list> next_idx (idx); next_idx.erase (next_idx.begin ()); tmp.make_unique (); t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); } } break; case '{': { matrix.make_unique (); Cell tmpc = matrix.index (idx.front (), true); std::list<octave_value_list> next_idx (idx); next_idx.erase (next_idx.begin ()); std::string next_type = type.substr (1); if (tmpc.numel () != 1) err_indexed_cs_list (); octave_value tmp = tmpc(0); tmpc = Cell (); if (! tmp.is_defined () || tmp.is_zero_by_zero ()) { tmp = octave_value::empty_conv (type.substr (1), rhs); tmp.make_unique (); // probably a no-op. } else // optimization: ignore copy still stored inside array. tmp.make_unique (1); t_rhs = tmp.subsasgn (next_type, next_idx, rhs); } break; case '.': { if (! is_empty ()) { std::string nm = type_name (); error ("%s cannot be indexed with %c", nm.c_str (), type[0]); } // Do nothing; the next branch will handle it. } break; default: panic_impossible (); } } switch (type[0]) { case '(': { octave_value_list i = idx.front (); if (t_rhs.is_cell ()) octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); else if (t_rhs.is_null_value ()) octave_base_matrix<Cell>::delete_elements (i); else octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); count++; retval = octave_value (this); } break; case '{': { octave_value_list idxf = idx.front (); if (t_rhs.is_cs_list ()) { Cell tmp_cell = Cell (t_rhs.list_value ()); // Inquire the proper shape of the RHS. dim_vector didx = dims ().redim (idxf.length ()); for (octave_idx_type k = 0; k < idxf.length (); k++) if (! idxf(k).is_magic_colon ()) didx(k) = idxf(k).numel (); if (didx.numel () == tmp_cell.numel ()) tmp_cell = tmp_cell.reshape (didx); octave_base_matrix<Cell>::assign (idxf, tmp_cell); } else if (idxf.all_scalars () || do_index_op (idxf, true).numel () == 1) // Regularize a null matrix if stored into a cell. octave_base_matrix<Cell>::assign (idxf, Cell (t_rhs.storable_value ())); else err_nonbraced_cs_list_assignment (); count++; retval = octave_value (this); } break; case '.': { if (! is_empty ()) { std::string nm = type_name (); error ("%s cannot be indexed with %c", nm.c_str (), type[0]); } // Allow conversion of empty cell array to some other // type in cases like // // x = {}; x.f = rhs octave_value tmp = octave_value::empty_conv (type, rhs); return tmp.subsasgn (type, idx, rhs); } break; default: panic_impossible (); } return retval; } bool octave_cell::is_cellstr (void) const { bool retval; if (cellstr_cache.get ()) retval = true; else { retval = matrix.is_cellstr (); // Allocate empty cache to mark that this is indeed a cellstr. if (retval) cellstr_cache.reset (new Array<std::string> ()); } return retval; } void octave_cell::assign (const octave_value_list& idx, const Cell& rhs) { clear_cellstr_cache (); octave_base_matrix<Cell>::assign (idx, rhs); } void octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) { clear_cellstr_cache (); octave_base_matrix<Cell>::assign (idx, rhs); } void octave_cell::delete_elements (const octave_value_list& idx) { clear_cellstr_cache (); octave_base_matrix<Cell>::delete_elements (idx); } size_t octave_cell::byte_size (void) const { size_t retval = 0; for (octave_idx_type i = 0; i < numel (); i++) retval += matrix(i).byte_size (); return retval; } octave_value octave_cell::sort (octave_idx_type dim, sortmode mode) const { octave_value retval; if (! is_cellstr ()) error ("sort: only cell arrays of character strings may be sorted"); Array<std::string> tmp = cellstr_value (); tmp = tmp.sort (dim, mode); // We already have the cache. retval = new octave_cell (tmp); return retval; } octave_value octave_cell::sort (Array<octave_idx_type> &sidx, octave_idx_type dim, sortmode mode) const { octave_value retval; if (! is_cellstr ()) error ("sort: only cell arrays of character strings may be sorted"); Array<std::string> tmp = cellstr_value (); tmp = tmp.sort (sidx, dim, mode); // We already have the cache. retval = new octave_cell (tmp); return retval; } sortmode octave_cell::is_sorted (sortmode mode) const { sortmode retval = UNSORTED; if (! is_cellstr ()) error ("issorted: A is not a cell array of strings"); Array<std::string> tmp = cellstr_value (); retval = tmp.is_sorted (mode); return retval; } Array<octave_idx_type> octave_cell::sort_rows_idx (sortmode mode) const { Array<octave_idx_type> retval; if (! is_cellstr ()) error ("sortrows: only cell arrays of character strings may be sorted"); Array<std::string> tmp = cellstr_value (); retval = tmp.sort_rows_idx (mode); return retval; } sortmode octave_cell::is_sorted_rows (sortmode mode) const { sortmode retval = UNSORTED; if (! is_cellstr ()) error ("issorted: A is not a cell array of strings"); Array<std::string> tmp = cellstr_value (); retval = tmp.is_sorted_rows (mode); return retval; } bool octave_cell::is_true (void) const { error ("invalid conversion from cell array to logical value"); } octave_value_list octave_cell::list_value (void) const { return octave_value_list (matrix); } string_vector octave_cell::string_vector_value (bool pad) const { string_vector retval; octave_idx_type nel = numel (); int n_elts = 0; octave_idx_type max_len = 0; std::queue<string_vector> strvec_queue; for (octave_idx_type i = 0; i < nel; i++) { string_vector s = matrix(i).string_vector_value (); octave_idx_type s_len = s.numel (); n_elts += s_len ? s_len : 1; octave_idx_type s_max_len = s.max_length (); if (s_max_len > max_len) max_len = s_max_len; strvec_queue.push (s); } retval = string_vector (n_elts); octave_idx_type k = 0; for (octave_idx_type i = 0; i < nel; i++) { const string_vector s = strvec_queue.front (); strvec_queue.pop (); octave_idx_type s_len = s.numel (); if (s_len) { for (octave_idx_type j = 0; j < s_len; j++) { std::string t = s[j]; int t_len = t.length (); if (pad && max_len > t_len) t += std::string (max_len - t_len, ' '); retval[k++] = t; } } else if (pad) retval[k++] = std::string (max_len, ' '); else retval[k++] = ""; } return retval; } Array<std::string> octave_cell::cellstr_value (void) const { Array<std::string> retval; if (! is_cellstr ()) error ("invalid conversion from cell array to array of strings"); if (cellstr_cache->is_empty ()) *cellstr_cache = matrix.cellstr_value (); return *cellstr_cache; return retval; } bool octave_cell::print_as_scalar (void) const { return true; } void octave_cell::print (std::ostream& os, bool) { print_raw (os); } void octave_cell::print_raw (std::ostream& os, bool) const { int nd = matrix.ndims (); if (nd == 2) { octave_idx_type nr = rows (); octave_idx_type nc = columns (); if (nr > 0 && nc > 0) { newline (os); indent (os); os << "{"; newline (os); increment_indent_level (); for (octave_idx_type j = 0; j < nc; j++) { for (octave_idx_type i = 0; i < nr; i++) { octave_quit (); std::ostringstream buf; buf << "[" << i+1 << "," << j+1 << "]"; octave_value val = matrix(i,j); val.print_with_name (os, buf.str ()); } } decrement_indent_level (); indent (os); os << "}"; newline (os); } else { indent (os); os << "{}"; if (Vprint_empty_dimensions) os << "(" << nr << "x" << nc << ")"; newline (os); } } else { indent (os); dim_vector dv = matrix.dims (); os << "{" << dv.str () << " Cell Array}"; newline (os); } } void octave_cell::short_disp (std::ostream& os) const { os << (matrix.is_empty () ? "{}" : "..."); } #define CELL_ELT_TAG "<cell-element>" bool octave_cell::save_ascii (std::ostream& os) { dim_vector dv = dims (); if (dv.ndims () > 2) { os << "# ndims: " << dv.ndims () << "\n"; for (int i = 0; i < dv.ndims (); i++) os << " " << dv(i); os << "\n"; Cell tmp = cell_value (); for (octave_idx_type i = 0; i < dv.numel (); i++) { octave_value o_val = tmp.elem (i); // Recurse to print sub-value. bool b = save_text_data (os, o_val, CELL_ELT_TAG, false, 0); if (! b) return ! os.fail (); } } else { // Keep this case, rather than use generic code above for backward // compatibility. Makes load_ascii much more complex!! os << "# rows: " << rows () << "\n" << "# columns: " << columns () << "\n"; Cell tmp = cell_value (); for (octave_idx_type j = 0; j < tmp.cols (); j++) { for (octave_idx_type i = 0; i < tmp.rows (); i++) { octave_value o_val = tmp.elem (i, j); // Recurse to print sub-value. bool b = save_text_data (os, o_val, CELL_ELT_TAG, false, 0); if (! b) return ! os.fail (); } os << "\n"; } } return true; } bool octave_cell::load_ascii (std::istream& is) { clear_cellstr_cache (); string_vector keywords(2); keywords[0] = "ndims"; keywords[1] = "rows"; std::string kw; octave_idx_type val = 0; if (! extract_keyword (is, keywords, kw, val, true)) error ("load: failed to extract number of rows and columns"); if (kw == "ndims") { int mdims = static_cast<int> (val); if (mdims < 0) error ("load: failed to extract number of rows and columns"); dim_vector dv; dv.resize (mdims); for (int i = 0; i < mdims; i++) is >> dv(i); Cell tmp(dv); for (octave_idx_type i = 0; i < dv.numel (); i++) { octave_value t2; bool dummy; // recurse to read cell elements std::string nm = read_text_data (is, "", dummy, t2, i); if (nm != CELL_ELT_TAG) error ("load: cell array element had unexpected name"); if (is) tmp.elem (i) = t2; } if (! is) error ("load: failed to load matrix constant"); matrix = tmp; } else if (kw == "rows") { octave_idx_type nr = val; octave_idx_type nc = 0; if (nr < 0 || ! extract_keyword (is, "columns", nc) || nc < 0) error ("load: failed to extract number of rows and columns for cell array"); if (nr > 0 && nc > 0) { Cell tmp (nr, nc); for (octave_idx_type j = 0; j < nc; j++) { for (octave_idx_type i = 0; i < nr; i++) { octave_value t2; bool dummy; // recurse to read cell elements std::string nm = read_text_data (is, "", dummy, t2, i); if (nm != CELL_ELT_TAG) error ("load: cell array element had unexpected name"); if (is) tmp.elem (i, j) = t2; } } if (! is) error ("load: failed to load cell element"); matrix = tmp; } else if (nr == 0 || nc == 0) matrix = Cell (nr, nc); else panic_impossible (); } else panic_impossible (); return true; } bool octave_cell::save_binary (std::ostream& os, bool& save_as_floats) { dim_vector dv = dims (); if (dv.ndims () < 1) return false; // Use negative value for ndims int32_t di = - dv.ndims (); os.write (reinterpret_cast<char *> (&di), 4); for (int i = 0; i < dv.ndims (); i++) { di = dv(i); os.write (reinterpret_cast<char *> (&di), 4); } Cell tmp = cell_value (); for (octave_idx_type i = 0; i < dv.numel (); i++) { octave_value o_val = tmp.elem (i); // Recurse to print sub-value. bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, save_as_floats); if (! b) return false; } return true; } bool octave_cell::load_binary (std::istream& is, bool swap, octave::mach_info::float_format fmt) { clear_cellstr_cache (); int32_t mdims; if (! is.read (reinterpret_cast<char *> (&mdims), 4)) return false; if (swap) swap_bytes<4> (&mdims); if (mdims >= 0) return false; mdims = -mdims; int32_t di; dim_vector dv; dv.resize (mdims); for (int i = 0; i < mdims; i++) { if (! is.read (reinterpret_cast<char *> (&di), 4)) return false; if (swap) swap_bytes<4> (&di); dv(i) = di; } // Convert an array with a single dimension to be a row vector. // Octave should never write files like this, other software // might. if (mdims == 1) { mdims = 2; dv.resize (mdims); dv(1) = dv(0); dv(0) = 1; } octave_idx_type nel = dv.numel (); Cell tmp(dv); for (octave_idx_type i = 0; i < nel; i++) { octave_value t2; bool dummy; std::string doc; // recurse to read cell elements std::string nm = read_binary_data (is, swap, fmt, "", dummy, t2, doc); if (nm != CELL_ELT_TAG) error ("load: cell array element had unexpected name"); if (is) tmp.elem (i) = t2; } if (! is) error ("load: failed to load matrix constant"); matrix = tmp; return true; } void * octave_cell::mex_get_data (void) const { clear_cellstr_cache (); return matrix.mex_get_data (); } bool octave_cell::save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats) { #if defined (HAVE_HDF5) dim_vector dv = dims (); int empty = save_hdf5_empty (loc_id, name, dv); if (empty) return (empty > 0); hsize_t rank = dv.ndims (); hid_t space_hid, data_hid, size_hid; space_hid = data_hid = size_hid = -1; #if defined (HAVE_HDF5_18) data_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT, octave_H5P_DEFAULT, octave_H5P_DEFAULT); #else data_hid = H5Gcreate (loc_id, name, 0); #endif if (data_hid < 0) return false; // Have to save cell array shape, since can't have a // dataset of groups.... space_hid = H5Screate_simple (1, &rank, 0); if (space_hid < 0) { H5Gclose (data_hid); return false; } OCTAVE_LOCAL_BUFFER (octave_idx_type, hdims, rank); // Octave uses column-major, while HDF5 uses row-major ordering for (hsize_t i = 0; i < rank; i++) hdims[i] = dv(rank-i-1); #if defined (HAVE_HDF5_18) size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, octave_H5P_DEFAULT, octave_H5P_DEFAULT, octave_H5P_DEFAULT); #else size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, octave_H5P_DEFAULT); #endif if (size_hid < 0) { H5Sclose (space_hid); H5Gclose (data_hid); return false; } if (H5Dwrite (size_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL, octave_H5P_DEFAULT, hdims) < 0) { H5Dclose (size_hid); H5Sclose (space_hid); H5Gclose (data_hid); return false; } H5Dclose (size_hid); H5Sclose (space_hid); // Recursively add each element of the cell to this group. Cell tmp = cell_value (); octave_idx_type nel = dv.numel (); for (octave_idx_type i = 0; i < nel; i++) { std::ostringstream buf; int digits = static_cast<int> (std::floor (::log10 (static_cast<double> (nel)) + 1.0)); buf << "_" << std::setw (digits) << std::setfill ('0') << i; std::string s = buf.str (); if (! add_hdf5_data (data_hid, tmp.elem (i), s.c_str (), "", false, save_as_floats)) { H5Gclose (data_hid); return false; } } H5Gclose (data_hid); return true; #else octave_unused_parameter (loc_id); octave_unused_parameter (name); octave_unused_parameter (save_as_floats); warn_save ("hdf5"); return false; #endif } bool octave_cell::load_hdf5 (octave_hdf5_id loc_id, const char *name) { bool retval = false; #if defined (HAVE_HDF5) clear_cellstr_cache (); dim_vector dv; int empty = load_hdf5_empty (loc_id, name, dv); if (empty > 0) matrix.resize (dv); if (empty) return (empty > 0); #if defined (HAVE_HDF5_18) hid_t group_id = H5Gopen (loc_id, name, octave_H5P_DEFAULT); #else hid_t group_id = H5Gopen (loc_id, name); #endif if (group_id < 0) return false; #if defined (HAVE_HDF5_18) hid_t data_hid = H5Dopen (group_id, "dims", octave_H5P_DEFAULT); #else hid_t data_hid = H5Dopen (group_id, "dims"); #endif hid_t space_hid = H5Dget_space (data_hid); hsize_t rank = H5Sget_simple_extent_ndims (space_hid); if (rank != 1) { H5Dclose (data_hid); H5Gclose (group_id); return false; } OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); H5Sget_simple_extent_dims (space_hid, hdims, maxdims); // Octave uses column-major, while HDF5 uses row-major ordering. dv.resize (hdims[0]); OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL, octave_H5P_DEFAULT, tmp) < 0) { H5Dclose (data_hid); H5Gclose (group_id); return false; } H5Dclose (data_hid); H5Gclose (group_id); for (hsize_t i = 0, j = hdims[0] - 1; i < hdims[0]; i++, j--) dv(j) = tmp[i]; hdf5_callback_data dsub; herr_t retval2 = -1; Cell m (dv); int current_item = 0; hsize_t num_obj = 0; #if defined (HAVE_HDF5_18) group_id = H5Gopen (loc_id, name, octave_H5P_DEFAULT); #else group_id = H5Gopen (loc_id, name); #endif H5Gget_num_objs (group_id, &num_obj); H5Gclose (group_id); for (octave_idx_type i = 0; i < dv.numel (); i++) { if (current_item >= static_cast<int> (num_obj)) retval2 = -1; else retval2 = hdf5_h5g_iterate (loc_id, name, ¤t_item,&dsub); if (retval2 <= 0) break; octave_value ov = dsub.tc; m.elem (i) = ov; } if (retval2 >= 0) { matrix = m; retval = true; } #else octave_unused_parameter (loc_id); octave_unused_parameter (name); warn_load ("hdf5"); #endif return retval; } DEFUN (iscell, args, , doc: /* -*- texinfo -*- @deftypefn {} {} iscell (@var{x}) Return true if @var{x} is a cell array object. @seealso{ismatrix, isstruct, iscellstr, isa} @end deftypefn */) { if (args.length () != 1) print_usage (); return ovl (args(0).is_cell ()); } DEFUN (cell, args, , doc: /* -*- texinfo -*- @deftypefn {} {} cell (@var{n}) @deftypefnx {} {} cell (@var{m}, @var{n}) @deftypefnx {} {} cell (@var{m}, @var{n}, @var{k}, @dots{}) @deftypefnx {} {} cell ([@var{m} @var{n} @dots{}]) Create a new cell array object. If invoked with a single scalar integer argument, return a square @nospell{NxN} cell array. If invoked with two or more scalar integer arguments, or a vector of integer values, return an array with the given dimensions. @seealso{cellstr, mat2cell, num2cell, struct2cell} @end deftypefn */) { int nargin = args.length (); dim_vector dims; switch (nargin) { case 0: dims = dim_vector (0, 0); break; case 1: get_dimensions (args(0), "cell", dims); break; default: { dims.resize (nargin); for (int i = 0; i < nargin; i++) dims(i) = (args(i).is_empty () ? 0 : args(i).xnint_value ("cell: dimension must be a scalar integer")); } break; } dims.chop_trailing_singletons (); check_dimensions (dims, "cell"); return ovl (Cell (dims)); } DEFUN (iscellstr, args, , doc: /* -*- texinfo -*- @deftypefn {} {} iscellstr (@var{cell}) Return true if every element of the cell array @var{cell} is a character string. @seealso{ischar} @end deftypefn */) { if (args.length () != 1) print_usage (); return ovl (args(0).is_cellstr ()); } // Note that since Fcellstr calls Fiscellstr, we need to have // Fiscellstr defined first (to provide a declaration) and also we // should keep it in the same file (so we don't have to provide a // declaration) and so we don't have to use feval to call it. DEFUN (cellstr, args, , doc: /* -*- texinfo -*- @deftypefn {} {@var{cstr} =} cellstr (@var{strmat}) Create a new cell array object from the elements of the string array @var{strmat}. Each row of @var{strmat} becomes an element of @var{cstr}. Any trailing spaces in a row are deleted before conversion. To convert back from a cellstr to a character array use @code{char}. @seealso{cell, char} @end deftypefn */) { if (args.length () != 1) print_usage (); octave_value_list tmp = Fiscellstr (args, 1); if (tmp(0).is_true ()) return ovl (args(0)); else { string_vector s = args(0).xstring_vector_value ("cellstr: argument STRING must be a 2-D character array"); return ovl (s.is_empty () ? Cell (octave_value ("")) : Cell (s, true)); } } DEFUN (struct2cell, args, , doc: /* -*- texinfo -*- @deftypefn {} {@var{c} =} struct2cell (@var{s}) Create a new cell array from the objects stored in the struct object. If @var{f} is the number of fields in the structure, the resulting cell array will have a dimension vector corresponding to @code{[@var{f} size(@var{s})]}. For example: @example @group s = struct ("name", @{"Peter", "Hannah", "Robert"@}, "age", @{23, 16, 3@}); c = struct2cell (s) @result{} c = @{2x1x3 Cell Array@} c(1,1,:)(:) @result{} @{ [1,1] = Peter [2,1] = Hannah [3,1] = Robert @} c(2,1,:)(:) @result{} @{ [1,1] = 23 [2,1] = 16 [3,1] = 3 @} @end group @end example @seealso{cell2struct, fieldnames} @end deftypefn */) { if (args.length () != 1) print_usage (); const octave_map m = args(0).xmap_value ("struct2cell: argument S must be a structure"); const dim_vector m_dv = m.dims (); octave_idx_type num_fields = m.nfields (); // The resulting dim_vector should have dimensions: // [numel(fields) size(struct)] // except if the struct is a column vector. dim_vector result_dv; if (m_dv(m_dv.ndims () - 1) == 1) result_dv.resize (m_dv.ndims ()); else result_dv.resize (m_dv.ndims () + 1); // Add 1 for the fields. result_dv(0) = num_fields; for (int i = 1; i < result_dv.ndims (); i++) result_dv(i) = m_dv(i-1); NoAlias<Cell> c (result_dv); octave_idx_type n_elts = m.numel (); // Fill c in one sweep. Note that thanks to octave_map structure, // we don't need a key lookup at all. for (octave_idx_type j = 0; j < n_elts; j++) for (octave_idx_type i = 0; i < num_fields; i++) c(i,j) = m.contents(i)(j); return ovl (c); } /* %!test %! keys = cellstr (char (floor (rand (11,10)*24+65)))'; %! vals = cellfun (@(x) mat2cell (rand (19,1), ones (19,1), 1), ... %! mat2cell ([1:11]', ones (11,1), 1), "uniformoutput", false)'; %! s = struct ([keys; vals]{:}); %! t = cell2struct ([vals{:}], keys, 2); %! assert (s, t); %! assert (struct2cell (s), [vals{:}]'); %! assert (fieldnames (s), keys'); */ mxArray * octave_cell::as_mxArray (void) const { mxArray *retval = new mxArray (dims ()); mxArray **elts = static_cast<mxArray **> (retval->get_data ()); mwSize nel = numel (); const octave_value *p = matrix.data (); for (mwIndex i = 0; i < nel; i++) elts[i] = new mxArray (p[i]); return retval; } octave_value octave_cell::map (unary_mapper_t umap) const { switch (umap) { #define FORWARD_MAPPER(UMAP) \ case umap_ ## UMAP: \ return matrix.UMAP () FORWARD_MAPPER (xisalnum); FORWARD_MAPPER (xisalpha); FORWARD_MAPPER (xisascii); FORWARD_MAPPER (xiscntrl); FORWARD_MAPPER (xisdigit); FORWARD_MAPPER (xisgraph); FORWARD_MAPPER (xislower); FORWARD_MAPPER (xisprint); FORWARD_MAPPER (xispunct); FORWARD_MAPPER (xisspace); FORWARD_MAPPER (xisupper); FORWARD_MAPPER (xisxdigit); FORWARD_MAPPER (xtoascii); FORWARD_MAPPER (xtolower); FORWARD_MAPPER (xtoupper); default: return octave_base_value::map (umap); } }