1
2
3
4
5
6
7
8
9
10
11
12 package edu.uci.ics.jung.graph.util;
13
14 import static com.google.common.base.Preconditions.checkNotNull;
15
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.Map;
21
22 import edu.uci.ics.jung.graph.Graph;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class DefaultParallelEdgeIndexFunction<V,E> implements EdgeIndexFunction<V,E>
39 {
40 protected Map<Context<Graph<V,E>,E>, Integer> edge_index = new HashMap<Context<Graph<V,E>,E>, Integer>();
41
42 private DefaultParallelEdgeIndexFunction() {
43 }
44
45
46
47
48
49
50 public static <V,E> DefaultParallelEdgeIndexFunction<V,E> getInstance() {
51 return new DefaultParallelEdgeIndexFunction<V,E>();
52 }
53
54
55
56
57
58
59 public int getIndex(Graph<V, E> graph, E e)
60 {
61 checkNotNull(graph, "graph must not be null");
62 checkNotNull(e, "'e' must not be null");
63 Integer index = edge_index.get(Context.<Graph<V,E>,E>getInstance(graph,e));
64
65 if(index == null) {
66 Pair<V> endpoints = graph.getEndpoints(e);
67 V u = endpoints.getFirst();
68 V v = endpoints.getSecond();
69 if(u.equals(v)) {
70 index = getIndex(graph, e, v);
71 } else {
72 index = getIndex(graph, e, u, v);
73 }
74 }
75 return index.intValue();
76 }
77
78 protected int getIndex(Graph<V,E> graph, E e, V v, V u) {
79 Collection<E> commonEdgeSet = new HashSet<E>(graph.getIncidentEdges(u));
80 commonEdgeSet.retainAll(graph.getIncidentEdges(v));
81 for(Iterator<E> iterator=commonEdgeSet.iterator(); iterator.hasNext(); ) {
82 E edge = iterator.next();
83 Pair<V> ep = graph.getEndpoints(edge);
84 V first = ep.getFirst();
85 V second = ep.getSecond();
86
87 if(first.equals(second) == true) {
88 iterator.remove();
89 }
90
91 if(first.equals(v) == false) {
92 iterator.remove();
93 }
94 }
95 int count=0;
96 for(E other : commonEdgeSet) {
97 if(e.equals(other) == false) {
98 edge_index.put(Context.<Graph<V,E>,E>getInstance(graph,other), count);
99 count++;
100 }
101 }
102 edge_index.put(Context.<Graph<V,E>,E>getInstance(graph,e), count);
103 return count;
104 }
105
106 protected int getIndex(Graph<V,E> graph, E e, V v) {
107 Collection<E> commonEdgeSet = new HashSet<E>();
108 for(E another : graph.getIncidentEdges(v)) {
109 V u = graph.getOpposite(v, another);
110 if(u.equals(v)) {
111 commonEdgeSet.add(another);
112 }
113 }
114 int count=0;
115 for(E other : commonEdgeSet) {
116 if(e.equals(other) == false) {
117 edge_index.put(Context.<Graph<V,E>,E>getInstance(graph,other), count);
118 count++;
119 }
120 }
121 edge_index.put(Context.<Graph<V,E>,E>getInstance(graph,e), count);
122 return count;
123 }
124
125
126
127
128
129
130
131
132
133
134 public void reset(Graph<V,E> graph, E e) {
135 Pair<V> endpoints = graph.getEndpoints(e);
136 getIndex(graph, e, endpoints.getFirst());
137 getIndex(graph, e, endpoints.getFirst(), endpoints.getSecond());
138 }
139
140
141
142
143
144 public void reset()
145 {
146 edge_index.clear();
147 }
148 }