26 #if defined (HAVE_CONFIG_H)
61 static int Vstruct_levels_to_print = 2;
65 static bool Vprint_struct_array_contents =
false;
75 c(i).break_closure_cycles (frame);
97 std::string nm = idx(0).string_value ();
107 "structure has no member '%s'", nm.c_str ());
113 err_invalid_index_for_assignment ()
115 error (
"invalid index for structure array assignment");
119 err_invalid_index_type (
const std::string& nm,
char t)
121 error (
"%s cannot be indexed with %c", nm.c_str (), t);
125 maybe_warn_invalid_field_name (
const std::string& key,
const char *who)
131 "%s: invalid structure field name '%s'",
135 "invalid structure field name '%s'",
142 const std::list<octave_value_list>& idx,
153 if (type.length () > 1 && type[1] ==
'.')
155 auto p = idx.begin ();
189 err_invalid_index_type (
type_name (), type[0]);
201 retval = retval(0).next_subsref (nargout, type, idx, skip);
208 const std::list<octave_value_list>& idx,
219 if (type.length () > 1 && type[1] ==
'.')
221 auto p = idx.begin ();
226 const Cell t = tmp.
index (idx.front (), auto_add);
247 const Cell t =
dotref (idx.front (), auto_add);
258 err_invalid_index_type (
type_name (), type[0]);
270 retval = retval.
next_subsref (auto_add, type, idx, skip);
286 const std::string& type)
290 if (type.length () > 0 && type[0] ==
'.' && ! val.
isstruct ())
300 const std::list<octave_value_list>& idx,
305 int n = type.length ();
309 if (idx.front ().empty ())
310 error (
"subsasgn: missing index in indexed assignment");
312 if (
n > 1 && ! (type.length () == 2 && type[0] ==
'(' && type[1] ==
'.'))
318 if (type.length () > 1 && type[1] ==
'.')
320 auto p = idx.begin ();
325 if (key_idx.
length () != 1)
326 error (
"subsasgn: dynamic structure field names must be strings");
329 = key_idx(0).xstring_value (
"subsasgn: dynamic structure field names must be strings");
331 maybe_warn_invalid_field_name (key,
"subsasgn");
333 std::list<octave_value_list> next_idx (idx);
338 next_idx.erase (next_idx.begin ());
339 next_idx.erase (next_idx.begin ());
341 std::string next_type = type.substr (2);
353 if (tmpc.
numel () != 1)
370 t_rhs =(orig_undefined
372 : tmp.
subsasgn (next_type, next_idx, rhs));
375 err_invalid_index_for_assignment ();
383 if (key_idx.
length () != 1)
384 error (
"subsasgn: dynamic structure field names must be strings");
387 = key_idx(0).xstring_value (
"subsasgn: dynamic structure field names must be strings");
389 maybe_warn_invalid_field_name (key,
"subsasgn");
391 std::list<octave_value_list> next_idx (idx);
393 next_idx.erase (next_idx.begin ());
395 std::string next_type = type.substr (1);
407 if (tmpc.
numel () == 1)
423 t_rhs = (orig_undefined
425 : tmp.
subsasgn (next_type, next_idx, rhs));
433 err_invalid_index_type (
type_name (), type[0]);
445 if (
n > 1 && type[1] ==
'.')
447 auto p = idx.begin ();
451 if (key_idx.
length () != 1)
452 error (
"subsasgn: dynamic structure field names must be strings");
455 = key_idx(0).xstring_value (
"subsasgn: dynamic structure field names must be strings");
457 maybe_warn_invalid_field_name (key,
"subsasgn");
468 didx(k) = idxf(k).numel ();
471 tmp_cell = tmp_cell.
reshape (didx);
509 error (
"invalid structure assignment");
524 if (key_idx.
length () != 1)
525 error (
"subsasgn: dynamic structure field names must be strings");
528 = key_idx(0).xstring_value (
"subsasgn: dynamic structure field names must be strings");
530 maybe_warn_invalid_field_name (key,
"subsasgn");
558 err_invalid_index_type (
type_name (), type[0]);
602 std::size_t retval = 0;
625 octave::unwind_protect_var<int> restore_var (Vstruct_levels_to_print);
627 if (Vstruct_levels_to_print >= 0)
629 bool max_depth_reached = (Vstruct_levels_to_print-- == 0);
631 bool print_fieldnames_only = (max_depth_reached
632 || ! Vprint_struct_array_contents);
638 os << dv.
str () <<
" struct array containing the fields:";
647 std::string key = key_list[i];
654 if (print_fieldnames_only)
666 if (print_fieldnames_only)
687 if (Vstruct_levels_to_print < 0)
705 return dims.
ndims () == 2 && dims(0) == 1 && dims(1) == 1;
731 std::string dimstr = dv.
str ();
732 return "[" + dimstr +
" " + tname +
"]";
745 os <<
"# ndims: " << dv.
ndims () <<
"\n";
747 for (
int i = 0; i < dv.
ndims (); i++)
751 os <<
"# length: " << nf <<
"\n";
759 std::string key = keys(i);
784 keywords[0] =
"ndims";
785 keywords[1] =
"length";
791 if (kw == keywords[0])
795 for (
int i = 0; i < mdims; i++)
804 if (! success ||
len < 0)
805 error (
"load: failed to extract number of elements in structure");
825 m.setfield (nm, tcell);
829 error (
"load: failed to load structure");
853 int32_t di = - dv.
ndims ();
854 os.write (
reinterpret_cast<char *
> (&di), 4);
855 for (
int i = 0; i < dv.
ndims (); i++)
858 os.write (
reinterpret_cast<char *
> (&di), 4);
862 os.write (
reinterpret_cast<char *
> (&
len), 4);
870 std::string key = keys(i);
889 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
904 for (
int i = 0; i < mdims; i++)
906 if (! is.read (
reinterpret_cast<char *
> (&di), 4))
913 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
939 m.setfield (nm, tcell);
943 error (
"load: failed to load structure");
959 #if defined (HAVE_HDF5)
963 #if defined (HAVE_HDF5_18)
967 data_hid = H5Gcreate (loc_id, name, 0);
969 if (data_hid < 0)
return false;
982 std::string key = keys(i);
998 octave_unused_parameter (loc_id);
999 octave_unused_parameter (name);
1000 octave_unused_parameter (save_as_floats);
1011 bool retval =
false;
1013 #if defined (HAVE_HDF5)
1019 int current_item = 0;
1020 hsize_t num_obj = 0;
1021 #if defined (HAVE_HDF5_18)
1024 hid_t group_id = H5Gopen (loc_id, name);
1026 H5Gget_num_objs (group_id, &num_obj);
1027 H5Gclose (group_id);
1032 while (current_item <
static_cast<int> (num_obj)
1041 m.setfield (dsub.
name, tcell);
1052 octave_unused_parameter (loc_id);
1053 octave_unused_parameter (name);
1069 for (
int i = 0; i < nf; i++)
1070 f[i] = kv[i].c_str ();
1080 for (
int i = 0; i < nf; i++)
1087 for (
mwIndex j = i; j < ntot; j += nf)
1088 elts[j] =
new mxArray (interleaved, p[k++]);
1107 bool retval =
false;
1114 void *here =
reinterpret_cast<void *
> (&sm_ptr);
1115 return (
x.get_rep ().fast_elem_insert_self (here,
btyp_struct)
1139 std::string nm = idx(0).string_value ();
1141 maybe_warn_invalid_field_name (nm,
"subsref");
1147 "structure has no member '%s'", nm.c_str ());
1154 const std::list<octave_value_list>& idx)
1162 retval =
dotref (idx.front ());
1164 if (idx.size () > 1)
1168 retval = to_array ().
subsref (type, idx);
1175 const std::list<octave_value_list>& idx,
1184 retval(0) =
dotref (idx.front ());
1186 if (idx.size () > 1)
1187 retval = retval(0).next_subsref (nargout, type, idx, skip);
1190 retval = to_array ().
subsref (type, idx, nargout);
1197 const std::list<octave_value_list>& idx,
1206 retval =
dotref (idx.front (), auto_add);
1208 if (idx.size () > 1)
1209 retval = retval.
next_subsref (auto_add, type, idx, skip);
1212 retval = to_array ().
subsref (type, idx, auto_add);
1228 const std::string& type)
1232 if (type.length () > 0 && type[0] ==
'.' && ! val.
isstruct ())
1242 const std::list<octave_value_list>& idx,
1247 if (idx.front ().empty ())
1248 error (
"subsasgn: missing index in indexed assignment");
1252 int n = type.length ();
1258 if (key_idx.
length () != 1)
1259 error (
"subsasgn: structure field names must be strings");
1262 = key_idx(0).xstring_value (
"subsasgn: structure field names must be strings");
1264 maybe_warn_invalid_field_name (key,
"subsasgn");
1268 std::list<octave_value_list> next_idx (idx);
1270 next_idx.erase (next_idx.begin ());
1272 std::string next_type = type.substr (1);
1293 t_rhs = (orig_undefined
1295 : tmp.
subsasgn (next_type, next_idx, rhs));
1307 retval = tmp.
subsasgn (type, idx, rhs);
1339 std::size_t retval = 0;
1362 octave::unwind_protect_var<int> restore_var (Vstruct_levels_to_print);
1364 if (Vstruct_levels_to_print >= 0)
1366 bool max_depth_reached = (Vstruct_levels_to_print-- == 0);
1368 bool print_fieldnames_only = max_depth_reached;
1377 std::string key = key_list[i];
1381 if (print_fieldnames_only)
1399 os <<
"<structure>";
1406 const std::string& name)
const
1408 bool retval =
false;
1412 if (Vstruct_levels_to_print < 0)
1413 os << name <<
" = ";
1424 os <<
"scalar structure containing the fields:";
1447 std::string dimstr = dv.
str ();
1448 return "[" + dimstr +
" " + tname +
"]";
1460 os <<
"# ndims: " << dv.
ndims () <<
"\n";
1462 for (
int i = 0; i < dv.
ndims (); i++)
1466 os <<
"# length: " << nf <<
"\n";
1474 std::string key = keys(i);
1481 return ! os.fail ();
1493 error (
"load: failed to extract number of elements in structure");
1511 m.setfield (nm, t2);
1515 error (
"load: failed to load structure");
1535 os.write (
reinterpret_cast<char *
> (&
len), 4);
1543 std::string key = keys(i);
1550 return ! os.fail ();
1560 bool success =
true;
1562 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
1584 m.setfield (nm, t2);
1588 error (
"load: failed to load structure");
1602 bool save_as_floats)
1604 #if defined (HAVE_HDF5)
1606 hid_t data_hid = -1;
1608 #if defined (HAVE_HDF5_18)
1612 data_hid = H5Gcreate (loc_id, name, 0);
1614 if (data_hid < 0)
return false;
1627 std::string key = keys(i);
1631 bool retval2 =
add_hdf5_data (data_hid, val, key,
"",
false,
1638 H5Gclose (data_hid);
1643 octave_unused_parameter (loc_id);
1644 octave_unused_parameter (name);
1645 octave_unused_parameter (save_as_floats);
1656 bool retval =
false;
1658 #if defined (HAVE_HDF5)
1664 int current_item = 0;
1665 hsize_t num_obj = 0;
1666 #if defined (HAVE_HDF5_18)
1669 hid_t group_id = H5Gopen (loc_id, name);
1671 H5Gget_num_objs (group_id, &num_obj);
1672 H5Gclose (group_id);
1677 while (current_item <
static_cast<int> (num_obj)
1683 m.setfield (dsub.
name, t2);
1694 octave_unused_parameter (loc_id);
1695 octave_unused_parameter (name);
1711 for (
int i = 0; i < nf; i++)
1712 f[i] = kv[i].c_str ();
1722 for (
int i = 0; i < nf; i++)
1729 for (
mwIndex j = i; j < ntot; j += nf)
1730 elts[j] =
new mxArray (interleaved, p[k++]);
1737 octave_scalar_struct::to_array ()
1758 DEFUN (
struct, args, ,
1816 int nargin = args.length ();
1826 if (nargin == 1 && args(0).isstruct ())
1827 return ovl (args(0));
1829 if (nargin == 1 && args(0).isobject ())
1830 return ovl (args(0).map_value ());
1832 if ((nargin == 1 || nargin == 2)
1833 && args(0).isempty () && args(0).is_real_matrix ())
1837 Array<std::string> cstr = args(1).xcellstr_value (
"struct: second argument should be a cell array of field names");
1847 for (
int i = 0; i < nargin; i += 2)
1849 if (! args(i).is_string () || i + 1 >= nargin)
1850 error (R
"(struct: additional arguments must occur as "field", VALUE pairs)");
1857 int first_dimensioned_value = 0;
1859 for (
int i = 1; i < nargin; i += 2)
1861 if (args(i).iscell ())
1865 if (! scalar (argdims))
1867 if (! first_dimensioned_value)
1870 first_dimensioned_value = i + 1;
1872 else if (dims != argdims)
1874 error (
"struct: dimensions of parameter %d "
1875 "do not match those of parameter %d",
1876 first_dimensioned_value, i+1);
1886 for (
int i = 0; i < nargin; i+= 2)
1890 std::string key (args(i).string_value ());
1892 maybe_warn_invalid_field_name (key,
"struct");
1900 if (args(i+1).iscell ())
1902 const Cell c (args(i+1).cell_value ());
1904 if (scalar (c.
dims ()))
1938 DEFUN (isstruct, args, ,
1945 if (args.length () != 1)
1948 return ovl (args(0).isstruct ());
1951 DEFUN (__fieldnames__, args, ,
1971 retval =
Cell (0, 1);
1973 retval =
Cell (keys);
1978 DEFUN (isfield, args, ,
1990 if (args.length () != 2)
1995 if (args(0).isstruct ())
2001 if (args(1).is_string ())
2003 std::string key = args(1).string_value ();
2005 retval =
m.isfield (key);
2007 else if (args(1).iscell ())
2009 Cell c = args(1).cell_value ();
2015 if (c(i).is_string ())
2017 std::string key = c(i).string_value ();
2019 bm(i) =
m.isfield (key);
2032 DEFUN (numfields, args, ,
2039 if (args.length () != 1)
2042 if (! args(0).isstruct ())
2043 error (
"numfields: argument must be a struct");
2045 return ovl (
static_cast<double> (args(0).nfields ()));
2062 invalid_cell2struct_fields_error ()
2064 error (
"cell2struct: FIELDS must be a cell array of strings or a scalar string");
2072 if (arg.
rows () != 1)
2073 invalid_cell2struct_fields_error ();
2089 invalid_cell2struct_fields_error ();
2091 retval(i) = c(i).string_value ();
2097 invalid_cell2struct_fields_error ();
2100 DEFUN (cell2struct, args, ,
2128 int nargin = args.length ();
2130 if (nargin < 2 || nargin > 3)
2133 const Cell vals = args(0).xcell_value (
"cell2struct: argument CELL must be of type cell");
2141 if (! args(2).is_real_scalar ())
2142 error (
"cell2struct: DIM must be a real scalar");
2144 dim = args(2).int_value () - 1;
2148 error (
"cell2struct: DIM must be a valid dimension");
2152 if (ext != fields.
numel ())
2153 error (
"cell2struct: number of FIELDS does not match dimension");
2162 rdv(0) = rdv(1-dim);
2167 for (
int i = dim + 1; i < nd; i++)
2207 DEFUN (rmfield, args, ,
2219 if (args.length () != 2)
2222 octave_map m = args(0).xmap_value (
"rmfield: first argument must be a struct");
2228 for (
int i = 0; i < fcell.
numel (); i++)
2230 std::string key = fcell(i).string_value ();
2232 if (!
m.isfield (key))
2233 error (
"rmfield: structure does not contain field %s", key.c_str ());
2256 DEFUN (struct_levels_to_print, args, nargout,
2271 "struct_levels_to_print", -1,
2275 DEFUN (print_struct_array_contents, args, nargout,
2295 "print_struct_array_contents");
2298 OCTAVE_END_NAMESPACE(
octave)
octave_value_list Fcellstr(const octave_value_list &=octave_value_list(), int=0)
void swap_bytes< 4 >(void *ptr)
charNDArray max(char d, const charNDArray &m)
int ndims() const
Size of the specified dimension.
const T * data() const
Size of the specified dimension.
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
octave_idx_type numel() const
Number of elements in the array.
Cell reshape(const dim_vector &new_dims) const
Cell index(const octave_value_list &idx, bool resize_ok=false) const
Vector representing the dimensions (size) of an Array.
std::string str(char sep='x') const
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
void resize(int n, int fill_value=0)
octave_idx_type ndims() const
Number of dimensions.
dim_vector redim(int n) const
Force certain dimensionality, preserving numel ().
static const idx_vector colon
void increment_indent_level() const
void indent(std::ostream &os) const
void newline(std::ostream &os) const
octave::refcount< octave_idx_type > m_count
void warn_load(const char *type) const
virtual bool is_magic_colon() const
void decrement_indent_level() const
friend class octave_value
void warn_save(const char *type) const
const_iterator cbegin() const
octave_idx_type nfields() const
const_iterator end() const
octave_idx_type rows() const
octave_scalar_map checkelem(octave_idx_type n) const
string_vector fieldnames() const
octave_fields::const_iterator const_iterator
octave_idx_type columns() const
void delete_elements(const octave::idx_vector &i)
void setfield(const std::string &key, const Cell &val)
octave_idx_type numel() const
const_iterator seek(const std::string &k) const
void assign(const std::string &k, const Cell &val)
std::string key(const_iterator p) const
const Cell & contents(const_iterator p) const
bool fast_elem_insert(octave_idx_type n, const octave_scalar_map &rhs)
const_iterator cend() const
octave_idx_type index(const_iterator p) const
const_iterator cend() const
const_iterator cbegin() const
const_iterator end() const
const octave_value & contents(const_iterator p) const
string_vector fieldnames() const
void setfield(const std::string &key, const octave_value &val)
const_iterator seek(const std::string &k) const
octave_idx_type nfields() const
octave_value getfield(const std::string &key) const
std::string key(const_iterator p) const
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
octave_idx_type nfields() const
bool print_name_tag(std::ostream &os, const std::string &name) const
mxArray * as_mxArray(bool interleaved) const
static octave_value numeric_conv(const octave_value &val, const std::string &type)
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
bool load_ascii(std::istream &is)
void print(std::ostream &os, bool pr_as_read_syntax=false)
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
octave_value dotref(const octave_value_list &idx, bool auto_add=false)
octave_map map_value() const
bool save_binary(std::ostream &os, bool save_as_floats)
bool save_ascii(std::ostream &os)
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
std::size_t byte_size() const
octave_scalar_map scalar_map_value() const
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
void break_closure_cycles(const std::shared_ptr< octave::stack_frame > &frame)
string_vector map_keys() const
std::string edit_display(const float_display_format &fmt, octave_idx_type i, octave_idx_type j) const
octave_idx_type numel() const
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
string_vector map_keys() const
bool fast_elem_insert(octave_idx_type n, const octave_value &x)
octave_idx_type numel() const
static octave_value numeric_conv(const octave_value &val, const std::string &type)
bool load_ascii(std::istream &is)
std::string edit_display(const float_display_format &fmt, octave_idx_type i, octave_idx_type j) const
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
mxArray * as_mxArray(bool interleaved) const
bool save_ascii(std::ostream &os)
octave_map map_value() const
std::string type_name() const
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
std::size_t byte_size() const
octave_idx_type nfields() const
void break_closure_cycles(const std::shared_ptr< octave::stack_frame > &frame)
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
bool print_name_tag(std::ostream &os, const std::string &name) const
octave_value fast_elem_extract(octave_idx_type n) const
octave_base_value * try_narrowing_conversion()
bool save_binary(std::ostream &os, bool save_as_floats)
Cell dotref(const octave_value_list &idx, bool auto_add=false)
void print(std::ostream &os, bool pr_as_read_syntax=false)
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
octave_idx_type length() const
octave_value_list list_value() const
bool is_undefined() const
Cell xcell_value(const char *fmt,...) const
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
octave_map xmap_value(const char *fmt,...) const
void print_with_name(std::ostream &os, const std::string &name) const
octave_idx_type rows() const
octave_value undef_subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
static octave_value empty_conv(const std::string &type, const octave_value &rhs=octave_value())
octave_value next_subsref(const std::string &type, const std::list< octave_value_list > &idx, std::size_t skip=1)
void break_closure_cycles(const std::shared_ptr< octave::stack_frame > &)
octave_value storable_value() const
bool is_zero_by_zero() const
octave_map map_value() const
std::string string_value(bool force=false) const
std::string type_name() const
std::size_t byte_size() const
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
octave_idx_type numel() const
const octave_hdf5_id octave_H5P_DEFAULT
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void error_with_id(const char *id, const char *fmt,...)
void warning_with_id(const char *id, const char *fmt,...)
void() error(const char *fmt,...)
#define panic_impossible()
#define panic_unless(cond)
void warn_empty_index(const std::string &type_name)
void err_indexed_cs_list()
void err_nonbraced_cs_list_assignment()
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
octave_hdf5_err hdf5_h5g_iterate(octave_hdf5_id loc_id, const char *name, int *idx, void *operator_data)
bool add_hdf5_data(octave_hdf5_id loc_id, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_global, bool save_as_floats)
std::string read_binary_data(std::istream &is, bool swap, octave::mach_info::float_format fmt, const std::string &filename, bool &global, octave_value &tc, std::string &doc)
bool save_binary_data(std::ostream &os, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_global, bool save_as_floats)
std::string read_text_data(std::istream &is, const std::string &filename, bool &global, octave_value &tc, octave_idx_type count, const bool do_name_validation)
std::string extract_keyword(std::istream &is, const char *keyword, const bool next_only)
bool save_text_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_global, int precision)
bool valid_identifier(const char *s)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.