Mercurial > octave
annotate scripts/miscellaneous/orderfields.m @ 30564:796f54d4ddbf stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
In all .txi and .texi files except gpl.txi and gpl.texi in the
doc/liboctave and doc/interpreter directories, change the copyright
to "Octave Project Developers", the same as used for other source
files. Update copyright notices for 2022 (not done since 2019). For
gpl.txi and gpl.texi, change the copyright notice to be "Free Software
Foundation, Inc." and leave the date at 2007 only because this file
only contains the text of the GPL, not anything created by the Octave
Project Developers.
Add Paul Thomas to contributors.in.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 28 Dec 2021 18:22:40 -0500 |
parents | 7854d5752dd2 |
children | 597f3ee61a48 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 2006-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26600
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5881 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23879
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5881 | 11 ## 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:
23879
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
13 ## (at your option) any later version. |
5881 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
18 ## GNU General Public License for more details. |
5881 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## 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:
23879
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5881 | 25 |
26 ## -*- texinfo -*- | |
21792 | 27 ## @deftypefn {} {@var{sout} =} orderfields (@var{s1}) |
28 ## @deftypefnx {} {@var{sout} =} orderfields (@var{s1}, @var{s2}) | |
29 ## @deftypefnx {} {@var{sout} =} orderfields (@var{s1}, @{@var{cellstr}@}) | |
30 ## @deftypefnx {} {@var{sout} =} orderfields (@var{s1}, @var{p}) | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
31 ## @deftypefnx {} {[@var{sout}, @var{p}] =} orderfields (@dots{}) |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
32 ## Return a @emph{copy} of @var{s1} with fields arranged alphabetically, or as |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
33 ## specified by the second input. |
5881 | 34 ## |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 ## Given one input struct @var{s1}, arrange field names alphabetically. |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
36 ## |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
21316
diff
changeset
|
37 ## If a second struct argument is given, arrange field names in @var{s1} as |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
21316
diff
changeset
|
38 ## they appear in @var{s2}. The second argument may also specify the order |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
21316
diff
changeset
|
39 ## in a cell array of strings @var{cellstr}. The second argument may also |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
21316
diff
changeset
|
40 ## be a permutation vector. |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
41 ## |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 ## The optional second output argument @var{p} is the permutation vector which |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 ## converts the original name order to the new name order. |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
44 ## |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
45 ## Examples: |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
46 ## |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
47 ## @example |
13930
fe088cef3503
doc: Add missing @group/@end group to orderfields docstring
Rik <octave@nomad.inbox5.com>
parents:
13929
diff
changeset
|
48 ## @group |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
49 ## s = struct ("d", 4, "b", 2, "a", 1, "c", 3); |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
50 ## t1 = orderfields (s) |
26600
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
51 ## @result{} t1 = |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
52 ## scalar structure containing the fields: |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
53 ## a = 1 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
54 ## b = 2 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
55 ## c = 3 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
56 ## d = 4 |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
57 ## @end group |
21316
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
58 ## @end example |
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
59 ## |
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
60 ## @example |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
61 ## @group |
15878
6be613476e37
doc: orderfields.m bug #38009
Andreas Weber <andy.weber.aw@gmail.com>
parents:
14868
diff
changeset
|
62 ## t = struct ("d", @{@}, "c", @{@}, "b", @{@}, "a", @{@}); |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
63 ## t2 = orderfields (s, t) |
26600
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
64 ## @result{} t2 = |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
65 ## scalar structure containing the fields: |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
66 ## d = 4 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
67 ## c = 3 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
68 ## b = 2 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
69 ## a = 1 |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
70 ## @end group |
21316
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
71 ## @end example |
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
72 ## |
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
73 ## @example |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
74 ## @group |
15878
6be613476e37
doc: orderfields.m bug #38009
Andreas Weber <andy.weber.aw@gmail.com>
parents:
14868
diff
changeset
|
75 ## t3 = orderfields (s, [3, 2, 4, 1]) |
26600
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
76 ## @result{} t3 = |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
77 ## scalar structure containing the fields: |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
78 ## a = 1 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
79 ## b = 2 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
80 ## c = 3 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
81 ## d = 4 |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
82 ## @end group |
21316
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
83 ## @end example |
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
84 ## |
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
85 ## @example |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
86 ## @group |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
87 ## [t4, p] = orderfields (s, @{"d", "c", "b", "a"@}) |
26600
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
88 ## @result{} t4 = |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
89 ## scalar structure containing the fields: |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
90 ## d = 4 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
91 ## c = 3 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
92 ## b = 2 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
93 ## a = 1 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
94 ## p = |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
95 ## 1 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
96 ## 4 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
97 ## 2 |
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
98 ## 3 |
13930
fe088cef3503
doc: Add missing @group/@end group to orderfields docstring
Rik <octave@nomad.inbox5.com>
parents:
13929
diff
changeset
|
99 ## @end group |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8920
diff
changeset
|
100 ## @end example |
5881 | 101 ## |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
102 ## @seealso{fieldnames, getfield, setfield, rmfield, isfield, isstruct, struct} |
5881 | 103 ## @end deftypefn |
104 | |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 function [sout, p] = orderfields (s1, s2) |
5881 | 106 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
107 if (nargin < 1) |
5881 | 108 print_usage (); |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
109 endif |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
110 |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
111 if (! isstruct (s1)) |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
112 error ("orderfields: S1 must be a struct"); |
5881 | 113 endif |
114 | |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
115 names = fieldnames (s1); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
116 |
5881 | 117 if (nargin == 1) |
118 ## One structure: return the fields in alphabetical order. | |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
119 [~, p] = sort (names); |
5881 | 120 elseif (nargin == 2) |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
121 |
6862 | 122 if (isstruct (s2)) |
5881 | 123 ## Two structures: return the fields in the order of s2. |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
124 names2 = fieldnames (s2); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
125 [ns1, idx1] = sort (names); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
126 [ns2, idx2] = sort (names2); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
127 if (! isequal (ns1, ns2)) |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
128 error ("orderfields: structures S1 and S2 do not have the same fields"); |
5881 | 129 endif |
23879
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
130 p = eye (numel (idx2))(idx2,:).' * idx1; |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
131 |
5881 | 132 elseif (iscellstr (s2)) |
133 ## A structure and a list of fields: order by the list of fields. | |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
134 names2 = s2(:); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
135 [ns1, idx1] = sort (names); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
136 [ns2, idx2] = sort (names2); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
137 if (! isequal (ns1, ns2)) |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
138 error ("orderfields: CELLSTR list does not match structure fields"); |
5881 | 139 endif |
23879
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
140 p = eye (numel (idx2))(idx2,:).' * idx1; |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
141 |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
142 elseif (isnumeric (s2)) |
5881 | 143 ## A structure and a permutation vector: permute the order of s1. |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
144 p = s2(:); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
145 if (! isequal (sort (p), (1:numel (names)).')) |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
146 error ("orderfields: invalid permutation vector P"); |
5881 | 147 endif |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
148 |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
149 else |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
150 error ("orderfields: second argument must be structure, cellstr, or permutation vector"); |
5881 | 151 endif |
152 endif | |
153 | |
154 ## Permute the names in the structure. | |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
155 names = names(p); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
156 C = struct2cell (s1); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
157 C = C(p,:); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
158 sout = cell2struct (C, names); |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
159 ## Inherit dimensions. |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
160 sout = reshape (sout, size (s1)); |
5881 | 161 |
162 endfunction | |
8594
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
163 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
164 |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
165 %!shared a, b, c |
23879
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
166 %! a = struct ("C", {1, 2}, "A", {3, 4}, "B", {5, 6}); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
167 %! b = struct ("A", 1, "B", 2, "C", 3); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
168 %! c = struct ("B", {7, 8}, "C", 9, "A", 10); |
8594
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
169 %!test |
23879
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
170 %! x = orderfields (b, a); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
171 %! assert (fieldnames (x), {"C"; "A"; "B"}); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
172 %! assert (x.A, 1); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
173 %! assert (x.B, 2); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
174 %! assert (x.C, 3); |
8594
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
175 %!test |
23879
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
176 %! [x, p] = orderfields (b, [3 2 1]); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
177 %! assert (fieldnames (x), {"C"; "B"; "A"}); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
178 %! assert (p, [3; 2; 1]); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
179 %! assert (x.A, 1); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
180 %! assert (x.B, 2); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
181 %! assert (x.C, 3); |
8594
756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents:
8455
diff
changeset
|
182 %!test |
23879
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
183 %! x = orderfields (b, {"B", "C", "A"}); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
184 %! assert (fieldnames (x), {"B"; "C"; "A"}); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
185 %! assert (x.A, 1); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
186 %! assert (x.B, 2); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
187 %! assert (x.C, 3); |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
188 %!test |
23879
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
189 %! x(1:2) = orderfields (c, {"C", "A", "B"}); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
190 %! assert (fieldnames (x), {"C"; "A"; "B"}); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
191 %! assert (x(2).A, 10); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
192 %! assert (x(2).B, 8); |
92a3b165689e
orderfields.m: Fix broken sorting when second argument given (bug #51723).
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
193 %! assert (x(2).C, 9); |
8605
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
194 |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
195 %!test |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
196 %! aa.x = {1, 2}; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
197 %! aa.y = 3; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
198 %! aa(2).x = {4, 5}; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
199 %! bb.y = {6, 7}; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
200 %! bb.x = 8; |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
201 %! aa(2) = orderfields (bb, aa); |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
202 %! assert (aa(2).x, 8); |
9fc72c114ce1
Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents:
8594
diff
changeset
|
203 %! assert (aa(2).y{1}, 6); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
204 |
22489
93ea313301f9
test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
205 ## Corner case of empty struct |
23573
1b4f4ec53b4a
use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents:
23572
diff
changeset
|
206 %!assert <*40224> (orderfields (struct ()), struct ()) |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
207 %!test |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
208 %! s(2,2).a = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
209 %! s(1,1).b = 2; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
210 %! s = resize (s, [1 0]); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
211 %! s2 = orderfields (s, {"b", "a"}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
212 %! assert (fieldnames (s2), {"b"; "a"}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
213 %! assert (size_equal (s, s2)); |
17620
83e6baf0bc3f
orderfields.m: Fix bug with empty input struct (bug #40224).
Rik <rik@octave.org>
parents:
16656
diff
changeset
|
214 |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
215 ## Test input validation |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
216 %!error <Invalid call> orderfields () |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
217 %!error <S1 must be a struct> orderfields (1) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
218 %!error <S1 and S2 do not have the same fields> |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
219 %! s1.a = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
220 %! s2.b = 2; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
221 %! orderfields (s1, s2); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
222 %!error <CELLSTR list does not match structure fields> |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
223 %! s1.a = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
224 %! orderfields (s1, {"b"}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
225 %!error <invalid permutation vector P> |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
226 %! s1.a = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
227 %! orderfields (s1, [2 1]); |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
228 %!error <invalid permutation vector P> |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
229 %! s1.a = 1; |
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
230 %! orderfields (s1, ones (2,2)); |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
231 %!error <second argument must be structure, cellstr, or permutation vector> |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
232 %! s1.a = 1; |
23349
4f07b4770eec
orderfields.m: recode algorithm to halve running time (bug #50688).
Guillaume Flandin
parents:
23220
diff
changeset
|
233 %! orderfields (s1, "foobar"); |