annotate libinterp/octave-value/ov-struct.cc @ 27061:d61825e693f2

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