octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Really fix indexing in orderfields.m


From: Jason Riedy
Subject: Really fix indexing in orderfields.m
Date: Tue, 27 Jan 2009 16:07:16 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

# HG changeset patch
# User Jason Riedy <address@hidden>
# Date 1233089444 18000
# Node ID 202bd681f367fad0c3966576569483be4deb0350
# Parent  827f0285a2016fd6ce72d2906e08faea48fb205a
Really fix indexing in orderfields.m

My previous fix did not work, and the tests were insufficient
to notice.  There was one original problem; the assignment to
a non-existent t(:) triggered an error about querying t's size
before it exists.  I replaced that error with a non-loop that
happened to pick only one name out of the struct and spread it
across all the fields.  The syntax struct.(name, name, ...)
doesn't work, although it'd be nice.

So the final fix is actually to add another loop to index
t(1), t(2), etc.  Not high-performance, but neither are
structs.

Sorry.  Struct arrays still hurt my head, so hopefully this is
a bit more correct.

diff -r 827f0285a201 -r 202bd681f367 scripts/ChangeLog
--- a/scripts/ChangeLog Tue Jan 27 12:48:34 2009 -0500
+++ b/scripts/ChangeLog Tue Jan 27 15:50:44 2009 -0500
@@ -1,3 +1,8 @@
+2009-01-27  Jason Riedy  <address@hidden>
+
+       * miscellaneous/orderfields.m: Really fix the indexing for struct
+       arrays.
+
 2009-01-27  Carlo de Falco  <address@hidden>
 
        * polynomial/spline.m: Doc fix.
diff -r 827f0285a201 -r 202bd681f367 scripts/miscellaneous/orderfields.m
--- a/scripts/miscellaneous/orderfields.m       Tue Jan 27 12:48:34 2009 -0500
+++ b/scripts/miscellaneous/orderfields.m       Tue Jan 27 15:50:44 2009 -0500
@@ -87,26 +87,49 @@
   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
-    args(2:2:end) = {s1.(names)};
+    for i = 1:numel (names)
+      el = names(i);
+      for k = 1:length (s1)
+       t(k).(el) = s1(k).(el);
+      endfor
+    endfor
   endif
-  t = struct (args{:});
 
 endfunction
 
-%!shared a, b
+%!shared a, b, c
 %! a = struct ("foo", {1, 2}, "bar", {3, 4});
 %! b = struct ("bar", 6, "foo", 5);
+%! c = struct ("bar", {7, 8}, "foo", 9);
 %!test
 %! a(2) = orderfields (b, a);
 %! assert (a(2).foo, 5)
+%! assert (a(2).bar, 6)
 %!test
 %! a(2) = orderfields (b, [2 1]);
 %! assert (a(2).foo, 5)
+%! assert (a(2).bar, 6)
 %!test
 %! a(2) = orderfields (b, fieldnames (a));
 %! assert (a(2).foo, 5)
+%! assert (a(2).bar, 6)
+%!test
+%! a(1:2) = orderfields (c, fieldnames (a));
+%! assert (a(2).foo, 9)
+%! assert (a(2).bar, 8)
+
+%!test
+%! aa.x = {1, 2};
+%! aa.y = 3;
+%! aa(2).x = {4, 5};
+%! bb.y = {6, 7};
+%! bb.x = 8;
+%! aa(2) = orderfields (bb, aa);
+%! assert (aa(2).x, 8);
+%! assert (aa(2).y{1}, 6);


reply via email to

[Prev in Thread] Current Thread [Next in Thread]