26 #if defined (HAVE_CONFIG_H)
34 #include "builtin-defun-decls.h"
82 assert (idx.
length () == 1);
84 std::string nm = idx(0).string_value ();
94 "structure has no member '%s'", nm.c_str ());
102 error (
"invalid index for structure array assignment");
108 error (
"%s cannot be indexed with %c", nm.c_str (), t);
118 "%s: invalid structure field name '%s'",
122 "invalid structure field name '%s'",
129 const std::list<octave_value_list>& idx,
140 if (
type.length () > 1 &&
type[1] ==
'.')
142 auto p = idx.begin ();
195 const std::list<octave_value_list>& idx,
206 if (
type.length () > 1 &&
type[1] ==
'.')
208 auto p = idx.begin ();
213 const Cell t = tmp.
index (idx.front (), auto_add);
234 const Cell t =
dotref (idx.front (), auto_add);
273 const std::string&
type)
287 const std::list<octave_value_list>& idx,
292 int n =
type.length ();
296 if (idx.front ().empty ())
297 error (
"missing index in indexed assignment");
299 if (
n > 1 && ! (
type.length () == 2 &&
type[0] ==
'(' &&
type[1] ==
'.'))
305 if (
type.length () > 1 &&
type[1] ==
'.')
307 auto p = idx.begin ();
312 assert (key_idx.
length () == 1);
314 std::string key = key_idx(0).string_value ();
318 std::list<octave_value_list> next_idx (idx);
323 next_idx.erase (next_idx.begin ());
324 next_idx.erase (next_idx.begin ());
326 std::string next_type =
type.substr (2);
338 if (tmpc.
numel () != 1)
355 t_rhs =(orig_undefined
357 : tmp.
subsasgn (next_type, next_idx, rhs));
368 assert (key_idx.
length () == 1);
370 std::string key = key_idx(0).string_value ();
374 std::list<octave_value_list> next_idx (idx);
376 next_idx.erase (next_idx.begin ());
378 std::string next_type =
type.substr (1);
390 if (tmpc.
numel () == 1)
406 t_rhs = (orig_undefined
408 : tmp.
subsasgn (next_type, next_idx, rhs));
428 if (
n > 1 &&
type[1] ==
'.')
430 auto p = idx.begin ();
434 assert (key_idx.
length () == 1);
436 std::string key = key_idx(0).string_value ();
449 didx(k) = idxf(k).numel ();
452 tmp_cell = tmp_cell.
reshape (didx);
490 error (
"invalid structure assignment");
505 assert (key_idx.
length () == 1);
507 std::string key = key_idx(0).string_value ();
570 std::string key =
map.
key (p);
597 bool print_fieldnames_only
604 os << dv.
str () <<
" struct array containing the fields:";
613 std::string key = key_list[i];
620 if (print_fieldnames_only)
632 if (print_fieldnames_only)
671 return dims.
ndims () == 2 && dims(0) == 1 && dims(1) == 1;
697 std::string dimstr = dv.
str ();
698 return "[" + dimstr +
" " + tname +
"]";
711 os <<
"# ndims: " << dv.
ndims () <<
"\n";
713 for (
int i = 0; i < dv.
ndims (); i++)
717 os <<
"# length: " << nf <<
"\n";
725 std::string key = keys(i);
750 keywords[0] =
"ndims";
751 keywords[1] =
"length";
757 if (kw == keywords[0])
761 for (
int i = 0; i < mdims; i++)
770 if (! success ||
len < 0)
771 error (
"load: failed to extract number of elements in structure");
790 m.setfield (nm, tcell);
794 error (
"load: failed to load structure");
818 int32_t di = - dv.
ndims ();
819 os.write (
reinterpret_cast<char *
> (&di), 4);
820 for (
int i = 0; i < dv.
ndims (); i++)
823 os.write (
reinterpret_cast<char *
> (&di), 4);
827 os.write (
reinterpret_cast<char *
> (&
len), 4);
835 std::string key = keys(i);
854 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
869 for (
int i = 0; i < mdims; i++)
871 if (! is.read (
reinterpret_cast<char *
> (&di), 4))
878 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
903 m.setfield (nm, tcell);
907 error (
"load: failed to load structure");
923 #if defined (HAVE_HDF5)
927 #if defined (HAVE_HDF5_18)
931 data_hid = H5Gcreate (loc_id,
name, 0);
933 if (data_hid < 0)
return false;
946 std::string key = keys(i);
962 octave_unused_parameter (loc_id);
963 octave_unused_parameter (
name);
964 octave_unused_parameter (save_as_floats);
977 #if defined (HAVE_HDF5)
983 int current_item = 0;
985 #if defined (HAVE_HDF5_18)
988 hid_t group_id = H5Gopen (loc_id,
name);
990 H5Gget_num_objs (group_id, &num_obj);
996 while (current_item <
static_cast<int> (num_obj)
1004 m.setfield (dsub.
name, tcell);
1015 octave_unused_parameter (loc_id);
1016 octave_unused_parameter (
name);
1032 for (
int i = 0; i < nf; i++)
1033 f[i] = kv[i].c_str ();
1043 for (
int i = 0; i < nf; i++)
1050 for (
mwIndex j = i; j < ntot; j += nf)
1051 elts[j] =
new mxArray (p[k++]);
1077 void *here =
reinterpret_cast<void *
>(&sm_ptr);
1078 return (
x.get_rep ().fast_elem_insert_self (here,
btyp_struct)
1093 assert (idx.
length () == 1);
1095 std::string nm = idx(0).string_value ();
1101 if (! auto_add &&
retval.is_undefined ())
1103 "structure has no member '%s'", nm.c_str ());
1110 const std::list<octave_value_list>& idx)
1120 if (idx.size () > 1)
1131 const std::list<octave_value_list>& idx,
1142 if (idx.size () > 1)
1153 const std::list<octave_value_list>& idx,
1164 if (idx.size () > 1)
1184 const std::string&
type)
1198 const std::list<octave_value_list>& idx,
1203 if (idx.front ().empty ())
1204 error (
"missing index in indexed assignment");
1208 int n =
type.length ();
1214 assert (key_idx.
length () == 1);
1216 std::string key = key_idx(0).string_value ();
1222 std::list<octave_value_list> next_idx (idx);
1224 next_idx.erase (next_idx.begin ());
1226 std::string next_type =
type.substr (1);
1247 t_rhs = (orig_undefined
1249 : tmp.
subsasgn (next_type, next_idx, rhs));
1283 std::string key =
map.
key (p);
1310 bool print_fieldnames_only = max_depth_reached;
1315 os <<
"scalar structure containing the fields:";
1326 std::string key = key_list[i];
1330 if (print_fieldnames_only)
1348 os <<
"<structure>";
1355 const std::string&
name)
const
1362 os <<
name <<
" = ";
1386 std::string dimstr = dv.
str ();
1387 return "[" + dimstr +
" " + tname +
"]";
1399 os <<
"# ndims: " << dv.
ndims () <<
"\n";
1401 for (
int i = 0; i < dv.
ndims (); i++)
1405 os <<
"# length: " << nf <<
"\n";
1413 std::string key = keys(i);
1420 return ! os.fail ();
1432 error (
"load: failed to extract number of elements in structure");
1450 m.setfield (nm, t2);
1454 error (
"load: failed to load structure");
1474 os.write (
reinterpret_cast<char *
> (&
len), 4);
1482 std::string key = keys(i);
1489 return ! os.fail ();
1499 bool success =
true;
1501 if (! is.read (
reinterpret_cast<char *
> (&
len), 4))
1523 m.setfield (nm, t2);
1527 error (
"load: failed to load structure");
1541 bool save_as_floats)
1543 #if defined (HAVE_HDF5)
1545 hid_t data_hid = -1;
1547 #if defined (HAVE_HDF5_18)
1551 data_hid = H5Gcreate (loc_id,
name, 0);
1553 if (data_hid < 0)
return false;
1566 std::string key = keys(i);
1570 bool retval2 =
add_hdf5_data (data_hid, val, key,
"",
false,
1577 H5Gclose (data_hid);
1582 octave_unused_parameter (loc_id);
1583 octave_unused_parameter (
name);
1584 octave_unused_parameter (save_as_floats);
1597 #if defined (HAVE_HDF5)
1603 int current_item = 0;
1604 hsize_t num_obj = 0;
1605 #if defined (HAVE_HDF5_18)
1608 hid_t group_id = H5Gopen (loc_id,
name);
1610 H5Gget_num_objs (group_id, &num_obj);
1611 H5Gclose (group_id);
1616 while (current_item <
static_cast<int> (num_obj)
1622 m.setfield (dsub.
name, t2);
1633 octave_unused_parameter (loc_id);
1634 octave_unused_parameter (
name);
1650 for (
int i = 0; i < nf; i++)
1651 f[i] = kv[i].c_str ();
1661 for (
int i = 0; i < nf; i++)
1668 for (
mwIndex j = i; j < ntot; j += nf)
1669 elts[j] =
new mxArray (p[k++]);
1695 DEFUN (
struct, args, ,
1752 int nargin = args.length ();
1762 if (nargin == 1 && args(0).isstruct ())
1763 return ovl (args(0));
1765 if (nargin == 1 && args(0).isobject ())
1766 return ovl (args(0).map_value ());
1768 if ((nargin == 1 || nargin == 2)
1769 && args(0).isempty () && args(0).is_real_matrix ())
1773 Array<std::string> cstr = args(1).xcellstr_value (
"struct: second argument should be a cell array of field names");
1783 for (
int i = 0; i < nargin; i += 2)
1785 if (! args(i).is_string () || i + 1 >= nargin)
1786 error (R
"(struct: additional arguments must occur as "field", VALUE pairs)");
1793 int first_dimensioned_value = 0;
1795 for (
int i = 1; i < nargin; i += 2)
1797 if (args(i).iscell ())
1803 if (! first_dimensioned_value)
1806 first_dimensioned_value = i + 1;
1808 else if (dims != argdims)
1810 error (
"struct: dimensions of parameter %d "
1811 "do not match those of parameter %d",
1812 first_dimensioned_value, i+1);
1822 for (
int i = 0; i < nargin; i+= 2)
1826 std::string key (args(i).string_value ());
1836 if (args(i+1).iscell ())
1838 const Cell c (args(i+1).cell_value ());
1872 DEFUN (isstruct, args, ,
1879 if (args.length () != 1)
1882 return ovl (args(0).isstruct ());
1885 DEFUN (__fieldnames__, args, ,
1912 DEFUN (isfield, args, ,
1924 if (args.length () != 2)
1929 if (args(0).isstruct ())
1935 if (args(1).is_string ())
1937 std::string key = args(1).string_value ();
1941 else if (args(1).iscell ())
1943 Cell c = args(1).cell_value ();
1949 if (c(i).is_string ())
1951 std::string key = c(i).string_value ();
1953 bm(i) =
m.isfield (key);
1966 DEFUN (numfields, args, ,
1973 if (args.length () != 1)
1976 if (! args(0).isstruct ())
1977 error (
"numfields: argument must be a struct");
1979 return ovl (
static_cast<double> (args(0).nfields ()));
1994 DEFUN (cell2struct, args, ,
2022 int nargin = args.length ();
2024 if (nargin < 2 || nargin > 3)
2027 if (! args(0).iscell ())
2028 error (
"cell2struct: argument CELL must be of type cell");
2030 if (! (args(1).iscellstr () || args(1).is_char_matrix ()))
2031 error (
"cell2struct: FIELDS must be a cell array of strings or a character matrix");
2037 if (! args(2).is_real_scalar ())
2038 error (
"cell2struct: DIM must be a real scalar");
2040 dim = args(2).int_value () - 1;
2044 error (
"cell2struct: DIM must be a valid dimension");
2046 const Cell vals = args(0).cell_value ();
2051 if (ext != fields.
numel ())
2052 error (
"cell2struct: number of FIELDS does not match dimension");
2058 assert (ext == rdv(dim));
2061 rdv(0) = rdv(1-dim);
2066 for (
int i = dim + 1; i < nd; i++)
2100 DEFUN (rmfield, args, ,
2112 if (args.length () != 2)
2115 octave_map m = args(0).xmap_value (
"rmfield: first argument must be a struct");
2121 for (
int i = 0; i < fcell.
numel (); i++)
2123 std::string key = fcell(i).string_value ();
2125 if (!
m.isfield (key))
2126 error (
"rmfield: structure does not contain field %s", key.c_str ());
2149 DEFUN (struct_levels_to_print, args, nargout,
2167 DEFUN (print_struct_array_contents, args, nargout,
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 T * data(void) const
Size of the specified dimension.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
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.
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.
dim_vector redim(int n) const
Force certain dimensionality, preserving numel ().
static const idx_vector colon
octave::refcount< octave_idx_type > count
void decrement_indent_level(void) const
void increment_indent_level(void) const
void indent(std::ostream &os) const
void newline(std::ostream &os) const
void warn_load(const char *type) const
virtual bool is_magic_colon(void) const
friend class octave_value
void warn_save(const char *type) const
octave_idx_type columns(void) const
void delete_elements(const idx_vector &i)
octave_scalar_map checkelem(octave_idx_type n) const
const_iterator cend(void) const
octave_fields::const_iterator const_iterator
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
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
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)
octave_idx_type nfields(void) const
bool save_binary(std::ostream &os, bool save_as_floats)
bool save_ascii(std::ostream &os)
size_t byte_size(void) const
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
mxArray * as_mxArray(void) const
octave_idx_type numel(void) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
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)
size_t byte_size(void) const
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
bool save_ascii(std::ostream &os)
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)
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)
mxArray * as_mxArray(void) const
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
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
static octave_value empty_conv(const std::string &type, const octave_value &rhs=octave_value())
octave_value undef_subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
bool is_cs_list(void) const
bool isstruct(void) const
bool is_zero_by_zero(void) const
octave_value storable_value(void) const
size_t byte_size(void) const
octave_value_list list_value(void) const
bool isobject(void) const
octave_map map_value(void) const
bool is_undefined(void) const
std::string type_name(void) const
dim_vector dims(void) const
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
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)
bool valid_identifier(const char *s)
#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 void maybe_warn_invalid_field_name(const std::string &key, const char *who)
static int Vstruct_levels_to_print
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::octave_value(const Array< char > &chm, char type) return retval
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
#define SET_INTERNAL_VARIABLE_WITH_LIMITS(NM, MINVAL, MAXVAL)
#define SET_INTERNAL_VARIABLE(NM)