changeset 9863:3381a9ed0a6f octave-forge

Vectorized m-functions, using fields2cell, cell2fields, cell2struct.
author i7tiol
date Sun, 25 Mar 2012 09:01:46 +0000
parents d6a81872c52d
children 464cab005ba0
files main/struct/DESCRIPTION main/struct/inst/getfields.m main/struct/inst/setfields.m main/struct/inst/tars.m
diffstat 4 files changed, 56 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/main/struct/DESCRIPTION	Sun Mar 25 08:59:59 2012 +0000
+++ b/main/struct/DESCRIPTION	Sun Mar 25 09:01:46 2012 +0000
@@ -1,12 +1,12 @@
 Name: Struct
-Version: 1.0.9
-Date: 2011-10-13
-Author: Etienne Grossmann <etienne@cs.uky.edu> and Olaf Till <olaf.till@uni-jena.de>
-Maintainer: Etienne Grossmann <etienne@cs.uky.edu>
+Version: 1.0.10
+Date: 2012-03-25
+Author: Etienne Grossmann <etienne@cs.uky.edu> and Olaf Till <i7tiol@t-online.de>
+Maintainer: Olaf Till <i7tiol@t-online.de>
 Title: Structure Handling.
 Description: Additional Structure manipulations functions.
 Categories: Structs
 Depends: octave (>= 2.9.7)
 Autoload: yes
-License: GPL version 2 or later
+License: GPL, see individual files for version
 Url: http://octave.sf.net
--- a/main/struct/inst/getfields.m	Sun Mar 25 08:59:59 2012 +0000
+++ b/main/struct/inst/getfields.m	Sun Mar 25 09:01:46 2012 +0000
@@ -2,7 +2,7 @@
 ##
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
-## the Free Software Foundation; either version 2 of the License, or
+## the Free Software Foundation; either version 3 of the License, or
 ## (at your option) any later version.
 ##
 ## This program is distributed in the hope that it will be useful,
@@ -16,19 +16,30 @@
 ## -*- texinfo -*-
 ## @deftypefn {Built-in Function} {} [@var{v1},...] =
 ## getfields (@var{s}, 'k1',...) = [@var{s}.k1,...]
-## Return selected values from a struct. Provides some compatibility
-## and some flexibility.
+## Return selected values from a scalar struct. Provides some
+## compatibility and some flexibility.
 ## @end deftypefn
-## @seealso{setfields,rmfield,isfield,isstruct,fields,cmpstruct,struct}
+## @seealso{setfields,rmfield,isfield,isstruct,struct}
 
 ## Author:        Etienne Grossmann <etienne@cs.uky.edu>
 ## Last modified: January 2003
 
-function [varargout] = getfields(s,varargin)
-    for i=length(varargin):-1:1
-	varargout{i} = s.(varargin{i});
-    end
-end
+## modified by Olaf Till
+
+function [varargout] = getfields (s, varargin)
+
+  if (! all (isfield (s, varargin)))
+    error ("some fields not present");
+  endif
+
+  if (all (size (s) <= 1))
+    varargout = fields2cell (s, varargin);
+  else
+    error ("structure must be scalar or empty");
+  endif
+
+endfunction
+
 %!
-%!assert(getfields(struct('key','value'),'key'),'value')
+%!assert (getfields (struct ('key', 'value'), 'key'), 'value');
 %!
--- a/main/struct/inst/setfields.m	Sun Mar 25 08:59:59 2012 +0000
+++ b/main/struct/inst/setfields.m	Sun Mar 25 09:01:46 2012 +0000
@@ -2,7 +2,7 @@
 ##
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
-## the Free Software Foundation; either version 2 of the License, or
+## the Free Software Foundation; either version 3 of the License, or
 ## (at your option) any later version.
 ##
 ## This program is distributed in the hope that it will be useful,
@@ -26,20 +26,29 @@
 ## November 2000: Paul Kienzle <pkienzle@users.sf.net>
 ##     return error rather than trapping to keyboard
 
-function s = setfields(s,varargin)
-if nargin == 0
-  s= struct; % doesn't work on older versions of octave
-elseif rem(nargin,2) != 1,
-  error('setfields: expected struct, key1, val1, key2, val2, ...\n') ; 
-endif
-	
-for i=1:2:nargin-1
-  if ! ischar(varargin{i}) ,
-    error('setfields: called with non-string key') ; 
+## modified by Olaf Till
+
+function s = setfields (s, varargin)
+
+  if ((nargs = nargin ()) == 0)
+    s = struct ();
+  elseif (all (size (s) <= 1))
+    if (rem (nargs, 2))
+      pairs = reshape (varargin, 2, []);
+      if (! iscellstr (pairs(1, :)))
+	error ("setfields: called with non-string key");
+      endif
+      if (isempty (s))
+	s = struct (); # might have been an empty array
+      endif
+      s = cell2fields (pairs(2, :), pairs(1, :), 2, s);
+    else
+      error ("setfields: expected struct, key1, val1, key2, val2, ...");
+    endif
   else
-    s.(varargin{i}) = varargin{i+1};
-  end
-end
+    error ("structure must be scalar or empty");
+  endif
+
 %!
-%!assert(setfields({},'key','value'),struct('key','value'))
+%!assert (setfields ({}, 'key', 'value'), struct ('key', 'value'));
 %!
\ No newline at end of file
--- a/main/struct/inst/tars.m	Sun Mar 25 08:59:59 2012 +0000
+++ b/main/struct/inst/tars.m	Sun Mar 25 09:01:46 2012 +0000
@@ -8,8 +8,10 @@
 ## Author:        Etienne Grossmann <etienne@isr.ist.utl.pt>
 ## Last modified: October 2000
 
-function s = tars(varargin)
+## modified by Olaf Till
+
+function s = tars (varargin)
 
-for i=1:nargin
-   s.(deblank(argn(i,:))) = varargin{i};
-end
+  s = cell2struct (varargin, deblank (cellstr (argn)), 2);
+
+endfunction