Skip to content

Commit

Permalink
reoptimize for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Oct 24, 2019
1 parent b5cfba1 commit 398539d
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 83 deletions.
12 changes: 9 additions & 3 deletions decodevarname.m
Expand Up @@ -30,12 +30,18 @@
% License: GPLv3 or 3-clause BSD license, see https://github.com/fangq/easyh5 for details
%

isunpack=jsonopt('UnpackHex',1,varargin{:});
newname=name;
if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))
return
isunpack=1;
if(nargin==2 && ~isstruct(varargin{1}))
isunpack=varargin{1};
elseif(nargin>1)
isunpack=jsonopt('UnpackHex',1,varargin{:});
end

if(isunpack)
if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))
return
end
if(exist('native2unicode','builtin'))
h2u=@hex2unicode;
newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${h2u($2)}');
Expand Down
2 changes: 1 addition & 1 deletion encodevarname.m
Expand Up @@ -37,7 +37,7 @@
% License: GPLv3 or 3-clause BSD license, see https://github.com/fangq/easyh5 for details
%

if(~isempty(regexp(str,'^[^A-Za-z]','once')))
if(~isvarname(str(1)))
if(exist('unicode2native','builtin'))
str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');
else
Expand Down
43 changes: 24 additions & 19 deletions jdatadecode.m
Expand Up @@ -54,7 +54,12 @@
%

newdata=data;
opt=varargin2struct(varargin{:});
opt=struct;
if(nargin==2)
opt=varargin{1};
elseif(nargin>2)
opt=varargin2struct(varargin{:});
end

%% process non-structure inputs
if(~isstruct(data))
Expand All @@ -75,8 +80,8 @@
len=length(data);
needbase64=jsonopt('Base64',0,opt);
format=jsonopt('FormatVersion',2,opt);
prefix=jsonopt('Prefix',sprintf('x0x%X','_'+0),opt);
if(~any(ismember(N_('_ArrayType_'),fn)) && any(ismember('x_ArrayType_',fn)))
prefix=jsonopt('Prefix','x0x5F',opt);
if(~isfield(data,N_('_ArrayType_')) && isfield(data,'x_ArrayType_'))
prefix='x';
opt.prefix='x';
end
Expand All @@ -95,15 +100,15 @@
end

%% handle array data
if(any(ismember(N_('_ArrayType_'),fn)) && (any(ismember(N_('_ArrayData_'),fn)) || any(ismember(N_('_ArrayZipData_'),fn))))
if(isfield(data,N_('_ArrayType_')) && (isfield(data,N_('_ArrayData_')) || isfield(data,N_('_ArrayZipData_'))))
newdata=cell(len,1);
for j=1:len
if(any(ismember(N_('_ArrayZipSize_'),fn)) && any(ismember(N_('_ArrayZipData_'),fn)))
if(isfield(data,N_('_ArrayZipSize_')) && isfield(data,N_('_ArrayZipData_')))
zipmethod='zip';
if(any(ismember(N_('_ArrayZipType_'),fn)))
if(isfield(data,N_('_ArrayZipType_')))
zipmethod=data(j).(N_('_ArrayZipType_'));
end
if(any(ismember(zipmethod,{'zlib','gzip','lzma','lzip','lz4','lz4hc'})))
if(~isempty(strmatch(zipmethod,{'zlib','gzip','lzma','lzip','lz4','lz4hc'})))
decompfun=str2func([zipmethod 'decode']);
if(needbase64)
ndata=reshape(typecast(decompfun(base64decode(data(j).(N_('_ArrayZipData_')))),data(j).(N_('_ArrayType_'))),data(j).(N_('_ArrayZipSize_'))(:)');
Expand All @@ -119,18 +124,18 @@
end
ndata=cast(data(j).(N_('_ArrayData_')),char(data(j).(N_('_ArrayType_'))));
end
if(any(ismember(N_('_ArrayZipSize_'),fn)))
if(isfield(data,N_('_ArrayZipSize_')))
ndata=reshape(ndata(:),fliplr(data(j).(N_('_ArrayZipSize_'))(:)'));
ndata=permute(ndata,ndims(ndata):-1:1);
end
iscpx=0;
if(any(ismember(N_('_ArrayIsComplex_'),fn)))
if(isfield(data,N_('_ArrayIsComplex_')))
if(data(j).(N_('_ArrayIsComplex_')))
iscpx=1;
end
end
if(any(ismember(N_('_ArrayIsSparse_'),fn)) && data(j).(N_('_ArrayIsSparse_')))
if(any(ismember(N_('_ArraySize_'),fn)))
if(isfield(data,N_('_ArrayIsSparse_')) && data(j).(N_('_ArrayIsSparse_')))
if(isfield(data,N_('_ArraySize_')))
dim=double(data(j).(N_('_ArraySize_'))(:)');
if(iscpx)
ndata(end-1,:)=complex(ndata(end-1,:),ndata(end,:));
Expand All @@ -154,7 +159,7 @@
end
ndata=sparse(ndata(1,:),ndata(2,:),ndata(3,:));
end
elseif(any(ismember(N_('_ArraySize_'),fn)))
elseif(isfield(data,N_('_ArraySize_')))
if(iscpx)
ndata=complex(ndata(1,:),ndata(2,:));
end
Expand All @@ -175,7 +180,7 @@
end

%% handle table data
if(any(ismember(N_('_TableRecords_'),fn)))
if(isfield(data,N_('_TableRecords_')))
newdata=cell(len,1);
for j=1:len
ndata=data(j).(N_('_TableRecords_'));
Expand Down Expand Up @@ -205,7 +210,7 @@
end

%% handle map data
if(any(ismember(N_('_MapData_'),fn)))
if(isfield(data,N_('_MapData_')))
newdata=cell(len,1);
for j=1:len
key=cell(1,length(data(j).(N_('_MapData_'))));
Expand All @@ -223,7 +228,7 @@
end

%% handle graph data
if(any(ismember(N_('_GraphNodes_'),fn)) && exist('graph','file') && exist('digraph','file'))
if(isfield(data,N_('_GraphNodes_')) && exist('graph','file') && exist('digraph','file'))
newdata=cell(len,1);
isdirected=1;
for j=1:len
Expand All @@ -241,12 +246,12 @@
nodetable=table;
end

if(any(ismember(N_('_GraphEdges_'),fn)))
if(isfield(data,N_('_GraphEdges_')))
edgedata=data(j).(N_('_GraphEdges_'));
elseif(any(ismember(N_('_GraphEdges0_'),fn)))
elseif(isfield(data,N_('_GraphEdges0_')))
edgedata=data(j).(N_('_GraphEdges0_'));
isdirected=0;
elseif(any(ismember(N_('_GraphMatrix_'),fn)))
elseif(isfield(data,N_('_GraphMatrix_')))
edgedata=jdatadecode(data(j).(N_('_GraphMatrix_')),varargin{:});
end

Expand All @@ -273,7 +278,7 @@
end

%% handle bytestream and arbitrary matlab objects
if(sum(ismember({N_('_ByteStream_'),N_('_DataInfo_')},fn))==2)
if(isfield(data,N_('_ByteStream_')) && isfield(data,N_('_DataInfo_'))==2)
newdata=cell(len,1);
for j=1:len
if(isfield(data(j).(N_('_DataInfo_')),'MATLABObjectClass'))
Expand Down
7 changes: 4 additions & 3 deletions jsonopt.m
Expand Up @@ -25,11 +25,12 @@
if(nargin<=2)
return;
end
key0=lower(key);
opt=varargin{1};
if(isstruct(opt))
if(isfield(opt,key))
if(isfield(opt,key0))
val=opt.(key0);
elseif(isfield(opt,key))
val=opt.(key);
elseif(isfield(opt,lower(key)))
val=opt.(lower(key));
end
end
8 changes: 4 additions & 4 deletions loadjson.m
Expand Up @@ -130,6 +130,10 @@
data=data{1};
end

if(jsonopt('JDataDecode',1,varargin{:})==1)
data=jdatadecode(data,'Base64',1,'Recursive',1,varargin{:});
end

if(isfield(opt,'progressbar_'))
close(opt.progressbar_);
end
Expand Down Expand Up @@ -425,10 +429,6 @@
end
end
pos=parse_char(inputstr, pos, '}');
if(jsonopt('JDataDecode',1,varargin{:})==1)
varargin{:}.Recursive=0;
object=jdatadecode(object,'Base64',1,varargin{:});
end
end

%%-------------------------------------------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions loadubjson.m
Expand Up @@ -92,6 +92,10 @@
if(jsoncount==1 && iscell(data))
data=data{1};
end

if(jsonopt('JDataDecode',1,varargin{:})==1)
data=jdatadecode(data,'Base64',0,'Recursive',1,varargin{:});
end
end

%%-------------------------------------------------------------------------
Expand Down Expand Up @@ -338,9 +342,6 @@
if(count==-1)
pos=parse_char(inputstr, pos, '}');
end
if(isstruct(object))
object=jdatadecode(object,'Recursive',0, 'Base64',0,varargin{:});
end
end

%%-------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions mergestruct.m
Expand Up @@ -27,7 +27,7 @@
end
fn=fieldnames(s2);
s=s1;
for i=1:length(fn)
s=setfield(s,fn{i},getfield(s2,fn{i}));
for i=1:length(fn)
s.(fn{i})=s2.(fn{i});
end

0 comments on commit 398539d

Please sign in to comment.