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