GNU Octave 7.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-2022 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
45
46// Data structures.
47
48class
50{
51public:
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 (void) = default;
63
64 octave_base_value * clone (void) const { return new octave_struct (*this); }
65 octave_base_value * empty_clone (void) 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 (void);
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 (void) 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 (void) const { return m_map.dims (); }
103
104 std::size_t byte_size (void) 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 (void) 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 (void) const { return true; }
122
123 bool is_constant (void) const { return true; }
124
125 bool isstruct (void) const { return true; }
126
127 builtin_type_t builtin_type (void) const { return btyp_struct; }
128
129 octave_map map_value (void) const { return m_map; }
130
131 string_vector map_keys (void) const { return m_map.fieldnames (); }
132
133 void print (std::ostream& os, bool pr_as_read_syntax = false);
134
135 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
136
137 bool print_name_tag (std::ostream& os, const std::string& name) const;
138
139 std::string edit_display (const float_display_format& fmt,
141
142 bool save_ascii (std::ostream& os);
143
144 bool load_ascii (std::istream& is);
145
146 bool save_binary (std::ostream& os, bool save_as_floats);
147
148 bool load_binary (std::istream& is, bool swap,
150
151 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
152
153 bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
154
155 mxArray * as_mxArray (bool interleaved) const;
156
158 fast_elem_extract (octave_idx_type n) const;
159
160 bool
161 fast_elem_insert (octave_idx_type n, const octave_value& x);
162
163protected:
164
165 // The associative array used to manage the structure data.
167
168private:
169
171};
172
173class
175{
176public:
177
179 : octave_base_value (), m_map () { }
180
182 : octave_base_value (), m_map (m) { }
183
184 octave_scalar_struct (const std::map<std::string, octave_value>& m)
185 : octave_base_value (), m_map (m) { }
186
188 : octave_base_value (), m_map (s.m_map) { }
189
190 ~octave_scalar_struct (void) = default;
191
193 { return new octave_scalar_struct (*this); }
195 { return new octave_scalar_struct (); }
196
197 void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame);
198
199 octave_value dotref (const octave_value_list& idx, bool auto_add = false);
200
201 octave_value subsref (const std::string& type,
202 const std::list<octave_value_list>& idx);
203
204 octave_value_list subsref (const std::string& type,
205 const std::list<octave_value_list>& idx, int);
206
207 octave_value subsref (const std::string& type,
208 const std::list<octave_value_list>& idx,
209 bool auto_add);
210
211 static octave_value numeric_conv (const octave_value& val,
212 const std::string& type);
213
214 octave_value subsasgn (const std::string& type,
215 const std::list<octave_value_list>& idx,
216 const octave_value& rhs);
217
218 octave_value squeeze (void) const { return m_map; }
219
220 octave_value permute (const Array<int>& vec, bool inv = false) const
221 { return octave_map (m_map).permute (vec, inv); }
222
223 octave_value do_index_op (const octave_value_list& idx,
224 bool resize_ok = false);
225
226 dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; }
227
228 std::size_t byte_size (void) const;
229
230 // This is the number of elements in each field. The total number
231 // of elements is numel () * nfields ().
233 {
234 return 1;
235 }
236
237 octave_idx_type nfields (void) const { return m_map.nfields (); }
238
239 octave_value reshape (const dim_vector& new_dims) const
240 { return octave_map (m_map).reshape (new_dims); }
241
242 octave_value resize (const dim_vector& dv, bool fill = false) const
243 { octave_map tmap = m_map; tmap.resize (dv, fill); return tmap; }
244
245 bool is_defined (void) const { return true; }
246
247 bool is_constant (void) const { return true; }
248
249 bool isstruct (void) const { return true; }
250
251 builtin_type_t builtin_type (void) const { return btyp_struct; }
252
253 octave_map map_value (void) const { return m_map; }
254
255 octave_scalar_map scalar_map_value (void) const { return m_map; }
256
257 string_vector map_keys (void) const { return m_map.fieldnames (); }
258
259 void print (std::ostream& os, bool pr_as_read_syntax = false);
260
261 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
262
263 bool print_name_tag (std::ostream& os, const std::string& name) const;
264
265 std::string edit_display (const float_display_format& fmt,
267
268 bool save_ascii (std::ostream& os);
269
270 bool load_ascii (std::istream& is);
271
272 bool save_binary (std::ostream& os, bool save_as_floats);
273
274 bool load_binary (std::istream& is, bool swap,
276
277 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
278
279 bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
280
281 mxArray * as_mxArray (bool interleaved) const;
282
283 bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
284
285protected:
286
287 // The associative array used to manage the structure data.
289
290private:
291
292 octave_value to_array (void);
293
295};
296
297#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:576
octave_map reshape(const dim_vector &dv) const
Definition: oct-map.cc:526
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:181
octave_value squeeze(void) const
Definition: ov-struct.h:218
dim_vector dims(void) const
Definition: ov-struct.h:226
octave_scalar_map scalar_map_value(void) const
Definition: ov-struct.h:255
string_vector map_keys(void) const
Definition: ov-struct.h:257
builtin_type_t builtin_type(void) const
Definition: ov-struct.h:251
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-struct.h:239
octave_scalar_struct(const std::map< std::string, octave_value > &m)
Definition: ov-struct.h:184
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-struct.h:220
octave_map map_value(void) const
Definition: ov-struct.h:253
octave_base_value * clone(void) const
Definition: ov-struct.h:192
bool is_defined(void) const
Definition: ov-struct.h:245
octave_idx_type nfields(void) const
Definition: ov-struct.h:237
octave_scalar_struct(const octave_scalar_struct &s)
Definition: ov-struct.h:187
octave_base_value * empty_clone(void) const
Definition: ov-struct.h:194
~octave_scalar_struct(void)=default
bool is_constant(void) const
Definition: ov-struct.h:247
octave_scalar_map m_map
Definition: ov-struct.h:288
octave_idx_type numel(void) const
Definition: ov-struct.h:232
bool isstruct(void) const
Definition: ov-struct.h:249
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-struct.h:242
octave_scalar_struct(void)
Definition: ov-struct.h:178
octave_map m_map
Definition: ov-struct.h:166
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-struct.h:118
octave_struct(void)
Definition: ov-struct.h:53
octave_map map_value(void) const
Definition: ov-struct.h:129
octave_value squeeze(void) const
Definition: ov-struct.h:94
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-struct.h:73
dim_vector dims(void) const
Definition: ov-struct.h:102
builtin_type_t builtin_type(void) const
Definition: ov-struct.h:127
octave_base_value * clone(void) const
Definition: ov-struct.h:64
octave_base_value * empty_clone(void) const
Definition: ov-struct.h:65
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-struct.h:115
bool isstruct(void) const
Definition: ov-struct.h:125
octave_struct(const octave_struct &s)
Definition: ov-struct.h:59
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
bool is_constant(void) const
Definition: ov-struct.h:123
octave_idx_type nfields(void) const
Definition: ov-struct.h:113
bool is_defined(void) const
Definition: ov-struct.h:121
~octave_struct(void)=default
string_vector map_keys(void) const
Definition: ov-struct.h:131
octave_idx_type numel(void) const
Definition: ov-struct.h:108
octave_idx_type length(void) const
Definition: ovl.h:113
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:619
octave_value squeeze(void) const
Definition: ov.h:485
octave_value reshape(const dim_vector &dv) const
Definition: ov.h:616
QString name
F77_RET_T const F77_DBLE * x
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:173
builtin_type_t
Definition: ov-base.h:75
@ btyp_struct
Definition: ov-base.h:90