annotate src/ov-struct.cc @ 10066:2cd940306a06

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