26 #if defined (HAVE_CONFIG_H)
34 #include "builtin-defun-decls.h"
74 c(i).break_closure_cycles (frame);
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,
304 int n = type.length ();
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 ();
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));
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 ();
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");
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");
801 m.setfield (nm, tcell);
805 error (
"load: failed to load structure");
829 int32_t di = - dv.
ndims ();
830 os.write (
reinterpret_cast<char *
> (&di), 4);
831 for (
int i = 0; i < dv.
ndims (); i++)
834 os.write (
reinterpret_cast<char *
> (&di), 4);
838 os.write (
reinterpret_cast<char *
> (&
len), 4);
846 std::string key = keys(i);
865 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
880 for (
int i = 0; i < mdims; i++)
882 if (! is.read (
reinterpret_cast<char *
> (&di), 4))
889 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
915 m.setfield (nm, tcell);
919 error (
"load: failed to load structure");
935 #if defined (HAVE_HDF5)
939 #if defined (HAVE_HDF5_18)
943 data_hid = H5Gcreate (loc_id, name, 0);
945 if (data_hid < 0)
return false;
958 std::string key = keys(i);
974 octave_unused_parameter (loc_id);
975 octave_unused_parameter (name);
976 octave_unused_parameter (save_as_floats);
989 #if defined (HAVE_HDF5)
995 int current_item = 0;
997 #if defined (HAVE_HDF5_18)
1000 hid_t group_id = H5Gopen (loc_id, name);
1002 H5Gget_num_objs (group_id, &num_obj);
1003 H5Gclose (group_id);
1008 while (current_item <
static_cast<int> (num_obj)
1017 m.setfield (dsub.
name, tcell);
1028 octave_unused_parameter (loc_id);
1029 octave_unused_parameter (name);
1045 for (
int i = 0; i < nf; i++)
1046 f[i] = kv[i].c_str ();
1052 mwSize nel =
numel ();
1054 mwSize ntot = nf * nel;
1056 for (
int i = 0; i < nf; i++)
1063 for (mwIndex j = i; j < ntot; j += nf)
1064 elts[j] =
new mxArray (interleaved, p[k++]);
1083 bool retval =
false;
1090 void *here =
reinterpret_cast<void *
>(&sm_ptr);
1091 return (
x.get_rep ().fast_elem_insert_self (here,
btyp_struct)
1115 std::string nm = idx(0).string_value ();
1123 "structure has no member '%s'", nm.c_str ());
1130 const std::list<octave_value_list>& idx)
1138 retval =
dotref (idx.front ());
1140 if (idx.size () > 1)
1151 const std::list<octave_value_list>& idx,
1160 retval(0) =
dotref (idx.front ());
1162 if (idx.size () > 1)
1163 retval = retval(0).next_subsref (nargout, type, idx, skip);
1173 const std::list<octave_value_list>& idx,
1182 retval =
dotref (idx.front (), auto_add);
1184 if (idx.size () > 1)
1185 retval = retval.
next_subsref (auto_add, type, idx, skip);
1204 const std::string& type)
1208 if (type.length () > 0 && type[0] ==
'.' && ! val.
isstruct ())
1218 const std::list<octave_value_list>& idx,
1223 if (idx.front ().empty ())
1224 error (
"missing index in indexed assignment");
1228 int n = type.length ();
1236 std::string key = key_idx(0).string_value ();
1242 std::list<octave_value_list> next_idx (idx);
1244 next_idx.erase (next_idx.begin ());
1246 std::string next_type = type.substr (1);
1267 t_rhs = (orig_undefined
1269 : tmp.
subsasgn (next_type, next_idx, rhs));
1281 retval = tmp.
subsasgn (type, idx, rhs);
1299 std::size_t retval = 0;
1328 bool print_fieldnames_only = max_depth_reached;
1337 std::string key = key_list[i];
1341 if (print_fieldnames_only)
1359 os <<
"<structure>";
1366 const std::string& name)
const
1368 bool retval =
false;
1373 os << name <<
" = ";
1384 os <<
"scalar structure containing the fields:";
1407 std::string dimstr = dv.
str ();
1408 return "[" + dimstr +
" " + tname +
"]";
1420 os <<
"# ndims: " << dv.
ndims () <<
"\n";
1422 for (
int i = 0; i < dv.
ndims (); i++)
1426 os <<
"# length: " << nf <<
"\n";
1434 std::string key = keys(i);
1441 return ! os.fail ();
1453 error (
"load: failed to extract number of elements in structure");
1471 m.setfield (nm, t2);
1475 error (
"load: failed to load structure");
1495 os.write (
reinterpret_cast<char *
> (&
len), 4);
1503 std::string key = keys(i);
1510 return ! os.fail ();
1520 bool success =
true;
1522 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
1544 m.setfield (nm, t2);
1548 error (
"load: failed to load structure");
1562 bool save_as_floats)
1564 #if defined (HAVE_HDF5)
1566 hid_t data_hid = -1;
1568 #if defined (HAVE_HDF5_18)
1572 data_hid = H5Gcreate (loc_id, name, 0);
1574 if (data_hid < 0)
return false;
1587 std::string key = keys(i);
1591 bool retval2 =
add_hdf5_data (data_hid, val, key,
"",
false,
1598 H5Gclose (data_hid);
1603 octave_unused_parameter (loc_id);
1604 octave_unused_parameter (name);
1605 octave_unused_parameter (save_as_floats);
1616 bool retval =
false;
1618 #if defined (HAVE_HDF5)
1624 int current_item = 0;
1625 hsize_t num_obj = 0;
1626 #if defined (HAVE_HDF5_18)
1629 hid_t group_id = H5Gopen (loc_id, name);
1631 H5Gget_num_objs (group_id, &num_obj);
1632 H5Gclose (group_id);
1637 while (current_item <
static_cast<int> (num_obj)
1643 m.setfield (dsub.
name, t2);
1654 octave_unused_parameter (loc_id);
1655 octave_unused_parameter (name);
1671 for (
int i = 0; i < nf; i++)
1672 f[i] = kv[i].c_str ();
1678 mwSize nel =
numel ();
1680 mwSize ntot = nf * nel;
1682 for (
int i = 0; i < nf; i++)
1689 for (mwIndex j = i; j < ntot; j += nf)
1690 elts[j] =
new mxArray (interleaved, p[k++]);
1718 DEFUN (
struct, args, ,
1776 int nargin = args.length ();
1786 if (nargin == 1 && args(0).isstruct ())
1787 return ovl (args(0));
1789 if (nargin == 1 && args(0).isobject ())
1790 return ovl (args(0).map_value ());
1792 if ((nargin == 1 || nargin == 2)
1793 && args(0).isempty () && args(0).is_real_matrix ())
1798 1).xcellstr_value (
"struct: second argument should be a cell array of field names");
1808 for (
int i = 0; i < nargin; i += 2)
1810 if (! args(i).is_string () || i + 1 >= nargin)
1811 error (R
"(struct: additional arguments must occur as "field", VALUE pairs)");
1818 int first_dimensioned_value = 0;
1820 for (
int i = 1; i < nargin; i += 2)
1822 if (args(i).iscell ())
1828 if (! first_dimensioned_value)
1831 first_dimensioned_value = i + 1;
1833 else if (dims != argdims)
1835 error (
"struct: dimensions of parameter %d "
1836 "do not match those of parameter %d",
1837 first_dimensioned_value, i+1);
1847 for (
int i = 0; i < nargin; i+= 2)
1851 std::string key (args(i).string_value ());
1861 if (args(i+1).iscell ())
1863 const Cell c (args(i+1).cell_value ());
1899 DEFUN (isstruct, args, ,
1906 if (args.length () != 1)
1909 return ovl (args(0).isstruct ());
1912 DEFUN (__fieldnames__, args, ,
1932 retval =
Cell (0, 1);
1934 retval =
Cell (keys);
1939 DEFUN (isfield, args, ,
1951 if (args.length () != 2)
1956 if (args(0).isstruct ())
1962 if (args(1).is_string ())
1964 std::string key = args(1).string_value ();
1966 retval =
m.isfield (key);
1968 else if (args(1).iscell ())
1970 Cell c = args(1).cell_value ();
1976 if (c(i).is_string ())
1978 std::string key = c(i).string_value ();
1980 bm(i) =
m.isfield (key);
1993 DEFUN (numfields, args, ,
2000 if (args.length () != 1)
2003 if (! args(0).isstruct ())
2004 error (
"numfields: argument must be a struct");
2006 return ovl (
static_cast<double> (args(0).nfields ()));
2025 error (
"cell2struct: FIELDS must be a cell array of strings or a scalar string");
2033 if (arg.
rows () != 1)
2052 retval(i) = c(i).string_value ();
2061 DEFUN (cell2struct, args, ,
2089 int nargin = args.length ();
2091 if (nargin < 2 || nargin > 3)
2095 = args(0).xcell_value (
"cell2struct: argument CELL must be of type cell");
2103 if (! args(2).is_real_scalar ())
2104 error (
"cell2struct: DIM must be a real scalar");
2106 dim = args(2).int_value () - 1;
2110 error (
"cell2struct: DIM must be a valid dimension");
2114 if (ext != fields.
numel ())
2115 error (
"cell2struct: number of FIELDS does not match dimension");
2124 rdv(0) = rdv(1-dim);
2129 for (
int i = dim + 1; i < nd; i++)
2169 DEFUN (rmfield, args, ,
2181 if (args.length () != 2)
2184 octave_map m = args(0).xmap_value (
"rmfield: first argument must be a struct");
2190 for (
int i = 0; i < fcell.
numel (); i++)
2192 std::string key = fcell(i).string_value ();
2194 if (!
m.isfield (key))
2195 error (
"rmfield: structure does not contain field %s", key.c_str ());
2218 DEFUN (struct_levels_to_print, args, nargout,
2233 "struct_levels_to_print", -1,
2237 DEFUN (print_struct_array_contents, args, nargout,
2257 "print_struct_array_contents");
void swap_bytes< 4 >(void *ptr)
charNDArray max(char d, const charNDArray &m)
OCTARRAY_OVERRIDABLE_FUNC_API const T * data(void) const
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API void make_unique(void)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_OVERRIDABLE_FUNC_API const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
OCTARRAY_OVERRIDABLE_FUNC_API 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 ().
static const idx_vector colon
void * get_data(void) const
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
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
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
const Cell & contents(const_iterator p) const
bool fast_elem_insert(octave_idx_type n, const octave_scalar_map &rhs)
octave_idx_type index(const_iterator p) const
const_iterator cbegin(void) const
const octave_value & contents(const_iterator p) 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_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
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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,...)
void panic_unless(bool cond)
#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 Array< std::string > get_cell2struct_fields(const octave_value &arg)
static bool scalar(const dim_vector &dims)
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.
bool valid_identifier(const char *s)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)