Mercurial > octave-nkf
annotate libinterp/octave-value/ov-struct.cc @ 20593:c41595061186
eliminate more simple uses of error_state
* betainc.cc, file-io.cc, ov-class.cc, ov-struct.cc:
Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 05 Oct 2015 23:09:54 -0400 |
parents | c6224b4e7774 |
children | eef93a493ce3 |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19632
diff
changeset
|
3 Copyright (C) 1996-2015 John W. Eaton |
2376 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2376 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2376 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
3503 | 27 #include <iostream> |
2376 | 28 |
3933 | 29 #include "Cell.h" |
4358 | 30 #include "defun.h" |
2376 | 31 #include "error.h" |
4358 | 32 #include "gripes.h" |
15149
62a35ae7d6a2
use forward decls for mxArray in ov.h and ov-base.h
John W. Eaton <jwe@octave.org>
parents:
15057
diff
changeset
|
33 #include "mxarray.h" |
2979 | 34 #include "oct-lvalue.h" |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
35 #include "oct-hdf5.h" |
2376 | 36 #include "ov-struct.h" |
37 #include "unwind-prot.h" | |
6811 | 38 #include "utils.h" |
2948 | 39 #include "variables.h" |
2376 | 40 |
4750 | 41 #include "Array-util.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
42 #include "oct-locbuf.h" |
4750 | 43 |
4687 | 44 #include "byte-swap.h" |
20482
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20267
diff
changeset
|
45 #include "ls-oct-text.h" |
4687 | 46 #include "ls-oct-binary.h" |
47 #include "ls-hdf5.h" | |
48 #include "ls-utils.h" | |
5759 | 49 #include "pr-output.h" |
4687 | 50 |
2376 | 51 |
4612 | 52 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct"); |
2376 | 53 |
11474 | 54 // How many levels of structure elements should we print? |
55 static int Vstruct_levels_to_print = 2; | |
56 | |
57 // TRUE means print struct array contents, up to the number of levels | |
58 // specified by struct_levels_to_print. | |
59 static bool Vprint_struct_array_contents = false; | |
60 | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
61 octave_base_value * |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
62 octave_struct::try_narrowing_conversion (void) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
63 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
64 octave_base_value *retval = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
65 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
66 if (numel () == 1) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
67 retval = new octave_scalar_struct (map.checkelem (0)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
68 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
69 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
70 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
71 |
4513 | 72 Cell |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
73 octave_struct::dotref (const octave_value_list& idx, bool auto_add) |
2962 | 74 { |
4513 | 75 Cell retval; |
3933 | 76 |
77 assert (idx.length () == 1); | |
2962 | 78 |
3933 | 79 std::string nm = idx(0).string_value (); |
80 | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
81 octave_map::const_iterator p = map.seek (nm); |
2376 | 82 |
4219 | 83 if (p != map.end ()) |
3933 | 84 retval = map.contents (p); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
85 else if (auto_add) |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
86 retval = (numel () == 0) ? Cell (dim_vector (1, 1)) : Cell (dims ()); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
87 else |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
88 error_with_id ("Octave:invalid-indexing", |
15731
18f168880226
error_ids: Adding ids and documentation
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents:
15614
diff
changeset
|
89 "structure has no member '%s'", nm.c_str ()); |
2376 | 90 |
91 return retval; | |
92 } | |
93 | |
4513 | 94 #if 0 |
3933 | 95 static void |
10370
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
96 gripe_invalid_index1 (void) |
3933 | 97 { |
98 error ("invalid index for structure array"); | |
99 } | |
4513 | 100 #endif |
3933 | 101 |
102 static void | |
103 gripe_invalid_index_for_assignment (void) | |
104 { | |
105 error ("invalid index for structure array assignment"); | |
106 } | |
107 | |
108 static void | |
109 gripe_invalid_index_type (const std::string& nm, char t) | |
110 { | |
111 error ("%s cannot be indexed with %c", nm.c_str (), t); | |
112 } | |
113 | |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
114 static void |
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
115 gripe_failed_assignment (void) |
3933 | 116 { |
117 error ("assignment to structure element failed"); | |
118 } | |
119 | |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
120 static void |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
121 maybe_warn_invalid_field_name (const std::string& key, const char *who) |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
122 { |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
123 if (! valid_identifier (key)) |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
124 { |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
125 if (who) |
19886
e9a0bd0b125c
Rename 'matlab-incompatible' warning to 'language-extension'.
Carnë Draug <carandraug@octave.org>
parents:
19731
diff
changeset
|
126 warning_with_id ("Octave:language-extension", |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
127 "%s: invalid structure field name '%s'", |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
128 who, key.c_str ()); |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
129 else |
19886
e9a0bd0b125c
Rename 'matlab-incompatible' warning to 'language-extension'.
Carnë Draug <carandraug@octave.org>
parents:
19731
diff
changeset
|
130 warning_with_id ("Octave:language-extension", |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
131 "invalid structure field name '%s'", |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
132 key.c_str ()); |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
133 } |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
134 } |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
135 |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
136 octave_value_list |
4247 | 137 octave_struct::subsref (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
138 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
139 int nargout) |
3933 | 140 { |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
141 octave_value_list retval; |
3933 | 142 |
143 int skip = 1; | |
144 | |
145 switch (type[0]) | |
146 { | |
147 case '(': | |
148 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
149 if (type.length () > 1 && type[1] == '.') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
150 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
151 std::list<octave_value_list>::const_iterator p = idx.begin (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
152 octave_value_list key_idx = *++p; |
3933 | 153 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
154 const Cell tmp = dotref (key_idx); |
3933 | 155 |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
156 const Cell t = tmp.index (idx.front ()); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
157 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
158 retval(0) = (t.numel () == 1) ? t(0) : octave_value (t, true); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
159 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
160 // We handled two index elements, so tell |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
161 // next_subsref to skip both of them. |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
162 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
163 skip++; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
164 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
165 else |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
166 retval(0) = do_index_op (idx.front ()); |
3933 | 167 } |
168 break; | |
169 | |
170 case '.': | |
171 { | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14625
diff
changeset
|
172 if (map.numel () > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
173 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
174 const Cell t = dotref (idx.front ()); |
3933 | 175 |
20267
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20253
diff
changeset
|
176 retval(0) = (t.numel () == 1) ? t(0) : octave_value (t, true); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
177 } |
3933 | 178 } |
179 break; | |
180 | |
181 case '{': | |
182 gripe_invalid_index_type (type_name (), type[0]); | |
183 break; | |
184 | |
185 default: | |
186 panic_impossible (); | |
187 } | |
188 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
189 // FIXME: perhaps there should be an |
4994 | 190 // octave_value_list::next_subsref member function? See also |
191 // octave_user_function::subsref. | |
192 | |
193 if (idx.size () > 1) | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
194 retval = retval(0).next_subsref (nargout, type, idx, skip); |
3933 | 195 |
196 return retval; | |
197 } | |
198 | |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
199 octave_value |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
200 octave_struct::subsref (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
201 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
202 bool auto_add) |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
203 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
204 octave_value retval; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
205 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
206 int skip = 1; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
207 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
208 switch (type[0]) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
209 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
210 case '(': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
211 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
212 if (type.length () > 1 && type[1] == '.') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
213 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
214 std::list<octave_value_list>::const_iterator p = idx.begin (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
215 octave_value_list key_idx = *++p; |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
216 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
217 const Cell tmp = dotref (key_idx, auto_add); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
218 |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
219 const Cell t = tmp.index (idx.front (), auto_add); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
220 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
221 retval = (t.numel () == 1) ? t(0) : octave_value (t, true); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
222 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
223 // We handled two index elements, so tell |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
224 // next_subsref to skip both of them. |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
225 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
226 skip++; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
227 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
228 else |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
229 retval = do_index_op (idx.front (), auto_add); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
230 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
231 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
232 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
233 case '.': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
234 { |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14625
diff
changeset
|
235 if (map.numel () > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
236 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
237 const Cell t = dotref (idx.front (), auto_add); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
238 |
20267
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20253
diff
changeset
|
239 retval = (t.numel () == 1) ? t(0) : octave_value (t, true); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
240 } |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
241 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
242 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
243 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
244 case '{': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
245 gripe_invalid_index_type (type_name (), type[0]); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
246 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
247 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
248 default: |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
249 panic_impossible (); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
250 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
251 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
252 // FIXME: perhaps there should be an |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
253 // octave_value_list::next_subsref member function? See also |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
254 // octave_user_function::subsref. |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
255 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
256 if (idx.size () > 1) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
257 retval = retval.next_subsref (auto_add, type, idx, skip); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
258 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
259 return retval; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
260 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
261 |
8031
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
262 /* |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
263 %!test |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
264 %! x(1).a.a = 1; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
265 %! x(2).a.a = 2; |
8031
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
266 %! assert (size (x), [1, 2]); |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
267 %! assert (x(1).a.a, 1); |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
268 %! assert (x(2).a.a, 2); |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
269 */ |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
270 |
3933 | 271 octave_value |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
272 octave_struct::numeric_conv (const octave_value& val, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
273 const std::string& type) |
3933 | 274 { |
275 octave_value retval; | |
276 | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
277 if (type.length () > 0 && type[0] == '.' && ! val.is_map ()) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
278 retval = octave_map (); |
3933 | 279 else |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
280 retval = val; |
3933 | 281 |
282 return retval; | |
283 } | |
284 | |
285 octave_value | |
4247 | 286 octave_struct::subsasgn (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
287 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
288 const octave_value& rhs) |
2376 | 289 { |
3933 | 290 octave_value retval; |
291 | |
292 int n = type.length (); | |
293 | |
294 octave_value t_rhs = rhs; | |
295 | |
9286
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
296 if (idx.front ().empty ()) |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
297 { |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
298 error ("missing index in indexed assignment"); |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
299 return retval; |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
300 } |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
301 |
3933 | 302 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) |
303 { | |
304 switch (type[0]) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
305 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
306 case '(': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
307 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
308 if (type.length () > 1 && type[1] == '.') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
309 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
310 std::list<octave_value_list>::const_iterator p = idx.begin (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
311 octave_value_list t_idx = *p; |
3933 | 312 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
313 octave_value_list key_idx = *++p; |
4513 | 314 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
315 assert (key_idx.length () == 1); |
3933 | 316 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
317 std::string key = key_idx(0).string_value (); |
3933 | 318 |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
319 maybe_warn_invalid_field_name (key, "subsasgn"); |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
320 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
321 std::list<octave_value_list> next_idx (idx); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
322 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
323 // We handled two index elements, so subsasgn to |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
324 // needs to skip both of them. |
3933 | 325 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
326 next_idx.erase (next_idx.begin ()); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
327 next_idx.erase (next_idx.begin ()); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
328 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
329 std::string next_type = type.substr (2); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
330 |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
331 Cell tmpc (1, 1); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
332 octave_map::iterator pkey = map.seek (key); |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
333 if (pkey != map.end ()) |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
334 { |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
335 map.contents (pkey).make_unique (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
336 tmpc = map.contents (pkey).index (idx.front (), true); |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
337 } |
3933 | 338 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
339 // FIXME: better code reuse? |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
340 // cf. octave_cell::subsasgn and the case below. |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
341 if (tmpc.numel () == 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
342 { |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
343 octave_value& tmp = tmpc(0); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
344 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
345 bool orig_undefined = tmp.is_undefined (); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
346 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
347 if (orig_undefined || tmp.is_zero_by_zero ()) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
348 { |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
349 tmp = octave_value::empty_conv (next_type, rhs); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
350 tmp.make_unique (); // probably a no-op. |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
351 } |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
352 else |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
353 // optimization: ignore the copy |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
354 // still stored inside our map. |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
355 tmp.make_unique (1); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
356 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
357 t_rhs =(orig_undefined |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
358 ? tmp.undef_subsasgn (next_type, next_idx, rhs) |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
359 : tmp.subsasgn (next_type, next_idx, rhs)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
360 } |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
361 else |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
362 gripe_indexed_cs_list (); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
363 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
364 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
365 gripe_invalid_index_for_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
366 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
367 break; |
3933 | 368 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
369 case '.': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
370 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
371 octave_value_list key_idx = idx.front (); |
3933 | 372 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
373 assert (key_idx.length () == 1); |
3933 | 374 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
375 std::string key = key_idx(0).string_value (); |
3933 | 376 |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
377 maybe_warn_invalid_field_name (key, "subsasgn"); |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
378 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
379 std::list<octave_value_list> next_idx (idx); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
380 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
381 next_idx.erase (next_idx.begin ()); |
3933 | 382 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
383 std::string next_type = type.substr (1); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
384 |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
385 Cell tmpc (1, 1); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
386 octave_map::iterator pkey = map.seek (key); |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
387 if (pkey != map.end ()) |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
388 { |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
389 map.contents (pkey).make_unique (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
390 tmpc = map.contents (pkey); |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
391 } |
3933 | 392 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
393 // FIXME: better code reuse? |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
394 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
395 if (tmpc.numel () == 1) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
396 { |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
397 octave_value& tmp = tmpc(0); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
398 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
399 bool orig_undefined = tmp.is_undefined (); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
400 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
401 if (orig_undefined || tmp.is_zero_by_zero ()) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
402 { |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
403 tmp = octave_value::empty_conv (next_type, rhs); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
404 tmp.make_unique (); // probably a no-op. |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
405 } |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
406 else |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
407 // optimization: ignore the copy |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
408 // still stored inside our map. |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
409 tmp.make_unique (1); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
410 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
411 t_rhs = (orig_undefined |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
412 ? tmp.undef_subsasgn (next_type, next_idx, rhs) |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
413 : tmp.subsasgn (next_type, next_idx, rhs)); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
414 } |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
415 else |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
416 gripe_indexed_cs_list (); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
417 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
418 break; |
3933 | 419 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
420 case '{': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
421 gripe_invalid_index_type (type_name (), type[0]); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
422 break; |
3933 | 423 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
424 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
425 panic_impossible (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
426 } |
3933 | 427 } |
428 | |
429 if (! error_state) | |
430 { | |
431 switch (type[0]) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
432 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
433 case '(': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
434 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
435 if (n > 1 && type[1] == '.') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
436 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
437 std::list<octave_value_list>::const_iterator p = idx.begin (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
438 octave_value_list key_idx = *++p; |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
439 octave_value_list idxf = idx.front (); |
3933 | 440 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
441 assert (key_idx.length () == 1); |
3933 | 442 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
443 std::string key = key_idx(0).string_value (); |
3933 | 444 |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
445 maybe_warn_invalid_field_name (key, "subsasgn"); |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
446 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
447 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
448 { |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
449 if (t_rhs.is_cs_list ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
450 { |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
451 Cell tmp_cell = Cell (t_rhs.list_value ()); |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
452 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
453 // Inquire the proper shape of the RHS. |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
454 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
455 dim_vector didx = dims ().redim (idxf.length ()); |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
456 for (octave_idx_type k = 0; k < idxf.length (); k++) |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
457 if (! idxf(k).is_magic_colon ()) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
458 didx(k) = idxf(k).numel (); |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
459 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
460 if (didx.numel () == tmp_cell.numel ()) |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
461 tmp_cell = tmp_cell.reshape (didx); |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
462 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
463 |
10749
df1a3e0ebbff
important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10747
diff
changeset
|
464 map.assign (idxf, key, tmp_cell); |
3933 | 465 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
466 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
467 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
468 count++; |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
469 retval = octave_value (this); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
470 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
471 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
472 gripe_failed_assignment (); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
473 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
474 else |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
475 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
476 const octave_map& cmap = |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
477 const_cast<const octave_map &> (map); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
478 // cast to const reference, avoid forced key insertion. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
479 if (idxf.all_scalars () |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
480 || cmap.contents (key).index (idxf, true).numel () |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
481 == 1) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
482 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
483 map.assign (idxf, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
484 key, Cell (t_rhs.storable_value ())); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
485 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
486 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
487 count++; |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
488 retval = octave_value (this); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
489 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
490 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
491 gripe_failed_assignment (); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
492 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
493 else if (! error_state) |
10030
83bb2a78c07d
improve simple assignments error checks and messages
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
494 gripe_nonbraced_cs_list_assignment (); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
495 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
496 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
497 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
498 gripe_failed_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
499 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
500 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
501 { |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14625
diff
changeset
|
502 if (t_rhs.is_map () || t_rhs.is_object ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
503 { |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
504 octave_map rhs_map = t_rhs.map_value (); |
4197 | 505 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
506 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
507 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
508 map.assign (idx.front (), rhs_map); |
5592 | 509 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
510 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
511 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
512 count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
513 retval = octave_value (this); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
514 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
515 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
516 gripe_failed_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
517 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
518 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
519 error ("invalid structure assignment"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
520 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
521 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
522 { |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14625
diff
changeset
|
523 if (t_rhs.is_null_value ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
524 { |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14625
diff
changeset
|
525 map.delete_elements (idx.front ()); |
5592 | 526 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
527 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
528 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
529 count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
530 retval = octave_value (this); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
531 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
532 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
533 gripe_failed_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
534 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
535 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
536 error ("invalid structure assignment"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
537 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
538 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
539 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
540 break; |
3933 | 541 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
542 case '.': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
543 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
544 octave_value_list key_idx = idx.front (); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
545 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
546 assert (key_idx.length () == 1); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
547 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
548 std::string key = key_idx(0).string_value (); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
549 |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
550 maybe_warn_invalid_field_name (key, "subsasgn"); |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
551 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
552 if (t_rhs.is_cs_list ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
553 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
554 Cell tmp_cell = Cell (t_rhs.list_value ()); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
555 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
556 // The shape of the RHS is irrelevant, we just want |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
557 // the number of elements to agree and to preserve the |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
558 // shape of the left hand side of the assignment. |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
559 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
560 if (numel () == tmp_cell.numel ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
561 tmp_cell = tmp_cell.reshape (dims ()); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
562 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
563 map.setfield (key, tmp_cell); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
564 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
565 else |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
566 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
567 Cell tmp_cell(1, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
568 tmp_cell(0) = t_rhs.storable_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
569 map.setfield (key, tmp_cell); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
570 } |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
571 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
572 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
573 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
574 count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
575 retval = octave_value (this); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
576 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
577 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
578 gripe_failed_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
579 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
580 break; |
3933 | 581 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
582 case '{': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
583 gripe_invalid_index_type (type_name (), type[0]); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
584 break; |
3933 | 585 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
586 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
587 panic_impossible (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
588 } |
3933 | 589 } |
590 else | |
591 gripe_failed_assignment (); | |
592 | |
10754
92eb5fb58ebc
fix resize with structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10753
diff
changeset
|
593 retval.maybe_mutate (); |
92eb5fb58ebc
fix resize with structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10753
diff
changeset
|
594 |
3933 | 595 return retval; |
2376 | 596 } |
597 | |
7046 | 598 octave_value |
599 octave_struct::do_index_op (const octave_value_list& idx, bool resize_ok) | |
600 { | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
601 // octave_map handles indexing itself. |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8587
diff
changeset
|
602 return map.index (idx, resize_ok); |
7046 | 603 } |
604 | |
4791 | 605 size_t |
606 octave_struct::byte_size (void) const | |
607 { | |
608 // Neglect the size of the fieldnames. | |
609 | |
610 size_t retval = 0; | |
611 | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
612 for (octave_map::const_iterator p = map.begin (); p != map.end (); p++) |
4791 | 613 { |
614 std::string key = map.key (p); | |
615 | |
616 octave_value val = octave_value (map.contents (p)); | |
617 | |
618 retval += val.byte_size (); | |
619 } | |
620 | |
621 return retval; | |
622 } | |
623 | |
2376 | 624 void |
18416
bcd71a2531d3
Support disp/display overloading in classdef
Michael Goffioul <michael.goffioul@gmail.com>
parents:
18384
diff
changeset
|
625 octave_struct::print (std::ostream& os, bool) |
2901 | 626 { |
627 print_raw (os); | |
628 } | |
629 | |
630 void | |
3523 | 631 octave_struct::print_raw (std::ostream& os, bool) const |
2376 | 632 { |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
10030
diff
changeset
|
633 unwind_protect frame; |
2376 | 634 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
10030
diff
changeset
|
635 frame.protect_var (Vstruct_levels_to_print); |
2376 | 636 |
3961 | 637 if (Vstruct_levels_to_print >= 0) |
2376 | 638 { |
11474 | 639 bool max_depth_reached = Vstruct_levels_to_print-- == 0; |
3961 | 640 |
11474 | 641 bool print_fieldnames_only |
642 = (max_depth_reached || ! Vprint_struct_array_contents); | |
2376 | 643 |
2901 | 644 increment_indent_level (); |
2376 | 645 |
11474 | 646 newline (os); |
647 indent (os); | |
648 dim_vector dv = dims (); | |
649 os << dv.str () << " struct array containing the fields:"; | |
650 newline (os); | |
3932 | 651 |
11474 | 652 increment_indent_level (); |
4604 | 653 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
654 string_vector key_list = map.fieldnames (); |
5880 | 655 |
20267
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20253
diff
changeset
|
656 for (octave_idx_type i = 0; i < key_list.numel (); i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
657 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
658 std::string key = key_list[i]; |
5880 | 659 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
660 Cell val = map.contents (key); |
2376 | 661 |
11474 | 662 newline (os); |
3961 | 663 |
11474 | 664 if (print_fieldnames_only) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
665 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
666 indent (os); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
667 os << key; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
668 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
669 else |
11474 | 670 { |
671 octave_value tmp (val); | |
672 tmp.print_with_name (os, key); | |
673 } | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
674 } |
2376 | 675 |
11474 | 676 if (print_fieldnames_only) |
677 newline (os); | |
4604 | 678 |
2901 | 679 decrement_indent_level (); |
11474 | 680 decrement_indent_level (); |
2376 | 681 } |
682 else | |
2901 | 683 { |
3961 | 684 indent (os); |
685 os << "<structure>"; | |
2901 | 686 newline (os); |
687 } | |
2376 | 688 } |
689 | |
2901 | 690 bool |
3523 | 691 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901 | 692 { |
3961 | 693 bool retval = false; |
694 | |
2901 | 695 indent (os); |
3961 | 696 |
697 if (Vstruct_levels_to_print < 0) | |
698 os << name << " = "; | |
699 else | |
700 { | |
701 os << name << " ="; | |
702 newline (os); | |
703 retval = true; | |
704 } | |
705 | |
706 return retval; | |
2901 | 707 } |
708 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
709 static bool |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
710 scalar (const dim_vector& dims) |
4744 | 711 { |
20253
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
20216
diff
changeset
|
712 return dims.length () == 2 && dims(0) == 1 && dims(1) == 1; |
4744 | 713 } |
714 | |
715 | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
716 bool |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
717 octave_struct::save_ascii (std::ostream& os) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
718 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
719 octave_map m = map_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
720 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
721 octave_idx_type nf = m.nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
722 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
723 const dim_vector dv = dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
724 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
725 os << "# ndims: " << dv.length () << "\n"; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
726 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
727 for (int i = 0; i < dv.length (); i++) |
20253
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
20216
diff
changeset
|
728 os << " " << dv(i); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
729 os << "\n"; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
730 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
731 os << "# length: " << nf << "\n"; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
732 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
733 // Iterating over the list of keys will preserve the order of the |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
734 // fields. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
735 string_vector keys = m.fieldnames (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
736 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
737 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
738 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
739 std::string key = keys(i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
740 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
741 octave_value val = map.contents (key); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
742 |
20482
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20267
diff
changeset
|
743 bool b = save_text_data (os, val, key, false, 0); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
744 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
745 if (! b) |
18384
bd9d34f28b0f
Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
746 return ! os.fail (); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
747 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
748 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
749 return true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
750 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
751 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
752 bool |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
753 octave_struct::load_ascii (std::istream& is) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
754 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
755 octave_idx_type len = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
756 dim_vector dv (1, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
757 bool success = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
758 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
759 // KLUGE: earlier Octave versions did not save extra dimensions with struct, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
760 // and as a result did not preserve dimensions for empty structs. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
761 // The default dimensions were 1x1, which we want to preserve. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
762 string_vector keywords(2); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
763 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
764 keywords[0] = "ndims"; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
765 keywords[1] = "length"; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
766 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
767 std::string kw; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
768 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
769 if (extract_keyword (is, keywords, kw, len, true)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
770 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
771 if (kw == keywords[0]) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
772 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
773 int mdims = std::max (static_cast<int> (len), 2); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
774 dv.resize (mdims); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
775 for (int i = 0; i < mdims; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
776 is >> dv(i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
777 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
778 success = extract_keyword (is, keywords[1], len); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
779 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
780 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
781 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
782 success = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
783 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
784 if (success && len >= 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
785 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
786 if (len > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
787 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
788 octave_map m (dv); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
789 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
790 for (octave_idx_type j = 0; j < len; j++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
791 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
792 octave_value t2; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
793 bool dummy; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
794 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
795 // recurse to read cell elements |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
796 std::string nm |
20482
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20267
diff
changeset
|
797 = read_text_data (is, std::string (), dummy, t2, j); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
798 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
799 if (!is) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
800 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
801 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
802 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
803 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
804 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
805 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
806 error ("load: internal error loading struct elements"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
807 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
808 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
809 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
810 m.setfield (nm, tcell); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
811 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
812 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
813 if (is) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
814 map = m; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
815 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
816 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
817 error ("load: failed to load structure"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
818 success = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
819 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
820 } |
18712
6113e0c6920b
maint: Clean up extra spaces before/after parentheses.
Rik <rik@octave.org>
parents:
18538
diff
changeset
|
821 else if (len == 0) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
822 map = octave_map (dv); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
823 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
824 panic_impossible (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
825 } |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
826 else |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
827 { |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
828 error ("load: failed to extract number of elements in structure"); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
829 success = false; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
830 } |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
831 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
832 return success; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
833 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
834 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
835 bool |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
836 octave_struct::save_binary (std::ostream& os, bool& save_as_floats) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
837 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
838 octave_map m = map_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
839 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
840 octave_idx_type nf = m.nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
841 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
842 dim_vector d = dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
843 if (d.length () < 1) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
844 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
845 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
846 // Use negative value for ndims |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14625
diff
changeset
|
847 int32_t di = - d.length (); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
848 os.write (reinterpret_cast<char *> (&di), 4); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
849 for (int i = 0; i < d.length (); i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
850 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
851 di = d(i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
852 os.write (reinterpret_cast<char *> (&di), 4); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
853 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
854 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
855 int32_t len = nf; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
856 os.write (reinterpret_cast<char *> (&len), 4); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
857 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
858 // Iterating over the list of keys will preserve the order of the |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
859 // fields. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
860 string_vector keys = m.fieldnames (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
861 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
862 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
863 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
864 std::string key = keys(i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
865 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
866 octave_value val = map.contents (key); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
867 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
868 bool b = save_binary_data (os, val, key, "", 0, save_as_floats); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
869 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
870 if (! b) |
18384
bd9d34f28b0f
Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
871 return ! os.fail (); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
872 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
873 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
874 return true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
875 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
876 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
877 bool |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
878 octave_struct::load_binary (std::istream& is, bool swap, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
879 oct_mach_info::float_format fmt) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
880 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
881 bool success = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
882 int32_t len; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
883 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
884 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
885 if (swap) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
886 swap_bytes<4> (&len); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
887 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
888 dim_vector dv (1, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
889 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
890 if (len < 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
891 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
892 // We have explicit dimensions. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
893 int mdims = -len; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
894 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
895 int32_t di; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
896 dv.resize (mdims); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
897 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
898 for (int i = 0; i < mdims; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
899 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
900 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
901 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
902 if (swap) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
903 swap_bytes<4> (&di); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
904 dv(i) = di; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
905 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
906 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
907 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
908 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
909 if (swap) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
910 swap_bytes<4> (&len); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
911 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
912 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
913 if (len > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
914 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
915 octave_map m (dv); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
916 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
917 for (octave_idx_type j = 0; j < len; j++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
918 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
919 octave_value t2; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
920 bool dummy; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
921 std::string doc; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
922 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
923 // recurse to read cell elements |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
924 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
925 dummy, t2, doc); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
926 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
927 if (!is) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
928 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
929 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
930 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
931 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
932 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
933 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
934 error ("load: internal error loading struct elements"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
935 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
936 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
937 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
938 m.setfield (nm, tcell); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
939 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
940 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
941 if (is) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
942 map = m; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
943 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
944 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
945 error ("load: failed to load structure"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
946 success = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
947 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
948 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
949 else if (len == 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
950 map = octave_map (dv); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
951 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
952 success = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
953 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
954 return success; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
955 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
956 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
957 bool |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
958 octave_struct::save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats) |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
959 { |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
960 #if defined (HAVE_HDF5) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
961 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
962 hid_t data_hid = -1; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
963 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
964 #if HAVE_HDF5_18 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
965 data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
966 #else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
967 data_hid = H5Gcreate (loc_id, name, 0); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
968 #endif |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
969 if (data_hid < 0) return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
970 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
971 // recursively add each element of the structure to this group |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
972 octave_map m = map_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
973 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
974 octave_idx_type nf = m.nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
975 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
976 // Iterating over the list of keys will preserve the order of the |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
977 // fields. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
978 string_vector keys = m.fieldnames (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
979 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
980 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
981 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
982 std::string key = keys(i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
983 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
984 octave_value val = map.contents (key); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
985 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
986 bool retval2 = add_hdf5_data (data_hid, val, key, "", false, |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
987 save_as_floats); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
988 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
989 if (! retval2) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
990 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
991 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
992 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
993 H5Gclose (data_hid); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
994 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
995 return true; |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
996 |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
997 #else |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
998 gripe_save ("hdf5"); |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
999 return false; |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1000 #endif |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1001 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1002 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1003 bool |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1004 octave_struct::load_hdf5 (octave_hdf5_id loc_id, const char *name) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1005 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1006 bool retval = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1007 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1008 #if defined (HAVE_HDF5) |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1009 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1010 hdf5_callback_data dsub; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1011 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1012 herr_t retval2 = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1013 octave_map m (dim_vector (1, 1)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1014 int current_item = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1015 hsize_t num_obj = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1016 #if HAVE_HDF5_18 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1017 hid_t group_id = H5Gopen (loc_id, name, H5P_DEFAULT); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1018 #else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1019 hid_t group_id = H5Gopen (loc_id, name); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1020 #endif |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1021 H5Gget_num_objs (group_id, &num_obj); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1022 H5Gclose (group_id); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1023 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1024 // FIXME: fields appear to be sorted alphabetically on loading. |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1025 // Why is that happening? |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1026 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1027 while (current_item < static_cast<int> (num_obj) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1028 && (retval2 = H5Giterate (loc_id, name, ¤t_item, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1029 hdf5_read_next_data, &dsub)) > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1030 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1031 octave_value t2 = dsub.tc; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1032 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1033 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1034 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1035 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1036 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1037 error ("load: internal error loading struct elements"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1038 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1039 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1040 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1041 m.setfield (dsub.name, tcell); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1042 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1043 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1044 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1045 if (retval2 >= 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1046 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1047 map = m; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1048 retval = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1049 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1050 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1051 #else |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1052 gripe_load ("hdf5"); |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1053 #endif |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1054 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1055 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1056 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1057 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1058 mxArray * |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1059 octave_struct::as_mxArray (void) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1060 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1061 int nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1062 string_vector kv = map_keys (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1063 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1064 OCTAVE_LOCAL_BUFFER (const char *, f, nf); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1065 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1066 for (int i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1067 f[i] = kv[i].c_str (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1068 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1069 mxArray *retval = new mxArray (dims (), nf, f); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1070 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1071 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1072 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1073 mwSize nel = numel (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1074 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1075 mwSize ntot = nf * nel; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1076 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1077 for (int i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1078 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1079 Cell c = map.contents (kv[i]); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1080 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1081 const octave_value *p = c.data (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1082 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1083 mwIndex k = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1084 for (mwIndex j = i; j < ntot; j += nf) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1085 elts[j] = new mxArray (p[k++]); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1086 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1087 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1088 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1089 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1090 |
10760
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1091 octave_value |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1092 octave_struct::fast_elem_extract (octave_idx_type n) const |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1093 { |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1094 if (n < map.numel ()) |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1095 return map.checkelem (n); |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1096 else |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1097 return octave_value (); |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1098 } |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1099 |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1100 bool |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1101 octave_struct::fast_elem_insert (octave_idx_type n, |
10760
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1102 const octave_value& x) |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1103 { |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1104 bool retval = false; |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1105 |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1106 if (n < map.numel ()) |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1107 { |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1108 // To avoid copying the scalar struct, it just stores a pointer to |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1109 // itself. |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1110 const octave_scalar_map *sm_ptr; |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1111 void *here = reinterpret_cast<void *>(&sm_ptr); |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14625
diff
changeset
|
1112 return (x.get_rep ().fast_elem_insert_self (here, btyp_struct) |
10760
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1113 && map.fast_elem_insert (n, *sm_ptr)); |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1114 } |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1115 |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1116 return retval; |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1117 } |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1118 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1119 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_scalar_struct, "scalar struct", |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1120 "struct"); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1121 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1122 octave_value |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1123 octave_scalar_struct::dotref (const octave_value_list& idx, bool auto_add) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1124 { |
15613
126285fce876
check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15534
diff
changeset
|
1125 octave_value retval; |
126285fce876
check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15534
diff
changeset
|
1126 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1127 assert (idx.length () == 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1128 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1129 std::string nm = idx(0).string_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1130 |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
1131 maybe_warn_invalid_field_name (nm, "subsref"); |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
1132 |
15613
126285fce876
check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15534
diff
changeset
|
1133 retval = map.getfield (nm); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1134 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1135 if (! auto_add && retval.is_undefined ()) |
15731
18f168880226
error_ids: Adding ids and documentation
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents:
15614
diff
changeset
|
1136 error_with_id ("Octave:invalid-indexing", |
18f168880226
error_ids: Adding ids and documentation
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents:
15614
diff
changeset
|
1137 "structure has no member '%s'", nm.c_str ()); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1138 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1139 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1140 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1141 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1142 octave_value |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1143 octave_scalar_struct::subsref (const std::string& type, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1144 const std::list<octave_value_list>& idx) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1145 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1146 octave_value retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1147 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1148 if (type[0] == '.') |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1149 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1150 int skip = 1; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1151 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1152 retval = dotref (idx.front ()); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1153 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1154 if (idx.size () > 1) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1155 retval = retval.next_subsref (type, idx, skip); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1156 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1157 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1158 retval = to_array ().subsref (type, idx); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1159 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1160 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1161 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1162 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1163 octave_value_list |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1164 octave_scalar_struct::subsref (const std::string& type, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1165 const std::list<octave_value_list>& idx, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1166 int nargout) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1167 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1168 octave_value_list retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1169 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1170 if (type[0] == '.') |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1171 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1172 int skip = 1; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1173 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1174 retval(0) = dotref (idx.front ()); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1175 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1176 if (idx.size () > 1) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1177 retval = retval(0).next_subsref (nargout, type, idx, skip); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1178 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1179 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1180 retval = to_array ().subsref (type, idx, nargout); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1181 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1182 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1183 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1184 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1185 octave_value |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1186 octave_scalar_struct::subsref (const std::string& type, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1187 const std::list<octave_value_list>& idx, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1188 bool auto_add) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1189 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1190 octave_value retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1191 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1192 if (type[0] == '.') |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1193 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1194 int skip = 1; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1195 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1196 retval = dotref (idx.front (), auto_add); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1197 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1198 if (idx.size () > 1) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1199 retval = retval.next_subsref (auto_add, type, idx, skip); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1200 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1201 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1202 retval = to_array ().subsref (type, idx, auto_add); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1203 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1204 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1205 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1206 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1207 /* |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1208 %!test |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1209 %! x(1).a.a = 1; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1210 %! x(2).a.a = 2; |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1211 %! assert (size (x), [1, 2]); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1212 %! assert (x(1).a.a, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1213 %! assert (x(2).a.a, 2); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1214 */ |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1215 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1216 octave_value |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1217 octave_scalar_struct::numeric_conv (const octave_value& val, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1218 const std::string& type) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1219 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1220 octave_value retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1221 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1222 if (type.length () > 0 && type[0] == '.' && ! val.is_map ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1223 retval = octave_map (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1224 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1225 retval = val; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1226 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1227 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1228 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1229 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1230 octave_value |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1231 octave_scalar_struct::subsasgn (const std::string& type, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1232 const std::list<octave_value_list>& idx, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1233 const octave_value& rhs) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1234 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1235 octave_value retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1236 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1237 if (idx.front ().empty ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1238 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1239 error ("missing index in indexed assignment"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1240 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1241 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1242 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1243 if (type[0] == '.') |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1244 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1245 int n = type.length (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1246 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1247 octave_value t_rhs = rhs; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1248 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1249 octave_value_list key_idx = idx.front (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1250 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1251 assert (key_idx.length () == 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1252 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1253 std::string key = key_idx(0).string_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1254 |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
1255 maybe_warn_invalid_field_name (key, "subsasgn"); |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
1256 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1257 if (n > 1) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1258 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1259 std::list<octave_value_list> next_idx (idx); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1260 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1261 next_idx.erase (next_idx.begin ()); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1262 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1263 std::string next_type = type.substr (1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1264 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1265 octave_value tmp; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1266 octave_map::iterator pkey = map.seek (key); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1267 if (pkey != map.end ()) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1268 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1269 map.contents (pkey).make_unique (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1270 tmp = map.contents (pkey); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1271 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1272 |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1273 bool orig_undefined = tmp.is_undefined (); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1274 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1275 if (orig_undefined || tmp.is_zero_by_zero ()) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1276 { |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1277 tmp = octave_value::empty_conv (next_type, rhs); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1278 tmp.make_unique (); // probably a no-op. |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1279 } |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1280 else |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1281 // optimization: ignore the copy still stored inside our map. |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1282 tmp.make_unique (1); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1283 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1284 t_rhs = (orig_undefined |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1285 ? tmp.undef_subsasgn (next_type, next_idx, rhs) |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
1286 : tmp.subsasgn (next_type, next_idx, rhs)); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1287 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1288 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1289 if (! error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1290 map.setfield (key, t_rhs.storable_value ()); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1291 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1292 gripe_failed_assignment (); |
10749
df1a3e0ebbff
important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10747
diff
changeset
|
1293 |
df1a3e0ebbff
important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10747
diff
changeset
|
1294 count++; |
df1a3e0ebbff
important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10747
diff
changeset
|
1295 retval = this; |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1296 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1297 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1298 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1299 // Forward this case to octave_struct. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1300 octave_value tmp (new octave_struct (octave_map (map))); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1301 retval = tmp.subsasgn (type, idx, rhs); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1302 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1303 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1304 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1305 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1306 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1307 octave_value |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1308 octave_scalar_struct::do_index_op (const octave_value_list& idx, bool resize_ok) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1309 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1310 // octave_map handles indexing itself. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1311 return octave_map (map).index (idx, resize_ok); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1312 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1313 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1314 size_t |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1315 octave_scalar_struct::byte_size (void) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1316 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1317 // Neglect the size of the fieldnames. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1318 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1319 size_t retval = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1320 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1321 for (octave_map::const_iterator p = map.begin (); p != map.end (); p++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1322 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1323 std::string key = map.key (p); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1324 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1325 octave_value val = octave_value (map.contents (p)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1326 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1327 retval += val.byte_size (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1328 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1329 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1330 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1331 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1332 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1333 void |
18416
bcd71a2531d3
Support disp/display overloading in classdef
Michael Goffioul <michael.goffioul@gmail.com>
parents:
18384
diff
changeset
|
1334 octave_scalar_struct::print (std::ostream& os, bool) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1335 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1336 print_raw (os); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1337 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1338 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1339 void |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1340 octave_scalar_struct::print_raw (std::ostream& os, bool) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1341 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1342 unwind_protect frame; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1343 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1344 frame.protect_var (Vstruct_levels_to_print); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1345 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1346 if (Vstruct_levels_to_print >= 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1347 { |
11474 | 1348 bool max_depth_reached = Vstruct_levels_to_print-- == 0; |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1349 |
11474 | 1350 bool print_fieldnames_only = max_depth_reached; |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1351 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1352 increment_indent_level (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1353 |
13112
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12483
diff
changeset
|
1354 if (! Vcompact_format) |
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12483
diff
changeset
|
1355 newline (os); |
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12483
diff
changeset
|
1356 |
11474 | 1357 indent (os); |
1358 os << "scalar structure containing the fields:"; | |
1359 newline (os); | |
13112
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12483
diff
changeset
|
1360 if (! Vcompact_format) |
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12483
diff
changeset
|
1361 newline (os); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1362 |
11474 | 1363 increment_indent_level (); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1364 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1365 string_vector key_list = map.fieldnames (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1366 |
20267
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20253
diff
changeset
|
1367 for (octave_idx_type i = 0; i < key_list.numel (); i++) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1368 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1369 std::string key = key_list[i]; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1370 |
12143
ed129a03f93c
octave_scalar_struct::print_raw: avoid unnecessary conversion of map contents to Cell
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1371 octave_value val = map.contents (key); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1372 |
11474 | 1373 if (print_fieldnames_only) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1374 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1375 indent (os); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1376 os << key; |
12143
ed129a03f93c
octave_scalar_struct::print_raw: avoid unnecessary conversion of map contents to Cell
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1377 dim_vector dv = val.dims (); |
ed129a03f93c
octave_scalar_struct::print_raw: avoid unnecessary conversion of map contents to Cell
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1378 os << ": " << dv.str () << " " << val.type_name (); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1379 newline (os); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1380 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1381 else |
12143
ed129a03f93c
octave_scalar_struct::print_raw: avoid unnecessary conversion of map contents to Cell
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1382 val.print_with_name (os, key); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1383 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1384 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1385 decrement_indent_level (); |
11474 | 1386 decrement_indent_level (); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1387 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1388 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1389 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1390 indent (os); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1391 os << "<structure>"; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1392 newline (os); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1393 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1394 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1395 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1396 bool |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1397 octave_scalar_struct::print_name_tag (std::ostream& os, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1398 const std::string& name) const |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1399 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1400 bool retval = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1401 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1402 indent (os); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1403 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1404 if (Vstruct_levels_to_print < 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1405 os << name << " = "; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1406 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1407 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1408 os << name << " ="; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1409 newline (os); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1410 retval = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1411 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1412 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1413 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1414 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1415 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1416 bool |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1417 octave_scalar_struct::save_ascii (std::ostream& os) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1418 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1419 octave_map m = map_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1420 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1421 octave_idx_type nf = m.nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1422 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1423 const dim_vector dv = dims (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1424 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1425 os << "# ndims: " << dv.length () << "\n"; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1426 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1427 for (int i = 0; i < dv.length (); i++) |
20253
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
20216
diff
changeset
|
1428 os << " " << dv(i); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1429 os << "\n"; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1430 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1431 os << "# length: " << nf << "\n"; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1432 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1433 // Iterating over the list of keys will preserve the order of the |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1434 // fields. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1435 string_vector keys = m.fieldnames (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1436 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1437 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1438 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1439 std::string key = keys(i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1440 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1441 octave_value val = map.contents (key); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1442 |
20482
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20267
diff
changeset
|
1443 bool b = save_text_data (os, val, key, false, 0); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1444 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1445 if (! b) |
18384
bd9d34f28b0f
Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
1446 return ! os.fail (); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1447 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1448 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1449 return true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1450 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1451 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1452 bool |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1453 octave_scalar_struct::load_ascii (std::istream& is) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1454 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1455 bool success = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1456 octave_idx_type len = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1457 |
10753
bee1b1a2e29a
yield compatible dims from cell2struct + more fixes. build & tests OK
Jaroslav Hajek <highegg@gmail.com>
parents:
10751
diff
changeset
|
1458 if (extract_keyword (is, "length", len) && len >= 0) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1459 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1460 if (len > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1461 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1462 octave_scalar_map m; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1463 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1464 for (octave_idx_type j = 0; j < len; j++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1465 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1466 octave_value t2; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1467 bool dummy; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1468 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1469 // recurse to read cell elements |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1470 std::string nm |
20482
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20267
diff
changeset
|
1471 = read_text_data (is, std::string (), dummy, t2, j); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1472 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1473 if (!is) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1474 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1475 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1476 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1477 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1478 error ("load: internal error loading struct elements"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1479 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1480 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1481 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1482 m.setfield (nm, t2); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1483 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1484 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1485 if (is) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1486 map = m; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1487 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1488 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1489 error ("load: failed to load structure"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1490 success = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1491 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1492 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1493 else if (len == 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1494 map = octave_scalar_map (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1495 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1496 panic_impossible (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1497 } |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1498 else |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1499 { |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1500 error ("load: failed to extract number of elements in structure"); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1501 success = false; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1502 } |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1503 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1504 return success; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1505 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1506 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1507 bool |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1508 octave_scalar_struct::save_binary (std::ostream& os, bool& save_as_floats) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1509 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1510 octave_map m = map_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1511 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1512 octave_idx_type nf = m.nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1513 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1514 int32_t len = nf; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1515 os.write (reinterpret_cast<char *> (&len), 4); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1516 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1517 // Iterating over the list of keys will preserve the order of the |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1518 // fields. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1519 string_vector keys = m.fieldnames (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1520 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1521 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1522 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1523 std::string key = keys(i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1524 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1525 octave_value val = map.contents (key); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1526 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1527 bool b = save_binary_data (os, val, key, "", 0, save_as_floats); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1528 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1529 if (! b) |
18384
bd9d34f28b0f
Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
1530 return ! os.fail (); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1531 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1532 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1533 return true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1534 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1535 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1536 bool |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1537 octave_scalar_struct::load_binary (std::istream& is, bool swap, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1538 oct_mach_info::float_format fmt) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1539 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1540 bool success = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1541 int32_t len; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1542 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1543 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1544 if (swap) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1545 swap_bytes<4> (&len); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1546 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1547 dim_vector dv (1, 1); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1548 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1549 if (len > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1550 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1551 octave_scalar_map m; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1552 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1553 for (octave_idx_type j = 0; j < len; j++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1554 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1555 octave_value t2; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1556 bool dummy; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1557 std::string doc; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1558 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1559 // recurse to read cell elements |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1560 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1561 dummy, t2, doc); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1562 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1563 if (!is) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1564 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1565 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1566 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1567 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1568 error ("load: internal error loading struct elements"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1569 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1570 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1571 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1572 m.setfield (nm, t2); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1573 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1574 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1575 if (is) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1576 map = m; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1577 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1578 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1579 error ("load: failed to load structure"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1580 success = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1581 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1582 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1583 else if (len == 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1584 map = octave_scalar_map (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1585 else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1586 success = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1587 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1588 return success; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1589 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1590 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1591 bool |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1592 octave_scalar_struct::save_hdf5 (octave_hdf5_id loc_id, const char *name, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1593 bool save_as_floats) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1594 { |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1595 #if defined (HAVE_HDF5) |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1596 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1597 hid_t data_hid = -1; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1598 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1599 #if HAVE_HDF5_18 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1600 data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1601 #else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1602 data_hid = H5Gcreate (loc_id, name, 0); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1603 #endif |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1604 if (data_hid < 0) return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1605 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1606 // recursively add each element of the structure to this group |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1607 octave_scalar_map m = scalar_map_value (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1608 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1609 octave_idx_type nf = m.nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1610 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1611 // Iterating over the list of keys will preserve the order of the |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1612 // fields. |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1613 string_vector keys = m.fieldnames (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1614 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1615 for (octave_idx_type i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1616 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1617 std::string key = keys(i); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1618 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1619 octave_value val = map.contents (key); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1620 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1621 bool retval2 = add_hdf5_data (data_hid, val, key, "", false, |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1622 save_as_floats); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1623 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1624 if (! retval2) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1625 break; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1626 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1627 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1628 H5Gclose (data_hid); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1629 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1630 return true; |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1631 |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1632 #else |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1633 gripe_save ("hdf5"); |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1634 return false; |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1635 #endif |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1636 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1637 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1638 bool |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1639 octave_scalar_struct::load_hdf5 (octave_hdf5_id loc_id, const char *name) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1640 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1641 bool retval = false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1642 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1643 #if defined (HAVE_HDF5) |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1644 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1645 hdf5_callback_data dsub; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1646 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1647 herr_t retval2 = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1648 octave_scalar_map m; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1649 int current_item = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1650 hsize_t num_obj = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1651 #if HAVE_HDF5_18 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1652 hid_t group_id = H5Gopen (loc_id, name, H5P_DEFAULT); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1653 #else |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1654 hid_t group_id = H5Gopen (loc_id, name); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1655 #endif |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1656 H5Gget_num_objs (group_id, &num_obj); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1657 H5Gclose (group_id); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1658 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1659 // FIXME: fields appear to be sorted alphabetically on loading. |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1660 // Why is that happening? |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1661 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1662 while (current_item < static_cast<int> (num_obj) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1663 && (retval2 = H5Giterate (loc_id, name, ¤t_item, |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1664 hdf5_read_next_data, &dsub)) > 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1665 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1666 octave_value t2 = dsub.tc; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1667 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1668 if (error_state) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1669 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1670 error ("load: internal error loading struct elements"); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1671 return false; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1672 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1673 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1674 m.setfield (dsub.name, t2); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1675 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1676 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1677 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1678 if (retval2 >= 0) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1679 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1680 map = m; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1681 retval = true; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1682 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1683 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1684 #else |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1685 gripe_load ("hdf5"); |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1686 #endif |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19886
diff
changeset
|
1687 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1688 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1689 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1690 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1691 mxArray * |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1692 octave_scalar_struct::as_mxArray (void) const |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1693 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1694 int nf = nfields (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1695 string_vector kv = map_keys (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1696 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1697 OCTAVE_LOCAL_BUFFER (const char *, f, nf); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1698 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1699 for (int i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1700 f[i] = kv[i].c_str (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1701 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1702 mxArray *retval = new mxArray (dims (), nf, f); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1703 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1704 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1705 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1706 mwSize nel = numel (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1707 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1708 mwSize ntot = nf * nel; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1709 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1710 for (int i = 0; i < nf; i++) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1711 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1712 Cell c = map.contents (kv[i]); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1713 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1714 const octave_value *p = c.data (); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1715 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1716 mwIndex k = 0; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1717 for (mwIndex j = i; j < ntot; j += nf) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1718 elts[j] = new mxArray (p[k++]); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1719 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1720 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1721 return retval; |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1722 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1723 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1724 |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1725 octave_value |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1726 octave_scalar_struct::to_array (void) |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1727 { |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1728 return new octave_struct (octave_map (map)); |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1729 } |
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1730 |
10760
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1731 bool |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1732 octave_scalar_struct::fast_elem_insert_self (void *where, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1733 builtin_type_t btyp) const |
10760
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1734 { |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1735 |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1736 if (btyp == btyp_struct) |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1737 { |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1738 *(reinterpret_cast<const octave_scalar_map **>(where)) = ↦ |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1739 return true; |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1740 } |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1741 else |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1742 return false; |
76079e505f9d
optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents:
10754
diff
changeset
|
1743 } |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1744 |
4744 | 1745 DEFUN (struct, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1746 "-*- texinfo -*-\n\ |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
1747 @deftypefn {Built-in Function} {@var{s} =} struct ()\n\ |
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
1748 @deftypefnx {Built-in Function} {@var{s} =} struct (@var{field1}, @var{value1}, @var{field2}, @var{value2}, @dots{})\n\ |
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
1749 @deftypefnx {Built-in Function} {@var{s} =} struct (@var{obj})\n\ |
4744 | 1750 \n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1751 Create a scalar or array structure and initialize its values.\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1752 \n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1753 The @var{field1}, @var{field2}, @dots{} variables are strings specifying the\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1754 names of the fields and the @var{value1}, @var{value2}, @dots{} variables\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1755 can be of any type.\n\ |
4744 | 1756 \n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1757 If the values are cell arrays, create a structure array and initialize its\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1758 values. The dimensions of each cell array of values must match. Singleton\n\ |
20216
aa36fb998a4d
maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents:
20198
diff
changeset
|
1759 cells and non-cell values are repeated so that they fill the entire array. \n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1760 If the cells are empty, create an empty structure array with the specified\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1761 field names.\n\ |
9190 | 1762 \n\ |
1763 If the argument is an object, return the underlying struct.\n\ | |
15782
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1764 \n\ |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
15782
diff
changeset
|
1765 Observe that the syntax is optimized for struct @strong{arrays}. Consider\n\ |
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
15782
diff
changeset
|
1766 the following examples:\n\ |
15782
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1767 \n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1768 @example\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1769 @group\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1770 struct (\"foo\", 1)\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1771 @result{} scalar structure containing the fields:\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1772 foo = 1\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1773 \n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1774 struct (\"foo\", @{@})\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1775 @result{} 0x0 struct array containing the fields:\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1776 foo\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1777 \n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1778 struct (\"foo\", @{ @{@} @})\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1779 @result{} scalar structure containing the fields:\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1780 foo = @{@}(0x0)\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1781 \n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1782 struct (\"foo\", @{1, 2, 3@})\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1783 @result{} 1x3 struct array containing the fields:\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1784 foo\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1785 \n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1786 @end group\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1787 @end example\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1788 \n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1789 @noindent\n\ |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
1790 The first case is an ordinary scalar struct---one field, one value. The\n\ |
15782
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1791 second produces an empty struct array with one field and no values, since\n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
1792 being passed an empty cell array of struct array values. When the value is\n\ |
15782
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1793 a cell array containing a single entry, this becomes a scalar struct with\n\ |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
15782
diff
changeset
|
1794 that single entry as the value of the field. That single entry happens\n\ |
15782
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1795 to be an empty cell array.\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1796 \n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1797 Finally, if the value is a non-scalar cell array, then @code{struct}\n\ |
10ed43563df5
doc: overhaul struct docstring
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15781
diff
changeset
|
1798 produces a struct @strong{array}.\n\ |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
1799 @seealso{cell2struct, fieldnames, getfield, setfield, rmfield, isfield, orderfields, isstruct, structfun}\n\ |
4911 | 1800 @end deftypefn") |
4744 | 1801 { |
6946 | 1802 octave_value retval; |
4744 | 1803 |
1804 int nargin = args.length (); | |
1805 | |
5444 | 1806 // struct ([]) returns an empty struct. |
1807 | |
1808 // struct (empty_matrix) returns an empty struct with the same | |
1809 // dimensions as the empty matrix. | |
1810 | |
1811 // Note that struct () creates a 1x1 struct with no fields for | |
1812 // compatibility with Matlab. | |
4744 | 1813 |
10747
58c1b5402588
fix a showstopping bug in octave_fields reference counting
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
1814 if (nargin == 1 && args(0).is_map ()) |
58c1b5402588
fix a showstopping bug in octave_fields reference counting
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
1815 return args(0); |
58c1b5402588
fix a showstopping bug in octave_fields reference counting
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
1816 |
9190 | 1817 if (nargin == 1 && args(0).is_object ()) |
1818 { | |
10747
58c1b5402588
fix a showstopping bug in octave_fields reference counting
Jaroslav Hajek <highegg@gmail.com>
parents:
10742
diff
changeset
|
1819 retval = args(0).map_value (); |
9190 | 1820 |
1821 return retval; | |
1822 } | |
1823 | |
6946 | 1824 if ((nargin == 1 || nargin == 2) |
1825 && args(0).is_empty () && args(0).is_real_matrix ()) | |
1826 { | |
1827 Cell fields; | |
1828 | |
1829 if (nargin == 2) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1830 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1831 if (args(1).is_cellstr ()) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1832 retval = octave_map (args(0).dims (), args(1).cellstr_value ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1833 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1834 error ("struct: expecting cell array of field names as second argument"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1835 } |
6946 | 1836 else |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1837 retval = octave_map (args(0).dims ()); |
6946 | 1838 |
1839 return retval; | |
1840 } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1841 |
4744 | 1842 // Check for "field", VALUE pairs. |
1843 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1844 for (int i = 0; i < nargin; i += 2) |
4744 | 1845 { |
1846 if (! args(i).is_string () || i + 1 >= nargin) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1847 { |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12181
diff
changeset
|
1848 error ("struct: expecting alternating \"field\", VALUE pairs"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1849 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1850 } |
4744 | 1851 } |
1852 | |
1853 // Check that the dimensions of the values correspond. | |
1854 | |
1855 dim_vector dims (1, 1); | |
1856 | |
1857 int first_dimensioned_value = 0; | |
1858 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1859 for (int i = 1; i < nargin; i += 2) |
4744 | 1860 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1861 if (args(i).is_cell ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1862 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1863 dim_vector argdims (args(i).dims ()); |
4744 | 1864 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1865 if (! scalar (argdims)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1866 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1867 if (! first_dimensioned_value) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1868 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1869 dims = argdims; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1870 first_dimensioned_value = i + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1871 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1872 else if (dims != argdims) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1873 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1874 error ("struct: dimensions of parameter %d do not match those of parameter %d", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1875 first_dimensioned_value, i+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1876 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1877 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1878 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1879 } |
4744 | 1880 } |
1881 | |
1882 // Create the return value. | |
1883 | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1884 octave_map map (dims); |
4744 | 1885 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1886 for (int i = 0; i < nargin; i+= 2) |
4744 | 1887 { |
1888 // Get key. | |
1889 | |
1890 std::string key (args(i).string_value ()); | |
1891 | |
15614
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
1892 maybe_warn_invalid_field_name (key, "struct"); |
f2b8f90052fd
warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents:
15613
diff
changeset
|
1893 |
4744 | 1894 // Value may be v, { v }, or { v1, v2, ... } |
1895 // In the first two cases, we need to create a cell array of | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1896 // the appropriate dimensions filled with v. In the last case, |
4744 | 1897 // the cell array has already been determined to be of the |
1898 // correct dimensions. | |
1899 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1900 if (args(i+1).is_cell ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1901 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1902 const Cell c (args(i+1).cell_value ()); |
4744 | 1903 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1904 if (scalar (c.dims ())) |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1905 map.setfield (key, Cell (dims, c(0))); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1906 else |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1907 map.setfield (key, c); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1908 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1909 else |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
1910 map.setfield (key, Cell (dims, args(i+1))); |
4744 | 1911 } |
6946 | 1912 |
4744 | 1913 return octave_value (map); |
1914 } | |
1915 | |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1916 /* |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1917 %!shared x |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1918 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1919 %!assert (struct ("a",1, "b",3), x(1)) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1920 %!assert (isempty (x([]))) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1921 %!assert (isempty (struct ("a",{}, "b",{}))) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1922 %!assert (struct ("a",{1,2}, "b",{3,3}), x) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1923 %!assert (struct ("a",{1,2}, "b",3), x) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1924 %!assert (struct ("a",{1,2}, "b",{3}), x) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1925 %!assert (struct ("b",3, "a",{1,2}), x) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1926 %!assert (struct ("b",{3}, "a",{1,2}), x) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1927 %!test x = struct ([]); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1928 %!assert (size (x), [0,0]) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1929 %!assert (isstruct (x)) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1930 %!assert (isempty (fieldnames (x))) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1931 %!fail ('struct ("a",{1,2},"b",{1,2,3})', 'dimensions of parameter 2 do not match those of parameter 4') |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1932 %!fail ('struct (1,2,3,4)', 'struct: expecting alternating "field", VALUE pairs') |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1933 %!fail ('struct ("1",2,"3")', 'struct: expecting alternating "field", VALUE pairs') |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1934 */ |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
1935 |
4358 | 1936 DEFUN (isstruct, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1937 "-*- texinfo -*-\n\ |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
11359
diff
changeset
|
1938 @deftypefn {Built-in Function} {} isstruct (@var{x})\n\ |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
11359
diff
changeset
|
1939 Return true if @var{x} is a structure or a structure array.\n\ |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
11359
diff
changeset
|
1940 @seealso{ismatrix, iscell, isa}\n\ |
4358 | 1941 @end deftypefn") |
1942 { | |
1943 octave_value retval; | |
1944 | |
1945 if (args.length () == 1) | |
1946 retval = args(0).is_map (); | |
1947 else | |
5823 | 1948 print_usage (); |
4358 | 1949 |
1950 return retval; | |
1951 } | |
1952 | |
15781
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1953 DEFUN (__fieldnames__, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1954 "-*- texinfo -*-\n\ |
15781
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1955 @deftypefn {Built-in Function} {} __fieldnames__ (@var{struct})\n\ |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1956 @deftypefnx {Built-in Function} {} __fieldnames__ (@var{obj})\n\ |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1957 Internal function.\n\ |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1958 \n\ |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1959 Implements @code{fieldnames()} for structures and Octave objects.\n\ |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1960 @seealso{fieldnames}\n\ |
4358 | 1961 @end deftypefn") |
1962 { | |
1963 octave_value retval; | |
1964 | |
15781
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1965 // Input validation has already been done in fieldnames.m. |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1966 octave_value arg = args(0); |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1967 |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1968 octave_map m = arg.map_value (); |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1969 |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1970 string_vector keys = m.fieldnames (); |
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1971 |
20267
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20253
diff
changeset
|
1972 if (keys.numel () == 0) |
15781
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1973 retval = Cell (0, 1); |
4358 | 1974 else |
15781
c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents:
15731
diff
changeset
|
1975 retval = Cell (keys); |
4358 | 1976 |
1977 return retval; | |
1978 } | |
1979 | |
1980 DEFUN (isfield, args, , | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1981 "-*- texinfo -*-\n\ |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
1982 @deftypefn {Built-in Function} {} isfield (@var{x}, \"@var{name}\")\n\ |
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
1983 @deftypefnx {Built-in Function} {} isfield (@var{x}, @var{name})\n\ |
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
1984 Return true if the @var{x} is a structure and it includes an element named\n\ |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
1985 @var{name}.\n\ |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
1986 \n\ |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
1987 If @var{name} is a cell array of strings then a logical array of equal\n\ |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
1988 dimension is returned.\n\ |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
1989 @seealso{fieldnames}\n\ |
4358 | 1990 @end deftypefn") |
1991 { | |
1992 octave_value retval; | |
1993 | |
1994 int nargin = args.length (); | |
1995 | |
1996 if (nargin == 2) | |
1997 { | |
1998 retval = false; | |
1999 | |
10691
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2000 if (args(0).is_map ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
2001 { |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
2002 octave_map m = args(0).map_value (); |
4358 | 2003 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2004 // FIXME: should this work for all types that can do |
10691
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2005 // structure reference operations? |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2006 |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2007 if (args(1).is_string ()) |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2008 { |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2009 std::string key = args(1).string_value (); |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2010 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
2011 retval = m.isfield (key); |
10691
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2012 } |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2013 else if (args(1).is_cell ()) |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2014 { |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2015 Cell c = args(1).cell_value (); |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
2016 boolNDArray bm (c.dims ()); |
10691
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2017 octave_idx_type n = bm.numel (); |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2018 |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2019 for (octave_idx_type i = 0; i < n; i++) |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2020 { |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2021 if (c(i).is_string ()) |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2022 { |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2023 std::string key = c(i).string_value (); |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2024 |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
2025 bm(i) = m.isfield (key); |
10691
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2026 } |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2027 else |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2028 bm(i) = false; |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2029 } |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2030 |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2031 retval = bm; |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
2032 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
2033 } |
4358 | 2034 } |
2035 else | |
5823 | 2036 print_usage (); |
4358 | 2037 |
2038 return retval; | |
2039 } | |
2040 | |
18538
fcd87f68af4f
Deprecate nfields and replace with numfields.
Rik <rik@octave.org>
parents:
18537
diff
changeset
|
2041 DEFUN (numfields, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2042 "-*- texinfo -*-\n\ |
18538
fcd87f68af4f
Deprecate nfields and replace with numfields.
Rik <rik@octave.org>
parents:
18537
diff
changeset
|
2043 @deftypefn {Built-in Function} {} numfields (@var{s})\n\ |
9784 | 2044 Return the number of fields of the structure @var{s}.\n\ |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
2045 @seealso{fieldnames}\n\ |
9784 | 2046 @end deftypefn") |
2047 { | |
2048 octave_value retval; | |
2049 | |
2050 int nargin = args.length (); | |
2051 | |
2052 if (nargin == 1 && args(0).is_map ()) | |
2053 { | |
2054 retval = static_cast<double> (args(0).nfields ()); | |
2055 } | |
2056 else | |
2057 print_usage (); | |
2058 | |
2059 return retval; | |
2060 } | |
2061 | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2062 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2063 ## test isfield |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2064 %!test |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2065 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2066 %! assert (isfield (x, "b")); |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
2067 %!assert (isfield (struct ("a", "1"), "a")) |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2068 %!assert (isfield ({1}, "c"), false) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
2069 %!assert (isfield (struct ("a", "1"), 10), false) |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
2070 %!assert (isfield (struct ("a", "b"), "a "), false) |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
2071 %!assert (isfield (struct ("a", 1, "b", 2), {"a", "c"}), [true, false]) |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2072 */ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2073 |
4750 | 2074 DEFUN (cell2struct, args, , |
2075 "-*- texinfo -*-\n\ | |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
2076 @deftypefn {Built-in Function} {} cell2struct (@var{cell}, @var{fields})\n\ |
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
2077 @deftypefnx {Built-in Function} {} cell2struct (@var{cell}, @var{fields}, @var{dim})\n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2078 Convert @var{cell} to a structure.\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2079 \n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2080 The number of fields in @var{fields} must match the number of elements in\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2081 @var{cell} along dimension @var{dim}, that is\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2082 @code{numel (@var{fields}) == size (@var{cell}, @var{dim})}. If @var{dim}\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2083 is omitted, a value of 1 is assumed.\n\ |
4750 | 2084 \n\ |
2085 @example\n\ | |
2086 @group\n\ | |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
2087 A = cell2struct (@{\"Peter\", \"Hannah\", \"Robert\";\n\ |
7031 | 2088 185, 170, 168@},\n\ |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
2089 @{\"Name\",\"Height\"@}, 1);\n\ |
4750 | 2090 A(1)\n\ |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
2091 @result{}\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
2092 @{\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
2093 Name = Peter\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
2094 Height = 185\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
2095 @}\n\ |
4750 | 2096 \n\ |
2097 @end group\n\ | |
2098 @end example\n\ | |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
2099 @seealso{struct2cell, cell2mat, struct}\n\ |
4750 | 2100 @end deftypefn") |
2101 { | |
2102 octave_value retval; | |
2103 | |
13868
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2104 int nargin = args.length (); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2105 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2106 if (nargin == 2 || nargin == 3) |
4750 | 2107 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2108 if (! args(0).is_cell ()) |
13868
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2109 { |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2110 error ("cell2struct: argument CELL must be of type cell"); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2111 return retval; |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2112 } |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2113 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2114 if (! (args(1).is_cellstr () || args(1).is_char_matrix ())) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
2115 { |
13868
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2116 error ("cell2struct: FIELDS must be a cell array of strings or a character matrix"); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2117 return retval; |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2118 } |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2119 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2120 const Cell vals = args(0).cell_value (); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2121 const Array<std::string> fields = args(1).cellstr_value (); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2122 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2123 octave_idx_type ext = 0; |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2124 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2125 int dim = 0; |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2126 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2127 if (nargin == 3) |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2128 { |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2129 if (args(2).is_real_scalar ()) |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2130 dim = nargin == 2 ? 0 : args(2).int_value () - 1; |
10751
717ba2c3eef1
rewrite cell2struct, 1 failing test
Jaroslav Hajek <highegg@gmail.com>
parents:
10749
diff
changeset
|
2131 else |
717ba2c3eef1
rewrite cell2struct, 1 failing test
Jaroslav Hajek <highegg@gmail.com>
parents:
10749
diff
changeset
|
2132 { |
13868
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2133 error ("cell2struct: DIM must be a real scalar"); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2134 return retval; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
2135 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
2136 } |
13868
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2137 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2138 if (dim < 0) |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2139 { |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2140 error ("cell2struct: DIM must be a valid dimension"); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2141 return retval; |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2142 } |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2143 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2144 ext = vals.ndims () > dim ? vals.dims ()(dim) : 1; |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2145 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2146 if (ext != fields.numel ()) |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2147 { |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2148 error ("cell2struct: number of FIELDS does not match dimension"); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2149 return retval; |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2150 } |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2151 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2152 int nd = std::max (dim+1, vals.ndims ()); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2153 // result dimensions. |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2154 dim_vector rdv = vals.dims ().redim (nd); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2155 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2156 assert (ext == rdv(dim)); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2157 if (nd == 2) |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2158 { |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2159 rdv(0) = rdv(1-dim); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2160 rdv(1) = 1; |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2161 } |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2162 else |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2163 { |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2164 for (int i = dim + 1; i < nd; i++) |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2165 rdv(i-1) = rdv(i); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2166 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2167 rdv.resize (nd-1); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2168 } |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2169 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2170 octave_map map (rdv); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2171 Array<idx_vector> ia (dim_vector (nd, 1), idx_vector::colon); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2172 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2173 for (octave_idx_type i = 0; i < ext; i++) |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2174 { |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2175 ia(dim) = i; |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2176 map.setfield (fields(i), vals.index (ia).reshape (rdv)); |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2177 } |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2178 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2179 retval = map; |
4750 | 2180 } |
4751 | 2181 else |
5823 | 2182 print_usage (); |
4750 | 2183 |
2184 return retval; | |
2185 } | |
2186 | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2187 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2188 ## test cell2struct versus struct2cell |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2189 %!test |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2190 %! keys = cellstr (char (floor (rand (100,10)*24+65)))'; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2191 %! vals = mat2cell (rand (100,1), ones (100,1), 1)'; |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2192 %! s = struct ([keys; vals]{:}); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2193 %! t = cell2struct (vals, keys, 2); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2194 %! assert (s, t); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2195 %! assert (struct2cell (s), vals'); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2196 %! assert (fieldnames (s), keys'); |
13868
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2197 |
87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents:
13706
diff
changeset
|
2198 %!assert (cell2struct ({1; 2}, {"a"; "b"}), struct ("a", 1, "b", 2)); |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
2199 |
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
2200 %!assert (cell2struct ({}, {"f"}, 3), struct ("f", {})); |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2201 */ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2202 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2203 |
4817 | 2204 // So we can call Fcellstr directly. |
2205 extern octave_value_list Fcellstr (const octave_value_list& args, int); | |
2206 | |
2207 DEFUN (rmfield, args, , | |
2208 "-*- texinfo -*-\n\ | |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
2209 @deftypefn {Built-in Function} {@var{sout} =} rmfield (@var{s}, \"@var{f}\")\n\ |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
2210 @deftypefnx {Built-in Function} {@var{sout} =} rmfield (@var{s}, @var{f})\n\ |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
2211 Return a @emph{copy} of the structure (array) @var{s} with the field @var{f}\n\ |
19310
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
2212 removed.\n\ |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
2213 \n\ |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
2214 If @var{f} is a cell array of strings or a character array, remove each of\n\ |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
2215 the named fields.\n\ |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18712
diff
changeset
|
2216 @seealso{orderfields, fieldnames, isfield}\n\ |
5642 | 2217 @end deftypefn") |
4817 | 2218 { |
2219 octave_value retval; | |
2220 | |
2221 int nargin = args.length (); | |
2222 | |
2223 if (nargin == 2) | |
2224 { | |
10742
604e13a89c7f
initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10691
diff
changeset
|
2225 octave_map m = args(0).map_value (); |
4817 | 2226 |
2227 octave_value_list fval = Fcellstr (args(1), 1); | |
2228 | |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2229 Cell fcell = fval(0).cell_value (); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2230 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2231 for (int i = 0; i < fcell.numel (); i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
2232 { |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2233 std::string key = fcell(i).string_value (); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2234 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2235 if (m.isfield (key)) |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2236 m.rmfield (key); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2237 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
2238 { |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2239 error ("rmfield: structure does not contain field %s", |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2240 key.c_str ()); |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2241 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2242 break; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
2243 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
2244 } |
20593
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2245 |
c41595061186
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20482
diff
changeset
|
2246 retval = m; |
4817 | 2247 } |
2248 else | |
5823 | 2249 print_usage (); |
4817 | 2250 |
2251 return retval; | |
2252 } | |
2253 | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2254 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2255 ## test rmfield |
15534
360adb4a3136
doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
2256 %!shared x |
360adb4a3136
doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
2257 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; x(6).f="abc123"; |
360adb4a3136
doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
2258 %! |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2259 %!test |
15534
360adb4a3136
doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
2260 %! y = rmfield (x, "c"); |
360adb4a3136
doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
2261 %! assert (fieldnames (y), {"d"; "a"; "b"; "f"}); |
360adb4a3136
doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
2262 %! assert (size (y), [1, 6]); |
360adb4a3136
doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
2263 %!test |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2264 %! y = rmfield (x, {"a", "f"}); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2265 %! assert (fieldnames (y), {"d"; "b"; "c"}); |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
2266 %! assert (size (y), [1, 6]); |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2267 */ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
2268 |
11474 | 2269 DEFUN (struct_levels_to_print, args, nargout, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2270 "-*- texinfo -*-\n\ |
11474 | 2271 @deftypefn {Built-in Function} {@var{val} =} struct_levels_to_print ()\n\ |
2272 @deftypefnx {Built-in Function} {@var{old_val} =} struct_levels_to_print (@var{new_val})\n\ | |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
13868
diff
changeset
|
2273 @deftypefnx {Built-in Function} {} struct_levels_to_print (@var{new_val}, \"local\")\n\ |
11474 | 2274 Query or set the internal variable that specifies the number of\n\ |
2275 structure levels to display.\n\ | |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
13868
diff
changeset
|
2276 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
2277 When called from inside a function with the @qcode{\"local\"} option, the\n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2278 variable is changed locally for the function and any subroutines it calls.\n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
2279 The original variable value is restored when exiting the function.\n\ |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
2280 @seealso{print_struct_array_contents}\n\ |
11474 | 2281 @end deftypefn") |
2282 { | |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
2283 return SET_INTERNAL_VARIABLE_WITH_LIMITS (struct_levels_to_print, -1, |
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
2284 std::numeric_limits<int>::max ()); |
11474 | 2285 } |
2286 | |
2287 DEFUN (print_struct_array_contents, args, nargout, | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2288 "-*- texinfo -*-\n\ |
11474 | 2289 @deftypefn {Built-in Function} {@var{val} =} print_struct_array_contents ()\n\ |
2290 @deftypefnx {Built-in Function} {@var{old_val} =} print_struct_array_contents (@var{new_val})\n\ | |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
13868
diff
changeset
|
2291 @deftypefnx {Built-in Function} {} print_struct_array_contents (@var{new_val}, \"local\")\n\ |
11474 | 2292 Query or set the internal variable that specifies whether to print struct\n\ |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
2293 array contents.\n\ |
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
2294 \n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2295 If true, values of struct array elements are printed. This variable does\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2296 not affect scalar structures whose elements are always printed. In both\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2297 cases, however, printing will be limited to the number of levels specified\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2298 by @var{struct_levels_to_print}.\n\ |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
13868
diff
changeset
|
2299 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
2300 When called from inside a function with the @qcode{\"local\"} option, the\n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
2301 variable is changed locally for the function and any subroutines it calls.\n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
2302 The original variable value is restored when exiting the function.\n\ |
18533
ea0d4dea1a17
doc: Update documentation for functions in octave-value dir.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
2303 @seealso{struct_levels_to_print}\n\ |
11474 | 2304 @end deftypefn") |
2305 { | |
2306 return SET_INTERNAL_VARIABLE (print_struct_array_contents); | |
2307 } |