26#if defined (HAVE_CONFIG_H)
34#include "builtin-defun-decls.h"
74 c(i).break_closure_cycles (frame);
94 assert (idx.
length () == 1);
96 std::string nm = idx(0).string_value ();
106 "structure has no member '%s'", nm.c_str ());
114 error (
"invalid index for structure array assignment");
120 error (
"%s cannot be indexed with %c", nm.c_str (), t);
130 "%s: invalid structure field name '%s'",
134 "invalid structure field name '%s'",
141 const std::list<octave_value_list>& idx,
152 if (type.length () > 1 && type[1] ==
'.')
154 auto p = idx.begin ();
200 retval = retval(0).next_subsref (nargout, type, idx, skip);
207 const std::list<octave_value_list>& idx,
218 if (type.length () > 1 && type[1] ==
'.')
220 auto p = idx.begin ();
225 const Cell t = tmp.
index (idx.front (), auto_add);
246 const Cell t =
dotref (idx.front (), auto_add);
269 retval = retval.
next_subsref (auto_add, type, idx, skip);
285 const std::string& type)
289 if (type.length () > 0 && type[0] ==
'.' && ! val.
isstruct ())
299 const std::list<octave_value_list>& idx,
308 if (idx.front ().empty ())
309 error (
"missing index in indexed assignment");
311 if (n > 1 && ! (type.length () == 2 && type[0] ==
'(' && type[1] ==
'.'))
317 if (type.length () > 1 && type[1] ==
'.')
319 auto p = idx.begin ();
324 assert (key_idx.
length () == 1);
326 std::string key = key_idx(0).string_value ();
330 std::list<octave_value_list> next_idx (idx);
335 next_idx.erase (next_idx.begin ());
336 next_idx.erase (next_idx.begin ());
338 std::string next_type = type.substr (2);
350 if (tmpc.
numel () != 1)
367 t_rhs =(orig_undefined
369 : tmp.
subsasgn (next_type, next_idx, rhs));
380 assert (key_idx.
length () == 1);
382 std::string key = key_idx(0).string_value ();
386 std::list<octave_value_list> next_idx (idx);
388 next_idx.erase (next_idx.begin ());
390 std::string next_type = type.substr (1);
402 if (tmpc.
numel () == 1)
418 t_rhs = (orig_undefined
420 : tmp.
subsasgn (next_type, next_idx, rhs));
440 if (n > 1 && type[1] ==
'.')
442 auto p = idx.begin ();
446 assert (key_idx.
length () == 1);
448 std::string key = key_idx(0).string_value ();
461 didx(k) = idxf(k).numel ();
464 tmp_cell = tmp_cell.
reshape (didx);
502 error (
"invalid structure assignment");
517 assert (key_idx.
length () == 1);
519 std::string key = key_idx(0).string_value ();
578 std::size_t retval = 0;
607 bool print_fieldnames_only = (max_depth_reached
614 os << dv.
str () <<
" struct array containing the fields:";
623 std::string key = key_list[i];
630 if (print_fieldnames_only)
642 if (print_fieldnames_only)
681 return dims.
ndims () == 2 && dims(0) == 1 && dims(1) == 1;
707 std::string dimstr = dv.
str ();
708 return "[" + dimstr +
" " + tname +
"]";
721 os <<
"# ndims: " << dv.
ndims () <<
"\n";
723 for (
int i = 0; i < dv.
ndims (); i++)
727 os <<
"# length: " << nf <<
"\n";
735 std::string key = keys(i);
760 keywords[0] =
"ndims";
761 keywords[1] =
"length";
767 if (kw == keywords[0])
771 for (
int i = 0; i < mdims; i++)
780 if (! success ||
len < 0)
781 error (
"load: failed to extract number of elements in structure");
804 error (
"load: failed to load structure");
828 int32_t di = - dv.
ndims ();
829 os.write (
reinterpret_cast<char *
> (&di), 4);
830 for (
int i = 0; i < dv.
ndims (); i++)
833 os.write (
reinterpret_cast<char *
> (&di), 4);
837 os.write (
reinterpret_cast<char *
> (&
len), 4);
845 std::string key = keys(i);
864 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
879 for (
int i = 0; i < mdims; i++)
881 if (! is.read (
reinterpret_cast<char *
> (&di), 4))
888 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
917 error (
"load: failed to load structure");
933#if defined (HAVE_HDF5)
937#if defined (HAVE_HDF5_18)
941 data_hid = H5Gcreate (loc_id,
name, 0);
943 if (data_hid < 0)
return false;
956 std::string key = keys(i);
972 octave_unused_parameter (loc_id);
973 octave_unused_parameter (
name);
974 octave_unused_parameter (save_as_floats);
987#if defined (HAVE_HDF5)
993 int current_item = 0;
995#if defined (HAVE_HDF5_18)
998 hid_t group_id = H5Gopen (loc_id,
name);
1000 H5Gget_num_objs (group_id, &num_obj);
1001 H5Gclose (group_id);
1006 while (current_item <
static_cast<int> (num_obj)
1025 octave_unused_parameter (loc_id);
1026 octave_unused_parameter (
name);
1042 for (
int i = 0; i < nf; i++)
1043 f[i] = kv[i].c_str ();
1049 mwSize nel =
numel ();
1051 mwSize ntot = nf * nel;
1053 for (
int i = 0; i < nf; i++)
1060 for (mwIndex j = i; j < ntot; j += nf)
1061 elts[j] =
new mxArray (interleaved, p[k++]);
1080 bool retval =
false;
1087 void *here =
reinterpret_cast<void *
>(&sm_ptr);
1088 return (
x.get_rep ().fast_elem_insert_self (here,
btyp_struct)
1110 assert (idx.
length () == 1);
1112 std::string nm = idx(0).string_value ();
1120 "structure has no member '%s'", nm.c_str ());
1127 const std::list<octave_value_list>& idx)
1135 retval =
dotref (idx.front ());
1137 if (idx.size () > 1)
1148 const std::list<octave_value_list>& idx,
1157 retval(0) =
dotref (idx.front ());
1159 if (idx.size () > 1)
1160 retval = retval(0).next_subsref (nargout, type, idx, skip);
1170 const std::list<octave_value_list>& idx,
1179 retval =
dotref (idx.front (), auto_add);
1181 if (idx.size () > 1)
1182 retval = retval.
next_subsref (auto_add, type, idx, skip);
1201 const std::string& type)
1205 if (type.length () > 0 && type[0] ==
'.' && ! val.
isstruct ())
1215 const std::list<octave_value_list>& idx,
1220 if (idx.front ().empty ())
1221 error (
"missing index in indexed assignment");
1225 int n = type.length ();
1231 assert (key_idx.
length () == 1);
1233 std::string key = key_idx(0).string_value ();
1239 std::list<octave_value_list> next_idx (idx);
1241 next_idx.erase (next_idx.begin ());
1243 std::string next_type = type.substr (1);
1264 t_rhs = (orig_undefined
1266 : tmp.
subsasgn (next_type, next_idx, rhs));
1278 retval = tmp.
subsasgn (type, idx, rhs);
1296 std::size_t retval = 0;
1325 bool print_fieldnames_only = max_depth_reached;
1334 std::string key = key_list[i];
1338 if (print_fieldnames_only)
1356 os <<
"<structure>";
1363 const std::string&
name)
const
1365 bool retval =
false;
1370 os <<
name <<
" = ";
1381 os <<
"scalar structure containing the fields:";
1404 std::string dimstr = dv.
str ();
1405 return "[" + dimstr +
" " + tname +
"]";
1417 os <<
"# ndims: " << dv.
ndims () <<
"\n";
1419 for (
int i = 0; i < dv.
ndims (); i++)
1423 os <<
"# length: " << nf <<
"\n";
1431 std::string key = keys(i);
1438 return ! os.fail ();
1450 error (
"load: failed to extract number of elements in structure");
1472 error (
"load: failed to load structure");
1492 os.write (
reinterpret_cast<char *
> (&
len), 4);
1500 std::string key = keys(i);
1507 return ! os.fail ();
1517 bool success =
true;
1519 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
1545 error (
"load: failed to load structure");
1559 bool save_as_floats)
1561#if defined (HAVE_HDF5)
1563 hid_t data_hid = -1;
1565#if defined (HAVE_HDF5_18)
1569 data_hid = H5Gcreate (loc_id,
name, 0);
1571 if (data_hid < 0)
return false;
1584 std::string key = keys(i);
1588 bool retval2 =
add_hdf5_data (data_hid, val, key,
"",
false,
1595 H5Gclose (data_hid);
1600 octave_unused_parameter (loc_id);
1601 octave_unused_parameter (
name);
1602 octave_unused_parameter (save_as_floats);
1613 bool retval =
false;
1615#if defined (HAVE_HDF5)
1621 int current_item = 0;
1622 hsize_t num_obj = 0;
1623#if defined (HAVE_HDF5_18)
1626 hid_t group_id = H5Gopen (loc_id,
name);
1628 H5Gget_num_objs (group_id, &num_obj);
1629 H5Gclose (group_id);
1634 while (current_item <
static_cast<int> (num_obj)
1651 octave_unused_parameter (loc_id);
1652 octave_unused_parameter (
name);
1668 for (
int i = 0; i < nf; i++)
1669 f[i] = kv[i].c_str ();
1675 mwSize nel =
numel ();
1677 mwSize ntot = nf * nel;
1679 for (
int i = 0; i < nf; i++)
1686 for (mwIndex j = i; j < ntot; j += nf)
1687 elts[j] =
new mxArray (interleaved, p[k++]);
1713OCTAVE_NAMESPACE_BEGIN
1715DEFUN (
struct, args, ,
1773 int nargin = args.length ();
1783 if (nargin == 1 && args(0).isstruct ())
1784 return ovl (args(0));
1786 if (nargin == 1 && args(0).isobject ())
1787 return ovl (args(0).map_value ());
1789 if ((nargin == 1 || nargin == 2)
1790 && args(0).isempty () && args(0).is_real_matrix ())
1794 Array<std::string> cstr = args(1).xcellstr_value (
"struct: second argument should be a cell array of field names");
1804 for (
int i = 0; i < nargin; i += 2)
1806 if (! args(i).is_string () || i + 1 >= nargin)
1807 error (R
"(struct: additional arguments must occur as "field", VALUE pairs)");
1814 int first_dimensioned_value = 0;
1816 for (
int i = 1; i < nargin; i += 2)
1818 if (args(i).iscell ())
1824 if (! first_dimensioned_value)
1827 first_dimensioned_value = i + 1;
1829 else if (dims != argdims)
1831 error (
"struct: dimensions of parameter %d "
1832 "do not match those of parameter %d",
1833 first_dimensioned_value, i+1);
1843 for (
int i = 0; i < nargin; i+= 2)
1847 std::string key (args(i).string_value ());
1857 if (args(i+1).iscell ())
1859 const Cell c (args(i+1).cell_value ());
1893DEFUN (isstruct, args, ,
1900 if (args.length () != 1)
1903 return ovl (args(0).isstruct ());
1906DEFUN (__fieldnames__, args, ,
1926 retval =
Cell (0, 1);
1928 retval =
Cell (keys);
1933DEFUN (isfield, args, ,
1945 if (args.length () != 2)
1950 if (args(0).isstruct ())
1956 if (args(1).is_string ())
1958 std::string key = args(1).string_value ();
1962 else if (args(1).iscell ())
1964 Cell c = args(1).cell_value ();
1970 if (c(i).is_string ())
1972 std::string key = c(i).string_value ();
1987DEFUN (numfields, args, ,
1994 if (args.length () != 1)
1997 if (! args(0).isstruct ())
1998 error (
"numfields: argument must be a struct");
2000 return ovl (
static_cast<double> (args(0).nfields ()));
2019 error (
"cell2struct: FIELDS must be a cell array of strings or a scalar string");
2027 if (arg.
rows () != 1)
2046 retval(i) = c(i).string_value ();
2055DEFUN (cell2struct, args, ,
2083 int nargin = args.length ();
2085 if (nargin < 2 || nargin > 3)
2089 = args(0).xcell_value (
"cell2struct: argument CELL must be of type cell");
2097 if (! args(2).is_real_scalar ())
2098 error (
"cell2struct: DIM must be a real scalar");
2100 dim = args(2).int_value () - 1;
2104 error (
"cell2struct: DIM must be a valid dimension");
2108 if (ext != fields.
numel ())
2109 error (
"cell2struct: number of FIELDS does not match dimension");
2115 assert (ext == rdv(dim));
2118 rdv(0) = rdv(1-dim);
2123 for (
int i = dim + 1; i < nd; i++)
2163DEFUN (rmfield, args, ,
2175 if (args.length () != 2)
2178 octave_map m = args(0).xmap_value (
"rmfield: first argument must be a struct");
2184 for (
int i = 0; i < fcell.
numel (); i++)
2186 std::string key = fcell(i).string_value ();
2189 error (
"rmfield: structure does not contain field %s", key.c_str ());
2212DEFUN (struct_levels_to_print, args, nargout,
2227 "struct_levels_to_print", -1,
2231DEFUN (print_struct_array_contents, args, nargout,
2251 "print_struct_array_contents");
void swap_bytes< 4 >(void *ptr)
charNDArray max(char d, const charNDArray &m)
octave_idx_type numel(void) const
Number of elements in the array.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
const T * data(void) const
Size of the specified dimension.
int ndims(void) const
Size of the specified dimension.
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.
OCTAVE_API 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(void) const
Number of dimensions.
OCTAVE_API dim_vector redim(int n) const
Force certain dimensionality, preserving numel ().
void * get_data(void) const
static const idx_vector colon
octave::refcount< octave_idx_type > count
void decrement_indent_level(void) const
void increment_indent_level(void) const
OCTINTERP_API void indent(std::ostream &os) const
OCTINTERP_API void newline(std::ostream &os) const
OCTINTERP_API void warn_load(const char *type) const
virtual bool is_magic_colon(void) const
friend class octave_value
OCTINTERP_API void warn_save(const char *type) const
octave_idx_type columns(void) const
void rmfield(const std::string &key)
octave_idx_type nfields(void) const
octave_scalar_map checkelem(octave_idx_type n) const
const_iterator cend(void) const
octave_fields::const_iterator const_iterator
const Cell & contents(const_iterator p) const
bool isfield(const std::string &name) const
void delete_elements(const octave::idx_vector &i)
void setfield(const std::string &key, const Cell &val)
octave_idx_type rows(void) const
octave_idx_type numel(void) 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_iterator end(void) const
const_iterator cbegin(void) const
string_vector fieldnames(void) const
bool fast_elem_insert(octave_idx_type n, const octave_scalar_map &rhs)
octave_idx_type index(const_iterator p) const
const octave_value & contents(const_iterator p) const
const_iterator cbegin(void) const
const_iterator cend(void) const
octave_idx_type nfields(void) const
void setfield(const std::string &key, const octave_value &val)
const_iterator seek(const std::string &k) const
const_iterator end(void) const
octave_value getfield(const std::string &key) const
string_vector fieldnames(void) 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_value to_array(void)
bool print_name_tag(std::ostream &os, const std::string &name) const
mxArray * as_mxArray(bool interleaved) const
dim_vector dims(void) const
octave_scalar_map scalar_map_value(void) const
static octave_value numeric_conv(const octave_value &val, const std::string &type)
string_vector map_keys(void) const
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_map map_value(void) const
octave_value dotref(const octave_value_list &idx, bool auto_add=false)
std::size_t byte_size(void) const
octave_idx_type nfields(void) 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
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
octave_idx_type numel(void) 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)
std::string edit_display(const float_display_format &fmt, octave_idx_type i, octave_idx_type j) const
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
octave_base_value * try_narrowing_conversion(void)
bool fast_elem_insert(octave_idx_type n, const octave_value &x)
static octave_value numeric_conv(const octave_value &val, const std::string &type)
bool load_ascii(std::istream &is)
octave_map map_value(void) const
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)
dim_vector dims(void) const
std::string type_name(void) const
mxArray * as_mxArray(bool interleaved) const
bool save_ascii(std::ostream &os)
std::size_t byte_size(void) 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)
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_idx_type nfields(void) const
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)
string_vector map_keys(void) const
octave_idx_type numel(void) const
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
Cell cell_value(void) const
octave_idx_type length(void) const
bool all_scalars(void) const
OCTINTERP_API octave_value_list list_value(void) const
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
void print_with_name(std::ostream &os, const std::string &name) const
octave_idx_type rows(void) const
OCTINTERP_API octave_map xmap_value(const char *fmt,...) const
static OCTINTERP_API octave_value empty_conv(const std::string &type, const octave_value &rhs=octave_value())
OCTINTERP_API Cell xcell_value(const char *fmt,...) const
bool is_cs_list(void) const
bool is_string(void) const
OCTINTERP_API octave_value storable_value(void) const
Cell cell_value(void) const
void break_closure_cycles(const std::shared_ptr< octave::stack_frame > &)
bool isstruct(void) const
bool is_zero_by_zero(void) const
std::size_t byte_size(void) const
std::string string_value(bool force=false) const
OCTINTERP_API octave_value next_subsref(const std::string &type, const std::list< octave_value_list > &idx, std::size_t skip=1)
OCTINTERP_API octave_map map_value(void) const
bool isobject(void) const
bool is_undefined(void) const
OCTINTERP_API octave_idx_type length(void) const
OCTINTERP_API octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
OCTINTERP_API octave_value undef_subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
OCTINTERP_API void maybe_mutate(void)
std::string type_name(void) const
dim_vector dims(void) const
octave_idx_type numel(void) const
const octave_hdf5_id octave_H5P_DEFAULT
OCTINTERP_API void print_usage(void)
#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()
void warn_empty_index(const std::string &type_name)
void err_nonbraced_cs_list_assignment(void)
void err_indexed_cs_list(void)
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)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
OCTAVE_EXPORT octave_value_list Fcellstr(const octave_value_list &args, int)
static bool scalar(const dim_vector &dims)
static Array< std::string > get_cell2struct_fields(const octave_value &arg)
static void maybe_warn_invalid_field_name(const std::string &key, const char *who)
static int Vstruct_levels_to_print
static OCTAVE_NORETURN void invalid_cell2struct_fields_error(void)
static void err_invalid_index_for_assignment(void)
static bool Vprint_struct_array_contents
static void err_invalid_index_type(const std::string &nm, char t)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
OCTAVE_NAMESPACE_BEGIN bool valid_identifier(const char *s)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)