GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-struct.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_ov_struct_h)
27 #define octave_ov_struct_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdlib>
32 
33 #include <iosfwd>
34 #include <string>
35 
36 #include "mx-base.h"
37 #include "str-vec.h"
38 
39 #include "error.h"
40 #include "oct-map.h"
41 #include "ov-base.h"
42 #include "ov-typeinfo.h"
43 
44 class octave_value_list;
45 
46 // Data structures.
47 
48 class
50 {
51 public:
52 
54  : octave_base_value (), m_map () { }
55 
57  : octave_base_value (), m_map (m) { }
58 
60  : octave_base_value (), m_map (s.m_map) { }
61 
62  ~octave_struct () = default;
63 
64  octave_base_value * clone () const { return new octave_struct (*this); }
65  octave_base_value * empty_clone () const { return new octave_struct (); }
66 
67  void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame);
68 
69  octave_base_value * try_narrowing_conversion ();
70 
71  Cell dotref (const octave_value_list& idx, bool auto_add = false);
72 
73  octave_value subsref (const std::string& type,
74  const std::list<octave_value_list>& idx)
75  {
76  octave_value_list tmp = subsref (type, idx, 1);
77  return tmp.length () > 0 ? tmp(0) : octave_value ();
78  }
79 
80  octave_value_list subsref (const std::string&,
81  const std::list<octave_value_list>&, int);
82 
83  octave_value subsref (const std::string& type,
84  const std::list<octave_value_list>& idx,
85  bool auto_add);
86 
87  static octave_value numeric_conv (const octave_value& val,
88  const std::string& type);
89 
90  octave_value subsasgn (const std::string& type,
91  const std::list<octave_value_list>& idx,
92  const octave_value& rhs);
93 
94  octave_value squeeze () const { return m_map.squeeze (); }
95 
96  octave_value permute (const Array<int>& vec, bool inv = false) const
97  { return m_map.permute (vec, inv); }
98 
99  octave_value do_index_op (const octave_value_list& idx,
100  bool resize_ok = false);
101 
102  dim_vector dims () const { return m_map.dims (); }
103 
104  std::size_t byte_size () const;
105 
106  // This is the number of elements in each field. The total number
107  // of elements is numel () * nfields ().
109  {
110  return m_map.numel ();
111  }
112 
113  octave_idx_type nfields () const { return m_map.nfields (); }
114 
115  octave_value reshape (const dim_vector& new_dims) const
116  { return m_map.reshape (new_dims); }
117 
118  octave_value resize (const dim_vector& dv, bool fill = false) const
119  { octave_map tmap = m_map; tmap.resize (dv, fill); return tmap; }
120 
121  bool is_defined () const { return true; }
122 
123  bool is_constant () const { return true; }
124 
125  bool isstruct () const { return true; }
126 
128 
129  octave_map map_value () const { return m_map; }
130 
131  string_vector map_keys () const { return m_map.fieldnames (); }
132 
133  bool isfield (const std::string& field_name) const
134  { return m_map.isfield (field_name); }
135 
136  void print (std::ostream& os, bool pr_as_read_syntax = false);
137 
138  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
139 
140  bool print_name_tag (std::ostream& os, const std::string& name) const;
141 
142  std::string edit_display (const float_display_format& fmt,
143  octave_idx_type i, octave_idx_type j) const;
144 
145  bool save_ascii (std::ostream& os);
146 
147  bool load_ascii (std::istream& is);
148 
149  bool save_binary (std::ostream& os, bool save_as_floats);
150 
151  bool load_binary (std::istream& is, bool swap,
153 
154  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
155 
156  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
157 
158  mxArray * as_mxArray (bool interleaved) const;
159 
161  fast_elem_extract (octave_idx_type n) const;
162 
163  bool
164  fast_elem_insert (octave_idx_type n, const octave_value& x);
165 
166 protected:
167 
168  // The associative array used to manage the structure data.
170 
171 private:
172 
174 };
175 
176 class
178 {
179 public:
180 
182  : octave_base_value (), m_map () { }
183 
185  : octave_base_value (), m_map (m) { }
186 
187  octave_scalar_struct (const std::map<std::string, octave_value>& m)
188  : octave_base_value (), m_map (m) { }
189 
191  : octave_base_value (), m_map (s.m_map) { }
192 
193  ~octave_scalar_struct () = default;
194 
196  { return new octave_scalar_struct (*this); }
198  { return new octave_scalar_struct (); }
199 
200  void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame);
201 
202  octave_value dotref (const octave_value_list& idx, bool auto_add = false);
203 
204  octave_value subsref (const std::string& type,
205  const std::list<octave_value_list>& idx);
206 
207  octave_value_list subsref (const std::string& type,
208  const std::list<octave_value_list>& idx, int);
209 
210  octave_value subsref (const std::string& type,
211  const std::list<octave_value_list>& idx,
212  bool auto_add);
213 
214  static octave_value numeric_conv (const octave_value& val,
215  const std::string& type);
216 
217  octave_value subsasgn (const std::string& type,
218  const std::list<octave_value_list>& idx,
219  const octave_value& rhs);
220 
221  octave_value squeeze () const { return m_map; }
222 
223  octave_value permute (const Array<int>& vec, bool inv = false) const
224  { return octave_map (m_map).permute (vec, inv); }
225 
226  octave_value do_index_op (const octave_value_list& idx,
227  bool resize_ok = false);
228 
229  dim_vector dims () const { static dim_vector dv (1, 1); return dv; }
230 
231  std::size_t byte_size () const;
232 
233  // This is the number of elements in each field. The total number
234  // of elements is numel () * nfields ().
236  {
237  return 1;
238  }
239 
240  octave_idx_type nfields () const { return m_map.nfields (); }
241 
242  octave_value reshape (const dim_vector& new_dims) const
243  { return octave_map (m_map).reshape (new_dims); }
244 
245  octave_value resize (const dim_vector& dv, bool fill = false) const
246  { octave_map tmap = m_map; tmap.resize (dv, fill); return tmap; }
247 
248  bool is_defined () const { return true; }
249 
250  bool is_constant () const { return true; }
251 
252  bool isstruct () const { return true; }
253 
255 
256  octave_map map_value () const { return m_map; }
257 
258  octave_scalar_map scalar_map_value () const { return m_map; }
259 
260  string_vector map_keys () const { return m_map.fieldnames (); }
261 
262  bool isfield (const std::string& field_name) const
263  { return m_map.isfield (field_name); }
264 
265  void print (std::ostream& os, bool pr_as_read_syntax = false);
266 
267  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
268 
269  bool print_name_tag (std::ostream& os, const std::string& name) const;
270 
271  std::string edit_display (const float_display_format& fmt,
272  octave_idx_type i, octave_idx_type j) const;
273 
274  bool save_ascii (std::ostream& os);
275 
276  bool load_ascii (std::istream& is);
277 
278  bool save_binary (std::ostream& os, bool save_as_floats);
279 
280  bool load_binary (std::istream& is, bool swap,
282 
283  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
284 
285  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
286 
287  mxArray * as_mxArray (bool interleaved) const;
288 
289  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
290 
291 protected:
292 
293  // The associative array used to manage the structure data.
295 
296 private:
297 
298  octave_value to_array ();
299 
301 };
302 
303 #endif
Definition: Cell.h:43
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
void resize(const dim_vector &dv, bool fill=false)
Definition: oct-map.cc:574
octave_map reshape(const dim_vector &dv) const
Definition: oct-map.cc:524
octave_map permute(const Array< int > &vec, bool inv=false) const
Definition: oct-map.cc:465
octave_scalar_struct(const octave_scalar_map &m)
Definition: ov-struct.h:184
octave_idx_type nfields() const
Definition: ov-struct.h:240
octave_value squeeze() const
Definition: ov-struct.h:221
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-struct.h:242
octave_scalar_struct(const std::map< std::string, octave_value > &m)
Definition: ov-struct.h:187
builtin_type_t builtin_type() const
Definition: ov-struct.h:254
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-struct.h:223
octave_base_value * empty_clone() const
Definition: ov-struct.h:197
bool is_constant() const
Definition: ov-struct.h:250
octave_map map_value() const
Definition: ov-struct.h:256
octave_base_value * clone() const
Definition: ov-struct.h:195
~octave_scalar_struct()=default
octave_scalar_struct(const octave_scalar_struct &s)
Definition: ov-struct.h:190
dim_vector dims() const
Definition: ov-struct.h:229
bool is_defined() const
Definition: ov-struct.h:248
octave_scalar_map m_map
Definition: ov-struct.h:294
octave_scalar_map scalar_map_value() const
Definition: ov-struct.h:258
bool isfield(const std::string &field_name) const
Definition: ov-struct.h:262
string_vector map_keys() const
Definition: ov-struct.h:260
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-struct.h:245
octave_idx_type numel() const
Definition: ov-struct.h:235
bool isstruct() const
Definition: ov-struct.h:252
octave_map m_map
Definition: ov-struct.h:169
string_vector map_keys() const
Definition: ov-struct.h:131
bool is_constant() const
Definition: ov-struct.h:123
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-struct.h:118
octave_idx_type numel() const
Definition: ov-struct.h:108
builtin_type_t builtin_type() const
Definition: ov-struct.h:127
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-struct.h:73
bool is_defined() const
Definition: ov-struct.h:121
dim_vector dims() const
Definition: ov-struct.h:102
octave_map map_value() const
Definition: ov-struct.h:129
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-struct.h:115
octave_idx_type nfields() const
Definition: ov-struct.h:113
octave_struct(const octave_struct &s)
Definition: ov-struct.h:59
bool isstruct() const
Definition: ov-struct.h:125
octave_struct(const octave_map &m)
Definition: ov-struct.h:56
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-struct.h:96
~octave_struct()=default
bool isfield(const std::string &field_name) const
Definition: ov-struct.h:133
octave_base_value * empty_clone() const
Definition: ov-struct.h:65
octave_base_value * clone() const
Definition: ov-struct.h:64
octave_value squeeze() const
Definition: ov-struct.h:94
octave_idx_type length() const
Definition: ovl.h:113
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:574
octave_value reshape(const dim_vector &dv) const
Definition: ov.h:571
octave_value squeeze() const
Definition: ov.h:431
F77_RET_T const F77_DBLE * x
float_format
Definition: mach-info.h:38
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
int64_t octave_hdf5_id
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:181
builtin_type_t
Definition: ov-base.h:83
@ btyp_struct
Definition: ov-base.h:98