| 1 | /** \file bond.cpp
 | 
|---|
| 2 |  * 
 | 
|---|
| 3 |  * Function implementations for the classes BondLeaf, BondTree and bond.
 | 
|---|
| 4 |  * 
 | 
|---|
| 5 |  */
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include "molecules.hpp"
 | 
|---|
| 8 | 
 | 
|---|
| 9 | 
 | 
|---|
| 10 | /***************************************** Functions for class bond ********************************/
 | 
|---|
| 11 | 
 | 
|---|
| 12 | /** Empty Constructor for class bond.
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | bond::bond()
 | 
|---|
| 15 | {
 | 
|---|
| 16 |         leftatom = NULL;
 | 
|---|
| 17 |         rightatom = NULL;
 | 
|---|
| 18 |   previous = NULL;
 | 
|---|
| 19 |   next = NULL;
 | 
|---|
| 20 |         nr = -1;
 | 
|---|
| 21 |         HydrogenBond = 0;
 | 
|---|
| 22 |         BondDegree = 0;
 | 
|---|
| 23 |   Used = white;
 | 
|---|
| 24 |   Cyclic = false;
 | 
|---|
| 25 |   Type = Undetermined;
 | 
|---|
| 26 | };
 | 
|---|
| 27 | 
 | 
|---|
| 28 | /** Constructor for class bond, taking right and left bond partner
 | 
|---|
| 29 |  * \param *left left atom
 | 
|---|
| 30 |  * \param *right right atom
 | 
|---|
| 31 |  * \param degree bond degree
 | 
|---|
| 32 |  * \param number increasing index
 | 
|---|
| 33 |  */
 | 
|---|
| 34 | bond::bond(atom *left, atom *right, int degree=1, int number=0)
 | 
|---|
| 35 | {
 | 
|---|
| 36 |         leftatom = left;
 | 
|---|
| 37 |         rightatom = right;
 | 
|---|
| 38 |   previous = NULL;
 | 
|---|
| 39 |   next = NULL;
 | 
|---|
| 40 |         HydrogenBond = 0;
 | 
|---|
| 41 |   if ((left != NULL) && (right != NULL)) {
 | 
|---|
| 42 |         if ((left->type != NULL) && (left->type->Z == 1))
 | 
|---|
| 43 |                 HydrogenBond++;
 | 
|---|
| 44 |         if ((right->type != NULL) && (right->type->Z == 1))
 | 
|---|
| 45 |                 HydrogenBond++;
 | 
|---|
| 46 |   }
 | 
|---|
| 47 |   BondDegree = degree;
 | 
|---|
| 48 |   nr = number;
 | 
|---|
| 49 |   Used = white;
 | 
|---|
| 50 |   Cyclic = false;
 | 
|---|
| 51 | };
 | 
|---|
| 52 | bond::bond(atom *left, atom *right)
 | 
|---|
| 53 | {
 | 
|---|
| 54 |   leftatom = left;
 | 
|---|
| 55 |   rightatom = right;
 | 
|---|
| 56 |   previous = NULL;
 | 
|---|
| 57 |   next = NULL;
 | 
|---|
| 58 |   HydrogenBond = 0;
 | 
|---|
| 59 |   if ((left != NULL) && (right != NULL)) {
 | 
|---|
| 60 |     if ((left->type != NULL) && (left->type->Z == 1))
 | 
|---|
| 61 |       HydrogenBond++;
 | 
|---|
| 62 |     if ((right->type != NULL) && (right->type->Z == 1))
 | 
|---|
| 63 |       HydrogenBond++;
 | 
|---|
| 64 |   }
 | 
|---|
| 65 |   BondDegree = 1;
 | 
|---|
| 66 |   nr = 0;
 | 
|---|
| 67 |   Used = white;
 | 
|---|
| 68 |   Cyclic = false;
 | 
|---|
| 69 | };
 | 
|---|
| 70 | 
 | 
|---|
| 71 | /** Empty Destructor for class bond.
 | 
|---|
| 72 |  */
 | 
|---|
| 73 | bond::~bond()
 | 
|---|
| 74 | {
 | 
|---|
| 75 |   // remove this node from the list structure
 | 
|---|
| 76 |   if (previous != NULL) {
 | 
|---|
| 77 |     previous->next = next;
 | 
|---|
| 78 |   }
 | 
|---|
| 79 |   if (next != NULL) {
 | 
|---|
| 80 |     next->previous = previous;
 | 
|---|
| 81 |   }
 | 
|---|
| 82 | };
 | 
|---|
| 83 | 
 | 
|---|
| 84 | ostream & operator << (ostream &ost, bond &b) 
 | 
