| [14de469] | 1 | /** \file helpers.hpp | 
|---|
|  | 2 | * | 
|---|
|  | 3 | * Declaration of some auxiliary functions for memory dis-/allocation and so on | 
|---|
|  | 4 | */ | 
|---|
|  | 5 |  | 
|---|
|  | 6 | #ifndef HELPERS_HPP_ | 
|---|
|  | 7 | #define HELPERS_HPP_ | 
|---|
|  | 8 |  | 
|---|
|  | 9 | using namespace std; | 
|---|
|  | 10 |  | 
|---|
|  | 11 | #include <iostream> | 
|---|
|  | 12 | #include <iomanip> | 
|---|
|  | 13 | #include <fstream> | 
|---|
|  | 14 | #include <sstream> | 
|---|
|  | 15 | #include <math.h> | 
|---|
|  | 16 | #include <string> | 
|---|
| [1f6422] | 17 | #include <string.h> | 
|---|
| [14de469] | 18 | #include <stdio.h> | 
|---|
|  | 19 | #include <stdlib.h> | 
|---|
|  | 20 | #include <time.h> | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include "defs.hpp" | 
|---|
|  | 23 |  | 
|---|
| [6dea43] | 24 | // include config.h | 
|---|
|  | 25 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 26 | #include <config.h> | 
|---|
|  | 27 | #endif | 
|---|
|  | 28 |  | 
|---|
| [14de469] | 29 | /********************************************** helpful functions *********************************/ | 
|---|
|  | 30 |  | 
|---|
| [d3a46d] | 31 | // taken out of TREMOLO | 
|---|
|  | 32 | /*@-namechecks@*/ | 
|---|
|  | 33 | #ifndef __GNUC__ | 
|---|
|  | 34 | # undef __attribute__ | 
|---|
|  | 35 | # define __attribute__(x) | 
|---|
|  | 36 | #endif | 
|---|
|  | 37 | /*@=namechecks@*/ | 
|---|
|  | 38 |  | 
|---|
|  | 39 | /* Behandelt aufgetretene Fehler. error ist der Fehlertyp(enum Errors) | 
|---|
|  | 40 | void *SpecialData ist ein untypisierter Zeiger auf Spezielle Daten zur Fehlerbehandlung. | 
|---|
|  | 41 | Man koennte auch noch einen Zeiger auf eine Funktion uebergeben */ | 
|---|
|  | 42 | extern void /*@exits@*/ debug(const char *output); | 
|---|
|  | 43 | //__attribute__ ((__return__)); | 
|---|
|  | 44 | #define debug(data) debug_in((data), __FILE__, __LINE__) | 
|---|
|  | 45 |  | 
|---|
|  | 46 | extern void /*@exits@*/ debug_in(const char *output, | 
|---|
|  | 47 | const char *file, const int line); | 
|---|
|  | 48 | //__attribute__ ((__return__)); | 
|---|
|  | 49 |  | 
|---|
| [14de469] | 50 | double ask_value(const char *text); | 
|---|
|  | 51 | bool check_bounds(double *x, double *cell_size); | 
|---|
|  | 52 | void bound(double *b, double lower_bound, double upper_bound); | 
|---|
|  | 53 | void flip(double *x, double *y); | 
|---|
|  | 54 | int pot(int base, int n); | 
|---|
|  | 55 | void * Malloc(size_t size, const char* output); | 
|---|
|  | 56 | void * Calloc(size_t size, const char* output); | 
|---|
|  | 57 | void * ReAlloc(void * OldPointer, size_t size, const char* output); | 
|---|
|  | 58 | char* MallocString(size_t size, const char* output); | 
|---|
|  | 59 | void Free(void ** buffer, const char* output); | 
|---|
|  | 60 | char *FixedDigitNumber(const int FragmentNumber, const int digits); | 
|---|
|  | 61 |  | 
|---|
| [6d35e4] | 62 | /********************************************** helpful template functions *********************************/ | 
|---|
|  | 63 |  | 
|---|
| [8f75a4] | 64 | /** Creates a lookup table for true father's Atom::Nr -> atom ptr. | 
|---|
|  | 65 | * \param *out output stream for debugging | 
|---|
|  | 66 | * \param *start begin of chain list | 
|---|
|  | 67 | * \paran *end end of chain list | 
|---|
|  | 68 | * \param **Lookuptable pointer to return allocated lookup table (should be NULL on start) | 
|---|
|  | 69 | * \param count optional predetermined count for table (otherwise we set the count to highest true father id) | 
|---|
|  | 70 | * \return true - success, false - failure | 
|---|
|  | 71 | */ | 
|---|
|  | 72 | template <typename T> bool CreateFatherLookupTable(ofstream *out, T *start, T *end, T **&LookupTable, int count = 0) | 
|---|
|  | 73 | { | 
|---|
|  | 74 | bool status = true; | 
|---|
|  | 75 | T *Walker = NULL; | 
|---|
|  | 76 | int AtomNo; | 
|---|
|  | 77 |  | 
|---|
|  | 78 | if (LookupTable != NULL) { | 
|---|
|  | 79 | *out << "Pointer for Lookup table is not NULL! Aborting ..." <<endl; | 
|---|
|  | 80 | return false; | 
|---|
|  | 81 | } | 
|---|
|  | 82 |  | 
|---|
|  | 83 | // count them | 
|---|
|  | 84 | if (count == 0) { | 
|---|
|  | 85 | Walker = start; | 
|---|
|  | 86 | while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron | 
|---|
|  | 87 | Walker = Walker->next; | 
|---|
|  | 88 | count = (count < Walker->GetTrueFather()->nr) ? Walker->GetTrueFather()->nr : count; | 
|---|
|  | 89 | } | 
|---|
|  | 90 | } | 
|---|
|  | 91 | if (count <= 0) { | 
|---|
|  | 92 | *out << "Count of lookup list is 0 or less." << endl; | 
|---|
|  | 93 | return false; | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
|  | 96 | // allocat and fill | 
|---|
|  | 97 | LookupTable = (T **) Malloc(sizeof(T *)*count, "CreateFatherLookupTable - **LookupTable"); | 
|---|
|  | 98 | if (LookupTable == NULL) { | 
|---|
|  | 99 | cerr << "LookupTable memory allocation failed!" << endl; | 
|---|
|  | 100 | status = false; | 
|---|
|  | 101 | } else { | 
|---|
|  | 102 | Walker = start; | 
|---|
|  | 103 | while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron | 
|---|
|  | 104 | Walker = Walker->next; | 
|---|
|  | 105 | AtomNo = Walker->GetTrueFather()->nr; | 
|---|
|  | 106 | if ((AtomNo >= 0) && (AtomNo < count)) { | 
|---|
|  | 107 | //*out << "Setting LookupTable[" << AtomNo << "] to " << *Walker << endl; | 
|---|
|  | 108 | LookupTable[AtomNo] = Walker; | 
|---|
|  | 109 | } else { | 
|---|
|  | 110 | *out << "Walker " << *Walker << " exceeded range of nuclear ids [0, " << count << ")." << endl; | 
|---|
|  | 111 | status = false; | 
|---|
|  | 112 | break; | 
|---|
|  | 113 | } | 
|---|
|  | 114 | } | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
|  | 117 | return status; | 
|---|
|  | 118 | }; | 
|---|
|  | 119 |  | 
|---|
| [6d35e4] | 120 | /******************************** Some templates for list management ***********************************/ | 
|---|
|  | 121 |  | 
|---|
|  | 122 | /** Adds linking of an item to a list. | 
|---|
|  | 123 | * \param *walker | 
|---|
|  | 124 | * \return true - adding succeeded, false - error in list | 
|---|
|  | 125 | */ | 
|---|
|  | 126 | template <typename X> void link(X *walker, X *end) | 
|---|
|  | 127 | { | 
|---|
|  | 128 | X *vorher = end->previous; | 
|---|
|  | 129 | if (vorher != NULL) | 
|---|
|  | 130 | vorher->next = walker; | 
|---|
|  | 131 | end->previous = walker; | 
|---|
|  | 132 | walker->previous = vorher; | 
|---|
|  | 133 | walker->next = end; | 
|---|
|  | 134 | }; | 
|---|
|  | 135 |  | 
|---|
|  | 136 | /** Removes linking of an item in a list. | 
|---|
|  | 137 | * \param *walker | 
|---|
|  | 138 | * \return true - removing succeeded, false - given item not found in list | 
|---|
|  | 139 | */ | 
|---|
|  | 140 | template <typename X> void unlink(X *walker) | 
|---|
|  | 141 | { | 
|---|
|  | 142 | if (walker->next != NULL) | 
|---|
|  | 143 | walker->next->previous = walker->previous; | 
|---|
|  | 144 | if (walker->previous != NULL) | 
|---|
|  | 145 | walker->previous->next = walker->next; | 
|---|
|  | 146 | }; | 
|---|
|  | 147 |  | 
|---|
|  | 148 | /** Adds new item before an item \a *end in a list. | 
|---|
|  | 149 | * \param *pointer   item to be added | 
|---|
|  | 150 | * \param *end  end of list | 
|---|
|  | 151 | * \return true - addition succeeded, false - unable to add item to list | 
|---|
|  | 152 | */ | 
|---|
|  | 153 | template <typename X>  bool add(X *pointer, X *end) | 
|---|
|  | 154 | { | 
|---|
|  | 155 | if (end != NULL) { | 
|---|
|  | 156 | link(pointer, end); | 
|---|
|  | 157 | } else { | 
|---|
|  | 158 | pointer->previous = NULL; | 
|---|
|  | 159 | pointer->next = NULL; | 
|---|
|  | 160 | } | 
|---|
|  | 161 | return true; | 
|---|
|  | 162 | }; | 
|---|
|  | 163 |  | 
|---|
|  | 164 | /** Finds item in list | 
|---|
|  | 165 | * \param *suche  search criteria | 
|---|
|  | 166 | * \param *start  begin of list | 
|---|
|  | 167 | * \param *end  end of list | 
|---|
|  | 168 | * \return X - if found, NULL - if not found | 
|---|
|  | 169 | */ | 
|---|
|  | 170 | template <typename X, typename Y> X * find(Y *suche, X *start, X *end) | 
|---|
|  | 171 | { | 
|---|
|  | 172 | X *walker = start; | 
|---|
|  | 173 | while (walker->next != end) { // go through list | 
|---|
|  | 174 | walker = walker->next; // step onward beforehand | 
|---|
|  | 175 | if (*walker->sort == *suche) return (walker); | 
|---|
|  | 176 | } | 
|---|
|  | 177 | return NULL; | 
|---|
|  | 178 | }; | 
|---|
|  | 179 |  | 
|---|
|  | 180 | /** Removes an item from the list without check. | 
|---|
|  | 181 | * \param *walker item to be removed | 
|---|
|  | 182 | * \return true - removing succeeded, false - given item not found in list | 
|---|
|  | 183 | */ | 
|---|
|  | 184 | template <typename X> void removewithoutcheck(X *walker) | 
|---|
|  | 185 | { | 
|---|
|  | 186 | if (walker != NULL) { | 
|---|
|  | 187 | unlink(walker); | 
|---|
|  | 188 | delete(walker); | 
|---|
|  | 189 | walker = NULL; | 
|---|
|  | 190 | } | 
|---|
|  | 191 | }; | 
|---|
|  | 192 |  | 
|---|
|  | 193 | /** Removes an item from the list, checks if exists. | 
|---|
|  | 194 | * Checks beforehand if atom is really within molecule list. | 
|---|
|  | 195 | * \param *pointer   item to be removed | 
|---|
|  | 196 | * \param *start  begin of list | 
|---|
|  | 197 | * \param *end  end of list | 
|---|
|  | 198 | * \return true - removing succeeded, false - given item not found in list | 
|---|
|  | 199 | */ | 
|---|
|  | 200 | template <typename X> bool remove(X *pointer, X *start, X *end) | 
|---|
|  | 201 | { | 
|---|
|  | 202 | X *walker = find (pointer->sort, start, end); | 
|---|
|  | 203 | /*  while (walker->next != pointer) { // search through list | 
|---|
|  | 204 | walker = walker->next; | 
|---|
|  | 205 | if (walker == end) return false;  // item not found in list | 
|---|
|  | 206 | }*/ | 
|---|
|  | 207 | // atom found, now unlink | 
|---|
|  | 208 | if (walker != NULL) | 
|---|
|  | 209 | removewithoutcheck(walker); | 
|---|
|  | 210 | else | 
|---|
|  | 211 | return false; | 
|---|
|  | 212 | return true; | 
|---|
|  | 213 | }; | 
|---|
|  | 214 |  | 
|---|
|  | 215 | /** Cleans the whole list. | 
|---|
|  | 216 | * \param *start begin of list | 
|---|
|  | 217 | * \param *end end of list | 
|---|
|  | 218 | * \return true - list was cleaned successfully, false - error in list structure | 
|---|
|  | 219 | */ | 
|---|
|  | 220 | template <typename X> bool cleanup(X *start, X *end) | 
|---|
|  | 221 | { | 
|---|
|  | 222 | X *pointer = start->next; | 
|---|
|  | 223 | X *walker; | 
|---|
|  | 224 | while (pointer != end) { // go through list | 
|---|
|  | 225 | walker = pointer; // mark current | 
|---|
|  | 226 | pointer = pointer->next; // step onward beforehand | 
|---|
|  | 227 | // remove walker | 
|---|
|  | 228 | unlink(walker); | 
|---|
|  | 229 | delete(walker); | 
|---|
|  | 230 | walker = NULL; | 
|---|
|  | 231 | } | 
|---|
|  | 232 | return true; | 
|---|
|  | 233 | }; | 
|---|
|  | 234 |  | 
|---|
|  | 235 | /** Returns the first marker in a chain list. | 
|---|
|  | 236 | * \param *me one arbitrary item in chain list | 
|---|
|  | 237 | * \return poiner to first marker | 
|---|
|  | 238 | */ | 
|---|
|  | 239 | template <typename X> X *GetFirst(X *me) | 
|---|
|  | 240 | { | 
|---|
|  | 241 | X *Binder = me; | 
|---|
|  | 242 | while(Binder->previous != NULL) | 
|---|
|  | 243 | Binder = Binder->previous; | 
|---|
|  | 244 | return Binder; | 
|---|
|  | 245 | }; | 
|---|
|  | 246 |  | 
|---|
|  | 247 | /** Returns the last marker in a chain list. | 
|---|
|  | 248 | * \param *me one arbitrary item in chain list | 
|---|
|  | 249 | * \return poiner to last marker | 
|---|
|  | 250 | */ | 
|---|
|  | 251 | template <typename X> X *GetLast(X *me) | 
|---|
|  | 252 | { | 
|---|
|  | 253 | X *Binder = me; | 
|---|
|  | 254 | while(Binder->next != NULL) | 
|---|
|  | 255 | Binder = Binder->next; | 
|---|
|  | 256 | return Binder; | 
|---|
|  | 257 | }; | 
|---|
|  | 258 |  | 
|---|
|  | 259 | /** Frees a two-dimensional array. | 
|---|
|  | 260 | * \param *ptr pointer to array | 
|---|
|  | 261 | * \param dim first dim of array | 
|---|
|  | 262 | */ | 
|---|
|  | 263 | template <typename X> void Free2DArray(X **ptr, int dim) | 
|---|
|  | 264 | { | 
|---|
|  | 265 | int i; | 
|---|
|  | 266 | if (ptr != NULL) { | 
|---|
| [7f3b9d] | 267 | for(i=dim;i--;) | 
|---|
| [6d35e4] | 268 | if (ptr[i] != NULL) | 
|---|
|  | 269 | free(ptr[i]); | 
|---|
|  | 270 | free(ptr); | 
|---|
|  | 271 | } | 
|---|
|  | 272 | }; | 
|---|
| [14de469] | 273 |  | 
|---|
|  | 274 | /************************************* Class Verbose & Binary *******************************/ | 
|---|
|  | 275 |  | 
|---|
|  | 276 | /** Verbose is an IO manipulator, that writes tabs according to \a Verbosity level. | 
|---|
|  | 277 | */ | 
|---|
|  | 278 | class Verbose | 
|---|
|  | 279 | { | 
|---|
|  | 280 | public: | 
|---|
|  | 281 | Verbose(int value) : Verbosity(value) { } | 
|---|
|  | 282 |  | 
|---|
|  | 283 | ostream& print (ostream &ost) const; | 
|---|
|  | 284 | private: | 
|---|
|  | 285 | int Verbosity; | 
|---|
|  | 286 | }; | 
|---|
|  | 287 |  | 
|---|
|  | 288 | ostream& operator<<(ostream& ost,const Verbose& m); | 
|---|
|  | 289 |  | 
|---|
|  | 290 | /** Binary is an IO manipulator, that writes 0 and 1 according to number \a Binary. | 
|---|
|  | 291 | */ | 
|---|
|  | 292 | class Binary | 
|---|
|  | 293 | { | 
|---|
|  | 294 | public: | 
|---|
|  | 295 | Binary(int value) : BinaryNumber(value) { } | 
|---|
|  | 296 |  | 
|---|
|  | 297 | ostream& print (ostream &ost) const; | 
|---|
|  | 298 | private: | 
|---|
|  | 299 | int BinaryNumber; | 
|---|
|  | 300 | }; | 
|---|
|  | 301 |  | 
|---|
|  | 302 | ostream& operator<<(ostream& ost,const Binary& m); | 
|---|
|  | 303 |  | 
|---|
|  | 304 |  | 
|---|
|  | 305 |  | 
|---|
|  | 306 | #endif /*HELPERS_HPP_*/ | 
|---|