[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 | {
|
---|
[6ac7ee] | 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) {
|
---|
[6ac7ee] | 31 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output);
|
---|
[d3a46d] | 32 | }
|
---|
| 33 | #else
|
---|
[6ac7ee] | 34 | void debug_in(const char *output, const char *file, const int line) {} // print nothing
|
---|
[d3a46d] | 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 | */
|
---|
[6ac7ee] | 42 | void * Malloc(size_t size, const char* output)
|
---|
[14de469] | 43 | {
|
---|
[6ac7ee] | 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);
|
---|
[14de469] | 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 | */
|
---|
[6ac7ee] | 56 | void * Calloc(size_t size, const char* output)
|
---|
[14de469] | 57 | {
|
---|
[6ac7ee] | 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);
|
---|
[14de469] | 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 | */
|
---|
[6ac7ee] | 71 | void * ReAlloc(void * OldPointer, size_t size, const char* output)
|
---|
[14de469] | 72 | {
|
---|
[6ac7ee] | 73 | void *buffer = NULL;
|
---|
| 74 | if (OldPointer == NULL)
|
---|
| 75 | //cout << Verbose(0) << "ReAlloc impossible - old is NULL: " << output << endl;
|
---|
| 76 | buffer = (void *)malloc(size); // malloc
|
---|
| 77 | else
|
---|
| 78 | buffer = (void *)realloc(OldPointer, size); // realloc
|
---|
| 79 | if (buffer == NULL)
|
---|
| 80 | cout << Verbose(0) << "ReAlloc failed - new is NULL: " << output << endl;
|
---|
| 81 | return(buffer);
|
---|
[14de469] | 82 | };
|
---|
| 83 |
|
---|
| 84 | /** Wrapper for free'ing an allocated memory range with *output if it fails.
|
---|
| 85 | * \param *buffer pointer to buffer to be free'd
|
---|
| 86 | * \param *output message if free fails
|
---|
| 87 | */
|
---|
[6ac7ee] | 88 | void Free(void ** buffer, const char* output)
|
---|
[14de469] | 89 | {
|
---|
[6ac7ee] | 90 | if (*buffer == NULL) {
|
---|
| 91 | //cout << Verbose(5) << "Free not necesary: " << output << endl;
|
---|
| 92 | } else {
|
---|
| 93 | free(*buffer);
|
---|
| 94 | *buffer = NULL;
|
---|
| 95 | }
|
---|
[14de469] | 96 | };
|
---|
| 97 |
|
---|
| 98 | /** Malloc string array and set its length to the allocated length.
|
---|
| 99 | * \param *output message if malloc fails
|
---|
| 100 | * \param size number of chars to alloc for \a *buffer
|
---|
| 101 | * \return pointer to string array
|
---|
| 102 | */
|
---|
[6ac7ee] | 103 | char* MallocString(size_t size, const char* output)
|
---|
[14de469] | 104 | {
|
---|
[6ac7ee] | 105 | size_t i;
|
---|
| 106 | char *buffer;
|
---|
| 107 | buffer = (char *)malloc(sizeof(char) * (size+1)); // alloc
|
---|
| 108 | if (buffer == NULL)
|
---|
| 109 | cout << Verbose(0) << output << endl;
|
---|
| 110 | for (i=size;i--;) // reset
|
---|
| 111 | buffer[i] = i % 2 == 0 ? 'p': 'c';
|
---|
| 112 | buffer[size] = '\0'; // and set length marker on its end
|
---|
| 113 | return(buffer);
|
---|
[14de469] | 114 | }
|
---|
| 115 |
|
---|
| 116 | /** modulo operator for doubles.
|
---|
| 117 | * \param *b pointer to double
|
---|
| 118 | * \param lower_bound lower bound
|
---|
| 119 | * \param upper_bound upper bound
|
---|
| 120 | */
|
---|
| 121 | void bound(double *b, double lower_bound, double upper_bound)
|
---|
| 122 | {
|
---|
[6ac7ee] | 123 | double step = (upper_bound - lower_bound);
|
---|
| 124 | while (*b >= upper_bound)
|
---|
| 125 | *b -= step;
|
---|
| 126 | while (*b < lower_bound)
|
---|
| 127 | *b += step;
|
---|
| 128 | };
|
---|
[14de469] | 129 |
|
---|
| 130 | /** Flips two doubles.
|
---|
| 131 | * \param *x pointer to first double
|
---|
| 132 | * \param *y pointer to second double
|
---|
| 133 | */
|
---|
| 134 | void flip(double *x, double *y)
|
---|
| 135 | {
|
---|
[6ac7ee] | 136 | double tmp;
|
---|
| 137 | tmp = *x;
|
---|
| 138 | *x = *y;
|
---|
| 139 | *y = tmp;
|
---|
[14de469] | 140 | };
|
---|
| 141 |
|
---|
| 142 | /** Returns the power of \a n with respect to \a base.
|
---|
| 143 | * \param base basis
|
---|
| 144 | * \param n power
|
---|
| 145 | * \return \f$base^n\f$
|
---|
| 146 | */
|
---|
| 147 | int pot(int base, int n)
|
---|
| 148 | {
|
---|
[6ac7ee] | 149 | int res = 1;
|
---|
| 150 | int j;
|
---|
| 151 | for (j=n;j--;)
|
---|
| 152 | res *= base;
|
---|
| 153 | return res;
|
---|
[14de469] | 154 | };
|
---|
| 155 |
|
---|
| 156 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
|
---|
| 157 | * \param FragmentNumber total number of fragments to determine necessary number of digits
|
---|
| 158 | * \param digits number to create with 0 prefixed
|
---|
| 159 | * \return allocated(!) char array with number in digits, ten base.
|
---|
| 160 | */
|
---|
| 161 | char *FixedDigitNumber(const int FragmentNumber, const int digits)
|
---|
| 162 | {
|
---|
[6ac7ee] | 163 | char *returnstring;
|
---|
| 164 | int number = FragmentNumber;
|
---|
| 165 | int order = 0;
|
---|
| 166 | while (number != 0) { // determine number of digits needed
|
---|
| 167 | number = (int)floor(((double)number / 10.));
|
---|
| 168 | order++;
|
---|
| 169 | //cout << "Number is " << number << ", order is " << order << "." << endl;
|
---|
| 170 | }
|
---|
| 171 | // allocate string
|
---|
[1907a7] | 172 | returnstring = (char *) Malloc(sizeof(char)*(order+2), "FixedDigitNumber: *returnstring");
|
---|
[6ac7ee] | 173 | // terminate and fill string array from end backward
|
---|
| 174 | returnstring[order] = '\0';
|
---|
| 175 | number = digits;
|
---|
| 176 | for (int i=order;i--;) {
|
---|
| 177 | returnstring[i] = '0' + (char)(number % 10);
|
---|
| 178 | number = (int)floor(((double)number / 10.));
|
---|
| 179 | }
|
---|
| 180 | //cout << returnstring << endl;
|
---|
| 181 | return returnstring;
|
---|
[14de469] | 182 | };
|
---|
| 183 |
|
---|
[e198c7] | 184 | /** Tests whether a given string contains a valid number or not.
|
---|
| 185 | * \param *string
|
---|
| 186 | * \return true - is a number, false - is not a valid number
|
---|
| 187 | */
|
---|
[6ac7ee] | 188 | bool IsValidNumber( const char *string)
|
---|
[e198c7] | 189 | {
|
---|
[6ac7ee] | 190 | int ptr = 0;
|
---|
| 191 | if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot
|
---|
| 192 | ptr++;
|
---|
| 193 | if ((string[ptr] >= '0') && (string[ptr] <= '9'))
|
---|
| 194 | return true;
|
---|
| 195 | return false;
|
---|
[e198c7] | 196 | };
|
---|
| 197 |
|
---|
| 198 |
|
---|