| [14de469] | 1 | /** \file helpers.cpp | 
|---|
|  | 2 | * | 
|---|
| [6ac7ee] | 3 | * Implementation of some auxiliary functions for memory dis-/allocation and so on | 
|---|
| [14de469] | 4 | */ | 
|---|
|  | 5 |  | 
|---|
| [112b09] | 6 | #include "Helpers/MemDebug.hpp" | 
|---|
| [14de469] | 7 |  | 
|---|
| [952f38] | 8 | #include "Helpers/helpers.hpp" | 
|---|
| [0a4f7f] | 9 | #include "Helpers/fast_functions.hpp" | 
|---|
| [952f38] | 10 | #include "Helpers/Verbose.hpp" | 
|---|
|  | 11 | #include "Helpers/Log.hpp" | 
|---|
| [6cd79d] | 12 |  | 
|---|
| [986ed3] | 13 | #include <iostream> | 
|---|
|  | 14 |  | 
|---|
| [14de469] | 15 | /********************************************** helpful functions *********************************/ | 
|---|
|  | 16 |  | 
|---|
|  | 17 |  | 
|---|
|  | 18 | /** Asks for a double value and checks input | 
|---|
|  | 19 | * \param *text question | 
|---|
|  | 20 | */ | 
|---|
|  | 21 | double ask_value(const char *text) | 
|---|
|  | 22 | { | 
|---|
| [042f82] | 23 | double test = 0.1439851348959832147598734598273456723948652983045928346598365; | 
|---|
|  | 24 | do { | 
|---|
| [a67d19] | 25 | DoLog(0) && (Log() << Verbose(0) << text); | 
|---|
| [042f82] | 26 | cin >> test; | 
|---|
|  | 27 | } while (test == 0.1439851348959832147598734598273456723948652983045928346598365); | 
|---|
|  | 28 | return test; | 
|---|
| [14de469] | 29 | }; | 
|---|
|  | 30 |  | 
|---|
| [d3a46d] | 31 | /** Output of a debug message to stderr. | 
|---|
|  | 32 | * \param *P Problem at hand, points to ParallelSimulationData#me | 
|---|
|  | 33 | * \param output output string | 
|---|
|  | 34 | */ | 
|---|
|  | 35 | #ifdef HAVE_DEBUG | 
|---|
|  | 36 | void debug_in(const char *output, const char *file, const int line) { | 
|---|
| [042f82] | 37 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output); | 
|---|
| [d3a46d] | 38 | } | 
|---|
|  | 39 | #else | 
|---|
| [042f82] | 40 | void debug_in(const char *output, const char *file, const int line) {}  // print nothing | 
|---|
| [d3a46d] | 41 | #endif | 
|---|
| [14de469] | 42 |  | 
|---|
|  | 43 | /** modulo operator for doubles. | 
|---|
|  | 44 | * \param *b pointer to double | 
|---|
|  | 45 | * \param lower_bound lower bound | 
|---|
|  | 46 | * \param upper_bound upper bound | 
|---|
|  | 47 | */ | 
|---|
|  | 48 | void bound(double *b, double lower_bound, double upper_bound) | 
|---|
|  | 49 | { | 
|---|
| [042f82] | 50 | double step = (upper_bound - lower_bound); | 
|---|
|  | 51 | while (*b >= upper_bound) | 
|---|
|  | 52 | *b -= step; | 
|---|
|  | 53 | while (*b < lower_bound) | 
|---|
|  | 54 | *b += step; | 
|---|
| [6ac7ee] | 55 | }; | 
|---|
| [14de469] | 56 |  | 
|---|
| [5034e1] | 57 | /** Counts lines in file. | 
|---|
|  | 58 | * Note we are scanning lines from current position, not from beginning. | 
|---|
|  | 59 | * \param InputFile file to be scanned. | 
|---|
|  | 60 | */ | 
|---|
|  | 61 | int CountLinesinFile(ifstream &InputFile) | 
|---|
|  | 62 | { | 
|---|
| [920c70] | 63 | char *buffer = new char[MAXSTRINGSIZE]; | 
|---|
| [5034e1] | 64 | int lines=0; | 
|---|
|  | 65 |  | 
|---|
|  | 66 | int PositionMarker = InputFile.tellg();  // not needed as Inputfile is copied, given by value, not by ref | 
|---|
|  | 67 | // count the number of lines, i.e. the number of fragments | 
|---|
|  | 68 | InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines | 
|---|
|  | 69 | InputFile.getline(buffer, MAXSTRINGSIZE); | 
|---|
|  | 70 | while(!InputFile.eof()) { | 
|---|
|  | 71 | InputFile.getline(buffer, MAXSTRINGSIZE); | 
|---|
|  | 72 | lines++; | 
|---|
|  | 73 | } | 
|---|
|  | 74 | InputFile.seekg(PositionMarker, ios::beg); | 
|---|
| [920c70] | 75 | delete[](buffer); | 
|---|
| [5034e1] | 76 | return lines; | 
|---|
|  | 77 | }; | 
|---|
|  | 78 |  | 
|---|
| [14de469] | 79 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits. | 
|---|
|  | 80 | * \param FragmentNumber total number of fragments to determine necessary number of digits | 
|---|
|  | 81 | * \param digits number to create with 0 prefixed | 
|---|
|  | 82 | * \return allocated(!) char array with number in digits, ten base. | 
|---|
|  | 83 | */ | 
|---|
|  | 84 | char *FixedDigitNumber(const int FragmentNumber, const int digits) | 
|---|
|  | 85 | { | 
|---|
| [042f82] | 86 | char *returnstring; | 
|---|
|  | 87 | int number = FragmentNumber; | 
|---|
|  | 88 | int order = 0; | 
|---|
|  | 89 | while (number != 0) { // determine number of digits needed | 
|---|
|  | 90 | number = (int)floor(((double)number / 10.)); | 
|---|
|  | 91 | order++; | 
|---|
| [e138de] | 92 | //Log() << Verbose(0) << "Number is " << number << ", order is " << order << "." << endl; | 
|---|
| [042f82] | 93 | } | 
|---|
|  | 94 | // allocate string | 
|---|
| [920c70] | 95 | returnstring = new char[order + 2]; | 
|---|
| [042f82] | 96 | // terminate  and fill string array from end backward | 
|---|
|  | 97 | returnstring[order] = '\0'; | 
|---|
|  | 98 | number = digits; | 
|---|
|  | 99 | for (int i=order;i--;) { | 
|---|
|  | 100 | returnstring[i] = '0' + (char)(number % 10); | 
|---|
|  | 101 | number = (int)floor(((double)number / 10.)); | 
|---|
|  | 102 | } | 
|---|
| [e138de] | 103 | //Log() << Verbose(0) << returnstring << endl; | 
|---|
| [042f82] | 104 | return returnstring; | 
|---|
| [14de469] | 105 | }; | 
|---|
|  | 106 |  | 
|---|
| [e198c7] | 107 | /** Tests whether a given string contains a valid number or not. | 
|---|
|  | 108 | * \param *string | 
|---|
|  | 109 | * \return true - is a number, false - is not a valid number | 
|---|
|  | 110 | */ | 
|---|
| [6ac7ee] | 111 | bool IsValidNumber( const char *string) | 
|---|
| [e198c7] | 112 | { | 
|---|
| [042f82] | 113 | int ptr = 0; | 
|---|
|  | 114 | if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot | 
|---|
|  | 115 | ptr++; | 
|---|
|  | 116 | if ((string[ptr] >= '0') && (string[ptr] <= '9')) | 
|---|
|  | 117 | return true; | 
|---|
|  | 118 | return false; | 
|---|
| [e198c7] | 119 | }; | 
|---|
|  | 120 |  | 
|---|
| [f66195] | 121 | /** Comparison function for GSL heapsort on distances in two molecules. | 
|---|
|  | 122 | * \param *a | 
|---|
|  | 123 | * \param *b | 
|---|
|  | 124 | * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b | 
|---|
|  | 125 | */ | 
|---|
|  | 126 | int CompareDoubles (const void * a, const void * b) | 
|---|
|  | 127 | { | 
|---|
|  | 128 | if (*(double *)a > *(double *)b) | 
|---|
|  | 129 | return -1; | 
|---|
|  | 130 | else if (*(double *)a < *(double *)b) | 
|---|
|  | 131 | return 1; | 
|---|
|  | 132 | else | 
|---|
|  | 133 | return 0; | 
|---|
|  | 134 | }; | 
|---|
|  | 135 |  | 
|---|
|  | 136 |  | 
|---|
| [21b9c3] | 137 | /** | 
|---|
| [920c70] | 138 | * Calls exit(255). | 
|---|
| [21b9c3] | 139 | */ | 
|---|
| [6cd79d] | 140 | void performCriticalExit() { | 
|---|
| [21b9c3] | 141 | exit(255); | 
|---|
|  | 142 | } | 
|---|