Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/findshapes slow #270

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions +types/+untyped/+datastub/findShapes.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,29 @@
start = 1;
step = 1;
count = 0;
for i = 1:length(indices)
tempStart = indices(i);
if length(indices) - i <= count
% number of elements cannot possibly be larger than what we have.
for stepInd = 2:length(indices)
tempStep = indices(stepInd) - indices(1);
idealRange = indices(1):tempStep:indices(end);
if length(idealRange) <= count
break;
end
for j = 1:(length(indices)-i)
tempStep = indices(i+j) - indices(i);
for k = length(indices):-1:i
tempStop = indices(k);
idealRange = tempStart:tempStep:tempStop;
rangeMatches = ismembc(idealRange, indices);
numMatches = sum(rangeMatches);
if numMatches <= count
% number of intersected items is shorter than what we have.
break;
end
if all(rangeMatches)
start = tempStart;
step = tempStep;
stop = tempStop;
count = numMatches;
break;
end
end
rangeMatches = ismembc(idealRange, indices);
startInd = find(rangeMatches, 1);
stopInd = find(rangeMatches, 1, 'last');
splitPoints = find(~rangeMatches(startInd:stopInd)) + startInd - 1;
if ~isempty(splitPoints)
subStarts = [startInd (splitPoints + 1)];
subStops = [(splitPoints - 1) stopInd];
[~, largestSegInd] = max(subStops - subStarts + 1, [], 'linear');
startInd = subStarts(largestSegInd);
stopInd = subStops(largestSegInd);
end
subCount = sum(rangeMatches(startInd:stopInd));
if subCount > count
start = idealRange(startInd);
stop = idealRange(stopInd);
step = tempStep;
count = subCount;
end
end
optimalBlock = Block('start', start, 'step', step, 'stop', stop);
Expand Down