GNU Octave  6.2.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-2021 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 (), map () { }
55 
57  : octave_base_value (), map (m) { }
58 
60  : octave_base_value (), map (s.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  octave_base_value * try_narrowing_conversion (void);
68 
69  Cell dotref (const octave_value_list& idx, bool auto_add = false);
70 
71  octave_value subsref (const std::string& type,
72  const std::list<octave_value_list>& idx)
73  {
74  octave_value_list tmp = subsref (type, idx, 1);
75  return tmp.length () > 0 ? tmp(0) : octave_value ();
76  }
77 
78  octave_value_list subsref (const std::string&,
79  const std::list<octave_value_list>&, int);
80 
81  octave_value subsref (const std::string& type,
82  const std::list<octave_value_list>& idx,
83  bool auto_add);
84 
85  static octave_value numeric_conv (const octave_value& val,
86  const std::string& type);
87 
88  octave_value subsasgn (const std::string& type,
89  const std::list<octave_value_list>& idx,
90  const octave_value& rhs);
91 
92  octave_value squeeze (void) const { return map.squeeze (); }
93 
94  octave_value permute (const Array<int>& vec, bool inv = false) const
95  { return map.permute (vec, inv); }
96 
97  octave_value do_index_op (const octave_value_list& idx,
98  bool resize_ok = false);
99 
100  dim_vector dims (void) const { return map.dims (); }
101 
102  size_t byte_size (void) const;
103 
104  // This is the number of elements in each field. The total number
105  // of elements is numel () * nfields ().
106  octave_idx_type numel (void) const
107  {
108  return map.numel ();
109  }
110 
111  octave_idx_type nfields (void) const { return map.nfields (); }
112 
113  octave_value reshape (const dim_vector& new_dims) const
114  { return map.reshape (new_dims); }
115 
116  octave_value resize (const dim_vector& dv, bool fill = false) const
117  { octave_map tmap = map; tmap.resize (dv, fill); return tmap; }
118 
119  bool is_defined (void) const { return true; }
120 
121  bool is_constant (void) const { return true; }
122 
123  bool isstruct (void) const { return true; }
124 
125  builtin_type_t builtin_type (void) const { return btyp_struct; }
126 
127  octave_map map_value (void) const { return map; }
128 
129  string_vector map_keys (void) const { return map.fieldnames (); }
130 
131  void print (std::ostream& os, bool pr_as_read_syntax = false);
132 
133  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
134 
135  bool print_name_tag (std::ostream& os, const std::string& name) const;
136 
137  std::string edit_display (const float_display_format& fmt,
138  octave_idx_type i, octave_idx_type j) const;
139 
140  bool save_ascii (std::ostream& os);
141 
142  bool load_ascii (std::istream& is);
143 
144  bool save_binary (std::ostream& os, bool save_as_floats);
145 
146  bool load_binary (std::istream& is, bool swap,
148 
149  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
150 
151  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
152 
153  mxArray * as_mxArray (void) const;
154 
156  fast_elem_extract (octave_idx_type n) const;
157 
158  bool
159  fast_elem_insert (octave_idx_type n, const octave_value& x);
160 
161 protected:
162 
163  // The associative array used to manage the structure data.
165 
166 private:
167 
169 };
170 
171 class
173 {
174 public:
175 
177  : octave_base_value (), map () { }
178 
180  : octave_base_value (), map (m) { }
181 
182  octave_scalar_struct (const std::map<std::string, octave_value>& m)
183  : octave_base_value (), map (m) { }
184 
186  : octave_base_value (), map (s.map) { }
187 
188  ~octave_scalar_struct (void) = default;
189 
190  octave_base_value * clone (void) const
191  { return new octave_scalar_struct (*this); }
193  { return new octave_scalar_struct (); }
194 
195  octave_value dotref (const octave_value_list& idx, bool auto_add = false);
196 
197  octave_value subsref (const std::string& type,
198  const std::list<octave_value_list>& idx);
199 
200  octave_value_list subsref (const std::string& type,
201  const std::list<octave_value_list>& idx, int);
202 
203  octave_value subsref (const std::string& type,
204  const std::list<octave_value_list>& idx,
205  bool auto_add);
206 
207  static octave_value numeric_conv (const octave_value& val,
208  const std::string& type);
209 
210  octave_value subsasgn (const std::string& type,
211  const std::list<octave_value_list>& idx,
212  const octave_value& rhs);
213 
214  octave_value squeeze (void) const { return map; }
215 
216  octave_value permute (const Array<int>& vec, bool inv = false) const
217  { return octave_map (map).permute (vec, inv); }
218 
219  octave_value do_index_op (const octave_value_list& idx,
220  bool resize_ok = false);
221 
222  dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; }
223 
224  size_t byte_size (void) const;
225 
226  // This is the number of elements in each field. The total number
227  // of elements is numel () * nfields ().
228  octave_idx_type numel (void) const
229  {
230  return 1;
231  }
232 
233  octave_idx_type nfields (void) const { return map.nfields (); }
234 
235  octave_value reshape (const dim_vector& new_dims) const
236  { return octave_map (map).reshape (new_dims); }
237 
238  octave_value resize (const dim_vector& dv, bool fill = false) const
239  { octave_map tmap = map; tmap.resize (dv, fill); return tmap; }
240 
241  bool is_defined (void) const { return true; }
242 
243  bool is_constant (void) const { return true; }
244 
245  bool isstruct (void) const { return true; }
246 
247  builtin_type_t builtin_type (void) const { return btyp_struct; }
248 
249  octave_map map_value (void) const { return map; }
250 
251  octave_scalar_map scalar_map_value (void) const { return map; }
252 
253  string_vector map_keys (void) const { return map.fieldnames (); }
254 
255  void print (std::ostream& os, bool pr_as_read_syntax = false);
256 
257  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
258 
259  bool print_name_tag (std::ostream& os, const std::string& name) const;
260 
261  std::string edit_display (const float_display_format& fmt,
262  octave_idx_type i, octave_idx_type j) const;
263 
264  bool save_ascii (std::ostream& os);
265 
266  bool load_ascii (std::istream& is);
267 
268  bool save_binary (std::ostream& os, bool save_as_floats);
269 
270  bool load_binary (std::istream& is, bool swap,
272 
273  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
274 
275  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
276 
277  mxArray * as_mxArray (void) const;
278 
279  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
280 
281 protected:
282 
283  // The associative array used to manage the structure data.
285 
286 private:
287 
288  octave_value to_array (void);
289 
291 };
292 
293 #endif
Definition: Cell.h:43
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
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_base_value * empty_clone(void) const
Definition: ov-struct.h:192
octave_scalar_struct(const octave_scalar_map &m)
Definition: ov-struct.h:179
octave_value squeeze(void) const
Definition: ov-struct.h:214
dim_vector dims(void) const
Definition: ov-struct.h:222
octave_scalar_map scalar_map_value(void) const
Definition: ov-struct.h:251
string_vector map_keys(void) const
Definition: ov-struct.h:253
builtin_type_t builtin_type(void) const
Definition: ov-struct.h:247
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-struct.h:235
octave_scalar_struct(const std::map< std::string, octave_value > &m)
Definition: ov-struct.h:182
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-struct.h:216
octave_map map_value(void) const
Definition: ov-struct.h:249
bool is_defined(void) const
Definition: ov-struct.h:241
octave_idx_type nfields(void) const
Definition: ov-struct.h:233
octave_scalar_struct(const octave_scalar_struct &s)
Definition: ov-struct.h:185
~octave_scalar_struct(void)=default
bool is_constant(void) const
Definition: ov-struct.h:243
octave_base_value * clone(void) const
Definition: ov-struct.h:190
octave_idx_type numel(void) const
Definition: ov-struct.h:228
bool isstruct(void) const
Definition: ov-struct.h:245
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-struct.h:238
octave_scalar_map map
Definition: ov-struct.h:284
octave_scalar_struct(void)
Definition: ov-struct.h:176
octave_base_value * clone(void) const
Definition: ov-struct.h:64
octave_map map
Definition: ov-struct.h:164
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-struct.h:116
octave_struct(void)
Definition: ov-struct.h:53
octave_map map_value(void) const
Definition: ov-struct.h:127
octave_base_value * empty_clone(void) const
Definition: ov-struct.h:65
octave_value squeeze(void) const
Definition: ov-struct.h:92
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-struct.h:71
dim_vector dims(void) const
Definition: ov-struct.h:100
builtin_type_t builtin_type(void) const
Definition: ov-struct.h:125
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-struct.h:113
bool isstruct(void) const
Definition: ov-struct.h:123
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:94
bool is_constant(void) const
Definition: ov-struct.h:121
octave_idx_type nfields(void) const
Definition: ov-struct.h:111
bool is_defined(void) const
Definition: ov-struct.h:119
~octave_struct(void)=default
string_vector map_keys(void) const
Definition: ov-struct.h:129
octave_idx_type numel(void) const
Definition: ov-struct.h:106
octave_idx_type length(void) const
Definition: ovl.h:113
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:533
octave_value squeeze(void) const
Definition: ov.h:409
octave_value reshape(const dim_vector &dv) const
Definition: ov.h:530
QString name
F77_RET_T const F77_DBLE * x
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
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:158
builtin_type_t
Definition: ov-base.h:72
@ btyp_struct
Definition: ov-base.h:87