diff --git a/DDParsers/src/Evaluator/Evaluator.cpp b/DDParsers/src/Evaluator/Evaluator.cpp index 71bcd1b804646872140656e90cb5bede9d49277b..51c1a5df1590bc1613441ea73aaa918bc8ed4ef3 100644 --- a/DDParsers/src/Evaluator/Evaluator.cpp +++ b/DDParsers/src/Evaluator/Evaluator.cpp @@ -14,9 +14,11 @@ #include <cerrno> #include <cstring> #include <cstdlib> // for strtod() -#include "stack.src" -#include "string.src" -#include "hash_map.src" +#include <stack> +#include <string> +#include <unordered_map> + +using namespace std; // Disable some diagnostics, which we know, but need to ignore #if defined(__GNUC__) && !defined(__APPLE__) && !defined(__llvm__) @@ -50,14 +52,14 @@ struct Item { string expression; void *function; - explicit Item() : what(UNKNOWN), variable(0),expression(), function(0) {} - explicit Item(double x) : what(VARIABLE), variable(x),expression(), function(0) {} + explicit Item() : what(UNKNOWN), variable(0),expression(), function(0) {} + explicit Item(double x) : what(VARIABLE), variable(x),expression(), function(0) {} explicit Item(string x) : what(EXPRESSION),variable(0),expression(x),function(0) {} - explicit Item(void *x) : what(FUNCTION), variable(0),expression(), function(x) {} + explicit Item(void *x) : what(FUNCTION), variable(0),expression(), function(x) {} }; //typedef char * pchar; -typedef hash_map<string,Item> dic_type; +typedef unordered_map<string,Item> dic_type; /// Internal expression evaluator helper class struct EVAL::Object::Struct { @@ -77,7 +79,7 @@ struct EVAL::Object::Struct { theStruct->theCond.notify_one(); } Struct* theStruct; - std::unique_lock<std::mutex> theLg; + unique_lock<mutex> theLg; }; struct WriteLock { @@ -94,14 +96,14 @@ struct EVAL::Object::Struct { theStruct->theCond.notify_all(); } Struct* theStruct; - std::unique_lock<std::mutex> theLg; + unique_lock<mutex> theLg; }; dic_type theDictionary; int theReadersWaiting = 0; bool theWriterWaiting = false; - std::condition_variable theCond; - std::mutex theLock; + condition_variable theCond; + mutex theLock; }; namespace { @@ -642,7 +644,7 @@ static int setItem(const char * prefix, const char * name, } //--------------------------------------------------------------------------- - static void print_error_status(std::ostream& os, int status, char const* extra) { + static void print_error_status(ostream& os, int status, char const* extra) { static char prefix[] = "Evaluator::Object : "; const char* opt = (extra ? extra : ""); switch (status) { @@ -731,14 +733,14 @@ int Evaluator::Object::EvalStatus::error_position(const char* expression) const //--------------------------------------------------------------------------- void Evaluator::Object::EvalStatus::print_error() const { - std::stringstream str; + stringstream str; print_error(str); if ( str.str().empty() ) return; - std::cerr << str.str() << std::endl; + cerr << str.str() << endl; } //--------------------------------------------------------------------------- -void Evaluator::Object::EvalStatus::print_error(std::ostream& os) const { +void Evaluator::Object::EvalStatus::print_error(ostream& os) const { print_error_status(os, theStatus, thePosition); } @@ -770,22 +772,22 @@ int Evaluator::Object::setEnviron(const char* name, const char* value) { } //--------------------------------------------------------------------------- -std::pair<const char*,int> Evaluator::Object::getEnviron(const char* name) const { +pair<const char*,int> Evaluator::Object::getEnviron(const char* name) const { Struct::ReadLock guard(imp); Struct const* cImp = imp; dic_type::const_iterator iter = cImp->theDictionary.find(name); if (iter != cImp->theDictionary.end()) { - return std::make_pair(iter->second.expression.c_str(), EVAL::OK); + return make_pair(iter->second.expression.c_str(), EVAL::OK); } if ( ::strlen(name) > 3 ) { // Need to remove braces from ${xxxx} for call to getenv() string env_name(name+2,::strlen(name)-3); const char* env_str = ::getenv(env_name.c_str()); if ( 0 != env_str ) { - return std::make_pair(env_str, EVAL::OK); + return make_pair(env_str, EVAL::OK); } } - return std::make_pair(nullptr,EVAL::ERROR_UNKNOWN_VARIABLE); + return make_pair(nullptr,EVAL::ERROR_UNKNOWN_VARIABLE); } //--------------------------------------------------------------------------- @@ -906,30 +908,30 @@ Evaluator::~Evaluator() { } //--------------------------------------------------------------------------- -std::pair<int,double> Evaluator::evaluate(const std::string& expression) const { +pair<int,double> Evaluator::evaluate(const string& expression) const { auto result = object->evaluate(expression.c_str()); - return std::make_pair(result.status(),result.result()); + return make_pair(result.status(),result.result()); } //--------------------------------------------------------------------------- -std::pair<int,double> Evaluator::evaluate(const std::string& expression, std::ostream& os) const { +pair<int,double> Evaluator::evaluate(const string& expression, ostream& os) const { auto result = object->evaluate(expression.c_str()); int status = result.status(); if ( status != OK ) { result.print_error(os); } - return std::make_pair(result.status(),result.result()); + return make_pair(result.status(),result.result()); } //--------------------------------------------------------------------------- -int Evaluator::setEnviron(const std::string& name, const std::string& value) const { +int Evaluator::setEnviron(const string& name, const string& value) const { int result = object->setEnviron(name.c_str(), value.c_str()); return result; } //--------------------------------------------------------------------------- -std::pair<int,std::string> Evaluator::getEnviron(const std::string& name) const { - std::pair<int,std::string> result; +pair<int,string> Evaluator::getEnviron(const string& name) const { + pair<int,string> result; auto env_status = object->getEnviron(name.c_str()); result.first = env_status.second; if( env_status.first ) result.second = env_status.first; @@ -937,8 +939,8 @@ std::pair<int,std::string> Evaluator::getEnviron(const std::string& name) const } //--------------------------------------------------------------------------- -std::pair<int,std::string> Evaluator::getEnviron(const std::string& name, std::ostream& os) const { - std::pair<int,std::string> result; +pair<int,string> Evaluator::getEnviron(const string& name, ostream& os) const { + pair<int,string> result; auto env_status = object->getEnviron(name.c_str()); result.first = env_status.second; if ( env_status.first ) { @@ -951,13 +953,13 @@ std::pair<int,std::string> Evaluator::getEnviron(const std::string& name, std::o } //--------------------------------------------------------------------------- -int Evaluator::setVariable(const std::string& name, double value) const { +int Evaluator::setVariable(const string& name, double value) const { int result = object->setVariable(name.c_str(), value); return result; } //--------------------------------------------------------------------------- -int Evaluator::setVariable(const std::string& name, double value, std::ostream& os) const { +int Evaluator::setVariable(const string& name, double value, ostream& os) const { int result = object->setVariable(name.c_str(), value); if ( result != OK ) { print_error_status(os, result, name.c_str()); @@ -966,13 +968,13 @@ int Evaluator::setVariable(const std::string& name, double value, std::ostream& } //--------------------------------------------------------------------------- -int Evaluator::setVariable(const std::string& name, const std::string& value) const { +int Evaluator::setVariable(const string& name, const string& value) const { int result = object->setVariable(name.c_str(), value.c_str()); return result; } //--------------------------------------------------------------------------- -int Evaluator::setVariable(const std::string& name, const std::string& value, std::ostream& os) const { +int Evaluator::setVariable(const string& name, const string& value, ostream& os) const { int result = object->setVariable(name.c_str(), value.c_str()); if ( result != OK ) { print_error_status(os, result, name.c_str()); @@ -981,50 +983,50 @@ int Evaluator::setVariable(const std::string& name, const std::string& value, st } //--------------------------------------------------------------------------- -bool Evaluator::findVariable(const std::string& name) const { +bool Evaluator::findVariable(const string& name) const { bool ret; ret = object->findVariable(name.c_str()); return ret; } //--------------------------------------------------------------------------- -int Evaluator::setFunction(const std::string& name, double (*fun)()) const { +int Evaluator::setFunction(const string& name, double (*fun)()) const { int result = object->setFunction(name.c_str(), fun); return result; } //--------------------------------------------------------------------------- -int Evaluator::setFunction(const std::string& name, double (*fun)(double)) const { +int Evaluator::setFunction(const string& name, double (*fun)(double)) const { int result = object->setFunction(name.c_str(), fun); return result; } //--------------------------------------------------------------------------- -int Evaluator::setFunction(const std::string& name, double (*fun)(double, double)) const { +int Evaluator::setFunction(const string& name, double (*fun)(double, double)) const { int result = object->setFunction(name.c_str(), fun); return result; } //--------------------------------------------------------------------------- -int Evaluator::setFunction(const std::string& name, double (*fun)(double, double, double)) const { +int Evaluator::setFunction(const string& name, double (*fun)(double, double, double)) const { int result = object->setFunction(name.c_str(), fun); return result; } //--------------------------------------------------------------------------- -int Evaluator::setFunction(const std::string& name, double (*fun)(double, double, double, double)) const { +int Evaluator::setFunction(const string& name, double (*fun)(double, double, double, double)) const { int result = object->setFunction(name.c_str(), fun); return result; } //--------------------------------------------------------------------------- -int Evaluator::setFunction(const std::string& name, double (*fun)(double, double, double, double, double)) const { +int Evaluator::setFunction(const string& name, double (*fun)(double, double, double, double, double)) const { int result =object->setFunction(name.c_str(), fun); return result; } //--------------------------------------------------------------------------- -bool Evaluator::findFunction(const std::string& name, int npar) const { +bool Evaluator::findFunction(const string& name, int npar) const { bool ret; ret = object->findFunction(name.c_str(), npar); return ret; diff --git a/DDParsers/src/Evaluator/hash_map.src b/DDParsers/src/Evaluator/hash_map.src deleted file mode 100644 index c68ffb780406b90dfb0e535ab84c22268f2e7030..0000000000000000000000000000000000000000 --- a/DDParsers/src/Evaluator/hash_map.src +++ /dev/null @@ -1,258 +0,0 @@ -// -*- C++ -*- -// --------------------------------------------------------------------------- - -#ifdef DEBUG_MODE -#include <iostream> -#endif - -#ifndef HEP_HASH_MAP_SRC -#define HEP_HASH_MAP_SRC - -#include <string.h> -#include <utility> -#include "string.src" - - -/// Internal expression evaluato class -/* - * Simplified hash_map class. - * It provides only basic functions of the standard <hash_map> and - * is intended to be used as a replacement of the standard class where - * full functionality of <hash_map> is not required, but it is essential - * to have highly portable and effective code. - * - * This file should be used exclusively inside *.cc files. - * Usage inside header files can result to a clash with standard <hash_map>. - * - * @author Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch> - */ -template<class K, class T> -class hash_map { -private: - hash_map(const hash_map& c) - : table(0), cur_size(0), max_size(0), max_load(0), - grow(0.0), default_value(c.default_value) - { - } - hash_map& operator=(const hash_map& c) { - table = 0; - cur_size = 0; - max_size = 0; - max_load = 0; - grow = 0.0; - default_value = c.default_value; - return *this; - } -public: - struct Entry { // Hash_map entry - std::pair<const K,T> data; - Entry* next; - Entry(K k, T v, Entry* n) : data(k,v), next(n) {} - }; - - class hash_map_iterator { // Hash_map iterator - Entry* entry; - public: - hash_map_iterator() : entry(0) {} - hash_map_iterator(Entry & e) : entry(&e) {} - std::pair<const K,T>& operator * () const { return entry->data; } - std::pair<const K,T>* operator ->() const { return &(operator*()); } - bool operator==(hash_map_iterator i) const { - return (entry == i.entry); - } - bool operator!=(hash_map_iterator i) const { - return (entry != i.entry); - } - }; - - class hash_map_const_iterator { // Hash_map iterator - Entry const* entry; - public: - hash_map_const_iterator() : entry(0) {} - hash_map_const_iterator(Entry & e) : entry(&e) {} - std::pair<const K,T> const& operator * () const { return entry->data; } - std::pair<const K,T> const* operator ->() const { return &(operator*()); } - bool operator==(hash_map_const_iterator i) const { - return (entry == i.entry); - } - bool operator!=(hash_map_const_iterator i) const { - return (entry != i.entry); - } - }; - -public: - typedef unsigned int size_type; - typedef std::pair<const K,T> value_type; - typedef hash_map_iterator iterator; - typedef hash_map_const_iterator const_iterator; - -private: - Entry** table; // Hash table: pointers to entries - size_type cur_size; // Number of entries - size_type max_size; // Bucket_count - current size of the table - float max_load; // Keep (n) <= (max_size * max_load) - float grow; // When necessary, resize(max_size * grow) - const T default_value; // Default value used by [] - - size_type hash(const char * key) const { - size_type res = 0; - while(*key) { res = res*31 + *key++; } - return res; - } - - size_type hash(const string & key) const { - return hash(key.c_str()); - } - - bool eq(const char * a, const char * b) const { - return (strcmp(a, b) == 0); - } - - bool eq(const string & a, const string & b) const { - return (a == b); - } - - bool eq(const char * a, const string& b) const { - return (b == a); - } - - -public: - - // Constructor. - hash_map(const T & dv = T(), size_type n = 107) - : table(0), cur_size(0), max_size(0), default_value(dv) - { - set_load(); - resize(n); - } - - // Destructor. - ~hash_map() { - for(size_type i=0; i<max_size; i++) { - Entry* n = table[i]; - while(n) { Entry* p = n; n = p->next; delete p; } - } - delete [] table; - } - - // Sets load and grow parameters. - void set_load(float m = 0.7, float g = 1.7) { max_load = m; grow = g; } - - // Returns number of elements. - size_type size() const { return cur_size; } - - // Returns size of the hash table. - size_type bucket_count() const { return max_size; } - - // Resizes the hash table. - void resize(size_type s) { - if (s <= max_size) return; - Entry** tmp = table; - table = new Entry* [s]; - for (size_type k=0; k<s; k++) table[k] = 0; - for (size_type i=0; i<max_size; i++) { - Entry* n = tmp[i]; - while(n) { - Entry* p = n; - n = p->next; - size_type ii = hash(p->data.first) % s; - p->next = table[ii]; - table[ii] = p; - } - } - max_size = s; - delete [] tmp; - } - - // Subscripting. - T & operator[](const K & key) { - size_type i = hash(key) % max_size; - for (Entry* p=table[hash(key) % max_size]; p; p=p->next) { - if (eq(key,p->data.first)) return p->data.second; - } - if (cur_size++ >= max_size*max_load) { - resize(size_type(max_size*grow)); - i = hash(key) % max_size; - } - table[i] = new Entry(key, default_value, table[i]); - return table[i]->data.second; - } - - // Finds element with given key. - const_iterator find(const K & key) const { - size_type i = hash(key) % max_size; - for (Entry* p=table[i]; p; p=p->next) { - if (eq(key,p->data.first)) return const_iterator(*p); - } - return end(); - } - - iterator find(const K & key) { - size_type i = hash(key) % max_size; - for (Entry* p=table[i]; p; p=p->next) { - if (eq(key,p->data.first)) return iterator(*p); - } - return end(); - } - - // Finds element with a key equivalent. - template<typename K2> - const_iterator find(const K2 & key) const { - size_type i = hash(key) % max_size; - for (Entry* p=table[i]; p; p=p->next) { - if (eq(key,p->data.first)) return const_iterator(*p); - } - return end(); - } - - // Erases element with given key. - size_type erase(const K & key) { - size_type i = hash(key) % max_size; - Entry* p = table[i]; - if (p == 0) return 0; - if (eq(key,p->data.first)) { - table[i] = p->next; delete p; cur_size--; return 1; - } - Entry** pp = &table[i]; - for (p=p->next; p; p=p->next) { - if (eq(key,p->data.first)) { - *pp = p->next; delete p; cur_size--; return 1; - }else{ - pp = &(p->next); - } - } - return 0; - } - - // Clears the hash table. - void clear() { - for(size_type i=0; i<max_size; i++) { - for (Entry* p=table[i]; p;) { - Entry* pp = p; p = p->next; delete pp; - } - table[i] = 0; - } - cur_size = 0; - } - - // Returns end iterator. - const_iterator end() const { return const_iterator(); } - iterator end() { return iterator(); } - -#ifdef DEBUG_MODE - // Prints content of the hash table. - void print() { - std::cout << endl; - for(size_type i=0; i<max_size; i++) { - for (Entry* p=table[i]; p; p=p->next) { - std::cout - << '"' << p->data.first << '"' << " " << p->data.second - << std::endl; - } - } - } -#endif -}; - -#endif /* HEP_HASH_MAP_SRC */ diff --git a/DDParsers/src/Evaluator/stack.src b/DDParsers/src/Evaluator/stack.src deleted file mode 100644 index 83125887aab26433e481cac90f251d37af3cce5f..0000000000000000000000000000000000000000 --- a/DDParsers/src/Evaluator/stack.src +++ /dev/null @@ -1,48 +0,0 @@ -// -*- C++ -*- -// --------------------------------------------------------------------------- - -#ifndef HEP_STACK_SRC -#define HEP_STACK_SRC - -/// Internal expression evaluato class -/* - * Simplified stack class. - * It is intended to be used as a replacement of the standard class where - * full functionality of <stack> is not required, but it is essential - * to have highly portable and effective code. - * - * This file should be used exclusively inside *.cc files. - * Usage inside header files can result to a clash with standard <stack>. - * - * @author Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch> - */ -template<class T> -class stack { -private: - int k, max_size; - T * v; - - stack(const stack& c) = delete; - stack& operator=(const stack&) = delete; - -public: - stack() : k(0), max_size(20), v(new T[20]) {} - ~stack() { delete [] v; } - - int size() const { return k; } - T top () const { return v[k-1]; } - T & top () { return v[k-1]; } - void pop () { k--; } - void push(T a) { - if (k == max_size) { - T * w = v; - max_size *= 2; - v = new T[max_size]; - for (int i=0; i<k; i++) v[i] = w[i]; - delete [] w; - } - v[k++] = a; - } -}; - -#endif /* HEP_STACK_SRC */ diff --git a/DDParsers/src/Evaluator/string.src b/DDParsers/src/Evaluator/string.src deleted file mode 100644 index a449089b94a1a44ede7f4fb0ad501a3ff1ad080b..0000000000000000000000000000000000000000 --- a/DDParsers/src/Evaluator/string.src +++ /dev/null @@ -1,145 +0,0 @@ -// -*- C++ -*- -// --------------------------------------------------------------------------- - -#ifndef HEP_STRING_SRC -#define HEP_STRING_SRC - -#include <iostream> -#include <string.h> - -/// Internal expression evaluato class -/* - * Simplified string class. - * It provides only few basic functions of the standard <string> and - * is intended to be used as a replacement of the standard class where - * full functionality of <string> is not required, but it is essential - * to have highly portable and effective code. - * - * This file should be used exclusively inside *.cc files. - * Usage inside header files can result to a clash with standard <string>. - */ -struct string { - struct srep { - char* s; // pointer to data - int n; // reference count - srep() : s(0), n(1) {} - } *p; - - // Default constructor. - string() { p = new srep; p->s = 0; } - - // Constructor from character string. - string(const char* s) { - size_t len = strlen(s)+1; - p = new srep; p->s = new char[len]; strncpy(p->s, s, len); - } - - // Constructor from character substring. - string(const char* s, unsigned int n) { - p = new srep; p->s = new char[n+1]; strncpy(p->s, s, n); *(p->s+n) = '\0'; - } - - // Copy constructor from string. - string(const string& x) { - x.p->n++; - p = x.p; - } - - // Destructor. - ~string() { - if (--p->n == 0) { - delete [] p->s; - delete p; - } - } - - // Assignment from character string. - string& operator=(const char* s) { - if (p->n > 1) { // disconnect self - p->n--; - p = new srep; - }else{ - delete [] p->s; // free old string - } - p->s = new char[strlen(s)+1]; - strcpy(p->s, s); - return *this; - } - - // Assignment from string. - string& operator=(const string & x) { - if ( this != &x ) { - x.p->n++; // protect against "st = st" - if (--p->n == 0) { delete [] p->s; delete p; } - p = x.p; - } - return *this; - } - - // Returns C-style character string. - const char* c_str() const { return p->s; } -}; - -// -// Concatinations. -// -inline string operator+(char a, const string & b) { - string s; - size_t len = strlen(b.c_str())+2; - s.p->s = new char[len]; - s.p->s[0] = a; - strncpy(s.p->s+1, b.c_str(),len-1); - return s; -} - -inline string operator+(const char * a, const string & b) { - int lena = strlen(a); - int lenb = strlen(b.c_str()); - string s; - s.p->s = new char[lena+lenb+1]; - strncpy(s.p->s, a, lena+1); - strncpy(s.p->s+lena, b.c_str(), lenb+1); - return s; -} - -inline string operator+(const string & a, const char * b) { - int lena = strlen(a.c_str()), lenb = strlen(b); - string s; s.p->s = new char[lena+lenb+1]; - strncpy(s.p->s, a.c_str(),lena+1); - strncpy(s.p->s+lena, b,lenb+1); - return s; -} - -inline string operator+(const string & a, const string & b) { - int lena = strlen(a.c_str()); - int lenb = strlen(b.c_str()); - string s; s.p->s = new char[lena+lenb+1]; - strncpy(s.p->s, a.c_str(), lena+1); - strncpy(s.p->s+lena, b.c_str(), lenb+1); - return s; -} - -// -// Comparisons. -// -inline bool operator==(const string & x, const char* s) { - return strcmp(x.p->s, s) == 0; -} -inline bool operator==(const string & x, const string & y) { - return strcmp(x.p->s, y.p->s) == 0; -} -inline bool operator!=(const string & x, const char* s) { - return strcmp(x.p->s, s) != 0; -} -inline bool operator!=(const string & x, const string & y) { - return strcmp(x.p->s, y.p->s) != 0; -} - -// -// Output to a stream. -// -std::ostream & operator<<(std::ostream & s, const string & x) { - return s << x.p->s; -} - -#endif /* HEP_STRING_SRC */