|---|
| 85 | {
 | 
|---|
| 86 |   ost << "[" << b.leftatom->Name << " <" << b.BondDegree << "(H" << b.HydrogenBond << ")>" << b.rightatom->Name << "]";
 | 
|---|
| 87 |   return ost;
 | 
|---|
| 88 | };
 | 
|---|
| 89 | 
 | 
|---|
| 90 | /** Get the other atom in a bond if one is specified.
 | 
|---|
| 91 |  * \param *Atom the pointer to the one atom
 | 
|---|
| 92 |  * \return pointer to the other atom in the bond, NULL if no match (indicates something's wrong with the bond) 
 | 
|---|
| 93 |  */
 | 
|---|
| 94 | atom * bond::GetOtherAtom(atom *Atom) const
 | 
|---|
| 95 | {
 | 
|---|
| 96 |   if(leftatom == Atom) 
 | 
|---|
| 97 |     return rightatom;
 | 
|---|
| 98 |   if(rightatom == Atom) 
 | 
|---|
| 99 |     return leftatom;
 | 
|---|
| 100 |   return NULL;
 | 
|---|
| 101 | };
 | 
|---|
| 102 | 
 | 
|---|
| 103 | /** Get the other atom in a bond if one is specified.
 | 
|---|
| 104 |  * \param *Atom the pointer to the one atom
 | 
|---|
| 105 |  * \return pointer to the other atom in the bond, NULL if no match (indicates something's wrong with the bond) 
 | 
|---|
| 106 |  */
 | 
|---|
| 107 | bond * bond::GetFirstBond()
 | 
|---|
| 108 | {
 | 
|---|
| 109 |   return GetFirst(this);
 | 
|---|
| 110 | };
 | 
|---|
| 111 | 
 | 
|---|
| 112 | /** Get the other atom in a bond if one is specified.
 | 
|---|
| 113 |  * \param *Atom the pointer to the one atom
 | 
|---|
| 114 |  * \return pointer to the other atom in the bond, NULL if no match (indicates something's wrong with the bond) 
 | 
|---|
| 115 |  */
 | 
|---|
| 116 | bond * bond::GetLastBond()
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   return GetLast(this);
 | 
|---|
| 119 | };
 | 
|---|
| 120 | 
 | 
|---|
| 121 | /** Returns whether vertex was used in DFS.
 | 
|---|
| 122 |  * \return bond::Used
 | 
|---|
| 123 |  */
 | 
|---|
| 124 | enum Shading bond::IsUsed() 
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   return Used;
 | 
|---|
| 127 | };
 | 
|---|
| 128 | 
 | 
|---|
| 129 | /** Checks if an atom exists in a bond.
 | 
|---|
| 130 |  * \param *ptr pointer to atom
 | 
|---|
| 131 |  * \return true if it is either bond::leftatom or bond::rightatom, false otherwise
 | 
|---|
| 132 |  */
 | 
|---|
| 133 | bool bond::Contains(const atom *ptr)
 | 
|---|
| 134 | {
 | 
|---|
| 135 |   return ((leftatom == ptr) || (rightatom == ptr));
 | 
|---|
| 136 | };
 | 
|---|
| 137 | 
 | 
|---|
| 138 | /** Checks if an atom exists in a bond.
 | 
|---|
| 139 |  * \param nr index of atom
 | 
|---|
| 140 |  * \return true if it is either bond::leftatom or bond::rightatom, false otherwise
 | 
|---|
| 141 |  */
 | 
|---|
| 142 | bool bond::Contains(const int nr)
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   return ((leftatom->nr == nr) || (rightatom->nr == nr));
 | 
|---|
| 145 | };
 | 
|---|
| 146 | 
 | 
|---|
| 147 | /** Masks vertex as used in DFS.
 | 
|---|
| 148 |  * \return bond::Used, false if bond was already marked used
 | 
|---|
| 149 |  */
 | 
|---|
| 150 | bool bond::MarkUsed(enum Shading color) {
 | 
|---|
| 151 |   if (Used == black) {
 | 
|---|
| 152 |     cerr << "ERROR: Bond " << this << " was already marked black!." << endl;
 | 
|---|
| 153 |     return false;
 | 
|---|
| 154 |   } else {
 | 
|---|
| 155 |     Used = color;
 | 
|---|
| 156 |     return true;
 | 
|---|
| 157 |   }
 | 
|---|
| 158 | };
 | 
|---|
| 159 | 
 | 
|---|
| 160 | /** Resets used flag in DFS.
 | 
|---|
| 161 |  * \return bond::Used
 | 
|---|
| 162 |  */
 | 
|---|
| 163 | void bond::ResetUsed() {
 | 
|---|
| 164 |   Used = white;
 | 
|---|
| 165 | };
 | 
|---|