diff --git a/code/classes/SCDclass_algo.m b/code/classes/SCDclass_algo.m index 8a97167e8a4ef0adbfe44f22e6afa37b1408eec5..df9ccb77fe74f9572f11299f965617adbbcee25e 100644 --- a/code/classes/SCDclass_algo.m +++ b/code/classes/SCDclass_algo.m @@ -118,12 +118,13 @@ classdef SCDclass_algo fpinits = obj.fpinits; end - function [refdd,refddparent] = getrefdd(obj) + function refdd = getrefdd(obj) refdd = obj.refdatadictionaries; - refddparent = obj.refddparentalgo; end - + function refddparent = getrefddparentalgo(obj) + refddparent = obj.refddparentalgo; + end %% Setup functions setter and getter function obj = addstdinitfcn(obj,fcnhandle) diff --git a/code/classes/SCDclass_expcode.m b/code/classes/SCDclass_expcode.m index c7a478001fc94c5bac20f6729ed2b9c7c1cd4bb2..17dc92ba8d2d0de377ddcb3481480c0a960924c3 100644 --- a/code/classes/SCDclass_expcode.m +++ b/code/classes/SCDclass_expcode.m @@ -555,26 +555,35 @@ classdef SCDclass_expcode end function obj = sortalgoobjlist(obj) - % sort algoobj list such that ones without dependency on others - % (through parents of referenced data dictionaries) come first. + % sort algoobj list such that no algoobj has dependency on other + % algoobj that are further down the list. So running algoobj inits + % in this order ensures that everything is initialized in the correct order. + A = zeros(numel(obj.algoobjlist)); % init adiadency matrix + % This matrix element (col,row)=(j,i) will contain 1 if the + % algorithm in algoobjlist(i) references a data dictionary which + % depends on the algoritm in algoobjlist(j). + for ii=1:numel(obj.algoobjlist) myalgoobj = obj.algoobjlist{ii}; - [~,refddparent] = myalgoobj.getrefdd; - if isempty(refddparent) || all(cellfun(@isempty,refddparent)) + refddparentalgo = myalgoobj.getrefddparentalgo; + if isempty(refddparentalgo) || all(cellfun(@isempty,refddparentalgo)) A(:,ii) = 0; % no dependency else - % populate dependencies - % indices of dependent algos - for iref = 1:numel(refddparent) + % find indices of dependent algos + for iref = 1:numel(refddparentalgo) % loop on possibly many dd algos % index of this referenced parent algo in algoobjlist - jj = contains(obj.algonamelist,refddparent{iref}.getname); + jj = contains(obj.algonamelist,refddparentalgo{iref}.getname); A(jj,ii) = 1; end end end + % Use matlab tools to sort the graph. Could also write e.g. Kahn's + % algorithm explicitly (see wikipedia/topological_sorting) D = digraph(A); % directed graph - isort = toposort(D); % topological sorting + % topological sorting, maintaining index order where possible + isort = toposort(D,'Order','stable'); + obj.algoobjlist = obj.algoobjlist(isort); % sort the init obj list end