GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-struct.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_ov_struct_h)
24 #define octave_ov_struct_h 1
25 
26 #include <cstdlib>
27 
28 #include <iosfwd>
29 #include <string>
30 
31 #include "mx-base.h"
32 #include "str-vec.h"
33 
34 #include "error.h"
35 #include "oct-alloc.h"
36 #include "oct-map.h"
37 #include "ov-base.h"
38 #include "ov-typeinfo.h"
39 
40 class octave_value_list;
41 
42 class tree_walker;
43 
44 // Data structures.
45 
46 class
48 {
49 public:
50 
52  : octave_base_value (), map () { }
53 
55  : octave_base_value (), map (m) { }
56 
58  : octave_base_value (), map (s.map) { }
59 
60  ~octave_struct (void) { }
61 
62  octave_base_value *clone (void) const { return new octave_struct (*this); }
63  octave_base_value *empty_clone (void) const { return new octave_struct (); }
64 
65  octave_base_value *try_narrowing_conversion (void);
66 
67  Cell dotref (const octave_value_list& idx, bool auto_add = false);
68 
69  octave_value subsref (const std::string& type,
70  const std::list<octave_value_list>& idx)
71  {
72  octave_value_list tmp = subsref (type, idx, 1);
73  return tmp.length () > 0 ? tmp(0) : octave_value ();
74  }
75 
76  octave_value_list subsref (const std::string&,
77  const std::list<octave_value_list>&, int);
78 
79  octave_value subsref (const std::string& type,
80  const std::list<octave_value_list>& idx,
81  bool auto_add);
82 
83  static octave_value numeric_conv (const octave_value& val,
84  const std::string& type);
85 
86  octave_value subsasgn (const std::string& type,
87  const std::list<octave_value_list>& idx,
88  const octave_value& rhs);
89 
90  octave_value squeeze (void) const { return map.squeeze (); }
91 
92  octave_value permute (const Array<int>& vec, bool inv = false) const
93  { return map.permute (vec, inv); }
94 
95  octave_value do_index_op (const octave_value_list& idx,
96  bool resize_ok = false);
97 
98  dim_vector dims (void) const { return map.dims (); }
99 
100  size_t byte_size (void) const;
101 
102  // This is the number of elements in each field. The total number
103  // of elements is numel () * nfields ().
104  octave_idx_type numel (void) const
105  {
106  return map.numel ();
107  }
108 
109  octave_idx_type nfields (void) const { return map.nfields (); }
110 
111  octave_value reshape (const dim_vector& new_dims) const
112  { return map.reshape (new_dims); }
113 
114  octave_value resize (const dim_vector& dv, bool fill = false) const
115  { octave_map tmap = map; tmap.resize (dv, fill); return tmap; }
116 
117  bool is_defined (void) const { return true; }
118 
119  bool is_constant (void) const { return true; }
120 
121  bool is_map (void) const { return true; }
122 
123  builtin_type_t builtin_type (void) const { return btyp_struct; }
124 
125  octave_map map_value (void) const { return map; }
126 
127  string_vector map_keys (void) const { return map.fieldnames (); }
128 
129  void print (std::ostream& os, bool pr_as_read_syntax = false) const;
130 
131  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
132 
133  bool print_name_tag (std::ostream& os, const std::string& name) const;
134 
135  bool save_ascii (std::ostream& os);
136 
137  bool load_ascii (std::istream& is);
138 
139  bool save_binary (std::ostream& os, bool& save_as_floats);
140 
141  bool load_binary (std::istream& is, bool swap,
143 
144 #if defined (HAVE_HDF5)
145  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
146 
147  bool load_hdf5 (hid_t loc_id, const char *name);
148 #endif
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 
168 };
169 
170 class
172 {
173 public:
174 
176  : octave_base_value (), map () { }
177 
179  : octave_base_value (), map (m) { }
180 
182  : octave_base_value (), map (s.map) { }
183 
185 
186  octave_base_value *clone (void) const
187  { return new octave_scalar_struct (*this); }
188  octave_base_value *empty_clone (void) const
189  { return new octave_scalar_struct (); }
190 
191  octave_value dotref (const octave_value_list& idx, bool auto_add = false);
192 
193  octave_value subsref (const std::string& type,
194  const std::list<octave_value_list>& idx);
195 
196  octave_value_list subsref (const std::string& type,
197  const std::list<octave_value_list>& idx, int);
198 
199 
200  octave_value subsref (const std::string& type,
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 
207  octave_value subsasgn (const std::string& type,
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 is_map (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) const;
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  bool save_ascii (std::ostream& os);
259 
260  bool load_ascii (std::istream& is);
261 
262  bool save_binary (std::ostream& os, bool& save_as_floats);
263 
264  bool load_binary (std::istream& is, bool swap,
266 
267 #if defined (HAVE_HDF5)
268  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
269 
270  bool load_hdf5 (hid_t loc_id, const char *name);
271 #endif
272 
273  mxArray *as_mxArray (void) const;
274 
275  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
276 
277 protected:
278 
279  // The associative array used to manage the structure data.
281 
282 private:
283 
284  octave_value to_array (void);
285 
287 
289 };
290 
291 #endif