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