Skip to content
Snippets Groups Projects
Commit 8421977e authored by Giovanni Bussi's avatar Giovanni Bussi
Browse files

Fixed static data in h36

Fixed the initialization of some static data by treating them as
static vectors and initializing them with a lambda function.

In principle, this rules out possible problems arising when multiple threads
call the decode function simultaneously for the the first time.

The vectors are then declared as const for clarity.
parent 10cbe7e8
No related branches found
No related tags found
No related merge requests found
......@@ -20,14 +20,13 @@
along with plumed. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include "h36.h"
#include <vector>
#include "Exception.h"
namespace PLMD {
/// Tiny namespace for hybrid36 format.
/// This namespace includes freely available tools for h36 format.
/// I place them here for usage within PDB class. In case we need them
/// in other places they might be better encapsulated in a c++ class
/// and placed in a separate file.
namespace h36 {
......@@ -260,35 +259,32 @@ hy36encode(unsigned width, int value, char* result)
const char*
hy36decode(unsigned width, const char* s, unsigned s_size, int* result)
{
static int first_call = 1;
static int digits_values_upper[128U];
static int digits_values_lower[128U];
static const char*
ie_range = "internal error hy36decode: integer value out of range.";
unsigned i;
int di;
const char* errmsg;
if (first_call) {
first_call = 0;
for(i=0; i<128U; i++) digits_values_upper[i] = -1;
for(i=0; i<128U; i++) digits_values_lower[i] = -1;
for(i=0; i<36U; i++) {
di = digits_upper()[i];
static const std::vector<int> digits_values_upper_vector([]() {
std::vector<int> ret(128U,-1);
for(unsigned i=0; i<36U; i++) {
int di = digits_upper()[i];
if (di < 0 || di > 127) {
*result = 0;
return ie_range;
plumed_error()<<"internal error hy36decode: integer value out of range";
}
digits_values_upper[di] = i;
ret[di] = i;
}
for(i=0; i<36U; i++) {
di = digits_lower()[i];
return ret;
}());
static const int* digits_values_upper=digits_values_upper_vector.data();
static const std::vector<int> digits_values_lower_vector([]() {
std::vector<int> ret(128U,-1);
for(unsigned i=0; i<36U; i++) {
int di = digits_lower()[i];
if (di < 0 || di > 127) {
*result = 0;
return ie_range;
plumed_error()<<"internal error hy36decode: integer value out of range";
}
digits_values_lower[di] = i;
ret[di] = i;
}
}
return ret;
}());
static const int* digits_values_lower=digits_values_lower_vector.data();
int di;
const char* errmsg;
if (s_size == width) {
di = s[0];
if (di >= 0 && di <= 127) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment