source: molecuilder/src/helpers.hpp@ 17b3a5c

Last change on this file since 17b3a5c was 17b3a5c, checked in by Frederik Heber <heber@…>, 16 years ago

forward declarations used to untangle interdependet classes.

  • basically, everywhere in header files we removed '#include' lines were only pointer to the respective classes were used and the include line was moved to the implementation file.
  • as a sidenote, lots of funny errors happened because headers were included via a nesting over three other includes. Now, all should be declared directly as needed, as only very little include lines remain in header files.
  • Property mode set to 100755
File size: 4.0 KB
Line 
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
9using 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 */
35extern void /*@exits@*/ debug(const char *output);
36 //__attribute__ ((__return__));
37#define debug(data) debug_in((data), __FILE__, __LINE__)
38
39extern void /*@exits@*/ debug_in(const char *output,
40 const char *file, const int line);
41 //__attribute__ ((__return__));
42
43double ask_value(const char *text);
44bool check_bounds(double *x, double *cell_size);
45void bound(double *b, double lower_bound, double upper_bound);
46void flip(double *x, double *y);
47int pot(int base, int n);
48char *FixedDigitNumber(const int FragmentNumber, const int digits);
49bool IsValidNumber( const char *string);
50int CompareDoubles (const void * a, const void * b);
51double * ReturnFullMatrixforSymmetric(double *cell_size);
52
53/********************************************** helpful template functions *********************************/
54
55/** Creates a lookup table for true father's Atom::Nr -> atom ptr.
56 * \param *out output stream for debugging
57 * \param *start begin of chain list
58 * \paran *end end of chain list
59 * \param **Lookuptable pointer to return allocated lookup table (should be NULL on start)
60 * \param count optional predetermined count for table (otherwise we set the count to highest true father id)
61 * \return true - success, false - failure
62 */
63template <typename T> bool CreateFatherLookupTable(ofstream *out, T *start, T *end, T **&LookupTable, int count = 0)
64{
65 bool status = true;
66 T *Walker = NULL;
67 int AtomNo;
68
69 if (LookupTable != NULL) {
70 *out << "Pointer for Lookup table is not NULL! Aborting ..." <<endl;
71 return false;
72 }
73
74 // count them
75 if (count == 0) {
76 Walker = start;
77 while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
78 Walker = Walker->next;
79 count = (count < Walker->GetTrueFather()->nr) ? Walker->GetTrueFather()->nr : count;
80 }
81 }
82 if (count <= 0) {
83 *out << "Count of lookup list is 0 or less." << endl;
84 return false;
85 }
86
87 // allocat and fill
88 LookupTable = Malloc<T*>(count, "CreateFatherLookupTable - **LookupTable");
89 if (LookupTable == NULL) {
90 cerr << "LookupTable memory allocation failed!" << endl;
91 status = false;
92 } else {
93 for (int i=0;i<count;i++)
94 LookupTable[i] = NULL;
95 Walker = start;
96 while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
97 Walker = Walker->next;
98 AtomNo = Walker->GetTrueFather()->nr;
99 if ((AtomNo >= 0) && (AtomNo < count)) {
100 //*out << "Setting LookupTable[" << AtomNo << "] to " << *Walker << endl;
101 LookupTable[AtomNo] = Walker;
102 } else {
103 *out << "Walker " << *Walker << " exceeded range of nuclear ids [0, " << count << ")." << endl;
104 status = false;
105 break;
106 }
107 }
108 }
109
110 return status;
111};
112
113/** Frees a two-dimensional array.
114 * \param *ptr pointer to array
115 * \param dim first dim of array
116 */
117template <typename X> void Free2DArray(X **ptr, int dim)
118{
119 int i;
120 if (ptr != NULL) {
121 for(i=dim;i--;)
122 if (ptr[i] != NULL)
123 free(ptr[i]);
124 free(ptr);
125 }
126};
127
128template <typename T> void Increment(T *value, T inc)
129{
130 *value += inc;
131};
132
133template <typename T> void AbsoluteValue(T *value, T abs)
134{
135 *value = abs;
136};
137
138
139
140
141#endif /*HELPERS_HPP_*/
Note: See TracBrowser for help on using the repository browser.