Skip to content

Commit

Permalink
improved indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
ulfgri committed Nov 5, 2016
1 parent 2b8b865 commit fafcfd4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Basic/@gds_element/private/get_element_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static mxArray*
get_layer(element_t *pe)
{
if (pe->kind == GDS_SREF || pe->kind == GDS_AREF)
mexErrMsgTxt("get_element_data : element as no layer property");
return empty_matrix();
else
return mxCreateDoubleScalar((double)pe->layer);

Expand All @@ -143,7 +143,7 @@ static mxArray*
get_dtype(element_t *pe)
{
if (pe->kind == GDS_SREF || pe->kind == GDS_AREF)
mexErrMsgTxt("get_element_data : element as no [data|text|box|node] type property");
return empty_matrix();
else
return mxCreateDoubleScalar((double)pe->dtype);

Expand Down Expand Up @@ -266,7 +266,7 @@ get_sname(element_t *pe)
if (pe->kind == GDS_SREF || pe->kind == GDS_AREF)
return mxCreateString(pe->sname);
else
mexErrMsgTxt("get_element_data : element as no sname property");
return empty_matrix();

return NULL; /* make compiler happy */
}
Expand Down
35 changes: 33 additions & 2 deletions Basic/@gds_library/subsref.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@

% Ulf Griesmann, NIST, June 2011

switch ins.type
% convert cs-lists --> cell arrays
itype = {ins.type};
isubs = flatten({ins.subs});

% first indexing operator
switch itype{1}

case '()'

idx = ins.subs{:};
idx = isubs{1};

if ischar(idx) && idx == ':'
gstrs = glib.st(1:end);
Expand All @@ -39,5 +44,31 @@
error('gds_library.subsref : invalid indexing type.');

end

% pass additional structure indexing to structure subsref method
if length(itype) > 1
sins.type = itype{2};
sins.subs = isubs{2};
if iscell(gstrs)
gstrs = cellfun(@(x)subsref(x, sins), gstrs, 'Un',0);
else
gstrs = subsref(gstrs, sins);
end
end

% flatten a cell array
function fca = flatten(ca)

fca = cell(size(ca));

for k = 1:length(ca)
if iscell(ca{k})
ct = ca{k};
fca{k} = ct{1};
else
fca(k) = ca(k);
end
end
end

end
36 changes: 34 additions & 2 deletions Basic/@gds_structure/subsref.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@

% Ulf Griesmann, NIST, June 2011

switch ins.type
% convert cs-lists --> cell arrays
itype = {ins.type};
isubs = flatten({ins.subs});

% first indexing operator
switch itype{1}

case '()'

idx = ins.subs{:};
idx = isubs{1};

if ischar(idx) && idx == ':'
gelp = gstruct.el(1:end);
elseif length(idx) == 1
Expand All @@ -38,4 +44,30 @@

end

% pass additional element indexing to element subsref method
if length(itype) > 1
eins.type = itype{2};
eins.subs = isubs{2};
if iscell(gelp)
gelp = cellfun(@(x)subsref(x, eins), gelp, 'Un',0);
else
gelp = subsref(gelp, eins);
end
end

% flatten a cell array
function fca = flatten(ca)

fca = cell(size(ca));

for k = 1:length(ca)
if iscell(ca{k})
ct = ca{k};
fca{k} = ct{1};
else
fca(k) = ca(k);
end
end
end

end
2 changes: 1 addition & 1 deletion Basic/gdsii_version.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

% toolbox version and version date
tb_version = '136';
tb_date = '2016-October-24';
tb_date = '2016-November-04';
ltb_ver = [tb_version, ' (', tb_date, ')'];
if exist('OCTAVE_VERSION')
interpreter = 'Octave';
Expand Down
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2016-November-05
- version 137
- return [] in 'get_element_data' if an element does not have a requested
property instead of throwing an error.
- improved 'subsref' methods for gds_library and gds_structure objects that
enable more complex indexing operations. E.g. it is now possible to
write S(10).xy to get the xy record of the 10th element in a
structure without having to copy the element first.

2016-October-24
- version 136
- finally squashed bug in gdsio::now
Expand Down

0 comments on commit fafcfd4

Please sign in to comment.