📄 atom.java
字号:
CARBON /* C */, NITROGEN /* N */, OXYGEN /* O */, FLOURINE /* F */, PHOSPHORUS /* P */, SULFUR /* S */, POTASSIUM /* K */, VANADIUM /* V */, YTTRIUM /* Y */, IODINE /* I */, TUNGSTEN /* W */, URANIUM /* U */ }; /* --- instance variables --- */ protected int type; /* type, see flags/masks above */ protected int mark; /* marker (e.g. for a substructure) */ protected int bondcnt; /* current number of incident bonds */ protected Bond[] bonds; /* vector of incident bonds */ /*------------------------------------------------------------------*/ protected Atom (int type) { /* --- create an atom */ this.type = type; /* note the type of the atom */ this.bondcnt = 0; /* create an empty bond vector */ this.bonds = new Bond[4]; /* of default size */ } /* Atom() */ /*------------------------------------------------------------------*/ protected Atom (int type, int bondcnt) { /* --- create an atom */ this.type = type; /* note the type of the atom */ this.bondcnt = 0; /* create an empty bond vector */ this.bonds = new Bond[bondcnt]; } /* Atom() */ /*------------------------------------------------------------------*/ public void setType (int type) { this.type = type; } public int getType () { return this.type; } public int getElement () { return this.type & TYPEMASK; } public boolean isAromatic () { return (this.type & AROMATIC) != 0; } public int getCharge () { return Atom.getCharge(this.type); } /*------------------------------------------------------------------*/ public static int getElement (int t) { return t & TYPEMASK; } public static int codeCharge (int c) { return ((c < 0) ? -c | 0x10 : c) << CHARGESHIFT; } public static int getCharge (int t) { t = (t & CHARGEMASK) >> CHARGESHIFT; return ((t & 0x10) != 0) ? -(t & 0x0f) : (t & 0x0f); } /*------------------------------------------------------------------*/ protected void addBond (Bond bond) { /* --- add a bond to an atom */ int vsz; /* (new) vector size */ Bond[] vec; /* buffer for reallocation */ vsz = this.bonds.length; /* get the current vector size */ if (this.bondcnt >= vsz) { /* if the bond vector is full */ vsz += (vsz > 4) ? vsz >> 1 : 4; vec = new Bond[vsz]; /* enlarge the bond vector */ System.arraycopy(this.bonds, 0, vec, 0, this.bondcnt); this.bonds = vec; /* copy the existing bonds and */ } /* set the new bonds vector */ this.bonds[this.bondcnt++] = bond; } /* addBond() */ /* add the new bond to the vector */ /*------------------------------------------------------------------*/ protected void opt () { /* --- optimize memory usage */ if (this.bonds.length <= this.bondcnt) return; /* if vector has minimum size, abort */ Bond[] vec = new Bond[this.bondcnt]; System.arraycopy(this.bonds, 0, vec, 0, this.bondcnt); this.bonds = vec; /* get a vector of the right size */ } /* opt() */ /* and copy the bonds into it */ /*------------------------------------------------------------------*/ protected void sortBonds () { /* --- sort the bonds of an atom */ int i, k; /* loop variables, indices */ Bond b, x; /* to traverse/exchange the bonds */ int d, t; /* type of destination atom */ if (this.bondcnt > 10) { /* if not very few bonds to sort */ Arrays.sort(this.bonds, new Comparator () { public int compare (Object o1, Object o2) { Bond b1 = (Bond)o1, b2 = (Bond)o2; if (b1.type > b2.type) return +1; if (b1.type < b2.type) return -1; Atom d1 = (b1.src != Atom.this) ? b1.src : b1.dst; Atom d2 = (b2.src != Atom.this) ? b2.src : b2.dst; if (d1.type > d2.type) return +1; if (d1.type < d2.type) return -1; return 0; /* use the generic sorting function */ } } ); /* of the Arrays class with */ return; /* a special Comparator object */ } for (i = 0; ++i < this.bondcnt; ) { b = this.bonds[k = i]; /* traverse the bonds to insert */ d = (b.src != this) ? b.src.type : b.dst.type; do { /* traverse the preceding bonds */ x = this.bonds[k-1]; /* (find the place for insertion) */ if (b.type > x.type) /* if the bond type is greater, */ break; /* the insertion point is found */ if (b.type == x.type) { /* if the bond type is identical */ t = (x.src != this) ? x.src.type : x.dst.type; if (d >= t) break; /* if the atom type is no less, */ } /* the insertion point is found */ this.bonds[k] = x; /* shift the bond upwards */ } while (--k > 0); /* while not at start of vector */ this.bonds[k] = b; /* store the bond to insert */ } /* at the position found */ } /* sortBonds() */ /*-------------------------------------------------------------------- Do a simple insertion sort to sort the bonds. Since there should be no more than four bonds, insertion sort is the fastest method. The bonds are sorted, because their order is exploited to restrict the search for common substructures (search tree pruning). --------------------------------------------------------------------*/ protected void sortBondsEx () { /* --- sort the bonds of an atom */ int i, k; /* loop variables, indices */ Bond b, x; /* to traverse/exchange the bonds */ int d, t; /* type of destination atom */ if (this.bondcnt > 10) { /* if not very few bonds to sort */ Arrays.sort(this.bonds, new Comparator () { public int compare (Object o1, Object o2) { Bond b1 = (Bond)o1, b2 = (Bond)o2; if (b1.type > b2.type) return +1; if (b1.type < b2.type) return -1; Atom d1 = (b1.src != Atom.this) ? b1.src : b1.dst; Atom d2 = (b2.src != Atom.this) ? b2.src : b2.dst; if (d1.type > d2.type) return +1; if (d1.type < d2.type) return -1; if (b1.mark > b2.mark) return +1; if (b1.mark < b2.mark) return -1; return 0; /* use the generic sorting function */ } } ); /* of the Arrays class with */ return; /* a special Comparator object */ } for (i = 0; ++i < this.bondcnt; ) { b = this.bonds[k = i]; /* traverse the bonds to insert */ d = (b.src != this) ? b.src.type : b.dst.type; do { /* traverse the preceding bonds */ x = this.bonds[k-1]; /* (find the place for insertion) */ if (b.type > x.type) /* if the bond type is greater, */ break; /* the insertion point is found */ if (b.type == x.type) { /* if the bond type is identical */ t = (x.src != this) ? x.src.type : x.dst.type; if ((d > t) || ((d == t) && (b.mark >= x.mark))) break; /* compare the atom type */ } /* and maybe the bond marker */ this.bonds[k] = x; /* shift the bond upwards */ } while (--k > 0); /* while not at start of vector */ this.bonds[k] = b; /* store the bond to insert */ } /* at the position found */ } /* sortBondsEx() */ /*-------------------------------------------------------------------- In contrast to sortBonds, sortBondsEx takes the bond marker into account (for bonds with same type and same destination atom type). --------------------------------------------------------------------*/} /* class Atom */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -