| [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); | 
|---|
| [e198c7] | 61 | bool IsValidNumber( const char *string); | 
|---|
| [14de469] | 62 |  | 
|---|
| [6d35e4] | 63 | /********************************************** helpful template functions *********************************/ | 
|---|
|  | 64 |  | 
|---|
| [8f75a4] | 65 | /** Creates a lookup table for true father's Atom::Nr -> atom ptr. | 
|---|
|  | 66 | * \param *out output stream for debugging | 
|---|
|  | 67 | * \param *start begin of chain list | 
|---|
|  | 68 | * \paran *end end of chain list | 
|---|
|  | 69 | * \param **Lookuptable pointer to return allocated lookup table (should be NULL on start) | 
|---|
|  | 70 | * \param count optional predetermined count for table (otherwise we set the count to highest true father id) | 
|---|
|  | 71 | * \return true - success, false - failure | 
|---|
|  | 72 | */ | 
|---|
|  | 73 | template <typename T> bool CreateFatherLookupTable(ofstream *out, T *start, T *end, T **&LookupTable, int count = 0) | 
|---|
|  | 74 | { | 
|---|
|  | 75 | bool status = true; | 
|---|
|  | 76 | T *Walker = NULL; | 
|---|
|  | 77 | int AtomNo; | 
|---|
|  | 78 |  | 
|---|
|  | 79 | if (LookupTable != NULL) { | 
|---|
|  | 80 | *out << "Pointer for Lookup table is not NULL! Aborting ..." <<endl; | 
|---|
|  | 81 | return false; | 
|---|
|  | 82 | } | 
|---|
|  | 83 |  | 
|---|
|  | 84 | // count them | 
|---|
|  | 85 | if (count == 0) { | 
|---|
|  | 86 | Walker = start; | 
|---|
|  | 87 | while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron | 
|---|
|  | 88 | Walker = Walker->next; | 
|---|
|  | 89 | count = (count < Walker->GetTrueFather()->nr) ? Walker->GetTrueFather()->nr : count; | 
|---|
|  | 90 | } | 
|---|
|  | 91 | } | 
|---|
|  | 92 | if (count <= 0) { | 
|---|
|  | 93 | *out << "Count of lookup list is 0 or less." << endl; | 
|---|
|  | 94 | return false; | 
|---|
|  | 95 | } | 
|---|
|  | 96 |  | 
|---|
|  | 97 | // allocat and fill | 
|---|
|  | 98 | LookupTable = (T **) Malloc(sizeof(T *)*count, "CreateFatherLookupTable - **LookupTable"); | 
|---|
|  | 99 | if (LookupTable == NULL) { | 
|---|
|  | 100 | cerr << "LookupTable memory allocation failed!" << endl; | 
|---|
|  | 101 | status = false; | 
|---|
|  | 102 | } else { | 
|---|
| [3ccc3e] | 103 | for (int i=0;i<count;i++) | 
|---|
|  | 104 | LookupTable[i] = NULL; | 
|---|
| [8f75a4] | 105 | Walker = start; | 
|---|
|  | 106 | while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron | 
|---|
|  | 107 | Walker = Walker->next; | 
|---|
|  | 108 | AtomNo = Walker->GetTrueFather()->nr; | 
|---|
|  | 109 | if ((AtomNo >= 0) && (AtomNo < count)) { | 
|---|
|  | 110 | //*out << "Setting LookupTable[" << AtomNo << "] to " << *Walker << endl; | 
|---|
|  | 111 | LookupTable[AtomNo] = Walker; | 
|---|
|  | 112 | } else { | 
|---|
|  | 113 | *out << "Walker " << *Walker << " exceeded range of nuclear ids [0, " << count << ")." << endl; | 
|---|
|  | 114 | status = false; | 
|---|
|  | 115 | break; | 
|---|
|  | 116 | } | 
|---|
|  | 117 | } | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 | return status; | 
|---|
|  | 121 | }; | 
|---|
|  | 122 |  | 
|---|
| [6d35e4] | 123 | /******************************** Some templates for list management ***********************************/ | 
|---|
|  | 124 |  | 
|---|
|  | 125 | /** Adds linking of an item to a list. | 
|---|
|  | 126 | * \param *walker | 
|---|
|  | 127 | * \return true - adding succeeded, false - error in list | 
|---|
|  | 128 | */ | 
|---|
|  | 129 | template <typename X> void link(X *walker, X *end) | 
|---|
|  | 130 | { | 
|---|
|  | 131 | X *vorher = end->previous; | 
|---|
|  | 132 | if (vorher != NULL) | 
|---|
|  | 133 | vorher->next = walker; | 
|---|
|  | 134 | end->previous = walker; | 
|---|
|  | 135 | walker->previous = vorher; | 
|---|
|  | 136 | walker->next = end; | 
|---|
|  | 137 | }; | 
|---|
|  | 138 |  | 
|---|
|  | 139 | /** Removes linking of an item in a list. | 
|---|
|  | 140 | * \param *walker | 
|---|
|  | 141 | * \return true - removing succeeded, false - given item not found in list | 
|---|
|  | 142 | */ | 
|---|
|  | 143 | template <typename X> void unlink(X *walker) | 
|---|
|  | 144 | { | 
|---|
|  | 145 | if (walker->next != NULL) | 
|---|
|  | 146 | walker->next->previous = walker->previous; | 
|---|
|  | 147 | if (walker->previous != NULL) | 
|---|
|  | 148 | walker->previous->next = walker->next; | 
|---|
|  | 149 | }; | 
|---|
|  | 150 |  | 
|---|
|  | 151 | /** Adds new item before an item \a *end in a list. | 
|---|
|  | 152 | * \param *pointer   item to be added | 
|---|
|  | 153 | * \param *end  end of list | 
|---|
|  | 154 | * \return true - addition succeeded, false - unable to add item to list | 
|---|
|  | 155 | */ | 
|---|
|  | 156 | template <typename X>  bool add(X *pointer, X *end) | 
|---|
|  | 157 | { | 
|---|
|  | 158 | if (end != NULL) { | 
|---|
|  | 159 | link(pointer, end); | 
|---|
|  | 160 | } else { | 
|---|
|  | 161 | pointer->previous = NULL; | 
|---|
|  | 162 | pointer->next = NULL; | 
|---|
|  | 163 | } | 
|---|
|  | 164 | return true; | 
|---|
|  | 165 | }; | 
|---|
|  | 166 |  | 
|---|
|  | 167 | /** Finds item in list | 
|---|
|  | 168 | * \param *suche  search criteria | 
|---|
|  | 169 | * \param *start  begin of list | 
|---|
|  | 170 | * \param *end  end of list | 
|---|
|  | 171 | * \return X - if found, NULL - if not found | 
|---|
|  | 172 | */ | 
|---|
|  | 173 | template <typename X, typename Y> X * find(Y *suche, X *start, X *end) | 
|---|
|  | 174 | { | 
|---|
|  | 175 | X *walker = start; | 
|---|
|  | 176 | while (walker->next != end) { // go through list | 
|---|
|  | 177 | walker = walker->next; // step onward beforehand | 
|---|
|  | 178 | if (*walker->sort == *suche) return (walker); | 
|---|
|  | 179 | } | 
|---|
|  | 180 | return NULL; | 
|---|
|  | 181 | }; | 
|---|
|  | 182 |  | 
|---|
|  | 183 | /** Removes an item from the list without check. | 
|---|
|  | 184 | * \param *walker item to be removed | 
|---|
|  | 185 | * \return true - removing succeeded, false - given item not found in list | 
|---|
|  | 186 | */ | 
|---|
|  | 187 | template <typename X> void removewithoutcheck(X *walker) | 
|---|
|  | 188 | { | 
|---|
|  | 189 | if (walker != NULL) { | 
|---|
|  | 190 | unlink(walker); | 
|---|
|  | 191 | delete(walker); | 
|---|
|  | 192 | walker = NULL; | 
|---|
|  | 193 | } | 
|---|
|  | 194 | }; | 
|---|
|  | 195 |  | 
|---|
|  | 196 | /** Removes an item from the list, checks if exists. | 
|---|
|  | 197 | * Checks beforehand if atom is really within molecule list. | 
|---|
|  | 198 | * \param *pointer   item to be removed | 
|---|
|  | 199 | * \param *start  begin of list | 
|---|
|  | 200 | * \param *end  end of list | 
|---|
|  | 201 | * \return true - removing succeeded, false - given item not found in list | 
|---|
|  | 202 | */ | 
|---|
|  | 203 | template <typename X> bool remove(X *pointer, X *start, X *end) | 
|---|
|  | 204 | { | 
|---|
|  | 205 | X *walker = find (pointer->sort, start, end); | 
|---|
|  | 206 | /*  while (walker->next != pointer) { // search through list | 
|---|
|  | 207 | walker = walker->next; | 
|---|
|  | 208 | if (walker == end) return false;  // item not found in list | 
|---|
|  | 209 | }*/ | 
|---|
|  | 210 | // atom found, now unlink | 
|---|
|  | 211 | if (walker != NULL) | 
|---|
|  | 212 | removewithoutcheck(walker); | 
|---|
|  | 213 | else | 
|---|
|  | 214 | return false; | 
|---|
|  | 215 | return true; | 
|---|
|  | 216 | }; | 
|---|
|  | 217 |  | 
|---|
|  | 218 | /** Cleans the whole list. | 
|---|
|  | 219 | * \param *start begin of list | 
|---|
|  | 220 | * \param *end end of list | 
|---|
|  | 221 | * \return true - list was cleaned successfully, false - error in list structure | 
|---|
|  | 222 | */ | 
|---|
|  | 223 | template <typename X> bool cleanup(X *start, X *end) | 
|---|
|  | 224 | { | 
|---|
|  | 225 | X *pointer = start->next; | 
|---|
|  | 226 | X *walker; | 
|---|
|  | 227 | while (pointer != end) { // go through list | 
|---|
|  | 228 | walker = pointer; // mark current | 
|---|
|  | 229 | pointer = pointer->next; // step onward beforehand | 
|---|
|  | 230 | // remove walker | 
|---|
|  | 231 | unlink(walker); | 
|---|
|  | 232 | delete(walker); | 
|---|
|  | 233 | walker = NULL; | 
|---|
|  | 234 | } | 
|---|
|  | 235 | return true; | 
|---|
|  | 236 | }; | 
|---|
|  | 237 |  | 
|---|
|  | 238 | /** Returns the first marker in a chain list. | 
|---|
|  | 239 | * \param *me one arbitrary item in chain list | 
|---|
|  | 240 | * \return poiner to first marker | 
|---|
|  | 241 | */ | 
|---|
|  | 242 | template <typename X> X *GetFirst(X *me) | 
|---|
|  | 243 | { | 
|---|
|  | 244 | X *Binder = me; | 
|---|
|  | 245 | while(Binder->previous != NULL) | 
|---|
|  | 246 | Binder = Binder->previous; | 
|---|
|  | 247 | return Binder; | 
|---|
|  | 248 | }; | 
|---|
|  | 249 |  | 
|---|
|  | 250 | /** Returns the last marker in a chain list. | 
|---|
|  | 251 | * \param *me one arbitrary item in chain list | 
|---|
|  | 252 | * \return poiner to last marker | 
|---|
|  | 253 | */ | 
|---|
|  | 254 | template <typename X> X *GetLast(X *me) | 
|---|
|  | 255 | { | 
|---|
|  | 256 | X *Binder = me; | 
|---|
|  | 257 | while(Binder->next != NULL) | 
|---|
|  | 258 | Binder = Binder->next; | 
|---|
|  | 259 | return Binder; | 
|---|
|  | 260 | }; | 
|---|
|  | 261 |  | 
|---|
|  | 262 | /** Frees a two-dimensional array. | 
|---|
|  | 263 | * \param *ptr pointer to array | 
|---|
|  | 264 | * \param dim first dim of array | 
|---|
|  | 265 | */ | 
|---|
|  | 266 | template <typename X> void Free2DArray(X **ptr, int dim) | 
|---|
|  | 267 | { | 
|---|
|  | 268 | int i; | 
|---|
|  | 269 | if (ptr != NULL) { | 
|---|
| [7f3b9d] | 270 | for(i=dim;i--;) | 
|---|
| [6d35e4] | 271 | if (ptr[i] != NULL) | 
|---|
|  | 272 | free(ptr[i]); | 
|---|
|  | 273 | free(ptr); | 
|---|
|  | 274 | } | 
|---|
|  | 275 | }; | 
|---|
| [14de469] | 276 |  | 
|---|
|  | 277 | /************************************* Class Verbose & Binary *******************************/ | 
|---|
|  | 278 |  | 
|---|
|  | 279 | /** Verbose is an IO manipulator, that writes tabs according to \a Verbosity level. | 
|---|
|  | 280 | */ | 
|---|
|  | 281 | class Verbose | 
|---|
|  | 282 | { | 
|---|
|  | 283 | public: | 
|---|
|  | 284 | Verbose(int value) : Verbosity(value) { } | 
|---|
|  | 285 |  | 
|---|
|  | 286 | ostream& print (ostream &ost) const; | 
|---|
|  | 287 | private: | 
|---|
|  | 288 | int Verbosity; | 
|---|
|  | 289 | }; | 
|---|
|  | 290 |  | 
|---|
|  | 291 | ostream& operator<<(ostream& ost,const Verbose& m); | 
|---|
|  | 292 |  | 
|---|
|  | 293 | /** Binary is an IO manipulator, that writes 0 and 1 according to number \a Binary. | 
|---|
|  | 294 | */ | 
|---|
|  | 295 | class Binary | 
|---|
|  | 296 | { | 
|---|
|  | 297 | public: | 
|---|
|  | 298 | Binary(int value) : BinaryNumber(value) { } | 
|---|
|  | 299 |  | 
|---|
|  | 300 | ostream& print (ostream &ost) const; | 
|---|
|  | 301 | private: | 
|---|
|  | 302 | int BinaryNumber; | 
|---|
|  | 303 | }; | 
|---|
|  | 304 |  | 
|---|
|  | 305 | ostream& operator<<(ostream& ost,const Binary& m); | 
|---|
|  | 306 |  | 
|---|
|  | 307 |  | 
|---|
|  | 308 |  | 
|---|
|  | 309 | #endif /*HELPERS_HPP_*/ | 
|---|