Mercurial > octave
annotate src/ov-struct.cc @ 10691:e0ba186b242b
Fisfield: Accept cell arrays as name argument.
author | David Grundberg <davidg@cs.umu.se> |
---|---|
date | Wed, 09 Jun 2010 15:14:31 +0200 |
parents | 9c4daf174387 |
children | 604e13a89c7f |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
8920 | 4 2007, 2008, 2009 John W. Eaton |
2376 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2376 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2376 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3503 | 28 #include <iostream> |
2376 | 29 |
3933 | 30 #include "Cell.h" |
4358 | 31 #include "defun.h" |
2376 | 32 #include "error.h" |
4358 | 33 #include "gripes.h" |
2979 | 34 #include "oct-lvalue.h" |
2376 | 35 #include "ov-struct.h" |
36 #include "unwind-prot.h" | |
6811 | 37 #include "utils.h" |
2948 | 38 #include "variables.h" |
2376 | 39 |
4750 | 40 #include "Array-util.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
41 #include "oct-locbuf.h" |
4750 | 42 |
4687 | 43 #include "byte-swap.h" |
44 #include "ls-oct-ascii.h" | |
45 #include "ls-oct-binary.h" | |
46 #include "ls-hdf5.h" | |
47 #include "ls-utils.h" | |
5759 | 48 #include "pr-output.h" |
4687 | 49 |
3219 | 50 DEFINE_OCTAVE_ALLOCATOR(octave_struct); |
2376 | 51 |
4612 | 52 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct"); |
2376 | 53 |
4513 | 54 Cell |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
55 octave_struct::dotref (const octave_value_list& idx, bool auto_add) |
2962 | 56 { |
4513 | 57 Cell retval; |
3933 | 58 |
59 assert (idx.length () == 1); | |
2962 | 60 |
3933 | 61 std::string nm = idx(0).string_value (); |
62 | |
4219 | 63 Octave_map::const_iterator p = map.seek (nm); |
2376 | 64 |
4219 | 65 if (p != map.end ()) |
3933 | 66 retval = map.contents (p); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
67 else if (auto_add) |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
68 retval = (numel () == 0) ? Cell (dim_vector (1, 1)) : Cell (dims ()); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
69 else |
2376 | 70 error ("structure has no member `%s'", nm.c_str ()); |
71 | |
72 return retval; | |
73 } | |
74 | |
4513 | 75 #if 0 |
3933 | 76 static void |
10370
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
77 gripe_invalid_index1 (void) |
3933 | 78 { |
79 error ("invalid index for structure array"); | |
80 } | |
4513 | 81 #endif |
3933 | 82 |
83 static void | |
84 gripe_invalid_index_for_assignment (void) | |
85 { | |
86 error ("invalid index for structure array assignment"); | |
87 } | |
88 | |
89 static void | |
90 gripe_invalid_index_type (const std::string& nm, char t) | |
91 { | |
92 error ("%s cannot be indexed with %c", nm.c_str (), t); | |
93 } | |
94 | |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
95 static void |
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
96 gripe_failed_assignment (void) |
3933 | 97 { |
98 error ("assignment to structure element failed"); | |
99 } | |
100 | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
101 octave_value_list |
4247 | 102 octave_struct::subsref (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
103 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
104 int nargout) |
3933 | 105 { |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
106 octave_value_list retval; |
3933 | 107 |
108 int skip = 1; | |
109 | |
110 switch (type[0]) | |
111 { | |
112 case '(': | |
113 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
114 if (type.length () > 1 && type[1] == '.') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
115 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
116 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
|
117 octave_value_list key_idx = *++p; |
3933 | 118 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
119 const Cell tmp = dotref (key_idx); |
3933 | 120 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
121 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
122 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
123 const Cell t = tmp.index (idx.front ()); |
3933 | 124 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
125 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); |
3933 | 126 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
127 // We handled two index elements, so tell |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
128 // next_subsref to skip both of them. |
3933 | 129 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
130 skip++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
131 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
132 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
133 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
134 retval(0) = map.index (idx.front ()); |
3933 | 135 } |
136 break; | |
137 | |
138 case '.': | |
139 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
140 if (map.numel() > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
141 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
142 const Cell t = dotref (idx.front ()); |
3933 | 143 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
144 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
145 } |
3933 | 146 } |
147 break; | |
148 | |
149 case '{': | |
150 gripe_invalid_index_type (type_name (), type[0]); | |
151 break; | |
152 | |
153 default: | |
154 panic_impossible (); | |
155 } | |
156 | |
5775 | 157 // FIXME -- perhaps there should be an |
4994 | 158 // octave_value_list::next_subsref member function? See also |
159 // octave_user_function::subsref. | |
160 | |
161 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
|
162 retval = retval(0).next_subsref (nargout, type, idx, skip); |
3933 | 163 |
164 return retval; | |
165 } | |
166 | |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
167 octave_value |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
168 octave_struct::subsref (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
169 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
170 bool auto_add) |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
171 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
172 octave_value retval; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
173 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
174 int skip = 1; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
175 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
176 switch (type[0]) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
177 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
178 case '(': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
179 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
180 if (type.length () > 1 && type[1] == '.') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
181 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
182 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
|
183 octave_value_list key_idx = *++p; |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
184 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
185 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
|
186 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
187 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
188 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
189 const Cell t = tmp.index (idx.front (), auto_add); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
190 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
191 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
192 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
193 // We handled two index elements, so tell |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
194 // next_subsref to skip both of them. |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
195 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
196 skip++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
197 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
198 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
199 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
200 retval = map.index (idx.front (), auto_add); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
201 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
202 break; |
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 case '.': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
205 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
206 if (map.numel() > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
207 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
208 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
|
209 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
210 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
211 } |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
212 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
213 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
214 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
215 case '{': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
216 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
|
217 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
218 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
219 default: |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
220 panic_impossible (); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
221 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
222 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
223 // FIXME -- perhaps there should be an |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
224 // 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
|
225 // octave_user_function::subsref. |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
226 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
227 if (idx.size () > 1) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
228 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
|
229 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
230 return retval; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
231 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
232 |
8031
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
233 /* |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
234 %!test |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
235 %! x(1).a.a = 1; 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
|
236 %! 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
|
237 %! 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
|
238 %! 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
|
239 */ |
d9987dbdf91b
octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
240 |
3933 | 241 octave_value |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
242 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
|
243 const std::string& type) |
3933 | 244 { |
245 octave_value retval; | |
246 | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
247 if (type.length () > 0 && type[0] == '.' && ! val.is_map ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
248 retval = Octave_map (); |
3933 | 249 else |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
250 retval = val; |
3933 | 251 |
252 return retval; | |
253 } | |
254 | |
255 octave_value | |
4247 | 256 octave_struct::subsasgn (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
257 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
258 const octave_value& rhs) |
2376 | 259 { |
3933 | 260 octave_value retval; |
261 | |
262 int n = type.length (); | |
263 | |
264 octave_value t_rhs = rhs; | |
265 | |
9286
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
266 if (idx.front ().empty ()) |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
267 { |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
268 error ("missing index in indexed assignment"); |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
269 return retval; |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
270 } |
c2248cc4821a
don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents:
9190
diff
changeset
|
271 |
3933 | 272 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) |
273 { | |
274 switch (type[0]) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
275 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
276 case '(': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
277 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
278 if (type.length () > 1 && type[1] == '.') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
279 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
280 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
|
281 octave_value_list t_idx = *p; |
3933 | 282 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
283 octave_value_list key_idx = *++p; |
4513 | 284 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
285 assert (key_idx.length () == 1); |
3933 | 286 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
287 std::string key = key_idx(0).string_value (); |
3933 | 288 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
289 std::list<octave_value_list> next_idx (idx); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
290 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
291 // We handled two index elements, so subsasgn to |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
292 // needs to skip both of them. |
3933 | 293 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
294 next_idx.erase (next_idx.begin ()); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
295 next_idx.erase (next_idx.begin ()); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
296 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
297 std::string next_type = type.substr (2); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
298 |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
299 Cell tmpc (1, 1); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
300 Octave_map::iterator pkey = map.seek (key); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
301 if (pkey != map.end ()) |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
302 { |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
303 pkey->second.make_unique (); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
304 tmpc = pkey->second.index (idx.front (), true); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
305 } |
3933 | 306 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
307 // FIXME: better code reuse? cf. octave_cell::subsasgn and the case below. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
308 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
309 { |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8564
diff
changeset
|
310 if (tmpc.numel () == 1) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
311 { |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
312 octave_value& tmp = tmpc(0); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
313 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
314 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
315 { |
8564 | 316 tmp = octave_value::empty_conv (next_type, rhs); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
317 tmp.make_unique (); // probably a no-op. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
318 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
319 else |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
320 // optimization: ignore the copy still stored inside our map. |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
321 tmp.make_unique (1); |
8546
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 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
324 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
325 } |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
326 else |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8564
diff
changeset
|
327 gripe_indexed_cs_list (); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
328 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
329 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
330 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
331 gripe_invalid_index_for_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
332 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
333 break; |
3933 | 334 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
335 case '.': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
336 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
337 octave_value_list key_idx = idx.front (); |
3933 | 338 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
339 assert (key_idx.length () == 1); |
3933 | 340 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
341 std::string key = key_idx(0).string_value (); |
3933 | 342 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
343 std::list<octave_value_list> next_idx (idx); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
344 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
345 next_idx.erase (next_idx.begin ()); |
3933 | 346 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
347 std::string next_type = type.substr (1); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
348 |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
349 Cell tmpc (1, 1); |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
350 Octave_map::iterator pkey = map.seek (key); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
351 if (pkey != map.end ()) |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
352 { |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
353 pkey->second.make_unique (); |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
354 tmpc = pkey->second; |
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
355 } |
3933 | 356 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
357 // FIXME: better code reuse? |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
358 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
359 { |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8564
diff
changeset
|
360 if (tmpc.numel () == 1) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
361 { |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
362 octave_value& tmp = tmpc(0); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
363 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
364 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
365 { |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
366 tmp = octave_value::empty_conv (next_type, rhs); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
367 tmp.make_unique (); // probably a no-op. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
368 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
369 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
370 // optimization: ignore the copy still stored inside our map. |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
371 tmp.make_unique (1); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
372 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
373 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
374 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); |
8456
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
375 } |
c1709a45b45b
optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
376 else |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8564
diff
changeset
|
377 gripe_indexed_cs_list (); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
378 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
379 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
380 break; |
3933 | 381 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
382 case '{': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
383 gripe_invalid_index_type (type_name (), type[0]); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
384 break; |
3933 | 385 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
386 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
387 panic_impossible (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
388 } |
3933 | 389 } |
390 | |
391 if (! error_state) | |
392 { | |
393 switch (type[0]) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
394 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
395 case '(': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
396 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
397 if (n > 1 && type[1] == '.') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
398 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
399 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
|
400 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
|
401 octave_value_list idxf = idx.front (); |
3933 | 402 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
403 assert (key_idx.length () == 1); |
3933 | 404 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
405 std::string key = key_idx(0).string_value (); |
3933 | 406 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
407 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
408 { |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
409 if (t_rhs.is_cs_list ()) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
410 { |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
411 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
|
412 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
413 // 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
|
414 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
415 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
|
416 for (octave_idx_type k = 0; k < idxf.length (); k++) |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
417 if (! idxf(k).is_magic_colon ()) didx(k) = idxf(k).numel (); |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
418 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
419 if (didx.numel () == tmp_cell.numel ()) |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
420 tmp_cell = tmp_cell.reshape (didx); |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
421 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
422 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
423 map.assign (idxf, key, tmp_cell); |
3933 | 424 |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
425 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
426 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
427 count++; |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
428 retval = octave_value (this); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
429 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
430 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
431 gripe_failed_assignment (); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
432 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
433 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
434 { |
9087
961410931a4f
fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents:
9036
diff
changeset
|
435 const Octave_map& cmap = const_cast<const Octave_map &> (map); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
436 // cast map to const reference to avoid forced key insertion. |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
437 if (idxf.all_scalars () |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
438 || cmap.contents (key).index (idxf, true).numel () == 1) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
439 { |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
440 map.assign (idxf, key, t_rhs.storable_value ()); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
441 if (! error_state) |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
442 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
443 count++; |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
444 retval = octave_value (this); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
445 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
446 else |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
447 gripe_failed_assignment (); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
448 } |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
449 else if (! error_state) |
10030
83bb2a78c07d
improve simple assignments error checks and messages
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
450 gripe_nonbraced_cs_list_assignment (); |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
451 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
452 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
453 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
454 gripe_failed_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
455 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
456 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
457 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
458 if (t_rhs.is_map()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
459 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
460 Octave_map rhs_map = t_rhs.map_value (); |
4197 | 461 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
462 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
463 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
464 map.assign (idx.front (), rhs_map); |
5592 | 465 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
466 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
467 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
468 count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
469 retval = octave_value (this); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
470 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
471 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
472 gripe_failed_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
473 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
474 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
475 error ("invalid structure assignment"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
476 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
477 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
478 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
479 if (t_rhs.is_null_value()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
480 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
481 map.maybe_delete_elements (idx.front()); |
5592 | 482 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
483 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
484 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
485 count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
486 retval = octave_value (this); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
487 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
488 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
489 gripe_failed_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
490 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
491 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
492 error ("invalid structure assignment"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
493 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
494 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
495 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
496 break; |
3933 | 497 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
498 case '.': |
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 octave_value_list key_idx = idx.front (); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
501 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
502 assert (key_idx.length () == 1); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
503 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
504 std::string key = key_idx(0).string_value (); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
505 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
506 if (t_rhs.is_cs_list ()) |
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 Cell tmp_cell = Cell (t_rhs.list_value ()); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
509 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
510 // 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
|
511 // 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
|
512 // shape of the left hand side of the assignment. |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
513 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
514 if (numel () == tmp_cell.numel ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
515 tmp_cell = tmp_cell.reshape (dims ()); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
516 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
517 map.assign (key, tmp_cell); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
518 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
519 else |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
520 // Regularize a null matrix if stored into a struct component. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
521 map.assign (key, t_rhs.storable_value ()); |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
522 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
523 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
524 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
525 count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
526 retval = octave_value (this); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
527 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
528 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
529 gripe_failed_assignment (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
530 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
531 break; |
3933 | 532 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
533 case '{': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
534 gripe_invalid_index_type (type_name (), type[0]); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
535 break; |
3933 | 536 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
537 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
538 panic_impossible (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
539 } |
3933 | 540 } |
541 else | |
542 gripe_failed_assignment (); | |
543 | |
544 return retval; | |
2376 | 545 } |
546 | |
7046 | 547 octave_value |
548 octave_struct::do_index_op (const octave_value_list& idx, bool resize_ok) | |
549 { | |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8587
diff
changeset
|
550 // Octave_map handles indexing itself. |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8587
diff
changeset
|
551 return map.index (idx, resize_ok); |
7046 | 552 } |
553 | |
4791 | 554 size_t |
555 octave_struct::byte_size (void) const | |
556 { | |
557 // Neglect the size of the fieldnames. | |
558 | |
559 size_t retval = 0; | |
560 | |
561 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) | |
562 { | |
563 std::string key = map.key (p); | |
564 | |
565 octave_value val = octave_value (map.contents (p)); | |
566 | |
567 retval += val.byte_size (); | |
568 } | |
569 | |
570 return retval; | |
571 } | |
572 | |
2376 | 573 void |
3523 | 574 octave_struct::print (std::ostream& os, bool) const |
2901 | 575 { |
576 print_raw (os); | |
577 } | |
578 | |
579 void | |
3523 | 580 octave_struct::print_raw (std::ostream& os, bool) const |
2376 | 581 { |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
10030
diff
changeset
|
582 unwind_protect frame; |
2376 | 583 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
10030
diff
changeset
|
584 frame.protect_var (Vstruct_levels_to_print); |
2376 | 585 |
3961 | 586 if (Vstruct_levels_to_print >= 0) |
2376 | 587 { |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
588 bool print_keys_only = Vstruct_levels_to_print-- == 0; |
3961 | 589 |
2901 | 590 indent (os); |
591 os << "{"; | |
592 newline (os); | |
2376 | 593 |
2901 | 594 increment_indent_level (); |
2376 | 595 |
5598 | 596 octave_idx_type n = map.numel (); |
3932 | 597 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
598 if (n != 1 || print_keys_only) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
599 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
600 indent (os); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
601 dim_vector dv = dims (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
602 os << dv.str () << " struct array containing the fields:"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
603 newline (os); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
604 newline (os); |
4604 | 605 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
606 increment_indent_level (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
607 } |
4604 | 608 |
5880 | 609 string_vector key_list = map.keys (); |
610 | |
611 for (octave_idx_type i = 0; i < key_list.length (); i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
612 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
613 std::string key = key_list[i]; |
5880 | 614 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
615 Cell val = map.contents (key); |
2376 | 616 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
617 octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); |
3961 | 618 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
619 if (n != 1 || print_keys_only) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
620 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
621 indent (os); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
622 os << key; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
623 if (n == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
624 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
625 dim_vector dv = tmp.dims (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
626 os << ": " << dv.str () << " " << tmp.type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
627 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
628 newline (os); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
629 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
630 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
631 tmp.print_with_name (os, key); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
632 } |
2376 | 633 |
7571
c01ff6818f4c
simplify struct array printing
John W. Eaton <jwe@octave.org>
parents:
7460
diff
changeset
|
634 if (n != 1 || print_keys_only) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
635 decrement_indent_level (); |
4604 | 636 |
2901 | 637 decrement_indent_level (); |
2376 | 638 |
2901 | 639 indent (os); |
640 os << "}"; | |
641 newline (os); | |
2376 | 642 } |
643 else | |
2901 | 644 { |
3961 | 645 indent (os); |
646 os << "<structure>"; | |
2901 | 647 newline (os); |
648 } | |
2376 | 649 } |
650 | |
2901 | 651 bool |
3523 | 652 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901 | 653 { |
3961 | 654 bool retval = false; |
655 | |
2901 | 656 indent (os); |
3961 | 657 |
658 if (Vstruct_levels_to_print < 0) | |
659 os << name << " = "; | |
660 else | |
661 { | |
662 os << name << " ="; | |
663 newline (os); | |
664 retval = true; | |
665 } | |
666 | |
667 return retval; | |
2901 | 668 } |
669 | |
4744 | 670 static bool |
671 scalar (const dim_vector& dims) | |
672 { | |
673 return dims.length () == 2 && dims (0) == 1 && dims (1) == 1; | |
674 } | |
675 | |
676 /* | |
677 %!shared x | |
678 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3; | |
679 %!assert(struct('a',1,'b',3),x(1)) | |
5592 | 680 %!assert(isempty(x([]))) |
681 %!assert(isempty(struct('a',{},'b',{}))) | |
4744 | 682 %!assert(struct('a',{1,2},'b',{3,3}),x) |
683 %!assert(struct('a',{1,2},'b',3),x) | |
684 %!assert(struct('a',{1,2},'b',{3}),x) | |
685 %!assert(struct('b',3,'a',{1,2}),x) | |
686 %!assert(struct('b',{3},'a',{1,2}),x) | |
687 %!test x=struct([]); | |
688 %!assert(size(x),[0,0]); | |
689 %!assert(isstruct(x)); | |
690 %!assert(isempty(fieldnames(x))); | |
691 %!fail("struct('a',{1,2},'b',{1,2,3})","dimensions of parameter 2 do not match those of parameter 4") | |
5582 | 692 %!fail("struct(1,2,3,4)","struct expects alternating \"field\", VALUE pairs"); |
693 %!fail("struct('1',2,'3')","struct expects alternating \"field\", VALUE pairs"); | |
4744 | 694 */ |
695 | |
696 DEFUN (struct, args, , | |
697 "-*- texinfo -*-\n\ | |
698 @deftypefn {Built-in Function} {} struct (\"field\", @var{value}, \"field\", @var{value}, @dots{})\n\ | |
699 \n\ | |
700 Create a structure and initialize its value.\n\ | |
701 \n\ | |
702 If the values are cell arrays, create a structure array and initialize\n\ | |
703 its values. The dimensions of each cell array of values must match.\n\ | |
704 Singleton cells and non-cell values are repeated so that they fill\n\ | |
705 the entire array. If the cells are empty, create an empty structure\n\ | |
4911 | 706 array with the specified field names.\n\ |
9190 | 707 \n\ |
708 If the argument is an object, return the underlying struct.\n\ | |
4911 | 709 @end deftypefn") |
4744 | 710 { |
6946 | 711 octave_value retval; |
4744 | 712 |
713 int nargin = args.length (); | |
714 | |
5444 | 715 // struct ([]) returns an empty struct. |
716 | |
717 // struct (empty_matrix) returns an empty struct with the same | |
718 // dimensions as the empty matrix. | |
719 | |
720 // Note that struct () creates a 1x1 struct with no fields for | |
721 // compatibility with Matlab. | |
4744 | 722 |
9190 | 723 if (nargin == 1 && args(0).is_object ()) |
724 { | |
725 Octave_map m = args(0).map_value (); | |
726 retval = octave_value (new octave_struct (m)); | |
727 | |
728 return retval; | |
729 } | |
730 | |
6946 | 731 if ((nargin == 1 || nargin == 2) |
732 && args(0).is_empty () && args(0).is_real_matrix ()) | |
733 { | |
734 Cell fields; | |
735 | |
736 if (nargin == 2) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
737 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
738 if (args(1).is_cellstr ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
739 retval = Octave_map (args(0).dims (), args(1).cell_value ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
740 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
741 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
|
742 } |
6946 | 743 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
744 retval = Octave_map (args(0).dims ()); |
6946 | 745 |
746 return retval; | |
747 } | |
4744 | 748 |
749 // Check for "field", VALUE pairs. | |
750 | |
751 for (int i = 0; i < nargin; i += 2) | |
752 { | |
753 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
|
754 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
755 error ("struct expects alternating \"field\", VALUE pairs"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
756 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
757 } |
4744 | 758 } |
759 | |
760 // Check that the dimensions of the values correspond. | |
761 | |
762 dim_vector dims (1, 1); | |
763 | |
764 int first_dimensioned_value = 0; | |
765 | |
766 for (int i = 1; i < nargin; i += 2) | |
767 { | |
768 if (args(i).is_cell ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
769 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
770 dim_vector argdims (args(i).dims ()); |
4744 | 771 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
772 if (! scalar (argdims)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
773 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
774 if (! first_dimensioned_value) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
775 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
776 dims = argdims; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
777 first_dimensioned_value = i + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
778 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
779 else if (dims != argdims) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
780 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
781 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
|
782 first_dimensioned_value, i+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
783 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
784 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
785 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
786 } |
4744 | 787 } |
788 | |
789 // Create the return value. | |
790 | |
791 Octave_map map (dims); | |
792 | |
793 for (int i = 0; i < nargin; i+= 2) | |
794 { | |
795 // Get key. | |
796 | |
797 std::string key (args(i).string_value ()); | |
798 | |
799 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
800 return retval; |
4744 | 801 |
6811 | 802 if (! valid_identifier (key)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
803 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
804 error ("struct: invalid structure field name `%s'", key.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
805 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
806 } |
6811 | 807 |
4744 | 808 // Value may be v, { v }, or { v1, v2, ... } |
809 // In the first two cases, we need to create a cell array of | |
810 // the appropriate dimensions filled with v. In the last case, | |
811 // the cell array has already been determined to be of the | |
812 // correct dimensions. | |
813 | |
814 if (args(i+1).is_cell ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
815 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
816 const Cell c (args(i+1).cell_value ()); |
4744 | 817 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
818 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
819 return retval; |
4744 | 820 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
821 if (scalar (c.dims ())) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
822 map.assign (key, Cell (dims, c(0))); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
823 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
824 map.assign (key, c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
825 } |
4744 | 826 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
827 map.assign (key, Cell (dims, args(i+1))); |
4744 | 828 |
829 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
830 return retval; |
4744 | 831 } |
6946 | 832 |
4744 | 833 return octave_value (map); |
834 } | |
835 | |
4358 | 836 DEFUN (isstruct, args, , |
837 "-*- texinfo -*-\n\ | |
838 @deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
839 Return 1 if the value of the expression @var{expr} is a structure\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
840 (or a structure array).\n\ |
4358 | 841 @end deftypefn") |
842 { | |
843 octave_value retval; | |
844 | |
845 if (args.length () == 1) | |
846 retval = args(0).is_map (); | |
847 else | |
5823 | 848 print_usage (); |
4358 | 849 |
850 return retval; | |
851 } | |
852 | |
853 DEFUN (fieldnames, args, , | |
854 "-*- texinfo -*-\n\ | |
855 @deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\ | |
856 Return a cell array of strings naming the elements of the structure\n\ | |
857 @var{struct}. It is an error to call @code{fieldnames} with an\n\ | |
858 argument that is not a structure.\n\ | |
859 @end deftypefn") | |
860 { | |
861 octave_value retval; | |
862 | |
863 int nargin = args.length (); | |
864 | |
865 if (nargin == 1) | |
866 { | |
7336 | 867 octave_value arg = args(0); |
868 | |
869 if (arg.is_map () || arg.is_object ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
870 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
871 Octave_map m = arg.map_value (); |
7336 | 872 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
873 string_vector keys = m.keys (); |
7336 | 874 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
875 if (keys.length () == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
876 retval = Cell (0, 1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
877 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
878 retval = Cell (m.keys ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
879 } |
4358 | 880 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
881 gripe_wrong_type_arg ("fieldnames", args(0)); |
4358 | 882 } |
883 else | |
5823 | 884 print_usage (); |
4358 | 885 |
886 return retval; | |
887 } | |
888 | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
889 /* |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
890 %!# test preservation of fieldname order |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
891 %!test |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
892 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
893 %! assert(fieldnames(x), {"d"; "a"; "b"; "c"}); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
894 */ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
895 |
4358 | 896 DEFUN (isfield, args, , |
897 "-*- texinfo -*-\n\ | |
898 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ | |
10691
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
899 Return true if the expression @var{expr} is a structure and it\n\ |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
900 includes an element named @var{name}. If @var{name} is a cell\n\ |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
901 array, a logical array of equal dimension is returned.\n\ |
4358 | 902 @end deftypefn") |
903 { | |
904 octave_value retval; | |
905 | |
906 int nargin = args.length (); | |
907 | |
908 if (nargin == 2) | |
909 { | |
910 retval = false; | |
911 | |
10691
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
912 if (args(0).is_map ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
913 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
914 Octave_map m = args(0).map_value (); |
4358 | 915 |
10691
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
916 // FIXME -- should this work for all types that can do |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
917 // structure reference operations? |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
918 |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
919 if (args(1).is_string ()) |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
920 { |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
921 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
|
922 |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
923 retval = m.contains (key) != 0; |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
924 } |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
925 else if (args(1).is_cell ()) |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
926 { |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
927 Cell c = args(1).cell_value (); |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
928 boolMatrix bm (c.dims ()); |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
929 octave_idx_type n = bm.numel (); |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
930 |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
931 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
|
932 { |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
933 if (c(i).is_string ()) |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
934 { |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
935 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
|
936 |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
937 bm(i) = m.contains (key) != 0; |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
938 } |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
939 else |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
940 bm(i) = false; |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
941 } |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
942 |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
943 retval = bm; |
e0ba186b242b
Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents:
10370
diff
changeset
|
944 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
945 } |
4358 | 946 } |
947 else | |
5823 | 948 print_usage (); |
4358 | 949 |
950 return retval; | |
951 } | |
952 | |
9784 | 953 DEFUN (nfields, args, , |
954 "-*- texinfo -*-\n\ | |
955 @deftypefn {Built-in Function} {} nfields (@var{s})\n\ | |
956 Return the number of fields of the structure @var{s}.\n\ | |
957 @end deftypefn") | |
958 { | |
959 octave_value retval; | |
960 | |
961 int nargin = args.length (); | |
962 | |
963 if (nargin == 1 && args(0).is_map ()) | |
964 { | |
965 retval = static_cast<double> (args(0).nfields ()); | |
966 } | |
967 else | |
968 print_usage (); | |
969 | |
970 return retval; | |
971 } | |
972 | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
973 /* |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
974 %!# test isfield |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
975 %!test |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
976 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
977 %! assert (isfield (x, "b")); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
978 %!assert( isfield( struct("a", "1"), "a")); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
979 %!assert( isfield( {1}, "c"), logical (0)); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
980 %!assert( isfield( struct("a", "1"), 10), logical (0)); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
981 %!assert( isfield( struct("a", "b"), "a "), logical (0)); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
982 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
983 */ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
984 |
4750 | 985 // Check that the dimensions of the input arguments are correct. |
986 | |
987 static bool | |
988 cell2struct_check_args (const dim_vector& c_dv, const dim_vector& f_dv, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
989 bool is_cell, int dim) |
4750 | 990 { |
4751 | 991 bool retval = true; |
4750 | 992 |
993 if (dim >= 0 && dim < c_dv.length ()) | |
994 { | |
995 if (is_cell) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
996 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
997 if (f_dv.numel () != c_dv(dim)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
998 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
999 error ("cell2struct: numel (FIELD) != size (CELL, DIM)"); |
4750 | 1000 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1001 retval = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1002 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1003 } |
4750 | 1004 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1005 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1006 if (f_dv.length () > 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1007 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1008 error ("cell2struct: field array must be a 2-d matrix"); |
4750 | 1009 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1010 retval = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1011 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1012 else if (f_dv(0) != c_dv(dim)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1013 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1014 error ("cell2struct: size (FIELD, 1) != length (C, DIM)"); |
4750 | 1015 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1016 retval = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1017 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1018 } |
4750 | 1019 } |
1020 else | |
1021 { | |
1022 error ("cell2struct: DIM out of range"); | |
1023 | |
1024 retval = false; | |
1025 } | |
1026 | |
1027 return retval; | |
1028 } | |
1029 | |
1030 static void | |
5275 | 1031 cell2struct_construct_idx (Array<octave_idx_type>& ra_idx1, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1032 const Array<octave_idx_type>& ra_idx2, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1033 octave_idx_type dim, octave_idx_type fill_value) |
4750 | 1034 { |
5275 | 1035 octave_idx_type iidx = 0; |
4750 | 1036 |
5275 | 1037 for (octave_idx_type idx = 0; idx < ra_idx1.length (); idx++) |
4750 | 1038 { |
1039 if (idx == dim) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1040 ra_idx1.elem (idx) = fill_value; |
4750 | 1041 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1042 ra_idx1.elem (idx) = ra_idx2(iidx++); |
4750 | 1043 } |
1044 } | |
1045 | |
1046 DEFUN (cell2struct, args, , | |
1047 "-*- texinfo -*-\n\ | |
4817 | 1048 @deftypefn {Built-in Function} {} cell2struct (@var{cell}, @var{fields}, @var{dim})\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
1049 Convert @var{cell} to a structure. The number of fields in @var{fields}\n\ |
4817 | 1050 must match the number of elements in @var{cell} along dimension @var{dim},\n\ |
1051 that is @code{numel (@var{fields}) == size (@var{cell}, @var{dim})}.\n\ | |
4750 | 1052 \n\ |
1053 @example\n\ | |
1054 @group\n\ | |
7031 | 1055 A = cell2struct (@{'Peter', 'Hannah', 'Robert';\n\ |
1056 185, 170, 168@},\n\ | |
6519 | 1057 @{'Name','Height'@}, 1);\n\ |
4750 | 1058 A(1)\n\ |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1059 @result{} ans =\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1060 @{\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1061 Name = Peter\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1062 Height = 185\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1063 @}\n\ |
4750 | 1064 \n\ |
1065 @end group\n\ | |
1066 @end example\n\ | |
1067 @end deftypefn") | |
1068 { | |
1069 octave_value retval; | |
1070 | |
4751 | 1071 if (args.length () == 3) |
4750 | 1072 { |
4751 | 1073 Cell c = args(0).cell_value (); |
4750 | 1074 |
4751 | 1075 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1076 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1077 octave_value field = args(1); |
4750 | 1078 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1079 // Field is either cell or character matrix. |
4750 | 1080 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1081 // FIXME -- this could be simplified if we had |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1082 // cellstr and iscellstr functions available. |
4752 | 1083 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1084 bool field_is_cell = field.is_cell (); |
4750 | 1085 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1086 Cell field_cell; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1087 charMatrix field_char; |
4750 | 1088 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1089 if (field_is_cell) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1090 field_cell = field.cell_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1091 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1092 field_char = field.char_matrix_value (); |
4751 | 1093 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1094 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1095 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1096 // Retrieve the dimension value. |
4751 | 1097 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1098 // FIXME -- int_value () should print out the |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1099 // conversions it does to be Matlab compatible. |
4751 | 1100 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1101 octave_idx_type dim = args(2).int_value () - 1; |
4751 | 1102 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1103 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1104 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1105 dim_vector c_dv = c.dims (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1106 dim_vector field_dv = field.dims (); |
4751 | 1107 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1108 if (cell2struct_check_args (c_dv, field_dv, field_is_cell, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1109 dim)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1110 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1111 octave_idx_type c_dv_length = c_dv.length (); |
4751 | 1112 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1113 // Dimension vector for the Cell arrays to be |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1114 // put into the structure. |
4751 | 1115 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1116 dim_vector value_dv; |
4751 | 1117 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1118 // Initialize c_value_dv. |
4750 | 1119 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1120 if (c_dv_length == 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1121 value_dv = dim_vector (1, 1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1122 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1123 value_dv.resize (c_dv_length - 1); |
4751 | 1124 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1125 octave_idx_type idx_tmp = 0; |
4751 | 1126 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1127 for (octave_idx_type i = 0; i < c_dv_length; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1128 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1129 if (i != dim) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1130 value_dv.elem (idx_tmp++) = c_dv.elem (i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1131 } |
4751 | 1132 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1133 // All initializing is done, we can start moving |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1134 // values. |
4751 | 1135 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1136 Octave_map map; |
4751 | 1137 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1138 // If field is a cell array then we use all |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1139 // elements in array, on the other hand when |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1140 // field is a character array the number of |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1141 // elements is equals the number of rows. |
4751 | 1142 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1143 octave_idx_type field_numel |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1144 = field_is_cell ? field_dv.numel (): field_dv(0); |
4751 | 1145 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1146 // For matlab compatibility. |
4751 | 1147 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1148 if (field_numel == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1149 map.reshape (dim_vector (0, 1)); |
4750 | 1150 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1151 for (octave_idx_type i = 0; i < field_numel; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1152 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1153 // Construct cell array which goes into the |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1154 // structure together with the appropriate |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1155 // field name. |
4751 | 1156 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1157 Cell c_value (value_dv); |
4751 | 1158 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
1159 Array<octave_idx_type> value_idx (value_dv.length (), 1, 0); |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
1160 Array<octave_idx_type> c_idx (c_dv_length, 1, 0); |
4751 | 1161 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1162 for (octave_idx_type j = 0; j < value_dv.numel (); j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1163 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1164 // Need to do this to construct the |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1165 // appropriate idx for getting elements |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1166 // from the original cell array. |
4751 | 1167 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1168 cell2struct_construct_idx (c_idx, value_idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1169 dim, i); |
4751 | 1170 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1171 c_value.elem (value_idx) = c.elem (c_idx); |
4751 | 1172 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1173 increment_index (value_idx, value_dv); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1174 } |
4751 | 1175 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1176 std::string field_str; |
4750 | 1177 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1178 if (field_is_cell) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1179 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1180 // Matlab retrieves the field values |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1181 // column by column. |
4751 | 1182 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1183 octave_value field_tmp = field_cell.elem (i); |
4751 | 1184 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1185 field_str = field_tmp.string_value (); |
4751 | 1186 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1187 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1188 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1189 error ("cell2struct: fields have to be of type string"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1190 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1191 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1192 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1193 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1194 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1195 field_str = field_char.row_as_string (i); |
4751 | 1196 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1197 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1198 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1199 } |
4751 | 1200 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1201 if (! valid_identifier (field_str)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1202 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1203 error ("cell2struct: invalid field name `%s'", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1204 field_str.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1205 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1206 } |
6811 | 1207 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1208 map.reshape (value_dv); |
4751 | 1209 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1210 map.assign (field_str, c_value); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1211 } |
4751 | 1212 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1213 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1214 retval = map; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1215 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1216 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1217 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1218 error ("cell2struct: expecting third argument to be an integer"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1219 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1220 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1221 error ("cell2struct: expecting second argument to be a cell or character array"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1222 } |
4751 | 1223 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1224 error ("cell2struct: expecting first argument to be a cell array"); |
4750 | 1225 } |
4751 | 1226 else |
5823 | 1227 print_usage (); |
4750 | 1228 |
1229 return retval; | |
1230 } | |
1231 | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1232 /* |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1233 %!# test cell2struct versus struct2cell |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1234 %!test |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1235 %! keys = cellstr (char (floor (rand (100,10)*24+65)))'; |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1236 %! vals = mat2cell(rand (100,1), ones (100,1), 1)'; |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1237 %! s = struct ([keys; vals]{:}); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1238 %! t = cell2struct (vals, keys, 2); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1239 %! assert (s, t); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1240 %! assert (struct2cell (s), vals'); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1241 %! assert (fieldnames (s), keys'); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1242 */ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1243 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1244 |
4817 | 1245 // So we can call Fcellstr directly. |
1246 extern octave_value_list Fcellstr (const octave_value_list& args, int); | |
1247 | |
1248 DEFUN (rmfield, args, , | |
1249 "-*- texinfo -*-\n\ | |
1250 @deftypefn {Built-in Function} {} rmfield (@var{s}, @var{f})\n\ | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1251 Return a copy of the structure (array) @var{s} with the field @var{f} removed.\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1252 If @var{f} is a cell array of strings or a character array, remove the\n\ |
4817 | 1253 named fields.\n\ |
5642 | 1254 @seealso{cellstr, iscellstr, setfield}\n\ |
1255 @end deftypefn") | |
4817 | 1256 { |
1257 octave_value retval; | |
1258 | |
1259 int nargin = args.length (); | |
1260 | |
1261 if (nargin == 2) | |
1262 { | |
1263 Octave_map m = args(0).map_value (); | |
1264 | |
1265 octave_value_list fval = Fcellstr (args(1), 1); | |
1266 | |
1267 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1268 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1269 Cell fcell = fval(0).cell_value (); |
4817 | 1270 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1271 for (int i = 0; i < fcell.numel (); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1272 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1273 std::string key = fcell(i).string_value (); |
4817 | 1274 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1275 if (m.contains (key)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1276 m.del (key); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1277 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1278 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1279 error ("rmfield: structure does not contain field %s", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1280 key.c_str ()); |
4817 | 1281 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1282 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1283 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1284 } |
4817 | 1285 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1286 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1287 retval = m; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1288 } |
4817 | 1289 } |
1290 else | |
5823 | 1291 print_usage (); |
4817 | 1292 |
1293 return retval; | |
1294 } | |
1295 | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1296 /* |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1297 %!# test rmfield |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1298 %!test |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1299 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; x(6).f="abc123"; |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1300 %! y = rmfield (x, {"a", "f"}); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1301 %! assert (fieldnames (y), {"d"; "b"; "c"}); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1302 %! assert (size (y), [1, 6]); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1303 */ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10066
diff
changeset
|
1304 |
4817 | 1305 bool |
6974 | 1306 octave_struct::save_ascii (std::ostream& os) |
4817 | 1307 { |
1308 Octave_map m = map_value (); | |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1309 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1310 octave_idx_type nf = m.nfields (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1311 |
9851
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1312 const dim_vector dv = dims (); |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1313 |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1314 os << "# ndims: " << dv.length () << "\n"; |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1315 |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1316 for (int i = 0; i < dv.length (); i++) |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1317 os << " " << dv (i); |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1318 os << "\n"; |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1319 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1320 os << "# length: " << nf << "\n"; |
4817 | 1321 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1322 // Iterating over the list of keys will preserve the order of the |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1323 // fields. |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1324 string_vector keys = m.keys (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1325 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1326 for (octave_idx_type i = 0; i < nf; i++) |
4817 | 1327 { |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1328 std::string key = keys(i); |
4817 | 1329 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1330 octave_value val = map.contents (key); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1331 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1332 bool b = save_ascii_data (os, val, key, false, 0); |
4817 | 1333 |
1334 if (! b) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1335 return os; |
4817 | 1336 } |
1337 | |
1338 return true; | |
1339 } | |
1340 | |
1341 bool | |
1342 octave_struct::load_ascii (std::istream& is) | |
1343 { | |
5275 | 1344 octave_idx_type len = 0; |
9851
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1345 dim_vector dv (1, 1); |
4817 | 1346 bool success = true; |
1347 | |
9851
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1348 // KLUGE: earlier Octave versions did not save extra dimensions with struct, |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1349 // and as a result did not preserve dimensions for empty structs. |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1350 // The default dimensions were 1x1, which we want to preserve. |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1351 string_vector keywords(2); |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1352 |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1353 keywords[0] = "ndims"; |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1354 keywords[1] = "length"; |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1355 |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1356 std::string kw; |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1357 |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1358 if (extract_keyword (is, keywords, kw, len, true)) |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1359 { |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1360 if (kw == keywords[0]) |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1361 { |
10258 | 1362 int mdims = std::max (static_cast<int> (len), 2); |
9851
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1363 dv.resize (mdims); |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1364 for (int i = 0; i < mdims; i++) |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1365 is >> dv(i); |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1366 |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1367 success = extract_keyword (is, keywords[1], len); |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1368 } |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1369 } |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1370 else |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1371 success = false; |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1372 |
1fac51c5f83f
save dims for empty structs
Jaroslav Hajek <highegg@gmail.com>
parents:
9784
diff
changeset
|
1373 if (success && len >= 0) |
4817 | 1374 { |
1375 if (len > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1376 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1377 Octave_map m (dv); |
4817 | 1378 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1379 for (octave_idx_type j = 0; j < len; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1380 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1381 octave_value t2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1382 bool dummy; |
4817 | 1383 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1384 // recurse to read cell elements |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1385 std::string nm |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1386 = read_ascii_data (is, std::string (), dummy, t2, j); |
4817 | 1387 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1388 if (!is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1389 break; |
4817 | 1390 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1391 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1392 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1393 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1394 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1395 error ("load: internal error loading struct elements"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1396 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1397 } |
5433 | 1398 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1399 m.assign (nm, tcell); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1400 } |
4817 | 1401 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1402 if (is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1403 map = m; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1404 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1405 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1406 error ("load: failed to load structure"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1407 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1408 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1409 } |
4817 | 1410 else if (len == 0 ) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1411 map = Octave_map (dv); |
4817 | 1412 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1413 panic_impossible (); |
4817 | 1414 } |
1415 else { | |
1416 error ("load: failed to extract number of elements in structure"); | |
1417 success = false; | |
1418 } | |
1419 | |
1420 return success; | |
1421 } | |
1422 | |
1423 bool | |
1424 octave_struct::save_binary (std::ostream& os, bool& save_as_floats) | |
1425 { | |
1426 Octave_map m = map_value (); | |
1427 | |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1428 octave_idx_type nf = m.nfields (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1429 |
9857
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1430 dim_vector d = dims (); |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1431 if (d.length () < 1) |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1432 return false; |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1433 |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1434 // Use negative value for ndims |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1435 int32_t di = - d.length(); |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1436 os.write (reinterpret_cast<char *> (&di), 4); |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1437 for (int i = 0; i < d.length (); i++) |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1438 { |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1439 di = d(i); |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1440 os.write (reinterpret_cast<char *> (&di), 4); |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1441 } |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1442 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1443 int32_t len = nf; |
5760 | 1444 os.write (reinterpret_cast<char *> (&len), 4); |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1445 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1446 // Iterating over the list of keys will preserve the order of the |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1447 // fields. |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1448 string_vector keys = m.keys (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1449 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1450 for (octave_idx_type i = 0; i < nf; i++) |
4817 | 1451 { |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1452 std::string key = keys(i); |
4817 | 1453 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1454 octave_value val = map.contents (key); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1455 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1456 bool b = save_binary_data (os, val, key, "", 0, save_as_floats); |
4817 | 1457 |
1458 if (! b) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1459 return os; |
4817 | 1460 } |
1461 | |
1462 return true; | |
1463 } | |
1464 | |
1465 bool | |
1466 octave_struct::load_binary (std::istream& is, bool swap, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1467 oct_mach_info::float_format fmt) |
4817 | 1468 { |
1469 bool success = true; | |
5828 | 1470 int32_t len; |
5760 | 1471 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4817 | 1472 return false; |
1473 if (swap) | |
4944 | 1474 swap_bytes<4> (&len); |
4817 | 1475 |
9857
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1476 dim_vector dv (1, 1); |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1477 |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1478 if (len < 0) |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1479 { |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1480 // We have explicit dimensions. |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1481 int mdims = -len; |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1482 |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1483 int32_t di; |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1484 dv.resize (mdims); |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1485 |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1486 for (int i = 0; i < mdims; i++) |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1487 { |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1488 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1489 return false; |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1490 if (swap) |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1491 swap_bytes<4> (&di); |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1492 dv(i) = di; |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1493 } |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1494 |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1495 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1496 return false; |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1497 if (swap) |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1498 swap_bytes<4> (&len); |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1499 } |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1500 |
4817 | 1501 if (len > 0) |
1502 { | |
9857
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1503 Octave_map m (dv); |
4817 | 1504 |
5275 | 1505 for (octave_idx_type j = 0; j < len; j++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1506 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1507 octave_value t2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1508 bool dummy; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1509 std::string doc; |
4817 | 1510 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1511 // recurse to read cell elements |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1512 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1513 dummy, t2, doc); |
4817 | 1514 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1515 if (!is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1516 break; |
4817 | 1517 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1518 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1519 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1520 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1521 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1522 error ("load: internal error loading struct elements"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1523 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1524 } |
5433 | 1525 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1526 m.assign (nm, tcell); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1527 } |
4817 | 1528 |
1529 if (is) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1530 map = m; |
4817 | 1531 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1532 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1533 error ("load: failed to load structure"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1534 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1535 } |
4817 | 1536 } |
9857
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1537 else if (len == 0) |
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1538 map = Octave_map (dv); |
4817 | 1539 else |
9857
43a7adf62534
save dims for empty structs (binary fmt)
Jaroslav Hajek <highegg@gmail.com>
parents:
9851
diff
changeset
|
1540 success = false; |
4817 | 1541 |
1542 return success; | |
1543 } | |
1544 | |
1545 #if defined (HAVE_HDF5) | |
1546 | |
1547 bool | |
1548 octave_struct::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
1549 { | |
1550 hid_t data_hid = -1; | |
1551 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
1552 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
1553 data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
1554 #else |
4817 | 1555 data_hid = H5Gcreate (loc_id, name, 0); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
1556 #endif |
4817 | 1557 if (data_hid < 0) return false; |
1558 | |
1559 // recursively add each element of the structure to this group | |
1560 Octave_map m = map_value (); | |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1561 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1562 octave_idx_type nf = m.nfields (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1563 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1564 // Iterating over the list of keys will preserve the order of the |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1565 // fields. |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1566 string_vector keys = m.keys (); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1567 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1568 for (octave_idx_type i = 0; i < nf; i++) |
4817 | 1569 { |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1570 std::string key = keys(i); |
4817 | 1571 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1572 octave_value val = map.contents (key); |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1573 |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1574 bool retval2 = add_hdf5_data (data_hid, val, key, "", false, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1575 save_as_floats); |
4817 | 1576 |
1577 if (! retval2) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1578 break; |
4817 | 1579 } |
1580 | |
1581 H5Gclose (data_hid); | |
4837 | 1582 |
4817 | 1583 return true; |
1584 } | |
1585 | |
1586 bool | |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
9857
diff
changeset
|
1587 octave_struct::load_hdf5 (hid_t loc_id, const char *name) |
4817 | 1588 { |
1589 bool retval = false; | |
1590 | |
1591 hdf5_callback_data dsub; | |
1592 | |
1593 herr_t retval2 = 0; | |
6292 | 1594 Octave_map m (dim_vector (1, 1)); |
4817 | 1595 int current_item = 0; |
1596 hsize_t num_obj = 0; | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
1597 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
1598 hid_t group_id = H5Gopen (loc_id, name, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
1599 #else |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
1600 hid_t group_id = H5Gopen (loc_id, name); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
1601 #endif |
5060 | 1602 H5Gget_num_objs (group_id, &num_obj); |
1603 H5Gclose (group_id); | |
4817 | 1604 |
8907
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1605 // FIXME -- fields appear to be sorted alphabetically on loading. |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1606 // Why is that happening? |
5a956c026b6c
preserve field order when saving structs
John W. Eaton <jwe@octave.org>
parents:
8679
diff
changeset
|
1607 |
4817 | 1608 while (current_item < static_cast<int> (num_obj) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1609 && (retval2 = H5Giterate (loc_id, name, ¤t_item, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1610 hdf5_read_next_data, &dsub)) > 0) |
4817 | 1611 { |
5342 | 1612 octave_value t2 = dsub.tc; |
1613 | |
6293 | 1614 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
5433 | 1615 |
6293 | 1616 if (error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1617 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1618 error ("load: internal error loading struct elements"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1619 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1620 } |
5433 | 1621 |
6293 | 1622 m.assign (dsub.name, tcell); |
5336 | 1623 |
4817 | 1624 } |
1625 | |
1626 if (retval2 >= 0) | |
1627 { | |
1628 map = m; | |
1629 retval = true; | |
1630 } | |
1631 | |
1632 return retval; | |
1633 } | |
1634 | |
4687 | 1635 #endif |
1636 | |
5900 | 1637 mxArray * |
1638 octave_struct::as_mxArray (void) const | |
1639 { | |
1640 int nf = nfields (); | |
1641 string_vector kv = map_keys (); | |
6065 | 1642 |
1643 OCTAVE_LOCAL_BUFFER (const char *, f, nf); | |
1644 | |
5900 | 1645 for (int i = 0; i < nf; i++) |
6065 | 1646 f[i] = kv[i].c_str (); |
5900 | 1647 |
1648 mxArray *retval = new mxArray (dims (), nf, f); | |
1649 | |
1650 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); | |
1651 | |
6686 | 1652 mwSize nel = numel (); |
5900 | 1653 |
6686 | 1654 mwSize ntot = nf * nel; |
5900 | 1655 |
1656 for (int i = 0; i < nf; i++) | |
1657 { | |
1658 Cell c = map.contents (kv[i]); | |
1659 | |
1660 const octave_value *p = c.data (); | |
1661 | |
6686 | 1662 mwIndex k = 0; |
1663 for (mwIndex j = i; j < ntot; j += nf) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
1664 elts[j] = new mxArray (p[k++]); |
5900 | 1665 } |
1666 | |
1667 return retval; | |
1668 } |