[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 |
|
---|
| 8 | #include "helpers.hpp"
|
---|
[0a4f7f] | 9 | #include "Helpers/fast_functions.hpp"
|
---|
[36166d] | 10 | #include "verbose.hpp"
|
---|
[e138de] | 11 | #include "log.hpp"
|
---|
[6cd79d] | 12 |
|
---|
[14de469] | 13 | /********************************************** helpful functions *********************************/
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | /** Asks for a double value and checks input
|
---|
| 17 | * \param *text question
|
---|
| 18 | */
|
---|
| 19 | double ask_value(const char *text)
|
---|
| 20 | {
|
---|
[042f82] | 21 | double test = 0.1439851348959832147598734598273456723948652983045928346598365;
|
---|
| 22 | do {
|
---|
[a67d19] | 23 | DoLog(0) && (Log() << Verbose(0) << text);
|
---|
[042f82] | 24 | cin >> test;
|
---|
| 25 | } while (test == 0.1439851348959832147598734598273456723948652983045928346598365);
|
---|
| 26 | return test;
|
---|
[14de469] | 27 | };
|
---|
| 28 |
|
---|
[d3a46d] | 29 | /** Output of a debug message to stderr.
|
---|
| 30 | * \param *P Problem at hand, points to ParallelSimulationData#me
|
---|
| 31 | * \param output output string
|
---|
| 32 | */
|
---|
| 33 | #ifdef HAVE_DEBUG
|
---|
| 34 | void debug_in(const char *output, const char *file, const int line) {
|
---|
[042f82] | 35 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output);
|
---|
[d3a46d] | 36 | }
|
---|
| 37 | #else
|
---|
[042f82] | 38 | void debug_in(const char *output, const char *file, const int line) {} // print nothing
|
---|
[d3a46d] | 39 | #endif
|
---|
[14de469] | 40 |
|
---|
| 41 | /** modulo operator for doubles.
|
---|
| 42 | * \param *b pointer to double
|
---|
| 43 | * \param lower_bound lower bound
|
---|
| 44 | * \param upper_bound upper bound
|
---|
| 45 | */
|
---|
| 46 | void bound(double *b, double lower_bound, double upper_bound)
|
---|
| 47 | {
|
---|
[042f82] | 48 | double step = (upper_bound - lower_bound);
|
---|
| 49 | while (*b >= upper_bound)
|
---|
| 50 | *b -= step;
|
---|
| 51 | while (*b < lower_bound)
|
---|
| 52 | *b += step;
|
---|
[6ac7ee] | 53 | };
|
---|
[14de469] | 54 |
|
---|
[5034e1] | 55 | /** Counts lines in file.
|
---|
| 56 | * Note we are scanning lines from current position, not from beginning.
|
---|
| 57 | * \param InputFile file to be scanned.
|
---|
| 58 | */
|
---|
| 59 | int CountLinesinFile(ifstream &InputFile)
|
---|
| 60 | {
|
---|
[920c70] | 61 | char *buffer = new char[MAXSTRINGSIZE];
|
---|
[5034e1] | 62 | int lines=0;
|
---|
| 63 |
|
---|
| 64 | int PositionMarker = InputFile.tellg(); // not needed as Inputfile is copied, given by value, not by ref
|
---|
| 65 | // count the number of lines, i.e. the number of fragments
|
---|
| 66 | InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
|
---|
| 67 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 68 | while(!InputFile.eof()) {
|
---|
| 69 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 70 | lines++;
|
---|
| 71 | }
|
---|
| 72 | InputFile.seekg(PositionMarker, ios::beg);
|
---|
[920c70] | 73 | delete[](buffer);
|
---|
[5034e1] | 74 | return lines;
|
---|
| 75 | };
|
---|
| 76 |
|
---|
[14de469] | 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++;
|
---|
[e138de] | 90 | //Log() << Verbose(0) << "Number is " << number << ", order is " << order << "." << endl;
|
---|
[042f82] | 91 | }
|
---|
| 92 | // allocate string
|
---|
[920c70] | 93 | returnstring = new char[order + 2];
|
---|
[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 | }
|
---|
[e138de] | 101 | //Log() << Verbose(0) << returnstring << endl;
|
---|
[042f82] | 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 |
|
---|
[f66195] | 119 | /** Comparison function for GSL heapsort on distances in two molecules.
|
---|
| 120 | * \param *a
|
---|
| 121 | * \param *b
|
---|
| 122 | * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b
|
---|
| 123 | */
|
---|
| 124 | int CompareDoubles (const void * a, const void * b)
|
---|
| 125 | {
|
---|
| 126 | if (*(double *)a > *(double *)b)
|
---|
| 127 | return -1;
|
---|
| 128 | else if (*(double *)a < *(double *)b)
|
---|
| 129 | return 1;
|
---|
| 130 | else
|
---|
| 131 | return 0;
|
---|
| 132 | };
|
---|
| 133 |
|
---|
| 134 |
|
---|
[21b9c3] | 135 | /**
|
---|
[920c70] | 136 | * Calls exit(255).
|
---|
[21b9c3] | 137 | */
|
---|
[6cd79d] | 138 | void performCriticalExit() {
|
---|
[21b9c3] | 139 | exit(255);
|
---|
| 140 | }
|
---|