GNU Octave  4.4.1
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-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_ov_struct_h)
24 #define octave_ov_struct_h 1
25 
26 #include "octave-config.h"
27 
28 #include <cstdlib>
29 
30 #include <iosfwd>
31 #include <string>
32 
33 #include "mx-base.h"
34 #include "str-vec.h"
35 
36 #include "error.h"
37 #include "oct-map.h"
38 #include "ov-base.h"
39 #include "ov-typeinfo.h"
40 
41 class octave_value_list;
42 
43 // Data structures.
44 
45 class
47 {
48 public:
49 
51  : octave_base_value (), map () { }
52 
54  : octave_base_value (), map (m) { }
55 
57  : octave_base_value (), map (s.map) { }
58 
59  ~octave_struct (void) = default;
60 
61  octave_base_value * clone (void) const { return new octave_struct (*this); }
62  octave_base_value * empty_clone (void) const { return new octave_struct (); }
63 
64  octave_base_value * try_narrowing_conversion (void);
65 
66  Cell dotref (const octave_value_list& idx, bool auto_add = false);
67 
69  const std::list<octave_value_list>& idx)
70  {
71  octave_value_list tmp = subsref (type, idx, 1);
72  return tmp.length () > 0 ? tmp(0) : octave_value ();
73  }
74 
76  const std::list<octave_value_list>&, int);
77 
79  const std::list<octave_value_list>& idx,
80  bool auto_add);
81 
82  static octave_value numeric_conv (const octave_value& val,
83  const std::string& type);
84 
86  const std::list<octave_value_list>& idx,
87  const octave_value& rhs);
88 
89  octave_value squeeze (void) const { return map.squeeze (); }
90 
91  octave_value permute (const Array<int>& vec, bool inv = false) const
92  { return map.permute (vec, inv); }
93 
94  octave_value do_index_op (const octave_value_list& idx,
95  bool resize_ok = false);
96 
97  dim_vector dims (void) const { return map.dims (); }
98 
99  size_t byte_size (void) const;
100 
101  // This is the number of elements in each field. The total number
102  // of elements is numel () * nfields ().
103  octave_idx_type numel (void) const
104  {
105  return map.numel ();
106  }
107 
108  octave_idx_type nfields (void) const { return map.nfields (); }
109 
110  octave_value reshape (const dim_vector& new_dims) const
111  { return map.reshape (new_dims); }
112 
113  octave_value resize (const dim_vector& dv, bool fill = false) const
114  { octave_map tmap = map; tmap.resize (dv, fill); return tmap; }
115 
116  bool is_defined (void) const { return true; }
117 
118  bool is_constant (void) const { return true; }
119 
120  bool isstruct (void) const { return true; }
121 
122  builtin_type_t builtin_type (void) const { return btyp_struct; }
123 
124  octave_map map_value (void) const { return map; }
125 
126  string_vector map_keys (void) const { return map.fieldnames (); }
127 
128  void print (std::ostream& os, bool pr_as_read_syntax = false);
129 
130  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
131 
132  bool print_name_tag (std::ostream& os, const std::string& name) const;
133 
134  std::string edit_display (const float_display_format& fmt,
136 
137  bool save_ascii (std::ostream& os);
138 
139  bool load_ascii (std::istream& is);
140 
141  bool save_binary (std::ostream& os, bool& save_as_floats);
142 
143  bool load_binary (std::istream& is, bool swap,
145 
146  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
147 
148  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
149 
150  mxArray * as_mxArray (void) const;
151 
153  fast_elem_extract (octave_idx_type n) const;
154 
155  bool
156  fast_elem_insert (octave_idx_type n, const octave_value& x);
157 
158 protected:
159 
160  // The associative array used to manage the structure data.
162 
163 private:
164 
166 };
167 
168 class
170 {
171 public:
172 
174  : octave_base_value (), map () { }
175 
177  : octave_base_value (), map (m) { }
178 
179  octave_scalar_struct (const std::map<std::string, octave_value>& m)
180  : octave_base_value (), map (m) { }
181 
183  : octave_base_value (), map (s.map) { }
184 
185  ~octave_scalar_struct (void) = default;
186 
187  octave_base_value * clone (void) const
188  { return new octave_scalar_struct (*this); }
190  { return new octave_scalar_struct (); }
191 
192  octave_value dotref (const octave_value_list& idx, bool auto_add = false);
193 
195  const std::list<octave_value_list>& idx);
196 
198  const std::list<octave_value_list>& idx, int);
199 
201  const std::list<octave_value_list>& idx,
202  bool auto_add);
203 
204  static octave_value numeric_conv (const octave_value& val,
205  const std::string& type);
206 
208  const std::list<octave_value_list>& idx,
209  const octave_value& rhs);
210 
211  octave_value squeeze (void) const { return map; }
212 
213  octave_value permute (const Array<int>& vec, bool inv = false) const
214  { return octave_map (map).permute (vec, inv); }
215 
216  octave_value do_index_op (const octave_value_list& idx,
217  bool resize_ok = false);
218 
219  dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; }
220 
221  size_t byte_size (void) const;
222 
223  // This is the number of elements in each field. The total number
224  // of elements is numel () * nfields ().
225  octave_idx_type numel (void) const
226  {
227  return 1;
228  }
229 
230  octave_idx_type nfields (void) const { return map.nfields (); }
231 
232  octave_value reshape (const dim_vector& new_dims) const
233  { return octave_map (map).reshape (new_dims); }
234 
235  octave_value resize (const dim_vector& dv, bool fill = false) const
236  { octave_map tmap = map; tmap.resize (dv, fill); return tmap; }
237 
238  bool is_defined (void) const { return true; }
239 
240  bool is_constant (void) const { return true; }
241 
242  bool isstruct (void) const { return true; }
243 
244  builtin_type_t builtin_type (void) const { return btyp_struct; }
245 
246  octave_map map_value (void) const { return map; }
247 
248  octave_scalar_map scalar_map_value (void) const { return map; }
249 
250  string_vector map_keys (void) const { return map.fieldnames (); }
251 
252  void print (std::ostream& os, bool pr_as_read_syntax = false);
253 
254  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
255 
256  bool print_name_tag (std::ostream& os, const std::string& name) const;
257 
258  std::string edit_display (const float_display_format& fmt,
260 
261  bool save_ascii (std::ostream& os);
262 
263  bool load_ascii (std::istream& is);
264 
265  bool save_binary (std::ostream& os, bool& save_as_floats);
266 
267  bool load_binary (std::istream& is, bool swap,
269 
270  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
271 
272  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
273 
274  mxArray * as_mxArray (void) const;
275 
276  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
277 
278 protected:
279 
280  // The associative array used to manage the structure data.
282 
283 private:
284 
285  octave_value to_array (void);
286 
288 };
289 
290 #endif
octave_base_value * empty_clone(void) const
Definition: ov-struct.h:189
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-struct.h:91
octave_map squeeze(void) const
Definition: oct-map.cc:443
Definition: Cell.h:37
octave_scalar_struct(const octave_scalar_map &m)
Definition: ov-struct.h:176
bool is_constant(void) const
Definition: ov-struct.h:240
octave_idx_type nfields(void) const
Definition: ov-struct.h:108
string_vector fieldnames(void) const
Definition: oct-map.h:339
dim_vector dims(void) const
Definition: ov-struct.h:219
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-struct.h:232
idx subsref(val, idx) esult
Definition: ov.cc:3065
octave_map map_value(void) const
Definition: ov-struct.h:246
octave_value squeeze(void) const
Definition: ov-struct.h:89
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-struct.h:213
octave_scalar_struct(void)
Definition: ov-struct.h:173
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-struct.h:110
bool isstruct(void) const
Definition: ov-struct.h:242
octave_map map
Definition: ov-struct.h:161
octave_base_value * empty_clone(void) const
Definition: ov-struct.h:62
s
Definition: file-io.cc:2729
builtin_type_t
Definition: ov-base.h:71
octave_idx_type numel(void) const
Definition: ov-struct.h:103
octave_scalar_struct(const octave_scalar_struct &s)
Definition: ov-struct.h:182
bool swap
Definition: load-save.cc:738
octave_map permute(const Array< int > &vec, bool inv=false) const
Definition: oct-map.cc:466
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:158
nd deftypefn *std::string name
Definition: sysdep.cc:647
string_vector map_keys(void) const
Definition: ov-struct.h:126
octave_map reshape(const dim_vector &dv) const
Definition: oct-map.cc:527
octave_idx_type numel(void) const
Definition: ov-struct.h:225
bool save_as_floats
Definition: load-save.cc:1617
double tmp
Definition: data.cc:6252
int64_t octave_hdf5_id
octave_struct(void)
Definition: ov-struct.h:50
idx type
Definition: ov.cc:3114
octave_value squeeze(void) const
Definition: ov-struct.h:211
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-struct.h:113
octave_idx_type numel(void) const
Definition: oct-map.h:375
dim_vector dims(void) const
Definition: ov-struct.h:97
bool isstruct(void) const
Definition: ov-struct.h:120
octave_map map_value(void) const
Definition: ov-struct.h:124
builtin_type_t builtin_type(void) const
Definition: ov-struct.h:244
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-struct.h:68
octave_idx_type nfields(void) const
Definition: oct-map.h:330
octave_base_value * clone(void) const
Definition: ov-struct.h:61
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-struct.h:235
octave_idx_type nfields(void) const
Definition: ov-struct.h:230
octave_map map(dims)
idx subsasgn(val, idx, 0) esult
Definition: ov.cc:3114
octave_scalar_map scalar_map_value(void) const
Definition: ov-struct.h:248
octave_struct(const octave_map &m)
Definition: ov-struct.h:53
bool is_defined(void) const
Definition: ov-struct.h:116
for i
Definition: data.cc:5264
void resize(const dim_vector &dv, bool fill=false)
Definition: oct-map.cc:577
octave_scalar_map map
Definition: ov-struct.h:281
dim_vector dims(void) const
Definition: oct-map.h:416
octave_struct(const octave_struct &s)
Definition: ov-struct.h:56
write the output to stdout if nargout is
Definition: load-save.cc:1612
octave_base_value * clone(void) const
Definition: ov-struct.h:187
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
builtin_type_t builtin_type(void) const
Definition: ov-struct.h:122
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
dim_vector dv
Definition: sub2ind.cc:263
bool is_defined(void) const
Definition: ov-struct.h:238
octave::stream os
Definition: file-io.cc:627
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
bool is_constant(void) const
Definition: ov-struct.h:118
octave_scalar_struct(const std::map< std::string, octave_value > &m)
Definition: ov-struct.h:179
string_vector map_keys(void) const
Definition: ov-struct.h:250