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 <iostream>
|
---|
12 | #include <iomanip>
|
---|
13 | #include <fstream>
|
---|
14 | #include <sstream>
|
---|
15 | #include <math.h>
|
---|
16 | #include <string>
|
---|
17 | #include <stdio.h>
|
---|
18 | #include <stdlib.h>
|
---|
19 | #include <time.h>
|
---|
20 |
|
---|
21 | #include "defs.hpp"
|
---|
22 |
|
---|
23 | // include config.h
|
---|
24 | #ifdef HAVE_CONFIG_H
|
---|
25 | #include <config.h>
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | /********************************************** helpful functions *********************************/
|
---|
29 |
|
---|
30 | // taken out of TREMOLO
|
---|
31 | /*@-namechecks@*/
|
---|
32 | #ifndef __GNUC__
|
---|
33 | # undef __attribute__
|
---|
34 | # define __attribute__(x)
|
---|
35 | #endif
|
---|
36 | /*@=namechecks@*/
|
---|
37 |
|
---|
38 | /* Behandelt aufgetretene Fehler. error ist der Fehlertyp(enum Errors)
|
---|
39 | void *SpecialData ist ein untypisierter Zeiger auf Spezielle Daten zur Fehlerbehandlung.
|
---|
40 | Man koennte auch noch einen Zeiger auf eine Funktion uebergeben */
|
---|
41 | extern void /*@exits@*/ debug(const char *output);
|
---|
42 | //__attribute__ ((__return__));
|
---|
43 | #define debug(data) debug_in((data), __FILE__, __LINE__)
|
---|
44 |
|
---|
45 | extern void /*@exits@*/ debug_in(const char *output,
|
---|
46 | const char *file, const int line);
|
---|
47 | //__attribute__ ((__return__));
|
---|
48 |
|
---|
49 | double ask_value(const char *text);
|
---|
50 | bool check_bounds(double *x, double *cell_size);
|
---|
51 | void bound(double *b, double lower_bound, double upper_bound);
|
---|
52 | void flip(double *x, double *y);
|
---|
53 | int pot(int base, int n);
|
---|
54 | void * Malloc(size_t size, const char* output);
|
---|
55 | void * Calloc(size_t size, const char* output);
|
---|
56 | void * ReAlloc(void * OldPointer, size_t size, const char* output);
|
---|
57 | char* MallocString(size_t size, const char* output);
|
---|
58 | void Free(void ** buffer, const char* output);
|
---|
59 | char *FixedDigitNumber(const int FragmentNumber, const int digits);
|
---|
60 |
|
---|
61 | /********************************************** helpful template functions *********************************/
|
---|
62 |
|
---|
63 | /******************************** Some templates for list management ***********************************/
|
---|
64 |
|
---|
65 | /** Adds linking of an item to a list.
|
---|
66 | * \param *walker
|
---|
67 | * \return true - adding succeeded, false - error in list
|
---|
68 | */
|
---|
69 | template <typename X> void link(X *walker, X *end)
|
---|
70 | {
|
---|
71 | X *vorher = end->previous;
|
---|
72 | if (vorher != NULL)
|
---|
73 | vorher->next = walker;
|
---|
74 | end->previous = walker;
|
---|
75 | walker->previous = vorher;
|
---|
76 | walker->next = end;
|
---|
77 | };
|
---|
78 |
|
---|
79 | /** Removes linking of an item in a list.
|
---|
80 | * \param *walker
|
---|
81 | * \return true - removing succeeded, false - given item not found in list
|
---|
82 | */
|
---|
83 | template <typename X> void unlink(X *walker)
|
---|
84 | {
|
---|
85 | if (walker->next != NULL)
|
---|
86 | walker->next->previous = walker->previous;
|
---|
87 | if (walker->previous != NULL)
|
---|
88 | walker->previous->next = walker->next;
|
---|
89 | };
|
---|
90 |
|
---|
91 | /** Adds new item before an item \a *end in a list.
|
---|
92 | * \param *pointer item to be added
|
---|
93 | * \param *end end of list
|
---|
94 | * \return true - addition succeeded, false - unable to add item to list
|
---|
95 | */
|
---|
96 | template <typename X> bool add(X *pointer, X *end)
|
---|
97 | {
|
---|
98 | if (end != NULL) {
|
---|
99 | link(pointer, end);
|
---|
100 | } else {
|
---|
101 | pointer->previous = NULL;
|
---|
102 | pointer->next = NULL;
|
---|
103 | }
|
---|
104 | return true;
|
---|
105 | };
|
---|
106 |
|
---|
107 | /** Finds item in list
|
---|
108 | * \param *suche search criteria
|
---|
109 | * \param *start begin of list
|
---|
110 | * \param *end end of list
|
---|
111 | * \return X - if found, NULL - if not found
|
---|
112 | */
|
---|
113 | template <typename X, typename Y> X * find(Y *suche, X *start, X *end)
|
---|
114 | {
|
---|
115 | X *walker = start;
|
---|
116 | while (walker->next != end) { // go through list
|
---|
117 | walker = walker->next; // step onward beforehand
|
---|
118 | if (*walker->sort == *suche) return (walker);
|
---|
119 | }
|
---|
120 | return NULL;
|
---|
121 | };
|
---|
122 |
|
---|
123 | /** Removes an item from the list without check.
|
---|
124 | * \param *walker item to be removed
|
---|
125 | * \return true - removing succeeded, false - given item not found in list
|
---|
126 | */
|
---|
127 | template <typename X> void removewithoutcheck(X *walker)
|
---|
128 | {
|
---|
129 | if (walker != NULL) {
|
---|
130 | unlink(walker);
|
---|
131 | delete(walker);
|
---|
132 | walker = NULL;
|
---|
133 | }
|
---|
134 | };
|
---|
135 |
|
---|
136 | /** Removes an item from the list, checks if exists.
|
---|
137 | * Checks beforehand if atom is really within molecule list.
|
---|
138 | * \param *pointer item to be removed
|
---|
139 | * \param *start begin of list
|
---|
140 | * \param *end end of list
|
---|
141 | * \return true - removing succeeded, false - given item not found in list
|
---|
142 | */
|
---|
143 | template <typename X> bool remove(X *pointer, X *start, X *end)
|
---|
144 | {
|
---|
145 | X *walker = find (pointer->sort, start, end);
|
---|
146 | /* while (walker->next != pointer) { // search through list
|
---|
147 | walker = walker->next;
|
---|
148 | if (walker == end) return false; // item not found in list
|
---|
149 | }*/
|
---|
150 | // atom found, now unlink
|
---|
151 | if (walker != NULL)
|
---|
152 | removewithoutcheck(walker);
|
---|
153 | else
|
---|
154 | return false;
|
---|
155 | return true;
|
---|
156 | };
|
---|
157 |
|
---|
158 | /** Cleans the whole list.
|
---|
159 | * \param *start begin of list
|
---|
160 | * \param *end end of list
|
---|
161 | * \return true - list was cleaned successfully, false - error in list structure
|
---|
162 | */
|
---|
163 | template <typename X> bool cleanup(X *start, X *end)
|
---|
164 | {
|
---|
165 | X *pointer = start->next;
|
---|
166 | X *walker;
|
---|
167 | while (pointer != end) { // go through list
|
---|
168 | walker = pointer; // mark current
|
---|
169 | pointer = pointer->next; // step onward beforehand
|
---|
170 | // remove walker
|
---|
171 | unlink(walker);
|
---|
172 | delete(walker);
|
---|
173 | walker = NULL;
|
---|
174 | }
|
---|
175 | return true;
|
---|
176 | };
|
---|
177 |
|
---|
178 | /** Returns the first marker in a chain list.
|
---|
179 | * \param *me one arbitrary item in chain list
|
---|
180 | * \return poiner to first marker
|
---|
181 | */
|
---|
182 | template <typename X> X *GetFirst(X *me)
|
---|
183 | {
|
---|
184 | X *Binder = me;
|
---|
185 | while(Binder->previous != NULL)
|
---|
186 | Binder = Binder->previous;
|
---|
187 | return Binder;
|
---|
188 | };
|
---|
189 |
|
---|
190 | /** Returns the last marker in a chain list.
|
---|
191 | * \param *me one arbitrary item in chain list
|
---|
192 | * \return poiner to last marker
|
---|
193 | */
|
---|
194 | template <typename X> X *GetLast(X *me)
|
---|
195 | {
|
---|
196 | X *Binder = me;
|
---|
197 | while(Binder->next != NULL)
|
---|
198 | Binder = Binder->next;
|
---|
199 | return Binder;
|
---|
200 | };
|
---|
201 |
|
---|
202 | /** Frees a two-dimensional array.
|
---|
203 | * \param *ptr pointer to array
|
---|
204 | * \param dim first dim of array
|
---|
205 | */
|
---|
206 | template <typename X> void Free2DArray(X **ptr, int dim)
|
---|
207 | {
|
---|
208 | int i;
|
---|
209 | if (ptr != NULL) {
|
---|
210 | for(i=0;i<dim;i++)
|
---|
211 | if (ptr[i] != NULL)
|
---|
212 | free(ptr[i]);
|
---|
213 | free(ptr);
|
---|
214 | }
|
---|
215 | };
|
---|
216 |
|
---|
217 | /************************************* Class Verbose & Binary *******************************/
|
---|
218 |
|
---|
219 | /** Verbose is an IO manipulator, that writes tabs according to \a Verbosity level.
|
---|
220 | */
|
---|
221 | class Verbose
|
---|
222 | {
|
---|
223 | public:
|
---|
224 | Verbose(int value) : Verbosity(value) { }
|
---|
225 |
|
---|
226 | ostream& print (ostream &ost) const;
|
---|
227 | private:
|
---|
228 | int Verbosity;
|
---|
229 | };
|
---|
230 |
|
---|
231 | ostream& operator<<(ostream& ost,const Verbose& m);
|
---|
232 |
|
---|
233 | /** Binary is an IO manipulator, that writes 0 and 1 according to number \a Binary.
|
---|
234 | */
|
---|
235 | class Binary
|
---|
236 | {
|
---|
237 | public:
|
---|
238 | Binary(int value) : BinaryNumber(value) { }
|
---|
239 |
|
---|
240 | ostream& print (ostream &ost) const;
|
---|
241 | private:
|
---|
242 | int BinaryNumber;
|
---|
243 | };
|
---|
244 |
|
---|
245 | ostream& operator<<(ostream& ost,const Binary& m);
|
---|
246 |
|
---|
247 |
|
---|
248 |
|
---|
249 | #endif /*HELPERS_HPP_*/
|
---|