Changeset View
Changeset View
Standalone View
Standalone View
EvtGenBase/EvtStringHash.hh
Show All 27 Lines | class EvtStringHash { | ||||
public: | public: | ||||
inline EvtStringHash( int size ); | inline EvtStringHash( int size ); | ||||
inline void add( const std::string& str, T* data ); | inline void add( const std::string& str, T* data ); | ||||
inline T* get( const std::string& str ); | inline T* get( const std::string& str ); | ||||
inline ~EvtStringHash(); | inline ~EvtStringHash(); | ||||
private: | private: | ||||
EvtStringHash(); | EvtStringHash(); | ||||
int _size; | int m_size; | ||||
inline int hash( const std::string& str ); | inline int hash( const std::string& str ); | ||||
std::string*** _strings; | std::string*** m_strings; | ||||
T*** _data; | T*** m_data; | ||||
int* _entries; | int* m_entries; | ||||
}; | }; | ||||
template <class T> | template <class T> | ||||
EvtStringHash<T>::EvtStringHash( int size ) | EvtStringHash<T>::EvtStringHash( int size ) | ||||
{ | { | ||||
_size = size; | m_size = size; | ||||
typedef std::string** EvtStringPtrPtr; | typedef std::string** EvtStringPtrPtr; | ||||
typedef T** TPtrPtr; | typedef T** TPtrPtr; | ||||
_strings = new EvtStringPtrPtr[_size]; | m_strings = new EvtStringPtrPtr[m_size]; | ||||
_data = new TPtrPtr[_size]; | m_data = new TPtrPtr[m_size]; | ||||
_entries = new int[_size]; | m_entries = new int[m_size]; | ||||
int i; | int i; | ||||
for ( i = 0; i < _size; i++ ) { | for ( i = 0; i < m_size; i++ ) { | ||||
_entries[i] = 0; | m_entries[i] = 0; | ||||
} | } | ||||
} | } | ||||
template <class T> | template <class T> | ||||
EvtStringHash<T>::~EvtStringHash() | EvtStringHash<T>::~EvtStringHash() | ||||
{ | { | ||||
int i; | int i; | ||||
for ( i = 0; i < _size; i++ ) { | for ( i = 0; i < m_size; i++ ) { | ||||
int j; | int j; | ||||
for ( j = 0; j < _entries[i]; j++ ) { | for ( j = 0; j < m_entries[i]; j++ ) { | ||||
delete _strings[i][j]; | delete m_strings[i][j]; | ||||
} | } | ||||
if ( _entries[i] > 0 ) { | if ( m_entries[i] > 0 ) { | ||||
delete[] _strings[i]; | delete[] m_strings[i]; | ||||
delete[] _data[i]; | delete[] m_data[i]; | ||||
} | } | ||||
} | } | ||||
delete[] _strings; | delete[] m_strings; | ||||
delete[] _data; | delete[] m_data; | ||||
delete[] _entries; | delete[] m_entries; | ||||
} | } | ||||
template <class T> | template <class T> | ||||
void EvtStringHash<T>::add( const std::string& str, T* data ) | void EvtStringHash<T>::add( const std::string& str, T* data ) | ||||
{ | { | ||||
int ihash = hash( str ); | int ihash = hash( str ); | ||||
typedef std::string* EvtStringPtr; | typedef std::string* EvtStringPtr; | ||||
typedef T* TPtr; | typedef T* TPtr; | ||||
std::string** newstrings = new EvtStringPtr[_entries[ihash] + 1]; | std::string** newstrings = new EvtStringPtr[m_entries[ihash] + 1]; | ||||
T** newdata = new TPtr[_entries[ihash] + 1]; | T** newdata = new TPtr[m_entries[ihash] + 1]; | ||||
int i; | int i; | ||||
for ( i = 0; i < _entries[ihash]; i++ ) { | for ( i = 0; i < m_entries[ihash]; i++ ) { | ||||
newstrings[i] = _strings[ihash][i]; | newstrings[i] = m_strings[ihash][i]; | ||||
newdata[i] = _data[ihash][i]; | newdata[i] = m_data[ihash][i]; | ||||
} | } | ||||
newstrings[_entries[ihash]] = new std::string; | newstrings[m_entries[ihash]] = new std::string; | ||||
*( newstrings[_entries[ihash]] ) = str; | *( newstrings[m_entries[ihash]] ) = str; | ||||
newdata[_entries[ihash]] = data; | newdata[m_entries[ihash]] = data; | ||||
if ( _entries[ihash] != 0 ) { | if ( m_entries[ihash] != 0 ) { | ||||
delete[] _strings[ihash]; | delete[] m_strings[ihash]; | ||||
delete[] _data[ihash]; | delete[] m_data[ihash]; | ||||
} | } | ||||
_entries[ihash]++; | m_entries[ihash]++; | ||||
_strings[ihash] = newstrings; | m_strings[ihash] = newstrings; | ||||
_data[ihash] = newdata; | m_data[ihash] = newdata; | ||||
} | } | ||||
template <class T> | template <class T> | ||||
T* EvtStringHash<T>::get( const std::string& str ) | T* EvtStringHash<T>::get( const std::string& str ) | ||||
{ | { | ||||
int ihash = hash( str ); | int ihash = hash( str ); | ||||
int i; | int i; | ||||
for ( i = 0; i < _entries[ihash]; i++ ) { | for ( i = 0; i < m_entries[ihash]; i++ ) { | ||||
if ( *( _strings[ihash][i] ) == str ) | if ( *( m_strings[ihash][i] ) == str ) | ||||
return _data[ihash][i]; | return m_data[ihash][i]; | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
template <class T> | template <class T> | ||||
int EvtStringHash<T>::hash( const std::string& str ) | int EvtStringHash<T>::hash( const std::string& str ) | ||||
{ | { | ||||
const char* cstr = str.c_str(); | const char* cstr = str.c_str(); | ||||
int i = 0; | int i = 0; | ||||
int value = 0; | int value = 0; | ||||
while ( cstr[i] != 0 ) { | while ( cstr[i] != 0 ) { | ||||
value += (int)cstr[i]; | value += (int)cstr[i]; | ||||
i++; | i++; | ||||
} | } | ||||
return value % _size; | return value % m_size; | ||||
} | } | ||||
#endif | #endif |