The benchmark works as far as I can tell (passes internal tests). We just need to...
[IRC.git] / Robust / src / Benchmarks / oooJava / DelaunayRefinement / DirectedGraph.java
1 public class DirectedGraph implements Graph {
2   protected HashSet nodes;
3
4   public DirectedGraph() {
5     // nodes = Collections.synchronizedSet(new HashSet());
6     nodes = new HashSet();
7   }
8
9   public boolean addNeighbor(Node src, Node dest) {
10     GraphNode src_c = (GraphNode) src;
11     GraphNode dest_c = (GraphNode) dest;
12     return src_c.addOutNeighbor(dest_c) ? dest_c.addInNeighbor(src_c) : false;
13   }
14
15   public boolean addNode(Node n) {
16     return nodes.add((GraphNode) n);
17   }
18
19   public boolean containsNode(Node n) {
20     return nodes.contains(n);
21   }
22
23   public Node createNode(Object n) {
24     return new GraphNode(n);
25   }
26
27   // Not proper way to do it, but it seems that no code uses it, so
28   // this should be okay.
29   public Iterator getInNeighbors(Node src) {
30     GraphNode src_c = (GraphNode) src;
31     // return Collections.unmodifiableCollection(src_c.getInNeighbors());
32     return src_c.getInNeighborsCopy();
33   }
34
35   public int getInNeighborsSize(Node node) {
36     return ((GraphNode)node).inNeighbors.size();
37   }
38
39   public int getNumNodes() {
40     return nodes.size();
41   }
42
43   public Iterator getOutNeighbors(Node src) {
44     GraphNode src_c = (GraphNode) src;
45     // return Collections.unmodifiableCollection(src_c.getOutNeighbors());
46     return src_c.getOutNeighborsCopy();
47   }
48
49   public int getOutNeighborsSize(Node node) {
50     return ((GraphNode)node).outNeighbors.size();
51   }
52
53   public Node getRandom() {
54     return (Node) nodes.iterator().next();
55   }
56
57   public boolean hasNeighbor(Node src, Node dest) {
58     GraphNode src_c = (GraphNode) src;
59     GraphNode dest_c = (GraphNode) dest;
60     return src_c.hasOutNeighbor(dest_c);
61   }
62
63   public boolean removeNeighbor(Node src, Node dest) {
64     GraphNode src_c = (GraphNode) src;
65     GraphNode dest_c = (GraphNode) dest;
66     return src_c.removeOutNeighbor(dest_c) ? dest_c.removeInNeighbor(src_c) : false;
67   }
68
69   public boolean removeNode(Node n) {
70     removeConnectingEdges((GraphNode) n);
71     return nodes.remove(n);
72   }
73
74   protected void removeConnectingEdges(GraphNode n) {
75     GraphNode g;
76     for (Iterator iterator1 = n.getOutNeighborsCopy(); iterator1.hasNext(); removeNeighbor(n, g)) {
77       g = (GraphNode) iterator1.next();
78     }
79
80     for (Iterator iterator2 = n.getInNeighborsCopy(); iterator2.hasNext(); removeNeighbor(g, n)) {
81       g = (GraphNode) iterator2.next();
82     }
83
84   }
85
86   public Object getNodeData(Node n) {
87     return ((GraphNode) n).data;
88   }
89
90   public Object setNodeData(Node n, Object d) {
91     GraphNode gn = (GraphNode) n;
92     Object retval = gn.data;
93     gn.data = d;
94     return retval;
95   }
96
97   public Iterator iterator() {
98     return nodes.iterator();
99   }
100
101   public boolean isDirected() {
102     return true;
103   }
104 }