| [14de469] | 1 | /** \file helpers.cpp
 | 
|---|
 | 2 |  *
 | 
|---|
 | 3 |  * Implementation of some auxiliary functions for memory dis-/allocation and so on 
 | 
|---|
 | 4 |  */
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | 
 | 
|---|
 | 7 | #include "helpers.hpp"
 | 
|---|
 | 8 | 
 | 
|---|
 | 9 | /********************************************** helpful functions *********************************/
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | 
 | 
|---|
 | 12 | /** Asks for a double value and checks input
 | 
|---|
 | 13 |  * \param *text question
 | 
|---|
 | 14 |  */
 | 
|---|
 | 15 | double ask_value(const char *text)
 | 
|---|
 | 16 | {
 | 
|---|
 | 17 |   double test = 0.1439851348959832147598734598273456723948652983045928346598365;
 | 
|---|
 | 18 |   do {
 | 
|---|
 | 19 |     cout << Verbose(0) << text;
 | 
|---|
 | 20 |     cin >> test;
 | 
|---|
 | 21 |   } while (test == 0.1439851348959832147598734598273456723948652983045928346598365);
 | 
|---|
 | 22 |   return test;   
 | 
|---|
 | 23 | };
 | 
|---|
 | 24 | 
 | 
|---|
| [d3a46d] | 25 | /** Output of a debug message to stderr.
 | 
|---|
 | 26 |  * \param *P Problem at hand, points to ParallelSimulationData#me
 | 
|---|
 | 27 |  * \param output output string
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | #ifdef HAVE_DEBUG
 | 
|---|
 | 30 | void debug_in(const char *output, const char *file, const int line) {
 | 
|---|
 | 31 |   if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output);
 | 
|---|
 | 32 | }
 | 
|---|
 | 33 | #else
 | 
|---|
 | 34 | void debug_in(const char *output, const char *file, const int line) {}  // print nothing
 | 
|---|
 | 35 | #endif
 | 
|---|
| [14de469] | 36 | 
 | 
|---|
 | 37 | /** Wrapper for allocating'ing a memory range with *output if it fails.
 | 
|---|
 | 38 |  * \param size number of chars to alloc for \a *buffer
 | 
|---|
 | 39 |  * \param *output message if malloc fails
 | 
|---|
 | 40 |  * \return pointer to memory range
 | 
|---|
 | 41 |  */
 | 
|---|
 | 42 | void * Malloc(size_t size, const char* output) 
 | 
|---|
 | 43 | {
 | 
|---|
 | 44 |   void *buffer = NULL;
 | 
|---|
 | 45 |   buffer = (void *)malloc(size); // alloc
 | 
|---|
 | 46 |   if (buffer == NULL)
 | 
|---|
 | 47 |     cout << Verbose(0) << "Malloc failed - pointer is NULL: " << output << endl;
 | 
|---|
 | 48 |   return(buffer);
 | 
|---|
 | 49 | };
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | /** Wrapper for allocating'ing a memory range with *output if it fails.
 | 
|---|
 | 52 |  * \param size number of chars to alloc for \a *buffer
 | 
|---|
 | 53 |  * \param *output message if malloc fails
 | 
|---|
 | 54 |  * \return pointer to memory range
 | 
|---|
 | 55 |  */
 | 
|---|
 | 56 | void * Calloc(size_t size, const char* output) 
 | 
|---|
 | 57 | {
 | 
|---|
 | 58 |   void *buffer = NULL;
 | 
|---|
 | 59 |   buffer = (void *)calloc(size, (size_t)0); // alloc
 | 
|---|
 | 60 |   if (buffer == NULL)
 | 
|---|
 | 61 |     cout << Verbose(0) << "Calloc failed - pointer is NULL: " << output << endl;
 | 
|---|
 | 62 |   return(buffer);
 | 
|---|
 | 63 | };
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | /** Wrapper for reallocating'ing a memory range with *output if it fails.
 | 
|---|
 | 66 |  * \param *OldPointer pointer to old memory range
 | 
|---|
 | 67 |  * \param size number of chars to alloc for \a *buffer
 | 
|---|
 | 68 |  * \param *output message if malloc fails
 | 
|---|
 | 69 |  * \return pointer to memory range
 | 
|---|
 | 70 |  */
 | 
|---|
 | 71 | void * ReAlloc(void * OldPointer, size_t size, const char* output) 
 | 
|---|
 | 72 | {
 | 
|---|
 | 73 |   void *buffer = NULL;
 | 
|---|
 | 74 |   if (OldPointer == NULL)
 | 
|---|
 | 75 |         cout << Verbose(0) << "ReAlloc impossible - old is NULL: " << output << endl;
 | 
|---|
 | 76 |   buffer = (void *)realloc(OldPointer, size); // alloc
 | 
|---|
 | 77 |   if (buffer == NULL)
 | 
|---|
 | 78 |     cout << Verbose(0) << "ReAlloc failed - new is NULL: " << output << endl;
 | 
|---|
 | 79 |   return(buffer);
 | 
|---|
 | 80 | };
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | /** Wrapper for free'ing an allocated memory range with *output if it fails.
 | 
|---|
 | 83 |  * \param *buffer pointer to buffer to be free'd
 | 
|---|
 | 84 |  * \param *output message if free fails
 | 
|---|
 | 85 |  */
 | 
|---|
 | 86 | void Free(void ** buffer, const char* output) 
 | 
