annotate scripts/miscellaneous/orderfields.m @ 8612:20d23d65cc84

fix & optimize orderfields
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 28 Jan 2009 07:23:35 +0100
parents 9fc72c114ce1
children eb63fbe60fab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2006, 2007 Paul Kienzle
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
2 ##
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
4 ##
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6862
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6862
diff changeset
8 ## your option) any later version.
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
9 ##
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
14 ##
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
15 ## 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: 6862
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6862
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
18
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
6713
26f3d9bf9562 [project @ 2007-06-13 10:34:14 by dbateman]
dbateman
parents: 5887
diff changeset
20 ## @deftypefn {Function File} {[@var{t}, @var{p}] =} orderfields (@var{s1}, @var{s2})
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
21 ## Return a struct with fields arranged alphabetically or as specified
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
22 ## by @var{s2} and a corresponding permutation vector.
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
23 ##
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
24 ## Given one struct, arrange field names in @var{s1} alphabetically.
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
25 ##
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
26 ## Given two structs, arrange field names in @var{s1} as they appear
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
27 ## in @var{s2}. The second argument may also specify the order in
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
28 ## a permutation vector or a cell array of strings.
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
29 ##
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
30 ## @seealso{getfield, rmfield, isfield, isstruct, fieldnames, struct}
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
31 ## @end deftypefn
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
32
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
33 ## Author: Paul Kienzle <pkienzle@users.sf.net>
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
34 ## Adapted-By: jwe
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
35
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
36 function [t, p] = orderfields (s1, s2)
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
37
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
38 if (nargin == 1 || nargin == 2)
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
39 if (! isstruct (s1))
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
40 error ("orderfields: expecting argument to be a struct");
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
41 endif
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
42 else
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
43 print_usage ();
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
44 endif
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
45
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
46 if (nargin == 1)
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
47 ## One structure: return the fields in alphabetical order.
6862
d63339cbb205 [project @ 2007-09-05 06:49:29 by jwe]
jwe
parents: 6713
diff changeset
48 if (isstruct (s1))
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
49 names = sort (fieldnames (s1));
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
50 endif
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
51 elseif (nargin == 2)
6862
d63339cbb205 [project @ 2007-09-05 06:49:29 by jwe]
jwe
parents: 6713
diff changeset
52 if (isstruct (s2))
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
53 ## Two structures: return the fields in the order of s2.
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
54 names = fieldnames (s2);
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
55 if (! isequal (sort (fieldnames (s1)), sort (names)))
5887
39cbf6a484bb [project @ 2006-07-14 21:02:33 by jwe]
jwe
parents: 5881
diff changeset
56 error ("orderfields: structures do not have same fields");
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
57 endif
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
58 elseif (iscellstr (s2))
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
59 ## A structure and a list of fields: order by the list of fields.
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
60 t1 = sort (fieldnames (s1));
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
61 t2 = sort (s2(:));
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
62 if (! isequal (t1, t2))
5887
39cbf6a484bb [project @ 2006-07-14 21:02:33 by jwe]
jwe
parents: 5881
diff changeset
63 error ("orderfields: name list does not match structure fields");
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
64 endif
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
65 names = s2;
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
66 elseif (isvector (s2))
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
67 ## A structure and a permutation vector: permute the order of s1.
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
68 names = fieldnames (s1);
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
69 t1 = sort (s2);
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
70 t1 = t1(:)';
6862
d63339cbb205 [project @ 2007-09-05 06:49:29 by jwe]
jwe
parents: 6713
diff changeset
71 t2 = 1:numel (names);
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
72 if (! isequal (t1, t2))
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
73 error ("orderfields: invalid permutation vector");
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
74 endif
6862
d63339cbb205 [project @ 2007-09-05 06:49:29 by jwe]
jwe
parents: 6713
diff changeset
75 names = names (s2);
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
76 endif
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
77 endif
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
78
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
79 ## Find permutation vector which converts the original name order
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
80 ## into the new name order. Note: could save a couple of sorts
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
81 ## in some cases, but performance isn't critical.
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
82
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
83 if (nargout == 2)
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
84 [oldel, oldidx] = sort (fieldnames (s1));
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
85 [newel, newidx] = sort (names);
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
86 p = oldidx(newidx);
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
87 endif
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
88
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
89 ## Permute the names in the structure.
6862
d63339cbb205 [project @ 2007-09-05 06:49:29 by jwe]
jwe
parents: 6713
diff changeset
90 if (numel (s1) == 0)
8605
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
91 args = cell (1, 2 * numel (names));
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
92 args(1:2:end) = names;
8455
fd11a08a9b31 disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
93 args(2:2:end) = {[]};
8605
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
94 t = struct (args{:});
6862
d63339cbb205 [project @ 2007-09-05 06:49:29 by jwe]
jwe
parents: 6713
diff changeset
95 else
8612
20d23d65cc84 fix & optimize orderfields
Jaroslav Hajek <highegg@gmail.com>
parents: 8605
diff changeset
96 n = numel (s1);
8605
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
97 for i = 1:numel (names)
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
98 el = names(i);
8612
20d23d65cc84 fix & optimize orderfields
Jaroslav Hajek <highegg@gmail.com>
parents: 8605
diff changeset
99 [t(1:n).(el)] = s1(:).(el);
8605
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
100 endfor
8612
20d23d65cc84 fix & optimize orderfields
Jaroslav Hajek <highegg@gmail.com>
parents: 8605
diff changeset
101 ## inherit dimensions
20d23d65cc84 fix & optimize orderfields
Jaroslav Hajek <highegg@gmail.com>
parents: 8605
diff changeset
102 t = reshape (t, size (s1));
6862
d63339cbb205 [project @ 2007-09-05 06:49:29 by jwe]
jwe
parents: 6713
diff changeset
103 endif
5881
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
104
70b3f8f1a793 [project @ 2006-07-08 03:54:17 by jwe]
jwe
parents:
diff changeset
105 endfunction
8594
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
106
8605
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
107 %!shared a, b, c
8594
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
108 %! a = struct ("foo", {1, 2}, "bar", {3, 4});
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
109 %! b = struct ("bar", 6, "foo", 5);
8605
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
110 %! c = struct ("bar", {7, 8}, "foo", 9);
8594
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
111 %!test
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
112 %! a(2) = orderfields (b, a);
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
113 %! assert (a(2).foo, 5)
8605
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
114 %! assert (a(2).bar, 6)
8594
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
115 %!test
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
116 %! a(2) = orderfields (b, [2 1]);
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
117 %! assert (a(2).foo, 5)
8605
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
118 %! assert (a(2).bar, 6)
8594
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
119 %!test
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
120 %! a(2) = orderfields (b, fieldnames (a));
756b0ba61350 orderfields.m: avoid loop for non-empty cases. New tests.
Jason Riedy <jason@acm.org>
parents: 8455
diff changeset
121 %! assert (a(2).foo, 5)
8605
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
122 %! assert (a(2).bar, 6)
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
123 %!test
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
124 %! a(1:2) = orderfields (c, fieldnames (a));
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
125 %! assert (a(2).foo, 9)
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
126 %! assert (a(2).bar, 8)
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
127
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
128 %!test
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
129 %! aa.x = {1, 2};
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
130 %! aa.y = 3;
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
131 %! aa(2).x = {4, 5};
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
132 %! bb.y = {6, 7};
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
133 %! bb.x = 8;
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
134 %! aa(2) = orderfields (bb, aa);
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
135 %! assert (aa(2).x, 8);
9fc72c114ce1 Really fix indexing in orderfields.m
Jason Riedy <jason@acm.org>
parents: 8594
diff changeset
136 %! assert (aa(2).y{1}, 6);