| 1 | /** \file bond.cpp | 
|---|
| 2 | * | 
|---|
| 3 | * Function implementations for the classes BondLeaf, BondTree and bond. | 
|---|
| 4 | * | 
|---|
| 5 | */ | 
|---|
| 6 |  | 
|---|
| 7 | #include "bond.hpp" | 
|---|
| 8 |  | 
|---|
| 9 | /***************************************** Functions for class bond ********************************/ | 
|---|
| 10 |  | 
|---|
| 11 | /** Empty Constructor for class bond. | 
|---|
| 12 | */ | 
|---|
| 13 | bond::bond() | 
|---|
| 14 | { | 
|---|
| 15 | leftatom = NULL; | 
|---|
| 16 | rightatom = NULL; | 
|---|
| 17 | previous = NULL; | 
|---|
| 18 | next = NULL; | 
|---|
| 19 | nr = -1; | 
|---|
| 20 | HydrogenBond = 0; | 
|---|
| 21 | BondDegree = 0; | 
|---|
| 22 | Used = white; | 
|---|
| 23 | Cyclic = false; | 
|---|
| 24 | Type = Undetermined; | 
|---|
| 25 | }; | 
|---|
| 26 |  | 
|---|
| 27 | /** Constructor for class bond, taking right and left bond partner | 
|---|
| 28 | * \param *left left atom | 
|---|
| 29 | * \param *right right atom | 
|---|
| 30 | * \param degree bond degree | 
|---|
| 31 | * \param number increasing index | 
|---|
| 32 | */ | 
|---|
| 33 | bond::bond(atom *left, atom *right, int degree=1, int number=0) | 
|---|
| 34 | { | 
|---|
| 35 | leftatom = left; | 
|---|
| 36 | rightatom = right; | 
|---|
| 37 | previous = NULL; | 
|---|
| 38 | next = NULL; | 
|---|
| 39 | HydrogenBond = 0; | 
|---|
| 40 | if ((left != NULL) && (right != NULL)) { | 
|---|
| 41 | if ((left->type != NULL) && (left->type->Z == 1)) | 
|---|
| 42 | HydrogenBond++; | 
|---|
| 43 | if ((right->type != NULL) && (right->type->Z == 1)) | 
|---|
| 44 | HydrogenBond++; | 
|---|
| 45 | } | 
|---|
| 46 | BondDegree = degree; | 
|---|
| 47 | nr = number; | 
|---|
| 48 | Used = white; | 
|---|
| 49 | Cyclic = false; | 
|---|
| 50 | }; | 
|---|
| 51 | bond::bond(atom *left, atom *right) | 
|---|
| 52 | { | 
|---|
| 53 | leftatom = left; | 
|---|
| 54 | rightatom = right; | 
|---|
| 55 | previous = NULL; | 
|---|
| 56 | next = NULL; | 
|---|
| 57 | HydrogenBond = 0; | 
|---|
| 58 | if ((left != NULL) && (right != NULL)) { | 
|---|
| 59 | if ((left->type != NULL) && (left->type->Z == 1)) | 
|---|
| 60 | HydrogenBond++; | 
|---|
| 61 | if ((right->type != NULL) && (right->type->Z == 1)) | 
|---|
| 62 | HydrogenBond++; | 
|---|
| 63 | } | 
|---|
| 64 | BondDegree = 1; | 
|---|
| 65 | nr = 0; | 
|---|
| 66 | Used = white; | 
|---|
| 67 | Cyclic = false; | 
|---|
| 68 | }; | 
|---|
| 69 |  | 
|---|
| 70 | /** Empty Destructor for class bond. | 
|---|
| 71 | */ | 
|---|
| 72 | bond::~bond() | 
|---|
| 73 | { | 
|---|
| 74 | // remove this node from the list structure | 
|---|
| 75 | if (previous != NULL) { | 
|---|
| 76 | previous->next = next; | 
|---|
| 77 | } | 
|---|
| 78 | if (next != NULL) { | 
|---|
| 79 | next->previous = previous; | 
|---|
| 80 | } | 
|---|
| 81 | }; | 
|---|
| 82 |  | 
|---|
| 83 | ostream & operator << (ostream &ost, const bond &b) | 
|---|
| 84 | { | 
|---|
| 85 | ost << "[" << b.leftatom->Name << " <" << b.BondDegree << "(H" << b.HydrogenBond << ")>" << b.rightatom->Name << "]"; | 
|---|
| 86 | return ost; | 
|---|
| 87 | }; | 
|---|
| 88 |  | 
|---|
| 89 | /** Get the other atom in a bond if one is specified. | 
|---|
| 90 | * \param *Atom the pointer to the one atom | 
|---|
| 91 | * \return pointer to the other atom in the bond, NULL if no match (indicates something's wrong with the bond) | 
|---|
| 92 | */ | 
|---|
| 93 | atom * bond::GetOtherAtom(atom *Atom) const | 
|---|
| 94 | { | 
|---|
| 95 | if(leftatom == Atom) | 
|---|
| 96 | return rightatom; | 
|---|
| 97 | if(rightatom == Atom) | 
|---|
| 98 | return leftatom; | 
|---|
| 99 | cerr << "Bond " << *this << " does not contain atom " << *Atom << "!" << endl; | 
|---|
| 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 number) | 
|---|
| 143 | { | 
|---|
| 144 | return ((leftatom->nr == number) || (rightatom->nr == number)); | 
|---|
| 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 | }; | 
|---|