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 | int pot(int base, int n);
|
---|
47 | int CountLinesinFile(ifstream &InputFile);
|
---|
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 | void performCriticalExit();
|
---|
53 |
|
---|
54 | /********************************************** helpful template functions *********************************/
|
---|
55 |
|
---|
56 | /** Flips two values.
|
---|
57 | * \param x first value
|
---|
58 | * \param y second value
|
---|
59 | */
|
---|
60 | template <typename T> void flip(T &x, T &y)
|
---|
61 | {
|
---|
62 | T tmp;
|
---|
63 | tmp = x;
|
---|
64 | x = y;
|
---|
65 | y = tmp;
|
---|
66 | };
|
---|
67 |
|
---|
68 | /** Creates a lookup table for true father's Atom::Nr -> atom ptr.
|
---|
69 | * \param *out output stream for debugging
|
---|
70 | * \param *start begin of chain list
|
---|
71 | * \paran *end end of chain list
|
---|
72 | * \param **Lookuptable pointer to return allocated lookup table (should be NULL on start)
|
---|
73 | * \param count optional predetermined count for table (otherwise we set the count to highest true father id)
|
---|
74 | * \return true - success, false - failure
|
---|
75 | */
|
---|
76 | template <typename T> bool CreateFatherLookupTable(ofstream *out, T *start, T *end, T **&LookupTable, int count = 0)
|
---|
77 | {
|
---|
78 | bool status = true;
|
---|
79 | T *Walker = NULL;
|
---|
80 | int AtomNo;
|
---|
81 |
|
---|
82 | if (LookupTable != NULL) {
|
---|
83 | *out << "Pointer for Lookup table is not NULL! Aborting ..." <<endl;
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 |
|
---|
87 | // count them
|
---|
88 | if (count == 0) {
|
---|
89 | Walker = start;
|
---|
90 | while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
|
---|
91 | Walker = Walker->next;
|
---|
92 | count = (count < Walker->GetTrueFather()->nr) ? Walker->GetTrueFather()->nr : count;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | if (count <= 0) {
|
---|
96 | *out << "Count of lookup list is 0 or less." << endl;
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | // allocat and fill
|
---|
101 | LookupTable = Malloc<T*>(count, "CreateFatherLookupTable - **LookupTable");
|
---|
102 | if (LookupTable == NULL) {
|
---|
103 | cerr << "LookupTable memory allocation failed!" << endl;
|
---|
104 | status = false;
|
---|
105 | } else {
|
---|
106 | for (int i=0;i<count;i++)
|
---|
107 | LookupTable[i] = NULL;
|
---|
108 | Walker = start;
|
---|
109 | while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
|
---|
110 | Walker = Walker->next;
|
---|
111 | AtomNo = Walker->GetTrueFather()->nr;
|
---|
112 | if ((AtomNo >= 0) && (AtomNo < count)) {
|
---|
113 | //*out << "Setting LookupTable[" << AtomNo << "] to " << *Walker << endl;
|
---|
114 | LookupTable[AtomNo] = Walker;
|
---|
115 | } else {
|
---|
116 | *out << "Walker " << *Walker << " exceeded range of nuclear ids [0, " << count << ")." << endl;
|
---|
117 | status = false;
|
---|
118 | break;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | return status;
|
---|
124 | };
|
---|
125 |
|
---|
126 | /** Frees a two-dimensional array.
|
---|
127 | * \param *ptr pointer to array
|
---|
128 | * \param dim first dim of array
|
---|
129 | */
|
---|
130 | template <typename X> void Free2DArray(X **ptr, int dim)
|
---|
131 | {
|
---|
132 | int i;
|
---|
133 | if (ptr != NULL) {
|
---|
134 | for(i=dim;i--;)
|
---|
135 | if (ptr[i] != NULL)
|
---|
136 | free(ptr[i]);
|
---|
137 | free(ptr);
|
---|
138 | }
|
---|
139 | };
|
---|
140 |
|
---|
141 | template <typename T> void Increment(T *value, T *inc)
|
---|
142 | {
|
---|
143 | *value += *inc;
|
---|
144 | };
|
---|
145 |
|
---|
146 | template <typename T> void AbsoluteValue(T *value, T *abs)
|
---|
147 | {
|
---|
148 | *value = *abs;
|
---|
149 | };
|
---|
150 |
|
---|
151 | template <typename T> void IncrementalAbsoluteValue(T *value, T *abs)
|
---|
152 | {
|
---|
153 | *value = *abs;
|
---|
154 | (*abs) += 1;
|
---|
155 | };
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|
159 | #endif /*HELPERS_HPP_*/
|
---|