comparison m-toolbox/sltpda/sortCmds.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function cmds = sortCmds(cmds)
2
3 % SORTCMDS sort a command list so that all inputs and outputs are valid.
4 %
5 % M Hewitson 27-03-07
6 %
7
8 disp(' + sorting command list');
9
10 nswapped = 1;
11 while nswapped > 0
12
13 nswapped = 0;
14 % first assign numbers
15 for j=1:length(cmds)
16 cmds(j).n = j;
17 end
18
19 nc = length(cmds);
20 for j=1:nc
21
22 cmd = cmds(j);
23 % if I have inputs, find the commands
24 % that come before me
25 cs = [];
26 if ~isempty(cmd.ins)
27 % go through my inputs
28 for i=1:length(cmd.ins)
29 input = cmd.ins{i};
30 % check against all commands
31 for k=1:nc
32 outputs = cmds(k).outs;
33 for o=1:length(outputs)
34 if strcmp(outputs{o}, input)
35 cs = [cs k];
36 end
37 end
38 end
39 end
40 end
41
42 % go through these
43 for k=1:length(cs)
44 csj = cs(k);
45 cmdn = j;
46
47 if csj > cmdn
48 % disp(sprintf('** swapping %d and %d', csj, cmdn))
49 nswapped = nswapped + 1;
50 c1 = cmds(csj);
51 c2 = cmds(cmdn);
52 % swap
53 cmds(cmdn) = c1;
54 cmds(csj) = c2;
55 end
56 end
57
58 end
59
60 % they should be sorted now so renumber
61 for j=1:length(cmds)
62 cmds(j).n = j;
63 end
64
65 end
66
67 % DON'T NEED THIS
68
69 % now remove unnecessary commands
70 % - find all commands whose output is not an input
71 useful = [];
72 newcmds = [];
73 for k=1:length(cmds)
74
75 cmd = cmds(k);
76 outs = cmd.outs;
77
78 % go through each of my outputs
79 hasUse = 0;
80 for j=1:length(outs)
81 out = char(outs{j});
82 % check against all other inputs
83 for i=k+1:length(cmds)
84 ccmd = cmds(i);
85 ins = ccmd.ins;
86 for a=1:length(ins)
87 in = char(ins(a));
88 % disp(sprintf('------- checking out(%s) against in(%s) ', out,
89 % in))
90 if strcmp(out, in)
91 hasUse = 1;
92 end
93 end
94 end
95 end
96 % All sinks fail this test but we don't want to discard them. In fact, we
97 % only want to discard lines that have an output and this output is not
98 % used.
99 if isempty(outs)
100 hasUse = 1;
101 end
102
103 % if hasUse
104 newcmds = [newcmds cmd];
105 % else
106 % disp(sprintf('## Discarding %s', cmd.cmd))
107 % end
108 %
109 end
110
111 % now we have a slightly reduced list. The next step of cleaning up should
112 % come in the fncs that run and write m files.
113 cmds = newcmds;
114
115
116 % END