Skip to content

Commit

Permalink
Move some methods arround to make it easier to add either hashed feat…
Browse files Browse the repository at this point in the history
…ures or strings.
  • Loading branch information
johnmay authored and egonw committed Feb 9, 2022
1 parent 608b4ea commit c8f5e05
Showing 1 changed file with 21 additions and 20 deletions.
Expand Up @@ -230,7 +230,7 @@ private String encodePath(List<IAtom> apath, List<IBond> bpath, StringBuilder bu
return buffer.toString();
}

private int appendHash(int hash, String str) {
private static int appendHash(int hash, String str) {
int len = str.length();
for (int i = 0; i < len; i++)
hash = 31 * hash + str.charAt(0);
Expand Down Expand Up @@ -262,7 +262,7 @@ private int hashRevPath(List<IAtom> apath, List<IBond> bpath) {
return hash;
}

private static final class State {
private final class State {
private int numPaths = 0;
private Random rand = new Random();
private BitSet fp;
Expand Down Expand Up @@ -326,18 +326,31 @@ void addHash(int x) {
* @return true - do encode/false - skip encoding
*/
public boolean isOrderedPath() {
return apath.size() == 1 ||
System.identityHashCode(apath.get(0)) <
return System.identityHashCode(apath.get(0)) <
System.identityHashCode(apath.get(apath.size()-1));
}

public void storePath() {
if (bpath.size() == 0) {
addHash(getAtomSymbol(apath.get(0)).hashCode());
} else {
if (!isOrderedPath())
return;
final int x;
if (compare(apath, bpath) >= 0) {
addHash(hashPath(apath, bpath));
} else {
addHash(hashRevPath(apath, bpath));
}
}
}
}

private void traversePaths(State state, IAtom beg, IBond prev) throws CDKException {
if (!hashPseudoAtoms && isPseudo(beg))
return;
state.push(beg, prev);
if (state.isOrderedPath())
state.addHash(encodeUniquePath(state.apath, state.bpath, state.buffer));
state.storePath();
if (state.numPaths > pathLimit)
throw new CDKException("Too many paths! Structure is likely a cage, reduce path length or increase path limit");
if (state.apath.size() < state.maxDepth) {
Expand Down Expand Up @@ -433,7 +446,7 @@ private int encodeUniquePath(IAtomContainer container,
* @param b atom b
* @return comparison &lt;0 a is less than b, &gt;0 a is more than b
*/
private int compare(IAtom a, IAtom b) {
private static int compare(IAtom a, IAtom b) {
final int elemA = getElem(a);
final int elemB = getElem(b);
if (elemA == elemB)
Expand Down Expand Up @@ -478,26 +491,14 @@ private int compare(List<IAtom> apath, List<IBond> bpath) {
return 0;
}

private int encodeUniquePath(List<IAtom> apath, List<IBond> bpath, StringBuilder buffer) {
if (bpath.size() == 0)
return getAtomSymbol(apath.get(0)).hashCode();
final int x;
if (compare(apath, bpath) >= 0) {
x = hashPath(apath, bpath);
} else {
x = hashRevPath(apath, bpath);
}
return x;
}

private static int getElem(IAtom atom) {
Integer elem = atom.getAtomicNumber();
if (elem == null)
elem = 0;
return elem;
}

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

0 comments on commit c8f5e05

Please sign in to comment.