Mercurial > pytave
changeset 298:df1bddece9d5
Use subsref in "cell" method for casting to cell
Remove custom loop and replace it with call to improved subsref.
* @pyobject/cell.m: Use subsref instead of loop.
* changed @pyobject/fieldnames.m: Use cell.
* changed @pyobject/methods.m: Use cell.
author | Colin Macdonald <cbm@m.fsf.org> |
---|---|
date | Wed, 03 Aug 2016 20:33:33 -0700 |
parents | e7ee2c2e64e8 |
children | 714d1bf1ef78 |
files | @pyobject/cell.m @pyobject/fieldnames.m @pyobject/methods.m |
diffstat | 3 files changed, 9 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/@pyobject/cell.m Wed Aug 03 20:22:42 2016 -0700 +++ b/@pyobject/cell.m Wed Aug 03 20:33:33 2016 -0700 @@ -67,15 +67,13 @@ ## @end defmethod -function c = cell (L) - ## XXX: better implementation available possible in C++? - - c = {}; - #c = cell (size (L)); # scary, b/c "size" calls "cell" - - for i = 1:length (L) # not numel - c{i} = subsref (L, struct ("type", "{}", "subs", {{i}})); - endfor +function c = cell (x) + ## FIXME: subsref should take care of this case + if (length (x) == 0) + c = cell (0, 1); + return + endif + c = subsref (x, struct ("type", "{}", "subs", {{":"}})); endfunction
--- a/@pyobject/fieldnames.m Wed Aug 03 20:22:42 2016 -0700 +++ b/@pyobject/fieldnames.m Wed Aug 03 20:33:33 2016 -0700 @@ -53,14 +53,7 @@ " and not a.startswith('_')]"]); names_obj = pycall (cmd, x); - ## FIXME: names = cellfun (@char, cell (names_obj), "uniformoutput", false); - len = length (names_obj); - names = cell (len, 1); - if (len > 0) - idx = struct ("type", "{}", "subs", {{1:len}}); - [names{1:len}] = subsref (names_obj, idx); - names = cellfun (@char, names, "uniformoutput", false); - endif + names = cellfun (@char, cell (names_obj), "uniformoutput", false); endfunction
--- a/@pyobject/methods.m Wed Aug 03 20:22:42 2016 -0700 +++ b/@pyobject/methods.m Wed Aug 03 20:33:33 2016 -0700 @@ -73,14 +73,7 @@ mtds_list_obj = pycall (cmd, x); - ## FIXME: mtds_list = cellfun (@char, cell (mtds_list_obj), "uniformoutput", false); - len = length (mtds_list_obj); - mtds_list = cell (len, 1); - if (len > 0) - idx = struct ("type", "{}", "subs", {{1:len}}); - [mtds_list{1:len}] = subsref (mtds_list_obj, idx); - mtds_list = cellfun (@char, mtds_list, "uniformoutput", false); - endif + mtds_list = cellfun (@char, cell (mtds_list_obj), "uniformoutput", false); if (nargout == 0) ## FIXME: should this be available as @pyobject/ismodule.m ?