Mercurial > hg > octave-kai
changeset 8594:756b0ba61350
orderfields.m: avoid loop for non-empty cases. New tests.
author | Jason Riedy <jason@acm.org> |
---|---|
date | Mon, 26 Jan 2009 22:43:29 -0500 |
parents | 4e39b00218d3 |
children | dee5d60257e4 |
files | scripts/ChangeLog scripts/miscellaneous/orderfields.m |
diffstat | 2 files changed, 21 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,7 @@ +2009-01-26 Jason Riedy <jason@acm.org> + + * miscellaneous/orderfields.m: Also avoid loop for non-empty structs. + 2009-01-17 Jaroslav Hajek <highegg@gmail.com> * optimization/fsolve.m: Disable Broyden updates for sparse jacobians.
--- a/scripts/miscellaneous/orderfields.m +++ b/scripts/miscellaneous/orderfields.m @@ -87,16 +87,26 @@ endif ## Permute the names in the structure. + args = cell (1, 2 * numel (names)); + args(1:2:end) = names; if (numel (s1) == 0) - args = cell (1, 2 * numel (names)); - args(1:2:end) = names; args(2:2:end) = {[]}; - t = struct (args{:}); else - for i = 1:numel (names) - el = names(i); - t(:).(el) = s1(:).(el); - endfor + args(2:2:end) = {s1.(names)}; endif + t = struct (args{:}); endfunction + +%!shared a, b +%! a = struct ("foo", {1, 2}, "bar", {3, 4}); +%! b = struct ("bar", 6, "foo", 5); +%!test +%! a(2) = orderfields (b, a); +%! assert (a(2).foo, 5) +%!test +%! a(2) = orderfields (b, [2 1]); +%! assert (a(2).foo, 5) +%!test +%! a(2) = orderfields (b, fieldnames (a)); +%! assert (a(2).foo, 5)