public static Dictionary <int, V> Apply <V>(Graph graph, BasicIterativeAlgorithmParameters <V> param, Dictionary <int, int> order = null) { var MOP = new Dictionary <int, V>(); foreach (BasicBlock blockTo in graph) { MOP[blockTo.BlockId] = param.StartingValue; foreach (var path in GraphAlgorithms.FindAllPaths(graph, blockTo.BlockId)) { var value = path.Aggregate(param.FirstValue, param.TransferFunction); MOP[blockTo.BlockId] = param.GatherOperation(new List <V> { MOP[blockTo.BlockId], value }); } } return(MOP); }
public static IterativeAlgorithmOutput <V> Apply <V>(Graph graph, BasicIterativeAlgorithmParameters <V> param, Dictionary <int, int> order = null) { IterativeAlgorithmOutput <V> result = new IterativeAlgorithmOutput <V>(); foreach (BasicBlock bb in graph) { result.Out[bb.BlockId] = param.StartingValue; } IEnumerable <BasicBlock> g = order == null ? graph : graph.OrderBy(x => order[x.BlockId]).Select(x => x);// order.Select(i => graph.getBlockById(i)); bool changed = true; while (changed) { changed = false; foreach (BasicBlock bb in g) { BasicBlocksList parents = param.ForwardDirection ? graph.getParents(bb.BlockId) : graph.getChildren(bb.BlockId); if (parents.Blocks.Count > 0) { result.In[bb.BlockId] = param.GatherOperation(parents.Blocks.Select(b => result.Out[b.BlockId])); } else { result.In[bb.BlockId] = param.FirstValue; } V newOut = param.TransferFunction(result.In[bb.BlockId], bb); changed = changed || !param.AreEqual(result.Out[bb.BlockId], newOut); result.Out[bb.BlockId] = newOut; } } if (!param.ForwardDirection) { result = new IterativeAlgorithmOutput <V> { In = result.Out, Out = result.In } } ; return(result); } }