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