| 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 | /*********************************************** includes ***********************************/
|
|---|
| 12 |
|
|---|
| 13 | // include config.h
|
|---|
| 14 | #ifdef HAVE_CONFIG_H
|
|---|
| 15 | #include <config.h>
|
|---|
| 16 | #endif
|
|---|
| 17 |
|
|---|
| 18 | #include <fstream>
|
|---|
| 19 |
|
|---|
| 20 | #include "memoryallocator.hpp"
|
|---|
| 21 |
|
|---|
| 22 | /********************************************** helpful functions *********************************/
|
|---|
| 23 |
|
|---|
| 24 | // taken out of TREMOLO
|
|---|
| 25 | /*@-namechecks@*/
|
|---|
| 26 | #ifndef __GNUC__
|
|---|
| 27 | # undef __attribute__
|
|---|
| 28 | # define __attribute__(x)
|
|---|
| 29 | #endif
|
|---|
| 30 | /*@=namechecks@*/
|
|---|
| 31 |
|
|---|
| 32 | /* Behandelt aufgetretene Fehler. error ist der Fehlertyp(enum Errors)
|
|---|
| 33 | void *SpecialData ist ein untypisierter Zeiger auf Spezielle Daten zur Fehlerbehandlung.
|
|---|
| 34 | Man koennte auch noch einen Zeiger auf eine Funktion uebergeben */
|
|---|
| 35 | extern void /*@exits@*/ debug(const char *output);
|
|---|
| 36 | //__attribute__ ((__return__));
|
|---|
| 37 | #define debug(data) debug_in((data), __FILE__, __LINE__)
|
|---|
| 38 |
|
|---|
| 39 | extern void /*@exits@*/ debug_in(const char *output,
|
|---|
| 40 | const char *file, const int line);
|
|---|
| 41 | //__attribute__ ((__return__));
|
|---|
| 42 |
|
|---|
| 43 | double ask_value(const char *text);
|
|---|
| 44 | bool check_bounds(double *x, double *cell_size);
|
|---|
| 45 | void bound(double *b, double lower_bound, double upper_bound);
|
|---|
| 46 | void flip(double *x, double *y);
|
|---|
| 47 | int pot(int base, int n);
|
|---|
| 48 | char *FixedDigitNumber(const int FragmentNumber, const int digits);
|
|---|
| 49 | bool IsValidNumber( const char *string);
|
|---|
| 50 | int CompareDoubles (const void * a, const void * b);
|
|---|
| 51 | double * ReturnFullMatrixforSymmetric(double *cell_size);
|
|---|
| 52 | static void performCriticalExit();
|
|---|
| 53 |
|
|---|
| 54 | /********************************************** helpful template functions *********************************/
|
|---|
| 55 |
|
|---|
| 56 | /** Creates a lookup table for true father's Atom::Nr -> atom ptr.
|
|---|
| 57 | * \param *out output stream for debugging
|
|---|
| 58 | * \param *start begin of chain list
|
|---|
| 59 | * \paran *end end of chain list
|
|---|
| 60 | * \param **Lookuptable pointer to return allocated lookup table (should be NULL on start)
|
|---|
| 61 | * \param count optional predetermined count for table (otherwise we set the count to highest true father id)
|
|---|
| 62 | * \return true - success, false - failure
|
|---|
| 63 | */
|
|---|
| 64 | template <typename T> bool CreateFatherLookupTable(ofstream *out, T *start, T *end, T **&LookupTable, int count = 0)
|
|---|
| 65 | {
|
|---|
| 66 | bool status = true;
|
|---|
| 67 | T *Walker = NULL;
|
|---|
| 68 | int AtomNo;
|
|---|
| 69 |
|
|---|
| 70 | if (LookupTable != NULL) {
|
|---|
| 71 | *out << "Pointer for Lookup table is not NULL! Aborting ..." <<endl;
|
|---|
| 72 | return false;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | // count them
|
|---|
| 76 | if (count == 0) {
|
|---|
| 77 | Walker = start;
|
|---|
| 78 | while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
|
|---|
| 79 | Walker = Walker->next;
|
|---|
| 80 | count = (count < Walker->GetTrueFather()->nr) ? Walker->GetTrueFather()->nr : count;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | if (count <= 0) {
|
|---|
| 84 | *out << "Count of lookup list is 0 or less." << endl;
|
|---|
| 85 | return false;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | // allocat and fill
|
|---|
| 89 | LookupTable = Malloc<T*>(count, "CreateFatherLookupTable - **LookupTable");
|
|---|
| 90 | if (LookupTable == NULL) {
|
|---|
| 91 | cerr << "LookupTable memory allocation failed!" << endl;
|
|---|
| 92 | status = false;
|
|---|
| 93 | } else {
|
|---|
| 94 | for (int i=0;i<count;i++)
|
|---|
| 95 | LookupTable[i] = NULL;
|
|---|
| 96 | Walker = start;
|
|---|
| 97 | while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
|
|---|
| 98 | Walker = Walker->next;
|
|---|
| 99 | AtomNo = Walker->GetTrueFather()->nr;
|
|---|
| 100 | if ((AtomNo >= 0) && (AtomNo < count)) {
|
|---|
| 101 | //*out << "Setting LookupTable[" << AtomNo << "] to " << *Walker << endl;
|
|---|
| 102 | LookupTable[AtomNo] = Walker;
|
|---|
| 103 | } else {
|
|---|
| 104 | *out << "Walker " << *Walker << " exceeded range of nuclear ids [0, " << count << ")." << endl;
|
|---|
| 105 | status = false;
|
|---|
| 106 | break;
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | return status;
|
|---|
| 112 | };
|
|---|
| 113 |
|
|---|
| 114 | /** Frees a two-dimensional array.
|
|---|
| 115 | * \param *ptr pointer to array
|
|---|
| 116 | * \param dim first dim of array
|
|---|
| 117 | */
|
|---|
| 118 | template <typename X> void Free2DArray(X **ptr, int dim)
|
|---|
| 119 | {
|
|---|
| 120 | int i;
|
|---|
| 121 | if (ptr != NULL) {
|
|---|
| 122 | for(i=dim;i--;)
|
|---|
| 123 | if (ptr[i] != NULL)
|
|---|
| 124 | free(ptr[i]);
|
|---|
| 125 | free(ptr);
|
|---|
| 126 | }
|
|---|
| 127 | };
|
|---|
| 128 |
|
|---|
| 129 | template <typename T> void Increment(T *value, T inc)
|
|---|
| 130 | {
|
|---|
| 131 | *value += inc;
|
|---|
| 132 | };
|
|---|
| 133 |
|
|---|
| 134 | template <typename T> void AbsoluteValue(T *value, T abs)
|
|---|
| 135 | {
|
|---|
| 136 | *value = abs;
|
|---|
| 137 | };
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | #endif /*HELPERS_HPP_*/
|
|---|