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