Skip to content

Commit c8f5e05

Browse files
johnmayegonw
authored andcommitted
Move some methods arround to make it easier to add either hashed features or strings.
1 parent 608b4ea commit c8f5e05

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

base/standard/src/main/java/org/openscience/cdk/fingerprint/Fingerprinter.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ private String encodePath(List<IAtom> apath, List<IBond> bpath, StringBuilder bu
230230
return buffer.toString();
231231
}
232232

233-
private int appendHash(int hash, String str) {
233+
private static int appendHash(int hash, String str) {
234234
int len = str.length();
235235
for (int i = 0; i < len; i++)
236236
hash = 31 * hash + str.charAt(0);
@@ -262,7 +262,7 @@ private int hashRevPath(List<IAtom> apath, List<IBond> bpath) {
262262
return hash;
263263
}
264264

265-
private static final class State {
265+
private final class State {
266266
private int numPaths = 0;
267267
private Random rand = new Random();
268268
private BitSet fp;
@@ -326,18 +326,31 @@ void addHash(int x) {
326326
* @return true - do encode/false - skip encoding
327327
*/
328328
public boolean isOrderedPath() {
329-
return apath.size() == 1 ||
330-
System.identityHashCode(apath.get(0)) <
329+
return System.identityHashCode(apath.get(0)) <
331330
System.identityHashCode(apath.get(apath.size()-1));
332331
}
332+
333+
public void storePath() {
334+
if (bpath.size() == 0) {
335+
addHash(getAtomSymbol(apath.get(0)).hashCode());
336+
} else {
337+
if (!isOrderedPath())
338+
return;
339+
final int x;
340+
if (compare(apath, bpath) >= 0) {
341+
addHash(hashPath(apath, bpath));
342+
} else {
343+
addHash(hashRevPath(apath, bpath));
344+
}
345+
}
346+
}
333347
}
334348

335349
private void traversePaths(State state, IAtom beg, IBond prev) throws CDKException {
336350
if (!hashPseudoAtoms && isPseudo(beg))
337351
return;
338352
state.push(beg, prev);
339-
if (state.isOrderedPath())
340-
state.addHash(encodeUniquePath(state.apath, state.bpath, state.buffer));
353+
state.storePath();
341354
if (state.numPaths > pathLimit)
342355
throw new CDKException("Too many paths! Structure is likely a cage, reduce path length or increase path limit");
343356
if (state.apath.size() < state.maxDepth) {
@@ -433,7 +446,7 @@ private int encodeUniquePath(IAtomContainer container,
433446
* @param b atom b
434447
* @return comparison &lt;0 a is less than b, &gt;0 a is more than b
435448
*/
436-
private int compare(IAtom a, IAtom b) {
449+
private static int compare(IAtom a, IAtom b) {
437450
final int elemA = getElem(a);
438451
final int elemB = getElem(b);
439452
if (elemA == elemB)
@@ -478,26 +491,14 @@ private int compare(List<IAtom> apath, List<IBond> bpath) {
478491
return 0;
479492
}
480493

481-
private int encodeUniquePath(List<IAtom> apath, List<IBond> bpath, StringBuilder buffer) {
482-
if (bpath.size() == 0)
483-
return getAtomSymbol(apath.get(0)).hashCode();
484-
final int x;
485-
if (compare(apath, bpath) >= 0) {
486-
x = hashPath(apath, bpath);
487-
} else {
488-
x = hashRevPath(apath, bpath);
489-
}
490-
return x;
491-
}
492-
493494
private static int getElem(IAtom atom) {
494495
Integer elem = atom.getAtomicNumber();
495496
if (elem == null)
496497
elem = 0;
497498
return elem;
498499
}
499500

500-
private String getAtomSymbol(IAtom atom) {
501+
private static String getAtomSymbol(IAtom atom) {
501502
// XXX: backwards compatibility
502503
// This is completely random, I believe the intention is because
503504
// paths were reversed with string manipulation to de-duplicate

0 commit comments

Comments
 (0)