annotate src/ov-struct.cc @ 12181:88ff30dcc048

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