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