GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-struct.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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
49{
50public:
51
54
56 : octave_base_value (), m_map (m) { }
57
60
61 ~octave_struct () = default;
62
63 octave_base_value * clone () const { return new octave_struct (*this); }
64 octave_base_value * empty_clone () const { return new octave_struct (); }
65
66 void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame);
67
69
70 Cell dotref (const octave_value_list& idx, bool auto_add = false);
71
72 octave_value subsref (const std::string& type,
73 const std::list<octave_value_list>& idx)
74 {
75 octave_value_list tmp = subsref (type, idx, 1);
76 return tmp.length () > 0 ? tmp(0) : octave_value ();
77 }
78
79 octave_value_list subsref (const std::string&,
80 const std::list<octave_value_list>&, int);
81
82 octave_value subsref (const std::string& type,
83 const std::list<octave_value_list>& idx,
84 bool auto_add);
85
86 static octave_value numeric_conv (const octave_value& val,
87 const std::string& type);
88
89 octave_value subsasgn (const std::string& type,
90 const std::list<octave_value_list>& idx,
91 const octave_value& rhs);
92
93 octave_value squeeze () const { return m_map.squeeze (); }
94
95 octave_value permute (const Array<int>& vec, bool inv = false) const
96 { return m_map.permute (vec, inv); }
97
99 bool resize_ok = false);
100
101 dim_vector dims () const { return m_map.dims (); }
102
103 std::size_t byte_size () const;
104
105 // This is the number of elements in each field. The total number
106 // of elements is numel () * nfields ().
108 {
109 return m_map.numel ();
110 }
111
112 octave_idx_type nfields () const { return m_map.nfields (); }
113
114 octave_value reshape (const dim_vector& new_dims) const
115 { return m_map.reshape (new_dims); }
116
117 octave_value resize (const dim_vector& dv, bool fill = false) const
118 { octave_map tmap = m_map; tmap.resize (dv, fill); return tmap; }
119
120 bool is_defined () const { return true; }
121
122 bool is_constant () const { return true; }
123
124 bool isstruct () const { return true; }
125
127
128 octave_map map_value () const { return m_map; }
129
130 string_vector map_keys () const { return m_map.fieldnames (); }
131
132 bool isfield (const std::string& field_name) const
133 { return m_map.isfield (field_name); }
134
135 void print (std::ostream& os, bool pr_as_read_syntax = false);
136
137 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
138
139 bool print_name_tag (std::ostream& os, const std::string& name) const;
140
141 std::string edit_display (const float_display_format& fmt,
143
144 bool save_ascii (std::ostream& os);
145
146 bool load_ascii (std::istream& is);
147
148 bool save_binary (std::ostream& os, bool save_as_floats);
149
150 bool load_binary (std::istream& is, bool swap,
151 octave::mach_info::float_format fmt);
152
153 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
154
155 bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
156
157 mxArray * as_mxArray (bool interleaved) const;
158
161
162 bool
164
165protected:
166
167 // The associative array used to manage the structure data.
169
170private:
171
173};
174
176{
177public:
178
181
184
185 octave_scalar_struct (const std::map<std::string, octave_value>& m)
186 : octave_base_value (), m_map (m) { }
187
190
192
194 { return new octave_scalar_struct (*this); }
196 { return new octave_scalar_struct (); }
197
198 void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame);
199
200 octave_value dotref (const octave_value_list& idx, bool auto_add = false);
201
202 octave_value subsref (const std::string& type,
203 const std::list<octave_value_list>& idx);
204
205 octave_value_list subsref (const std::string& type,
206 const std::list<octave_value_list>& idx, int);
207
208 octave_value subsref (const std::string& type,
209 const std::list<octave_value_list>& idx,
210 bool auto_add);
211
212 static octave_value numeric_conv (const octave_value& val,
213 const std::string& type);
214
215 octave_value subsasgn (const std::string& type,
216 const std::list<octave_value_list>& idx,
217 const octave_value& rhs);
218
219 octave_value squeeze () const { return m_map; }
220
221 octave_value permute (const Array<int>& vec, bool inv = false) const
222 { return octave_map (m_map).permute (vec, inv); }
223
225 bool resize_ok = false);
226
227 dim_vector dims () const { static dim_vector dv (1, 1); return dv; }
228
229 std::size_t byte_size () const;
230
231 // This is the number of elements in each field. The total number
232 // of elements is numel () * nfields ().
234 {
235 return 1;
236 }
237
238 octave_idx_type nfields () const { return m_map.nfields (); }
239
240 octave_value reshape (const dim_vector& new_dims) const
241 { return octave_map (m_map).reshape (new_dims); }
242
243 octave_value resize (const dim_vector& dv, bool fill = false) const
244 { octave_map tmap = m_map; tmap.resize (dv, fill); return tmap; }
245
246 bool is_defined () const { return true; }
247
248 bool is_constant () const { return true; }
249
250 bool isstruct () const { return true; }
251
253
254 octave_map map_value () const { return m_map; }
255
257
258 string_vector map_keys () const { return m_map.fieldnames (); }
259
260 bool isfield (const std::string& field_name) const
261 { return m_map.isfield (field_name); }
262
263 void print (std::ostream& os, bool pr_as_read_syntax = false);
264
265 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
266
267 bool print_name_tag (std::ostream& os, const std::string& name) const;
268
269 std::string edit_display (const float_display_format& fmt,
271
272 bool save_ascii (std::ostream& os);
273
274 bool load_ascii (std::istream& is);
275
276 bool save_binary (std::ostream& os, bool save_as_floats);
277
278 bool load_binary (std::istream& is, bool swap,
279 octave::mach_info::float_format fmt);
280
281 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
282
283 bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
284
285 mxArray * as_mxArray (bool interleaved) const;
286
287 bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
288
289protected:
290
291 // The associative array used to manage the structure data.
293
294private:
295
296 octave_value to_array ();
297
299};
300
301#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
Definition Cell.h:41
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
friend class octave_value
Definition ov-base.h:278
void resize(const dim_vector &dv, bool fill=false)
Definition oct-map.cc:574
octave_idx_type nfields() const
Definition oct-map.h:323
octave_map squeeze() const
Definition oct-map.cc:442
octave_map reshape(const dim_vector &dv) const
Definition oct-map.cc:524
dim_vector dims() const
Definition oct-map.h:409
octave_map permute(const Array< int > &vec, bool inv=false) const
Definition oct-map.cc:465
string_vector fieldnames() const
Definition oct-map.h:332
bool isfield(const std::string &name) const
Definition oct-map.h:326
octave_idx_type numel() const
Definition oct-map.h:368
string_vector fieldnames() const
Definition oct-map.h:219
bool isfield(const std::string &name) const
Definition oct-map.h:213
octave_idx_type nfields() const
Definition oct-map.h:210
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_base_value * empty_clone() const
Definition ov-struct.h:195
octave_scalar_struct(const octave_scalar_map &m)
Definition ov-struct.h:182
octave_idx_type nfields() const
Definition ov-struct.h:238
bool print_name_tag(std::ostream &os, const std::string &name) const
mxArray * as_mxArray(bool interleaved) const
octave_base_value * clone() const
Definition ov-struct.h:193
static octave_value numeric_conv(const octave_value &val, const std::string &type)
octave_value squeeze() const
Definition ov-struct.h:219
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
octave_value reshape(const dim_vector &new_dims) const
Definition ov-struct.h:240
octave_scalar_struct(const std::map< std::string, octave_value > &m)
Definition ov-struct.h:185
bool load_ascii(std::istream &is)
builtin_type_t builtin_type() const
Definition ov-struct.h:252
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition ov-struct.h:221
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_value dotref(const octave_value_list &idx, bool auto_add=false)
bool is_constant() const
Definition ov-struct.h:248
octave_map map_value() const
Definition ov-struct.h:254
~octave_scalar_struct()=default
octave_scalar_struct(const octave_scalar_struct &s)
Definition ov-struct.h:188
bool save_binary(std::ostream &os, bool save_as_floats)
dim_vector dims() const
Definition ov-struct.h:227
bool save_ascii(std::ostream &os)
bool is_defined() const
Definition ov-struct.h:246
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
std::size_t byte_size() const
octave_scalar_map m_map
Definition ov-struct.h:292
octave_scalar_map scalar_map_value() const
Definition ov-struct.h:256
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) 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)
bool isfield(const std::string &field_name) const
Definition ov-struct.h:260
string_vector map_keys() const
Definition ov-struct.h:258
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition ov-struct.h:243
std::string edit_display(const float_display_format &fmt, octave_idx_type i, octave_idx_type j) const
octave_idx_type numel() const
Definition ov-struct.h:233
bool isstruct() const
Definition ov-struct.h:250
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
octave_map m_map
Definition ov-struct.h:168
string_vector map_keys() const
Definition ov-struct.h:130
bool fast_elem_insert(octave_idx_type n, const octave_value &x)
bool is_constant() const
Definition ov-struct.h:122
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition ov-struct.h:117
octave_idx_type numel() const
Definition ov-struct.h:107
static octave_value numeric_conv(const octave_value &val, const std::string &type)
Definition ov-struct.cc:285
builtin_type_t builtin_type() const
Definition ov-struct.h:126
bool load_ascii(std::istream &is)
Definition ov-struct.cc:773
std::string edit_display(const float_display_format &fmt, octave_idx_type i, octave_idx_type j) const
Definition ov-struct.cc:709
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov-struct.h:72
bool is_defined() const
Definition ov-struct.h:120
dim_vector dims() const
Definition ov-struct.h:101
mxArray * as_mxArray(bool interleaved) const
bool save_ascii(std::ostream &os)
Definition ov-struct.cc:737
octave_map map_value() const
Definition ov-struct.h:128
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
Definition ov-struct.cc:299
octave_value reshape(const dim_vector &new_dims) const
Definition ov-struct.h:114
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
std::size_t byte_size() const
Definition ov-struct.cc:598
octave_idx_type nfields() const
Definition ov-struct.h:112
void break_closure_cycles(const std::shared_ptr< octave::stack_frame > &frame)
Definition ov-struct.cc:68
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition ov-struct.cc:956
octave_struct(const octave_struct &s)
Definition ov-struct.h:58
bool isstruct() const
Definition ov-struct.h:124
octave_struct(const octave_map &m)
Definition ov-struct.h:55
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition ov-struct.h:95
~octave_struct()=default
bool isfield(const std::string &field_name) const
Definition ov-struct.h:132
bool print_name_tag(std::ostream &os, const std::string &name) const
Definition ov-struct.cc:681
octave_value fast_elem_extract(octave_idx_type n) const
octave_base_value * try_narrowing_conversion()
Definition ov-struct.cc:80
bool save_binary(std::ostream &os, bool save_as_floats)
Definition ov-struct.cc:842
octave_base_value * empty_clone() const
Definition ov-struct.h:64
Cell dotref(const octave_value_list &idx, bool auto_add=false)
Definition ov-struct.cc:91
void print(std::ostream &os, bool pr_as_read_syntax=false)
Definition ov-struct.cc:617
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition ov-struct.cc:884
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition ov-struct.cc:586
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition ov-struct.cc:623
octave_base_value * clone() const
Definition ov-struct.h:63
octave_value squeeze() const
Definition ov-struct.h:93
octave_idx_type length() const
Definition ovl.h:111
F77_RET_T const F77_DBLE * x
int64_t octave_hdf5_id
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA_API(API)
Definition ov-base.h:185
builtin_type_t
Definition ov-base.h:83
@ btyp_struct
Definition ov-base.h:98