source: molecuilder/src/helpers.hpp@ c1b76e

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

"not working parsed molecule into subgraph splitting"-BUG fixed, BugFinder branch can be closed.

  • config::Load() - atoms were not in the right order for MaxOrder-test (12). Hence, the BondFragmentAdjacency could not be parsed. Now, we just take the subgraphs as the association of each atom to a molecule, i.e. we make a list and then re-link each atom to its new connected subgraph molecule, which is returned as the MoleculeListClass.

other fixes:

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[a0bcf1]1/** \file helpers.hpp
2 *
[e08f45]3 * Declaration of some auxiliary functions for memory dis-/allocation and so on
[a0bcf1]4 */
5
6#ifndef HELPERS_HPP_
7#define HELPERS_HPP_
8
9using namespace std;
10
[17b3a5c]11/*********************************************** includes ***********************************/
12
[4dca8e]13// include config.h
14#ifdef HAVE_CONFIG_H
15#include <config.h>
16#endif
17
[a0bcf1]18#include <fstream>
19
[390a2b]20#include "memoryallocator.hpp"
[77da65]21
[a0bcf1]22/********************************************** helpful functions *********************************/
23
[da83f8]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)
[a048fa]33 void *SpecialData ist ein untypisierter Zeiger auf Spezielle Daten zur Fehlerbehandlung.
34 Man koennte auch noch einen Zeiger auf eine Funktion uebergeben */
[da83f8]35extern void /*@exits@*/ debug(const char *output);
[a048fa]36 //__attribute__ ((__return__));
[da83f8]37#define debug(data) debug_in((data), __FILE__, __LINE__)
38
39extern void /*@exits@*/ debug_in(const char *output,
[a048fa]40 const char *file, const int line);
41 //__attribute__ ((__return__));
[da83f8]42
[a0bcf1]43double ask_value(const char *text);
44bool check_bounds(double *x, double *cell_size);
45void bound(double *b, double lower_bound, double upper_bound);
46int pot(int base, int n);
[ff9879]47int CountLinesinFile(ifstream &InputFile);
[a0bcf1]48char *FixedDigitNumber(const int FragmentNumber, const int digits);
[45105b]49bool IsValidNumber( const char *string);
[17b3a5c]50int CompareDoubles (const void * a, const void * b);
51double * ReturnFullMatrixforSymmetric(double *cell_size);
[a2db10]52void performCriticalExit();
[a0bcf1]53
[d50d2a]54/********************************************** helpful template functions *********************************/
55
[543794]56/** Flips two values.
[921097]57 * \param x first value
58 * \param y second value
[543794]59 */
[921097]60template <typename T> void flip(T &x, T &y)
[543794]61{
62 T tmp;
[921097]63 tmp = x;
64 x = y;
65 y = tmp;
[543794]66};
67
[c901e3]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)
[c1b4a4]73 * \param count optional predetermined size for table (otherwise we set the count to highest true father id)
[c901e3]74 * \return true - success, false - failure
75 */
76template <typename T> bool CreateFatherLookupTable(ofstream *out, T *start, T *end, T **&LookupTable, int count = 0)
77{
[a048fa]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
[8bc524]100 // allocate and fill
101 LookupTable = Calloc<T*>(count, "CreateFatherLookupTable - **LookupTable");
[a048fa]102 if (LookupTable == NULL) {
103 cerr << "LookupTable memory allocation failed!" << endl;
104 status = false;
105 } else {
106 Walker = start;
107 while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
108 Walker = Walker->next;
109 AtomNo = Walker->GetTrueFather()->nr;
110 if ((AtomNo >= 0) && (AtomNo < count)) {
111 //*out << "Setting LookupTable[" << AtomNo << "] to " << *Walker << endl;
112 LookupTable[AtomNo] = Walker;
113 } else {
114 *out << "Walker " << *Walker << " exceeded range of nuclear ids [0, " << count << ")." << endl;
115 status = false;
116 break;
117 }
118 }
119 }
120
121 return status;
[c901e3]122};
123
[d50d2a]124/** Frees a two-dimensional array.
125 * \param *ptr pointer to array
126 * \param dim first dim of array
127 */
128template <typename X> void Free2DArray(X **ptr, int dim)
129{
[a048fa]130 int i;
131 if (ptr != NULL) {
132 for(i=dim;i--;)
133 if (ptr[i] != NULL)
134 free(ptr[i]);
135 free(ptr);
136 }
[d50d2a]137};
[a0bcf1]138
[ff9879]139template <typename T> void Increment(T *value, T *inc)
[8ffe32]140{
[ff9879]141 *value += *inc;
[8ffe32]142};
143
[ff9879]144template <typename T> void AbsoluteValue(T *value, T *abs)
[8ffe32]145{
[ff9879]146 *value = *abs;
[8ffe32]147};
148
[ff9879]149template <typename T> void IncrementalAbsoluteValue(T *value, T *abs)
150{
151 *value = *abs;
152 (*abs) += 1;
153};
[a0bcf1]154
[390a2b]155
[17b3a5c]156
[a0bcf1]157#endif /*HELPERS_HPP_*/
Note: See TracBrowser for help on using the repository browser.