| [14de469] | 1 | /** \file helpers.cpp | 
|---|
|  | 2 | * | 
|---|
| [6ac7ee] | 3 | * Implementation of some auxiliary functions for memory dis-/allocation and so on | 
|---|
| [14de469] | 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 | { | 
|---|
| [042f82] | 17 | double test = 0.1439851348959832147598734598273456723948652983045928346598365; | 
|---|
|  | 18 | do { | 
|---|
|  | 19 | cout << Verbose(0) << text; | 
|---|
|  | 20 | cin >> test; | 
|---|
|  | 21 | } while (test == 0.1439851348959832147598734598273456723948652983045928346598365); | 
|---|
|  | 22 | return test; | 
|---|
| [14de469] | 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) { | 
|---|
| [042f82] | 31 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output); | 
|---|
| [d3a46d] | 32 | } | 
|---|
|  | 33 | #else | 
|---|
| [042f82] | 34 | void debug_in(const char *output, const char *file, const int line) {}  // print nothing | 
|---|
| [d3a46d] | 35 | #endif | 
|---|
| [14de469] | 36 |  | 
|---|
|  | 37 | /** modulo operator for doubles. | 
|---|
|  | 38 | * \param *b pointer to double | 
|---|
|  | 39 | * \param lower_bound lower bound | 
|---|
|  | 40 | * \param upper_bound upper bound | 
|---|
|  | 41 | */ | 
|---|
|  | 42 | void bound(double *b, double lower_bound, double upper_bound) | 
|---|
|  | 43 | { | 
|---|
| [042f82] | 44 | double step = (upper_bound - lower_bound); | 
|---|
|  | 45 | while (*b >= upper_bound) | 
|---|
|  | 46 | *b -= step; | 
|---|
|  | 47 | while (*b < lower_bound) | 
|---|
|  | 48 | *b += step; | 
|---|
| [6ac7ee] | 49 | }; | 
|---|
| [14de469] | 50 |  | 
|---|
|  | 51 | /** Flips two doubles. | 
|---|
|  | 52 | * \param *x pointer to first double | 
|---|
|  | 53 | * \param *y pointer to second double | 
|---|
|  | 54 | */ | 
|---|
|  | 55 | void flip(double *x, double *y) | 
|---|
|  | 56 | { | 
|---|
| [042f82] | 57 | double tmp; | 
|---|
|  | 58 | tmp = *x; | 
|---|
|  | 59 | *x = *y; | 
|---|
|  | 60 | *y = tmp; | 
|---|
| [14de469] | 61 | }; | 
|---|
|  | 62 |  | 
|---|
|  | 63 | /** Returns the power of \a n with respect to \a base. | 
|---|
|  | 64 | * \param base basis | 
|---|
|  | 65 | * \param n power | 
|---|
|  | 66 | * \return \f$base^n\f$ | 
|---|
|  | 67 | */ | 
|---|
|  | 68 | int pot(int base, int n) | 
|---|
|  | 69 | { | 
|---|
| [042f82] | 70 | int res = 1; | 
|---|
|  | 71 | int j; | 
|---|
|  | 72 | for (j=n;j--;) | 
|---|
|  | 73 | res *= base; | 
|---|
|  | 74 | return res; | 
|---|
| [14de469] | 75 | }; | 
|---|
|  | 76 |  | 
|---|
|  | 77 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits. | 
|---|
|  | 78 | * \param FragmentNumber total number of fragments to determine necessary number of digits | 
|---|
|  | 79 | * \param digits number to create with 0 prefixed | 
|---|
|  | 80 | * \return allocated(!) char array with number in digits, ten base. | 
|---|
|  | 81 | */ | 
|---|
|  | 82 | char *FixedDigitNumber(const int FragmentNumber, const int digits) | 
|---|
|  | 83 | { | 
|---|
| [042f82] | 84 | char *returnstring; | 
|---|
|  | 85 | int number = FragmentNumber; | 
|---|
|  | 86 | int order = 0; | 
|---|
|  | 87 | while (number != 0) { // determine number of digits needed | 
|---|
|  | 88 | number = (int)floor(((double)number / 10.)); | 
|---|
|  | 89 | order++; | 
|---|
|  | 90 | //cout << "Number is " << number << ", order is " << order << "." << endl; | 
|---|
|  | 91 | } | 
|---|
|  | 92 | // allocate string | 
|---|
| [29812d] | 93 | returnstring = Malloc<char>(order + 2, "FixedDigitNumber: *returnstring"); | 
|---|
| [042f82] | 94 | // terminate  and fill string array from end backward | 
|---|
|  | 95 | returnstring[order] = '\0'; | 
|---|
|  | 96 | number = digits; | 
|---|
|  | 97 | for (int i=order;i--;) { | 
|---|
|  | 98 | returnstring[i] = '0' + (char)(number % 10); | 
|---|
|  | 99 | number = (int)floor(((double)number / 10.)); | 
|---|
|  | 100 | } | 
|---|
|  | 101 | //cout << returnstring << endl; | 
|---|
|  | 102 | return returnstring; | 
|---|
| [14de469] | 103 | }; | 
|---|
|  | 104 |  | 
|---|
| [e198c7] | 105 | /** Tests whether a given string contains a valid number or not. | 
|---|
|  | 106 | * \param *string | 
|---|
|  | 107 | * \return true - is a number, false - is not a valid number | 
|---|
|  | 108 | */ | 
|---|
| [6ac7ee] | 109 | bool IsValidNumber( const char *string) | 
|---|
| [e198c7] | 110 | { | 
|---|
| [042f82] | 111 | int ptr = 0; | 
|---|
|  | 112 | if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot | 
|---|
|  | 113 | ptr++; | 
|---|
|  | 114 | if ((string[ptr] >= '0') && (string[ptr] <= '9')) | 
|---|
|  | 115 | return true; | 
|---|
|  | 116 | return false; | 
|---|
| [e198c7] | 117 | }; | 
|---|
|  | 118 |  | 
|---|
| [29812d] | 119 | /** | 
|---|
|  | 120 | * Allocates a memory range using malloc(). | 
|---|
|  | 121 | * Prints the provided error message in case of a failure. | 
|---|
|  | 122 | * | 
|---|
|  | 123 | * \param number of memory slices of type X to allocate | 
|---|
|  | 124 | * \param failure message which is printed if the allocation fails | 
|---|
|  | 125 | * \return pointer to the allocated memory range, will be NULL if a failure occurred | 
|---|
|  | 126 | */ | 
|---|
|  | 127 | template <> char* Malloc<char>(size_t size, const char* output) | 
|---|
|  | 128 | { | 
|---|
|  | 129 | char* buffer = NULL; | 
|---|
|  | 130 | buffer = (char*) malloc(sizeof(char) * (size + 1)); | 
|---|
|  | 131 | for (size_t i = size; i--;) | 
|---|
|  | 132 | buffer[i] = (i % 2 == 0) ? 'p': 'c'; | 
|---|
|  | 133 | buffer[size] = '\0'; | 
|---|
|  | 134 |  | 
|---|
|  | 135 | if (buffer == NULL) | 
|---|
|  | 136 | cout << Verbose(0) << "Malloc for datatype " << typeid(char).name() | 
|---|
|  | 137 | << " failed - pointer is NULL: " << output << endl; | 
|---|
|  | 138 |  | 
|---|
|  | 139 | return buffer; | 
|---|
|  | 140 | }; | 
|---|
|  | 141 |  | 
|---|
| [e198c7] | 142 |  | 
|---|