23
23
*/
24
24
package org .openscience .cdk .graph ;
25
25
26
- import com .google .common .collect .BiMap ;
27
- import com .google .common .collect .HashBiMap ;
28
26
import com .google .common .collect .Multimap ;
29
27
import com .google .common .collect .TreeMultimap ;
30
28
31
29
import java .util .BitSet ;
32
30
import java .util .Collection ;
31
+ import java .util .HashMap ;
32
+ import java .util .Map ;
33
33
import java .util .Objects ;
34
34
35
35
import static java .util .Arrays .copyOf ;
@@ -57,7 +57,8 @@ final class InitialCycles {
57
57
private final Multimap <Integer , Cycle > cycles = TreeMultimap .create ();
58
58
59
59
/** Index of edges in the graph */
60
- private final BiMap <Edge , Integer > edges ;
60
+ private final Map <Edge , Integer > edgeToIndex ;
61
+ private final Edge [] edges ;
61
62
62
63
/**
63
64
* Initial array size for 'ordering()'. This method sorts vertices by degree
@@ -123,16 +124,19 @@ private InitialCycles(final int[][] graph, final int limit, boolean biconnected)
123
124
// - edge representation: binary vector indicates whether an edge
124
125
// is present or
125
126
// - path representation: sequential list vertices forming the cycle
126
- edges = HashBiMap . create ( graph .length );
127
+ edgeToIndex = new HashMap <>( 2 * graph .length );
127
128
int n = graph .length ;
128
129
for (int v = 0 ; v < n ; v ++) {
129
130
for (int w : graph [v ]) {
130
131
if (w > v ) {
131
132
Edge edge = new Edge (v , w );
132
- edges .put (edge , edges .size ());
133
+ edgeToIndex .put (edge , edgeToIndex .size ());
133
134
}
134
135
}
135
136
}
137
+ edges = new Edge [edgeToIndex .size ()];
138
+ for (Map .Entry <Edge ,Integer > e : edgeToIndex .entrySet ())
139
+ edges [e .getValue ()] = e .getKey ();
136
140
137
141
// compute the initial set of cycles
138
142
compute ();
@@ -192,7 +196,7 @@ int numberOfCycles() {
192
196
* @return number of edges
193
197
*/
194
198
int numberOfEdges () {
195
- return edges .size ();
199
+ return edgeToIndex .size ();
196
200
}
197
201
198
202
/**
@@ -202,7 +206,7 @@ int numberOfEdges() {
202
206
* @return the edge at the given index
203
207
*/
204
208
Edge edge (int i ) {
205
- return edges . inverse (). get ( i ) ;
209
+ return edges [ i ] ;
206
210
}
207
211
208
212
/**
@@ -214,7 +218,7 @@ Edge edge(int i) {
214
218
* @return the index of the edge
215
219
*/
216
220
int indexOfEdge (final int u , final int v ) {
217
- return edges .get (new Edge (u , v ));
221
+ return edgeToIndex .get (new Edge (u , v ));
218
222
}
219
223
220
224
/**
@@ -226,7 +230,7 @@ int indexOfEdge(final int u, final int v) {
226
230
* @see #indexOfEdge(int, int)
227
231
*/
228
232
BitSet toEdgeVector (final int [] path ) {
229
- final BitSet incidence = new BitSet (edges .size ());
233
+ final BitSet incidence = new BitSet (edgeToIndex .size ());
230
234
int len = path .length - 1 ;
231
235
for (int i = 0 ; i < len ; i ++) {
232
236
incidence .set (indexOfEdge (path [i ], path [i + 1 ]));
0 commit comments