|---|
 | 87 | {
 | 
|---|
 | 88 |   if (*buffer == NULL) {
 | 
|---|
 | 89 |     //cout << Verbose(5) << "Free not necesary: " << output << endl;
 | 
|---|
 | 90 |   } else {
 | 
|---|
 | 91 |     free(*buffer);
 | 
|---|
 | 92 |   }
 | 
|---|
 | 93 |   *buffer = NULL;
 | 
|---|
 | 94 | };
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | /** Malloc string array and set its length to the allocated length.
 | 
|---|
 | 97 |  * \param *output message if malloc fails
 | 
|---|
 | 98 |  * \param size number of chars to alloc for \a *buffer
 | 
|---|
 | 99 |  * \return pointer to string array
 | 
|---|
 | 100 |  */
 | 
|---|
 | 101 | char* MallocString(size_t size, const char* output) 
 | 
|---|
 | 102 | {
 | 
|---|
 | 103 |   size_t i;
 | 
|---|
 | 104 |   char *buffer;
 | 
|---|
 | 105 |   buffer = (char *)malloc(sizeof(char) * (size+1)); // alloc
 | 
|---|
 | 106 |   if (buffer == NULL)
 | 
|---|
 | 107 |     cout << Verbose(0) << output << endl;
 | 
|---|
 | 108 |   for (i=0;i<size;i++)  // reset
 | 
|---|
 | 109 |     buffer[i] = i % 2 == 0 ? 'p': 'c';
 | 
|---|
 | 110 |   buffer[size] = '\0'; // and set length marker on its end
 | 
|---|
 | 111 |   return(buffer);
 | 
|---|
 | 112 | }
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 | /** modulo operator for doubles.
 | 
|---|
 | 115 |  * \param *b pointer to double
 | 
|---|
 | 116 |  * \param lower_bound lower bound
 | 
|---|
 | 117 |  * \param upper_bound upper bound
 | 
|---|
 | 118 |  */
 | 
|---|
 | 119 | void bound(double *b, double lower_bound, double upper_bound)
 | 
|---|
 | 120 | {
 | 
|---|
 | 121 |   double step = (upper_bound - lower_bound);
 | 
|---|
 | 122 |   while (*b >= upper_bound)
 | 
|---|
 | 123 |     *b -= step;    
 | 
|---|
 | 124 |   while (*b < lower_bound)
 | 
|---|
 | 125 |     *b += step;    
 | 
|---|
 | 126 | }; 
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 | /** Flips two doubles.
 | 
|---|
 | 129 |  * \param *x pointer to first double
 | 
|---|
 | 130 |  * \param *y pointer to second double
 | 
|---|
 | 131 |  */
 | 
|---|
 | 132 | void flip(double *x, double *y)
 | 
|---|
 | 133 | {
 | 
|---|
 | 134 |   double tmp;
 | 
|---|
 | 135 |   tmp = *x;
 | 
|---|
 | 136 |   *x = *y;
 | 
|---|
 | 137 |   *y = tmp;
 | 
|---|
 | 138 | };
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 | /** Returns the power of \a n with respect to \a base.
 | 
|---|
 | 141 |  * \param base basis
 | 
|---|
 | 142 |  * \param n power
 | 
|---|
 | 143 |  * \return \f$base^n\f$
 | 
|---|
 | 144 |  */
 | 
|---|
 | 145 | int pot(int base, int n)
 | 
|---|
 | 146 | {
 | 
|---|
 | 147 |   int res = 1;
 | 
|---|
 | 148 |   int j;
 | 
|---|
 | 149 |   for (j=0;j<n;j++)
 | 
|---|
 | 150 |     res *= base;
 | 
|---|
 | 151 |   return res;
 | 
|---|
 | 152 | };
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
 | 
|---|
 | 155 |  * \param FragmentNumber total number of fragments to determine necessary number of digits
 | 
|---|
 | 156 |  * \param digits number to create with 0 prefixed
 | 
|---|
 | 157 |  * \return allocated(!) char array with number in digits, ten base.
 | 
|---|
 | 158 |  */
 | 
|---|
 | 159 | char *FixedDigitNumber(const int FragmentNumber, const int digits)
 | 
|---|
 | 160 | {
 | 
|---|
 | 161 |   char *returnstring;
 | 
|---|
 | 162 |   int number = FragmentNumber;
 | 
|---|
 | 163 |   int order = 0;
 | 
|---|
 | 164 |   while (number != 0) { // determine number of digits needed
 | 
|---|
 | 165 |     number = (int)floor(((double)number / 10.));
 | 
|---|
 | 166 |     order++;
 | 
|---|
 | 167 |     //cout << "Number is " << number << ", order is " << order << "." << endl;
 | 
|---|
 | 168 |   }
 | 
|---|
 | 169 |   // allocate string
 | 
|---|
 | 170 |   returnstring = (char *) Malloc(sizeof(char)*(order+2), "MoleculeListClass::CreateFragmentNumberForOutput: *returnstring");
 | 
|---|
 | 171 |   // terminate  and fill string array from end backward
 | 
|---|
 | 172 |   returnstring[order] = '\0';
 | 
|---|
 | 173 |   number = digits;
 | 
|---|
 | 174 |   for (int i=order-1;i>=0;i--){
 | 
|---|
 | 175 |     returnstring[i] = '0' + (char)(number % 10);
 | 
|---|
 | 176 |     number = (int)floor(((double)number / 10.));
 | 
|---|
 | 177 |   }
 | 
|---|
 | 178 |   return returnstring;
 | 
|---|
 | 179 | };
 | 
|---|
 | 180 | 
 | 
|---|