package sds.assemble.controlflow;

import java.util.HashSet;
import java.util.Set;

/**
 * This class is for searching dominator node.
 * @author inagaki
 */
public class DominatorNodeSearcher {
	/**
	 * returns common dominator node from edges.
	 * @param edge1 first edge
	 * @param edge2 second edge
	 * @return common dominator node
	 */
	public static CFNode searchCommon(CFEdge edge1, CFEdge edge2) {
		return searchCommon(edge1.getDest(), edge2.getDest());
	}

	/**
	 * returns common dominator node from node and edge.
	 * @param node target node
	 * @param edge target edge
	 * @return common dominator node
	 */
	public static CFNode searchCommon(CFNode node, CFEdge edge) {
		return searchCommon(node, edge.getDest());
	}

	/**
	 * returns common dominator node from nodes.
	 * @param node1 first node
	 * @param node2 second node
	 * @return common dominator node
	 */
	public static CFNode searchCommon(CFNode node1, CFNode node2) {
		Set<CFNode> nodeSet = new HashSet<>();
		nodeSet.add(node1);
		while((node1 = node1.getImmediateDominator()) != null) {
			nodeSet.add(node1);
		}
		nodeSet.add(node2);
		while((node2 = node2.getImmediateDominator()) != null) {
			if(nodeSet.contains(node2)) {
				return node2;
			}
		}
		throw new CFNodeException("not found common dominator node.");
	}
}
@@ -1002,16 +1002,37 @@
 node1);%0A
+%09%09CFNode n1 = node1;%0A
 %09%09while(
@@ -1037,19 +1037,13 @@
 e((n
-ode
 1 = n
-ode
 1.ge
@@ -1088,27 +1088,24 @@
 odeSet.add(n
-ode
 1);%0A%09%09%7D%0A%09%09no
@@ -1122,16 +1122,37 @@
 node2);%0A
+%09%09CFNode n2 = node2;%0A
 %09%09while(
@@ -1157,19 +1157,13 @@
 e((n
-ode
 2 = n
-ode
 2.ge
@@ -1220,19 +1220,16 @@
 ntains(n
-ode
 2)) %7B%0A%09%09
@@ -1238,19 +1238,16 @@
 return n
-ode
 2;%0A%09%09%09%7D%0A
@@ -1314,10 +1314,159 @@
 node
-.%22
+ between %22%0A%09%09%09%09%09+ node1.getStart().getPc() + %22-%22 + node1.getEnd().getPc()%0A%09%09%09%09%09+ %22 and %22%0A%09%09%09%09%09+ node2.getStart().getPc() + %22-%22 + node2.getEnd().getPc()
 );%0A%09

change variable name and message for throwing exception.