static void DFSVisit(Node node, Dictionary<Node, int> nodeStateMap, List<Node> list) { nodeStateMap.Add(node, VISITING); List<Node> nodes = node.GetChildren(); IEnumerable iter = nodes; foreach (Node nodeI in iter) { if (IsNotVisited(nodeI, nodeStateMap)) DFSVisit(nodeI, nodeStateMap, list); } nodeStateMap[node] = VISITED; list.Add(node); }
/// <summary> /// Returns false if a depth-first search of Graph yields no back edges. /// If back edges found then return true and update cycleStack. /// </summary> /// <param name="node"></param> /// <param name="nodeStateMap"></param> /// <param name="cycleStack"></param> /// <returns></returns> public static Boolean DFSVisit(Node node, Dictionary<Node, int> nodeStateMap, List<Node> cycleStack) { cycleStack.Add(node); nodeStateMap.Add(node, visiting); List<Node> nodes = node.GetChildren(); IEnumerable iter = nodes; foreach (Node nodeI in iter) { if (IsNotVisited(nodeI, nodeStateMap)) { if (DFSVisit(nodeI, nodeStateMap, cycleStack)) return true; } else if (IsVisiting(nodeI, nodeStateMap)) { cycleStack.Insert(0,node); return true; } } nodeStateMap[node] = visited; cycleStack.RemoveAt(0); return false; }
// // TODO Jun: Re-evaluate the topsort implementation // static void DFSVisit(Node node, Dictionary <Node, int> nodeStateMap, List <Node> list, AST statementList) { nodeStateMap.Add(node, VISITING); List <Node> nodes = node.GetChildren(); Dictionary <int, Node> nodeDic = node.GetChildrenWithIndices(); IEnumerable iter = nodes; int j = 0; foreach (Node nodeI in iter) { if (node is IdentNode && nodeI is LiteralNode) { BuildIdentToLiteralStatement(node, nodeI, statementList); } else if (node is IdentNode && nodeI is IdentNode) { BuildIdentToIdentStatement(node, nodeI, statementList); } else if (node is IdentNode && nodeI is Block) { Block blocknode = (Block)nodeI; if (GraphUtilities.AnalyzeString(blocknode.Name) == SnapshotNodeType.Literal) { LiteralNode literal = new LiteralNode(blocknode.content, nodeI.Guid); BuildIdentToLiteralStatement(node, literal, statementList); } else { j = BuildIdentToCodeBlockIdentStatement(node, nodeI, nodes, statementList, j); } } else if (node is Operator && nodeI is Block) { j = BuildOperatorToBlockStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is Block) { j = BuildFuncToBlockStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is IdentNode) { j = BuildFuncToIdentStatement(node, nodeI, nodeDic, statementList, j); } else if (node is IdentNode && nodeI is Operator) { BuildIdentToOperatorStatement(node, statementList, nodeI); } else if (node is Operator && nodeI is Operator) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if (node is IdentNode && nodeI is Func) { BuildIdentToFuncStatement(node, nodeI, statementList); } else if (node is Func && nodeI is Func) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Operator && nodeI is Func) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is Operator) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Operator && nodeI is ArrayNode) || (node is Operator && nodeI is LiteralNode)) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Func && nodeI is ArrayNode) || (node is Func && nodeI is LiteralNode)) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Block && nodeI is Block)) { BuildBlockToBlockStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is Func)) { BuildBlockToFuncStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is Operator)) { BuildBlockToFuncStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is IdentNode)) { BuildBlockToIdentStatement(node, nodeI, statementList); } /*Block to Operator*/ else if (node is Block && nodeI is Operator) { //BuildBlockToOperatorStatement(node, nodeI, statementList); } //else if ((node is Block && nodeI is Func)) //{ // BuildBlockToBlockStatement(node, nodeI, statementList); //} else { if (node is Operator) { if (nodes.IndexOf(nodeI, j) == 0) { Assignment a = (Assignment)statementList.GetNode(node.Guid); ((BinExprNode)a.right).left = nodeI; ++j; } else { Assignment a = (Assignment)statementList.GetNode(node.Guid); ((BinExprNode)a.right).right = nodeI; } } else if (node is Func) { Assignment a = (Assignment)statementList.GetNode(node.Guid); FunctionCall f = ((FunctionCall)a.right); f.parameters[nodes.IndexOf(nodeI, j)] = nodeI; j = 0; } } if (IsNotVisited(nodeI, nodeStateMap)) { DFSVisit(nodeI, nodeStateMap, list, statementList); } } nodeStateMap[node] = VISITED; list.Add(node); }
// // TODO Jun: Re-evaluate the topsort implementation // static void DFSVisit(Node node, Dictionary<Node, int> nodeStateMap, List<Node> list, AST statementList) { nodeStateMap.Add(node, VISITING); List<Node> nodes = node.GetChildren(); Dictionary<int, Node> nodeDic = node.GetChildrenWithIndices(); IEnumerable iter = nodes; int j = 0; foreach (Node nodeI in iter) { if (node is IdentNode && nodeI is LiteralNode) { BuildIdentToLiteralStatement(node, nodeI, statementList); } else if (node is IdentNode && nodeI is IdentNode) { BuildIdentToIdentStatement(node, nodeI, statementList); } else if (node is IdentNode && nodeI is Block) { Block blocknode = (Block)nodeI; if (GraphBuilder.AnalyzeString(blocknode.Name) == SnapshotNodeType.Literal) { LiteralNode literal = new LiteralNode(blocknode.content, nodeI.Guid); BuildIdentToLiteralStatement(node, literal, statementList); } else { j = BuildIdentToCodeBlockIdentStatement(node, nodeI, nodes, statementList, j); } } else if (node is Operator && nodeI is Block) { j = BuildOperatorToBlockStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is Block) { j = BuildFuncToBlockStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is IdentNode) { j = BuildFuncToIdentStatement(node, nodeI, nodeDic, statementList, j); } else if (node is IdentNode && nodeI is Operator) { BuildIdentToOperatorStatement(node, statementList, nodeI); } else if (node is Operator && nodeI is Operator) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if (node is IdentNode && nodeI is Func) { BuildIdentToFuncStatement(node, nodeI, statementList); } else if (node is Func && nodeI is Func) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Operator && nodeI is Func) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is Operator) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Operator && nodeI is ArrayNode) || (node is Operator && nodeI is LiteralNode)) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Func && nodeI is ArrayNode) || (node is Func && nodeI is LiteralNode)) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Block && nodeI is Block)) { BuildBlockToBlockStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is Func)) { BuildBlockToFuncStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is Operator)) { BuildBlockToFuncStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is IdentNode)) { BuildBlockToIdentStatement(node, nodeI, statementList); } /*Block to Operator*/ else if (node is Block && nodeI is Operator) { //BuildBlockToOperatorStatement(node, nodeI, statementList); } //else if ((node is Block && nodeI is Func)) //{ // BuildBlockToBlockStatement(node, nodeI, statementList); //} else { if (node is Operator) { if (nodes.IndexOf(nodeI, j) == 0) { Assignment a = (Assignment)statementList.GetNode(node.Guid); ((BinExprNode)a.right).left = nodeI; ++j; } else { Assignment a = (Assignment)statementList.GetNode(node.Guid); ((BinExprNode)a.right).right = nodeI; } } else if (node is Func) { Assignment a = (Assignment)statementList.GetNode(node.Guid); FunctionCall f = ((FunctionCall)a.right); f.parameters[nodes.IndexOf(nodeI, j)] = nodeI; j = 0; } } if (IsNotVisited(nodeI, nodeStateMap)) { DFSVisit(nodeI, nodeStateMap, list, statementList); } } nodeStateMap[node] = VISITED; list.Add(node); }
/// <summary> /// Removes the node passed as parameter from the graph(represented using NodeMap) /// </summary> /// <param name="rem"></param> /// <returns></returns> public bool RemoveNode(Node rem) { if (nodeMap.ContainsKey(rem.Guid)) { IEnumerable iter = rem.GetChildren(); foreach (Node child in iter) { child.RemoveParent(rem); } iter = rem.GetParents(); foreach (Node dad in iter) { dad.RemoveChild(rem); } nodeMap.Remove(rem.Guid); nodeList.Remove(rem); return true; } return false; }
/// <summary> /// Remove all edges from or to the node passed as parameter. /// </summary> /// <param name="rem"></param> /// <returns></returns> public bool RemoveAllEdges(Node rem) { if (nodeMap.ContainsKey(rem.Guid)) { IEnumerable iter = rem.GetChildren(); foreach (Node child in iter) { rem.RemoveChild(child); child.RemoveParent(rem); } iter = rem.GetParents(); foreach (Node dad in iter) { rem.RemoveChild(dad); dad.RemoveChild(rem); } return true; } return false; }